forked from swanson/lagom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.rb
42 lines (33 loc) · 789 Bytes
/
generate.rb
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
require 'date'
require 'optparse'
options = {}
opts = OptionParser.new do |opts|
opts.banner = "Usage:\n\tgenerate.rb title category " +
"\n\nOptions:\n\tcategory\tblog, writeup"
end
opts.parse!
if ARGV.empty?
puts opts.banner
exit
end
options[:title] = ARGV.shift
options[:category] = ARGV.shift || "blog"
options[:layout] = options[:category] == "writeup" ? "writeup" : "post"
def slug(title)
title.downcase.gsub(/[^\w]/, " ").strip.gsub(/\s+/, '-')
end
template = '''---
layout: %{layout}
title: "%{title}"
categories:
- %{category}
---
Go-go-gadget blog post
'''
content = template % options
filename = "_posts/#{Date.today}-#{slug(options[:title])}.md"
File.open(filename, "w") do |file|
file.write(content)
end
system "subl -n . #{filename}"
puts "Crush it!"