-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
278 lines (224 loc) · 7.11 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
require 'rake'
require 'date'
require 'fileutils'
require 'open-uri'
require 'json'
require 'yaml'
require 'net/http'
require 'optparse'
require 'tzinfo'
def slugify(title)
slug = title.downcase
.gsub(/\s+|_/, '-')
.gsub(/[^a-z0-9\-]/, '')
.gsub(/^-|-$/, '')
slug
end
configuredTimezone = YAML.load_file('_config.yml')['timezone']
timezone = TZInfo::Timezone.get(configuredTimezone)
task :default => [:install, :serve, :build]
desc 'Install dependencies using Bundler'
task :install do
sh 'bundle install'
sh 'rake postinstall'
end
desc 'Post install tasks'
task :postinstall do
# Copy bootstrap files
bootstrap_gem_path = `bundle show bootstrap`.strip
sass_target_path = '_sass/bootstrap'
js_target_path = 'assets/js/bootstrap'
FileUtils.rm_rf(sass_target_path)
FileUtils.rm_rf(js_target_path)
FileUtils.mkdir_p(sass_target_path)
FileUtils.mkdir_p(js_target_path)
FileUtils.cp_r(Dir.glob("#{bootstrap_gem_path}/assets/stylesheets/*"), sass_target_path)
FileUtils.cp_r(Dir.glob("#{bootstrap_gem_path}/assets/javascripts/*"), js_target_path)
# Download lunr.js from https://unpkg.com/lunr/lunr.js and save it to assets/js/lunr.js
open('assets/js/lunr.js', 'wb') do |file|
file << URI.open('https://unpkg.com/lunr/lunr.min.js').read
end
end
desc 'Notify about new posts'
task :notify_new_post do
api_key = ENV['API_KEY']
if api_key.nil?
# try reading from secrets.yml
begin
secrets = YAML.load_file('secrets.yml')
api_key = secrets['apiKey']
rescue
end
end
if api_key.nil?
puts 'API_KEY is not set, not notifying'
exit 1
end
if !File.exist?('new_post.json')
puts 'No new post was created'
exit 0
end
new_post_raw = File.read('new_post.json')
if new_post_raw.nil?
puts 'Failed to read new_post.json'
exit 1
end
new_post = JSON.parse(new_post_raw)
required_props = ['title', 'url', 'summary']
if required_props.any? { |prop| new_post[prop].nil? }
puts 'new_post.json is missing one or more required properties: ' + required_props.join(', ')
exit 1
end
# safety check so we don't send accidentally notify about old posts
# if the latest post is not from within the last 24 hours, don't notify
if DateTime.parse(new_post['date']).to_time.utc < Time.now.utc - 24 * 60 * 60
puts 'Latest post is older than 24 hours, not notifying'
exit 0
end
puts 'Notifying about new post'
puts new_post
# Send POST to https://blog-api.ordbokapi.org/new-post
# The API server expects a JSON body like this:
# {
# "title": "New post title",
# "url": "https://blog.ordbokapi.org/2020/01/01/new-post.html",
# "summary": "A short summary of the new post"
# }
# The API server will respond with a 202 Accepted status code if the request is successful.
# The API key is expected to be passed in the Authorization header.
# read api server url from _data/api.yml (baseUrl)
api_config = YAML.load_file('_data/api.yml')
api_url = api_config['baseUrl'] + '/new-post'
uri = URI.parse(api_url)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
end
request = Net::HTTP::Post.new(uri.request_uri)
request['Authorization'] = api_key
request['Content-Type'] = 'application/json'
request.body = JSON.generate({
title: new_post['title'],
url: new_post['url'],
summary: new_post['summary']
})
response = http.request(request)
if response.code == '202'
puts 'Notification sent successfully'
else
puts 'Failed to send notification'
puts response.code
puts response.body
exit 1
end
end
desc 'Start the Jekyll server'
task :serve do
sh 'bundle exec jekyll serve --drafts --livereload'
end
desc 'Build the site'
task :build do
sh 'bundle exec jekyll build'
end
namespace :draft do |args|
desc 'Create a new draft'
task :new do
title = nil
OptionParser.new do |opts|
opts.banner = "Usage: rake draft:new -- -t 'Draft title'"
opts.on("-t", "--title TITLE", "Title of the draft") do |t|
title = t
end
args = opts.order!(ARGV) {}
opts.parse!(args)
end
if title.nil?
puts 'No title specified'
exit 1
end
filename = "_drafts/#{slugify(title)}.md"
if File.exist?(filename)
puts 'File already exists'
exit 1
end
FileUtils.mkdir_p('_drafts')
File.open(filename, 'w') do |file|
file.write("---\n")
file.write("layout: post\n")
file.write("title: \"#{title}\"\n")
file.write("date: #{DateTime.now.strftime('%Y-%m-%d %H:%M:%S %z')}\n")
file.write("author: \n")
file.write("image: \n")
file.write("categories: \n")
file.write("summary: \"\"\n")
file.write("---\n")
end
FileUtils.mkdir_p("assets/attachments/drafts/#{slugify(title)}")
puts "Created new draft: #{filename}"
exit 0
end
desc 'Publish a draft or all drafts'
task :publish do
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: rake draft:publish -- [options]"
opts.on("-t", "--title TITLE", "Title of the draft to publish") do |t|
options[:title] = t
end
opts.on("-a", "--all", "Publish all drafts") do |a|
options[:all] = a
end
args = opts.order!(ARGV) {}
opts.parse!(args)
end
publish_draft = lambda do |title|
slug = title.downcase.strip.gsub(' ', '-')
draft_filename = "_drafts/#{slug}.md"
if !File.exist?(draft_filename)
puts "Draft not found: #{draft_filename}"
return
end
now = DateTime.now
utcDate = now.new_offset(0)
localDate = timezone.utc_to_local(utcDate)
date = localDate.strftime('%Y-%m-%d')
filename = "_posts/#{date}-#{slug}.md"
if File.exist?(filename)
puts "Post already exists: #{filename}"
return
end
FileUtils.mv(draft_filename, filename)
content = File.read(filename)
content = content.sub(/date: .*/, "date: #{now.strftime('%Y-%m-%d %H:%M:%S %z')}")
File.open(filename, 'w') { |file| file.puts content }
draft_attachments_dir = "assets/attachments/drafts/#{slug}"
post_attachments_dir = "assets/attachments/#{now.strftime('%Y/%m/%d')}/#{slug}"
if Dir.exist?(draft_attachments_dir)
# only move the files if the post has attachments
if Dir.glob("#{draft_attachments_dir}/*").length > 0
FileUtils.mkdir_p(post_attachments_dir)
FileUtils.mv(Dir.glob("#{draft_attachments_dir}/*"), post_attachments_dir)
end
FileUtils.remove_dir(draft_attachments_dir)
end
puts "Published draft: #{filename}"
end
if options[:all]
drafts = Dir.glob('_drafts/*.md')
drafts.each do |draft|
title = File.basename(draft, '.md')
publish_draft.call(title)
end
elsif options[:title]
publish_draft.call(options[:title])
else
puts 'Error: No title specified and --all not set. Use --title to specify a draft or --all to publish all drafts.'
exit 1
end
exit 0
end
end
desc 'Get a timestamp suitable for post front matter'
task :timestamp do
puts DateTime.now.strftime('%Y-%m-%d %H:%M:%S %z')
end