This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
184 lines (157 loc) · 4.85 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
task :default => :build
#
# Set the values of
#
# $deploy_url = "http://www.example.com/somedir" # where the system will live
# $deploy_dir = "user@host:~/some-location/" # where the sources live
# $post_dir = "_posts/" # where posts are created
#
# ... or load them from a file, e.g.:
#
load '_rake-configuration.rb'
#
# Tasks start here
#
desc 'Clean up generated site'
task :clean do
cleanup
end
desc 'Preview on local machine (server with --auto)'
task :preview => :clean do
set_url('http://localhost:4000')
jekyll('serve --watch')
end
desc 'Static build (build using filesystem)'
task :build_static => :clean do
set_url(Dir.getwd + "/_site")
jekyll('build')
end
desc 'Build for deployment (but do not deploy)'
task :build => :clean do
set_url($deploy_url)
jekyll('build')
end
desc 'Build and deploy to remote server'
task :deploy => :build do
sh "rsync -avz --delete _site/ #{$deploy_dir}"
File.open("_last_deploy.txt", 'w') {|f| f.write(Time.new) }
end
desc 'Create a post'
task :create_post, [:post, :date, :content] do |t, args|
if args.post == nil or
(args.date and args.date.match(/[0-9]+-[0-9]+-[0-9]+/) == nil) then
puts "Usage: create post TITLE [DATE]"
puts "Date is in the form: Y-m-d"
exit 1
end
post_title= args.post
post_date= args.date || Time.new.strftime("%Y-%m-%d %H:%M:%S")
# remove the time from post_date (the filename does not support it)
filename = post_date[0..9] + "-" + post_title.gsub(' ', '_') + ".textile"
# generate a unique filename appending a number
i = 1
while File.exists?($post_dir + filename) do
filename = post_date[0..9] + "-" +
post_title.gsub(' ', '_') + "-" + i.to_s +
".textile"
i += 1
end
# the if is not really necessary anymore
if not File.exists?($post_dir + filename) then
File.open($post_dir + filename, 'w') do |f|
f.puts "---"
f.puts "title: \"#{post_title}\""
f.puts "layout: default"
f.puts "date: #{post_date}"
f.puts "---"
f.puts args.content if args.content != nil
end
puts "Post created under \"#{$post_dir}#{filename}\""
sh "open \"#{$post_dir}#{filename}\"" if args.content == nil
else
puts "A post with the same name already exists. Aborted."
end
# puts "You might want to: edit #{$post_dir}#{filename}"
end
desc 'Create a post with all changes since last deploy'
task :post_changes do |t, args|
content = "Recent changes on the website:\n\n"
content << "<ul>\n"
IO.popen('find * -newer _last_deploy.txt') do |io|
while (line = io.gets) do
content << myprocess(line)
end
end
content << "</ul>\n"
Rake::Task["create_post"].invoke("Recent Changes", Time.new.strftime("%Y-%m-%d %H:%M:%S"), content)
end
#
# support functions for generating list of changed files
#
def myprocess(filename)
new_filename = filename[0, filename.length - 1]
if file_matches(new_filename) then
puts new_filename
"<li><a href=\"{{site.url}}/#{file_change_ext(new_filename, ".html")}\">#{new_filename}</a></li>\n"
else
""
end
end
EXCLUSION_REGEXPS = [/.*~/, /_.*/, /javascripts/, /stylesheets/, /Rakefile/, /Gemfile/, /s[ca]ss/, /.*\.css/, /.*.js/]
def file_matches(filename)
output = EXCLUSION_REGEXPS.each.collect { |x| filename.index(x) != nil }.include?(true)
not output
end
def file_change_ext(filename, newext)
if File.extname(filename) == ".textile" or File.extname(filename) == ".md" then
filename.sub(File.extname(filename), newext)
else
filename
end
end
desc 'Check links for site already running on localhost:4000'
task :check_links do
begin
require 'anemone'
root = 'http://localhost:4000/'
Anemone.crawl(root, :discard_page_bodies => true) do |anemone|
anemone.after_crawl do |pagestore|
broken_links = Hash.new { |h, k| h[k] = [] }
pagestore.each_value do |page|
if page.code != 200
referrers = pagestore.pages_linking_to(page.url)
referrers.each do |referrer|
broken_links[referrer] << page
end
end
end
broken_links.each do |referrer, pages|
puts "#{referrer.url} contains the following broken links:"
pages.each do |page|
puts " HTTP #{page.code} #{page.url}"
end
end
end
end
rescue LoadError
abort 'Install anemone gem: gem install anemone'
end
end
def cleanup
sh 'rm -rf _site'
end
def jekyll(directives = '')
sh 'jekyll ' + directives
end
# set the url in the configuration file
def set_url(url)
config_filename = "_config.yml"
text = File.read(config_filename)
url_directive = Regexp.new(/url: .*$/)
if text.match(url_directive)
puts = text.gsub(url_directive, "url: #{url}")
else
puts = text + "\nurl: #{url}"
end
File.open(config_filename, "w") { |file| file << puts }
end