-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
52 lines (49 loc) · 1.23 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
begin
require 'ya2yaml'
$KCODE = 'u'
YAML_METHOD = :ya2yaml
rescue LoadError
YAML_METHOD = :to_yaml
end
require 'active_support'
desc "Generate new blog post"
task :post, :title do |t, args|
if args.title.blank?
puts %{Usage:}
puts %{ $ rake post["Post title"]}
exit 1
end
time = Time.new
metadata = {
'layout' => 'post',
'title' => args.title,
'created_at' => time
}
filename = "_posts/#{time.strftime("%Y-%m-%d")}-#{slugify(args.title)}.md"
File.open(filename, "w") do |f|
f.write(metadata.send(YAML_METHOD))
f.puts "---"
f.puts "Go crazy..."
end
puts "#{filename} created."
if ENV['EDITOR']
puts "Opening with #{ENV['EDITOR']}..."
exec(ENV['EDITOR'], filename) if fork.nil?
end
end
ACCENTS_TR = [
"aäáàâãcçeëéèêiïíìînñoöóòõôőuüúùûűAÄÁÀÂÃCÇEËÉÈÊIÏÍÌÎNÑOÖÓÒÕÔŐUÜÚÙÛŰ".scan(/./u),
"aaaaaacceeeeeiiiiinnooooooouuuuuuAAAAAACCEEEEEIIIIINNOOOOOOOUUUUUU".scan(/./u),
]
def slugify(str)
str.dup.tap do |s|
ACCENTS_TR.first.each_with_index do |char, i|
s.gsub!(char, ACCENTS_TR.last[i])
end
s.gsub!(/[^a-zA-Z0-9_ -]/, "")
s.gsub!(/[ _-]+/, " ")
s.strip!
s.tr!(' ', "-")
s.downcase!
end
end