-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rakefile.rb
139 lines (128 loc) · 4.08 KB
/
Rakefile.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
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
require 'rubygems'
require 'bundler/setup'
require 'stringex'
require 'fileutils'
## -- Config -- ##
public_dir = "public" # compiled site directory
posts_dir = "_posts" # directory for blog files
drafts_dir = "_drafts" # directory for draft blog files
new_post_ext = "md" # default new post file extension when using the new_post task
new_page_ext = "md" # default new page file extension when using the new_page task
#############################
# Start development #
#############################
desc "compile and run the blog site"
task :default do
pids = [
spawn("bundle exec jekyll server --watch --drafts"),
]
trap "INT" do
Process.kill "INT", *pids
exit 1
end
loop do
sleep 1
end
end
#############################
# Create a new Post or Page #
#############################
# usage rake new_post
desc "Create a new post in #{posts_dir}"
task :new_post, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your post: ")
end
filename = "#{posts_dir}/#{Time.now.strftime('%Y-%m-%d')}-#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
category = get_stdin("Enter category to your post: ")
tags = get_stdin("Enter tags to classify your post (comma separated): ")
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "tags: [#{tags}]"
post.puts "category: #{category}"
post.puts "links:"
post.puts "keywords:"
post.puts "---"
end
end
# usage rake new_draft
desc "Create a new draft in #{drafts_dir}"
task :new_draft, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your draft: ")
end
filename = "#{drafts_dir}/#{title.to_url}.#{new_post_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
dirname = File.dirname(drafts_dir)
unless File.directory?(dirname)
FileUtils.mkdir_p(dirname)
end
category = get_stdin("Enter category to your draft: ")
tags = get_stdin("Enter tags to classify your draft (comma separated): ")
puts "Creating new draft: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/&/,'&')}\""
post.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M:%S %z')}"
post.puts "tags: [draft, #{tags} ]"
post.puts "category: #{category}"
post.puts "links:"
post.puts "keywords:"
post.puts "---"
end
end
# usage rake new_page
desc "Create a new page"
task :new_page, :title do |t, args|
if args.title
title = args.title
else
title = get_stdin("Enter a title for your page: ")
end
filename = "#{title.to_url}.#{new_page_ext}"
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
tags = get_stdin("Enter tags to classify your page (comma separated): ")
puts "Creating new page: #{filename}"
open(filename, 'w') do |page|
page.puts "---"
page.puts "layout: page"
page.puts "permalink: /#{title.to_url}/"
page.puts "title: \"#{title}\""
page.puts "modified: #{Time.now.strftime('%Y-%m-%d %H:%M')}"
page.puts "tags: [#{tags}]"
page.puts "image:"
page.puts " feature: "
page.puts " credit: "
page.puts " creditlink: "
page.puts "share: "
page.puts "---"
end
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end