forked from johno/pixyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
111 lines (96 loc) · 2.88 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
drafts_dir = '_drafts'
posts_dir = '_posts'
# rake post['my new post']
desc 'create a new post with "rake post[\'post title\']"'
task :post, :title do |t, args|
if args.title
title = args.title
else
puts "Please try again. Remember to include the filename."
end
mkdir_p "#{posts_dir}"
filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.downcase.gsub(/[^\w]+/, '-')}.md"
puts "Creating new post: #{filename}"
File.open(filename, "w") do |f|
f << <<-EOS.gsub(/^ /, '')
---
layout: post
title: #{title}
date: #{Time.new.strftime('%Y-%m-%d %H:%M')}
categories:
---
EOS
end
# Uncomment the line below if you want the post to automatically open in your default text editor
# system ("#{ENV['EDITOR']} #{filename}")
end
# usage: rake draft['my new draft']
desc 'create a new draft post with "rake draft[\'draft title\']"'
task :draft, :title do |t, args|
if args.title
title = args.title
else
puts "Please try again. Remember to include the filename."
end
mkdir_p "#{drafts_dir}"
filename = "#{drafts_dir}/#{title.downcase.gsub(/[^\w]+/, '-')}.md"
puts "Creating new draft: #{filename}"
File.open(filename, "w") do |f|
f << <<-EOS.gsub(/^ /, '')
---
layout: post
title: #{title}
date: #{Time.new.strftime('%Y-%m-%d %H:%M')}
categories:
---
EOS
end
# Uncomment the line below if you want the draft to automatically open in your default text editor
# system ("#{ENV['EDITOR']} #{filename}")
end
desc 'preview the site with drafts'
task :preview do
puts "## Generating site"
puts "## Stop with ^C ( <CTRL>+C )"
system "jekyll serve --watch --drafts"
end
# usage: rake undraft['my-file.md']
desc 'publish a draft post with "rake undraft[\'draft-file.md\']"'
task :undraft, :file do |t, args|
if args.file
file = args.file
else
abort "Please try again. Remember to include the file name."
end
draft = "#{drafts_dir}/#{file}"
unless File.exists?(draft)
abort "Draft does not exist: #{draft}"
end
today = Time.now
post = "#{posts_dir}/#{today.strftime('%Y-%m-%d')}-#{file}"
# Slurp file in to memory
lines = IO.readlines(draft).map do |line|
dateline = /\s*^date:\s*(.*)\s*$/.match(line)
if dateline
puts "Original date of draft: #{dateline[1]}"
"date: #{today.strftime('%Y-%m-%d %H:%M')}"
else
line
end
end
print "Moving #{draft} to #{post}..."
FileUtils.mv(draft, post)
puts "done."
print "Modifying date for post to '#{today.strftime('%Y-%m-%d %H:%M')}'..."
File.open(post, 'w') do |file|
file.puts lines
end
puts "done."
# Uncomment the line below if you want the post to automatically open in your default text editor
# system ("#{ENV['EDITOR']} #{post}")
end
desc 'list tasks'
task :list do
puts "Tasks: #{(Rake::Task.tasks - [Rake::Task[:list]]).join(', ')}"
puts "(type rake -T for more detail)\n\n"
end