-
Notifications
You must be signed in to change notification settings - Fork 9
/
Rakefile
219 lines (185 loc) · 5.6 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#############################################################################
#
# Modified version of jekyllrb Rakefile
# https://github.com/jekyll/jekyll/blob/master/Rakefile
#
#############################################################################
require 'rake'
require 'date'
require 'yaml'
CONFIG = YAML.load(File.read('_config.yml'))
USERNAME = CONFIG["username"] || ENV['GIT_NAME']
REPO = CONFIG["repo"] || "#{USERNAME}.github.io"
# Determine source and destination branch
# User or organization: source -> master
# Project: master -> gh-pages
# Name of source branch for user/organization defaults to "source"
if REPO == "#{USERNAME}.github.io"
SOURCE_BRANCH = CONFIG['branch'] || "source"
DESTINATION_BRANCH = "master"
else
SOURCE_BRANCH = "master"
DESTINATION_BRANCH = "gh-pages"
end
#############################################################################
#
# Helper functions
#
#############################################################################
def replace_header(head, header_name)
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
end
def normalize_bullets(markdown)
markdown.gsub(/\s{2}\*{1}/, "-")
end
def linkify_prs(markdown)
markdown.gsub(/#(\d+)/) do |word|
"[#{word}]({{ site.repository }}/issues/#{word.delete("#")})"
end
end
def linkify_users(markdown)
markdown.gsub(/(@\w+)/) do |username|
"[#{username}](https://github.com/#{username.delete("@")})"
end
end
def linkify(markdown)
linkify_users(linkify_prs(markdown))
end
def liquid_escape(markdown)
markdown.gsub(/(`{[{%].+[}%]}`)/, "{% raw %}\\1{% endraw %}")
end
def remove_head_from_history(markdown)
index = markdown =~ /^##\s+\d+\.\d+\.\d+/
markdown[index..-1]
end
def converted_history(markdown)
remove_head_from_history(liquid_escape(linkify(normalize_bullets(markdown))))
end
# File activesupport/lib/active_support/inflector/transliterate.rb, line 80
def parameterize(string, sep = '-')
# replace accented chars with their ascii
# simplified from original to remove dependency
parameterized_string = string.dup.force_encoding('US-ASCII')
# Turn unwanted chars into the separator
# changed from original: allow A-Z
parameterized_string.gsub!(/[^a-zA-Z0-9\-_]+/, sep)
unless sep.nil? || sep.empty?
re_sep = Regexp.escape(sep)
# No more than one of the separator in a row.
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
# Remove leading/trailing separator.
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/, '')
end
parameterized_string.downcase
end
def check_destination
unless Dir.exist? CONFIG["destination"]
sh "mkdir #{CONFIG["destination"]}"
end
end
#############################################################################
#
# Post and page tasks
#
#############################################################################
namespace :post do
desc "Create a new post"
task :create do
title = ENV["title"] || "new-post"
begin
slug = parameterize(title)
puts slug
rescue => e
puts "Error: invalid characters in title"
exit -1
end
begin
date = ENV['date'] ? Date.parse(ENV['date']) : Date.today
rescue => e
puts "Error: date format must be YYYY-MM-DD"
exit -1
end
filename = File.join("_posts", "#{date}-#{slug}.md")
if File.exist?(filename)
puts "Error: post already exists"
exit -1
end
header = { "layout" => "post", "title" => title }
content = header.to_yaml + "---\n"
if IO.write(filename, content)
puts "Post #{filename} created"
else
puts "Error: #{filename} could not be written"
end
end
end
namespace :page do
desc "Create a new page"
task :create do
title = ENV["title"] || "new-page"
begin
slug = parameterize(title)
puts slug
rescue => e
puts "Error: invalid characters in title"
exit -1
end
folder = ENV["folder"] || "."
filename = File.join(folder, "#{slug}.md")
if File.exist?(filename)
puts "Error: page already exists"
exit -1
end
header = { "layout" => "page", "title" => title }
content = header.to_yaml + "---\n"
if IO.write(filename, content)
puts "Page #{filename} created"
else
puts "Error: #{filename} could not be written"
end
end
end
#############################################################################
#
# Site tasks
#
#############################################################################
namespace :site do
desc "Generate the site"
task :build do
check_destination
sh "bundle exec jekyll build"
end
desc "Generate the site and serve locally"
task :serve do
check_destination
sh "bundle exec jekyll serve"
end
desc "Generate the site, serve locally and watch for changes"
task :watch do
sh "bundle exec jekyll serve --watch"
end
desc "Generate the site and push changes to remote origin"
task :deploy do
# Detect pull request
if ENV['TRAVIS_PULL_REQUEST'].to_s.to_i > 0
puts 'Pull request detected. Not proceeding with deploy.'
exit
end
# Configure git if this is run in Travis CI
if ENV["TRAVIS"]
sh "git config --global user.name '#{ENV['GIT_NAME']}'"
sh "git config --global user.email '#{ENV['GIT_EMAIL']}'"
sh "git config --global push.default simple"
end
# Make sure destination folder exists as git repo
check_destination
# Generate the site
sh "bundle exec nanoc"#"bundle exec jekyll build"
# Commit and push to github
sha = `git log`.match(/[a-z0-9]{40}/)[0]
Dir.chdir(CONFIG["destination"]) do
puts "My work is done, Will I Dream...?"
end
end
end