-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjekyll.thor
56 lines (44 loc) · 1.37 KB
/
jekyll.thor
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
require "stringex"
require "date"
class Jekyll < Thor
desc "new", "create a new post"
method_option :editor, :default => "vim"
def new(*title)
title = title.join(" ")
date = Time.now.strftime('%Y-%m-%d')
create_file(date, title)
end
desc "next", "create the next post (tuesday & thursday)"
method_option :editor, :default => "vim"
def next(*title)
title = title.join(" ")
# We want the day after the latest post
# to exclude it from the process
latest = (Date.parse Dir["_posts/*.md"].sort.last[7..16]) + 1;
# get next Tuesday and next Thursday
dates = [get_next_day(latest, 2), get_next_day(latest, 4)]
date = dates.min.strftime('%Y-%m-%d')
create_file(date, title)
end
private
def create_file(date, title)
filename = "_posts/#{date}-#{title.to_url}.md"
timestamp = Time.now.strftime('%Y-%m-%d %H:%M')
if File.exist?(filename)
abort("#{filename} already exists!")
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "categories: []"
post.puts "date: #{timestamp}"
post.puts "---"
end
system(options[:editor], '.')
end
def get_next_day(date, day_of_week)
date + ((day_of_week - date.wday) % 7)
end
end