-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathnew_post.rb
53 lines (43 loc) · 956 Bytes
/
new_post.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
43
44
45
46
47
48
49
50
51
52
53
require "date"
require "optparse"
require 'json'
require 'shellwords'
require 'time'
options = {}
parser = OptionParser.new do |opts|
opts.banner = "Usage: new_post.rb -t TITLE"
opts.on("-tTITLE", "--title=TITLE", "Title of the post") do |t|
options[:title] = t
end
opts.on("-h", "--help", "Prints this help") do
puts opts
exit
end
end
parser.parse!
if options[:title].nil?
puts parser.help
exit
end
def to_slug(title)
title.downcase.gsub(/ /, "-").gsub(/[^a-z0-9-]/, "")
end
date = Time.now
filename = "_posts/#{date.to_date.iso8601}-#{to_slug options[:title]}.md"
if File.exists?(filename)
puts "Post already exists!"
return
end
File.open(filename, "w") do |file|
template = <<-EOF
---
layout: post
title: #{options[:title].to_json}
author: YOURNAMEHERE
date: #{date.iso8601}
---
EOF
file.write(template)
end
editor = ENV['EDITOR'] || ENV['VISUAL'] || 'vim'
Process.exec("#{editor} #{filename.shellescape}")