-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
170 lines (145 loc) · 4.32 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'rubygems'
require 'bundler/setup'
require 'rake'
require 'fileutils'
require 'yaml'
require 'time'
ROOT_DIR = File.dirname(__FILE__)
SITE_DIR = File.join(ROOT_DIR, '_site')
DRAFTS_DIR = File.join(ROOT_DIR, '_drafts')
POSTS_DIR = File.join(ROOT_DIR, '_posts')
def parse_post(post)
raise Exception.new("Invalid post file format") unless post[0] = "---"
eoh = post[1, post.length].index("---\n") + 1
header = YAML.load(post[1, eoh].join)
body = post[eoh + 1, post.length].join
return header, body
end
def write_post(header, body, file)
File.open(file, "w") do |f|
f << YAML::dump(header)
f << "---\n"
f << body
end
end
def edit_post(post)
editor = ENV['VISUAL'] || ENV['EDITOR']
system "#{editor} #{post}"
end
desc "Clear generated site."
task :clean do
puts "Clearing generated site"
rm_rf Dir.glob(File.join(SITE_DIR, '*'))
rmdir SITE_DIR
end
desc "Generate site."
task :build do
sh "jekyll build"
end
desc "Run local jekyll server"
task :server, [:port] do |t, args|
Rake::Task['clean'].invoke
sh "jekyll serve --watch --drafts --port #{args.port || 4000}"
end
desc "Create a new draft post"
task :draft, [:title] do |t, args|
# Get all drafts
drafts = Dir.glob(File.join(DRAFTS_DIR, 'draft-*.*'))
draft_id = 1
if drafts.length() > 0
draft_id = File.basename(drafts.sort()[-1]).gsub(/draft-/, '').gsub('.markdown', '').to_i() + 1
end
# Create a new file with a basic template
postname = "draft-#{draft_id}"
FileUtils.mkdir_p DRAFTS_DIR unless File.exist?(DRAFTS_DIR)
post = File.join(DRAFTS_DIR, "#{postname}.markdown")
# Set the post title if not provided
if args.title.nil?
default_title = "Draft Post #{draft_id}"
print "Enter post title [#{default_title}]: "
title = $stdin.gets().chomp()
title = default_title unless title.length() > 0
else
title = args.title
end
header = <<-END
---
layout: post
title: #{title}
---
New draft post
END
File.open(post, 'w') {|f| f << header }
edit_post(post)
puts "Created draft post #{postname}"
puts "To publish, use:"
puts " rake post [#{postname}]"
end
desc "Publish draft post. Arguments: [name]"
task :post, [:name] do |t, args|
if args.name
draft_name = File.extname(args.name).length() > 0 ? args.name : "#{args.name}.markdown"
draft_path = File.join(DRAFTS_DIR, draft_name)
else
# Get list of drafts and prompt user to pick
drafts = Dir.glob(File.join(DRAFTS_DIR, '*'))
if drafts.count > 1
menu = []
puts "Select draft to post:"
count = 1
drafts.each do |draft|
header, body = parse_post(IO.readlines(draft))
menu << "#{count}. #{header['title']} [#{File.basename(draft)}]"
count += 1
end
begin
menu.each { |m| puts m }
print "Which post? (1-#{drafts.length()} or q) "
choice = $stdin.gets().chomp()
if choice == 'q'
puts "Abort"
exit(-1)
end
choice = choice.to_i() - 1
end while (choice < 1 or choice > drafts.length())
draft_path = drafts[choice]
elsif drafts.count == 1
puts "Posting pending draft"
draft_path = drafts[0]
else
puts "No pending drafts"
exit(-1)
end
end
unless File.exist?(draft_path)
puts "Draft file #{draft_path} is missing"
exit(-1)
end
# Generate timestamp
published_timestamp = Time.now
date_prefix = published_timestamp.strftime("%Y-%m-%d")
header, body = parse_post(IO.readlines(draft_path))
# Create the post file
name = header["title"].strip.downcase.gsub(/ /, '-')
header["date"] = published_timestamp.strftime("%Y-%m-%d %H:%M:%S") unless header.include?("date")
post_path = File.join(POSTS_DIR, "#{date_prefix}-#{name}.markdown")
write_post(header, body, post_path)
# Clear draft
File.delete(draft_path)
puts "Posted: #{File.basename(post_path)}"
end
desc "Update published post. Arguments: [title]"
task :update, [:name] do |t, args|
unless args.name
puts "Usage: rake post[name]"
exit(-1)
end
updated_timestamp = Time.now
postname = args.name.strip.downcase.gsub(/ /, '-')
post_path = File.join(POSTS_DIR, "#{postname}.markdown")
header, body = parse_post(IO.readlines(post_path))
header["updated"] = updated_timestamp.strftime("%Y-%m-%d %H:%M:%S")
write_post(header, body, post_path)
edit_post(post_path)
puts "Updated: #{post_path}"
end