-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRakefile
69 lines (53 loc) · 1.38 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'yaml'
require 'open3'
EDITOR = ENV.fetch('EDITOR', 'atom')
task :default => :help
task :help do
puts "This is what you can do here:".green
system("rake -sT")
end
desc "Add a new post"
task :new_post, [:type, :title] do |t, args|
args.with_defaults(:type => 'blog')
args.with_defaults(:title => 'new-post')
author = Open3.popen3("git config user.name") {|stdin, stdout| stdout.read.chomp }
title = args[:title]
category = args[:type]
filename = make_filename(category,title)
puts "Creating new post file #{filename}".green
open(filename, 'wb') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title}\""
post.puts "author: \"#{author}\""
post.puts "date: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
post.puts "comments: true"
post.puts "category: #{category}"
post.puts "---"
end
`#{EDITOR} #{filename}`
end
def make_filename(type, title)
clean = clean_title(title)
"_posts/#{type}/#{Time.now.strftime('%Y-%m-%d')}-#{clean}.md"
end
def clean_title(title)
title.downcase.gsub(/&/, 'and').gsub(/[,'":\?!\(\)\[\]]/,'').gsub(/[\W\.]/, '-').gsub(/-+$/,'')
end
class String
def red
colorize(self, 31)
end
def green
colorize(self, 32)
end
def yellow
colorize(self, 33)
end
def blue
colorize(self, 34)
end
def colorize(text, color_code)
"\033[#{color_code}m#{text}\033[0m"
end
end