-
Notifications
You must be signed in to change notification settings - Fork 15
/
Rakefile
336 lines (309 loc) · 10.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
abort('Please run this using `bundle exec rake`') unless ENV["BUNDLE_BIN_PATH"]
require 'html-proofer'
require 'rszr'
task :default => ["test"]
task :test => [:checkbibtex, :spellcheck, :htmlproofer] do
puts "Tests complete."
end
task :resize160webp do
sh "mkdir -p assets/images/articles/160webp"
sh "mkdir -p assets/images/photos/160webp"
files = FileList['assets/images/articles/original/*.jpg', 'assets/images/photos/original/*.jpg']
files.each do |file|
puts "Resizing #{file}..."
target = file.gsub("original", "160webp").gsub("jpg","webp")
Rszr::Image.load(file).resize!(160, 120).save(target)
end
puts "Image resize complete."
end
task :resize160 do
sh "mkdir -p assets/images/articles/160"
sh "mkdir -p assets/images/photos/160"
files = FileList['assets/images/articles/original/*.jpg', 'assets/images/photos/original/*.jpg']
files.each do |file|
puts "Resizing #{file}..."
target = file.gsub("original", "160")
Rszr::Image.load(file).resize!(160, 120).save(target)
end
puts "Image resize complete."
end
task :resize240webp do
sh "mkdir -p assets/images/articles/240webp"
sh "mkdir -p assets/images/photos/240webp"
files = FileList['assets/images/articles/original/*.jpg', 'assets/images/photos/original/*.jpg']
files.each do |file|
puts "Resizing #{file}..."
target = file.gsub("original", "240webp").gsub("jpg","webp")
Rszr::Image.load(file).resize!(240, 180).save(target)
end
puts "Image resize complete."
end
task :resize240 do
sh "mkdir -p assets/images/articles/240"
sh "mkdir -p assets/images/photos/240"
files = FileList['assets/images/articles/original/*.jpg', 'assets/images/photos/original/*.jpg']
files.each do |file|
puts "Resizing #{file}..."
target = file.gsub("original", "240")
Rszr::Image.load(file).resize!(240, 180).save(target)
end
puts "Image resize complete."
end
task :resize600webp do
sh "mkdir -p assets/images/articles/600webp"
sh "mkdir -p assets/images/photos/600webp"
files = FileList['assets/images/articles/original/*.jpg', 'assets/images/photos/original/*.jpg']
files.each do |file|
puts "Resizing #{file}..."
target = file.gsub("original", "600webp").gsub("jpg","webp")
Rszr::Image.load(file).resize!(600, 450).save(target)
end
puts "Image resize complete."
end
task :resize600 do
sh "mkdir -p assets/images/articles/600"
sh "mkdir -p assets/images/photos/600"
files = FileList['assets/images/articles/original/*.jpg', 'assets/images/photos/original/*.jpg']
files.each do |file|
puts "Resizing #{file}..."
target = file.gsub("original", "600")
Rszr::Image.load(file).resize!(600, 450).save(target)
end
puts "Image resize complete."
end
task :resize => [:resize240, :resize600, :resize240webp, :resize600webp] do
puts "Tests complete."
end
desc "Make sure all bibtex entries have urls, that there are no duplicates, and count the number of doi-less articles and incollections"
task :checkbibtex do
require 'bibtex'
b = BibTeX.open('_bibliography/references.bib')
bad_entries = b['@entry[url!~.*]']
bad_entries.each do |entry|
puts "Found entry without url: #{entry.key}"
end
if bad_entries.length > 0 then
exit 1
else
puts "No url-less bibtex entries found!"
puts
end
if b.duplicates? then
puts "Duplicate entries in bibtex found:"
puts b.duplicates.map { |ds| ds.map { |d| d.key }.to_s }
puts "Are these genuinely different?"
puts
end
doiless = b['@article[doi!~.*], @incollection[doi!~.*]']
if doiless.length > 0 then
puts "Articles and incollections without doi:"
puts doiless.map {|e| e.key}
puts "Can any of these be given dois?"
end
end
desc "Test for broken internal links"
task :checklinksinternal => :jekyll do
options = {
:assume_extension => ".html",
:disable_external => true,
:empty_alt_ignore => true,
:alt_ignore => [ /.*/ ],
:url_ignore => [ "https://github.com//issues", "https://www.uka.org.uk/EasysiteWeb/getresource.axd?AssetID=169662" ],
:internal_domains => [ "genderkit.github.io", "127.0.0.1", "genderkit.org.uk" ],
:url_swap => {
/https?\:\/\/genderkit\.github\.io\/genderkit\// => "/",
/\/?genderkit\// => "/"
},
:typhoeus => {
:ssl_verifypeer => false,
:ssl_verifyhost => 0,
:timeout => 60
}
}
HTMLProofer.check_directory("./_site", options).run
end
desc "Test for broken external links"
task :checklinks => :jekyll do
options = {
:assume_extension => ".html",
:disable_external => false,
:empty_alt_ignore => true,
:alt_ignore => [ /.*/ ],
:internal_domains => [ "genderkit.github.io", "127.0.0.1", "genderkit.org.uk" ],
:url_ignore => [ "https://github.com//issues", "https://qtipcic.co.uk" ],
:url_swap => {
/https?\:\/\/genderkit\.github\.io\/genderkit\// => "/",
/\/?genderkit\// => "/"
},
:typhoeus => {
:ssl_verifypeer => false,
:ssl_verifyhost => 0,
:timeout => 60,
:headers => {
"User-Agent" => "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
"Accept" => '*/*'
}
}
}
HTMLProofer.check_directory("./_site", options).run
end
desc "Rewrite URLs in bibliography sections to be real links"
task :referencelinks do
# Update bibliography sections to include real HTML links to sources
files = FileList['_site/article/*/*.html','_site/resources/*/*.html','_site/*/*.html','_site/explore/*/*.html']
files.each do |file_name|
text = File.open(file_name, 'r'){ |file| file.read }
if text.gsub!(/\[online\] Available from: ([^\s<]*)/, '<a href="\1">Link</a>')
puts "rewriting #{file_name}..."
File.open(file_name, 'w'){|file| file.write(text)}
end
end
end
desc "Convert icons from SVG to PNG"
task :converticons do
files = FileList['assets/images/icons/svg/*.svg']
files.each do |file|
puts "Converting #{file}..."
target = file.gsub("icons/svg/", "icons/")
target = target.gsub(".svg", ".png")
sh "inkscape --export-png=#{target} --export-width=128 --export-height=128 #{file}"
end
end
desc "Convert logo from SVG to PNG"
task :convertlogo do
files = FileList['assets/images/logo/logo.svg']
files.each do |file|
puts "Converting #{file}..."
target = file.gsub(".svg", ".png")
sh "inkscape --export-png=#{target} --export-width=768 --export-height=384 #{file}"
Rszr::Image.load(target).save(target.gsub(".png", ".webp"))
Rszr::Image.load(target).resize!(192,96).save(target.gsub(".png", "192.png"))
Rszr::Image.load(target).resize!(192,96).save(target.gsub(".png", "192.webp"))
end
end
desc "Convert illustrations from SVG to PNG and WEBP"
task :convertillustrations do
files = FileList['assets/images/clothes/svg/*.svg']
files.each do |file|
puts "Converting #{file}..."
target = file.gsub("clothes/svg/", "clothes/")
target = target.gsub(".svg", ".png")
sh "inkscape --export-png=#{target} --export-area-drawing -y 0 #{file}"
end
end
desc "Build the site using Jekyll"
task :jekyll => :resize do
sh "bundle exec jekyll build"
Rake::Task["referencelinks"].invoke
end
desc "Test using proselint"
task :proselint => :jekyll do
files = FileList['_articles/*.md', '_identities/*.md']
files.each do |file|
results = `proselint #{file}`
if (results.length > 0)
puts "In #{file}:"
puts "#{results}"
end
end
end
class InternalLinkTrailingSlash < ::HTMLProofer::Check
def run
@html.css('a').each do |node|
@link = create_element(node)
if @link.internal? and not (@link.href.match(/\/(#.*)?$/) or @link.href[0] == "#" or @link.href.match(/\/.*\.pdf$/))
return add_issue("No trailing slash on internal link #{@link.href}", line: node.line)
end
end
end
end
class ProofHeaderIDs < ::HTMLProofer::Check
def run
@html.css('h1,h2,h3,h4,h5,h6').each do |node|
@link = create_element(node)
line = node.line
if @link.id.nil?
return add_issue("Heading has missing ID!", line: line)
end
end
end
end
desc "Test the built html"
task :htmlproofer => :jekyll do
options = {
:assume_extension => ".html",
:disable_external => true,
:check_html => true,
:enforce_https => false,
:allow_missing_href => false,
:validation => {
:report_missing_names => true
},
:url_swap => {
/(genderkit\.github\.io)?\/genderkit\// => "genderkit.github.io"
},
:internal_domains => [ "genderkit.github.io", "127.0.0.1", "genderkit.org.uk" ],
}
HTMLProofer.check_directory("./_site", options).run
end
def run_spellcheck(file)
cmd = "cat #{file} | aspell -p './whitelist' -H -d en_GB --encoding utf-8 --rem-html-check=alt --lset-html-skip ol:script:style list | cat"
result = `#{cmd}`
result
end
def run_spellcheck_markdown(file)
cmd = "cat #{file} | sed 's/image:.*//g' | sed 's/url:.*//g' | sed 's/creator:.*//g' | sed 's/{%[^%]*%}//g' | aspell -p './whitelist' -M -d en_GB --encoding utf-8 list | cat"
result = `#{cmd}`
result
end
desc "Spellcheck using Aspell"
task :spellcheck => [:spellcheckmarkdown, :jekyll] do
errors = 0
files = FileList['_site/**/*.html']
files.exclude("_site/books/*.html")
files.exclude("_site/organisations/*.html")
files.exclude("_site/publications/*.html")
files.exclude("_site/studies/*.html")
files.exclude("_site/credits/*.html")
files.exclude("_site/tools/*")
files.exclude("_site/explore/names-*/*.html")
files.exclude("_site/resources/national/*.html")
files.exclude("_site/resources/local/*.html")
files.exclude("_site/resources/research/*.html")
files.each do |file|
results = run_spellcheck(file)
if (results.length > 0)
puts "Found spelling errors in #{file}:"
puts results
errors = errors + 1
end
end
if errors > 0
puts "Spelling errors found! If you believe these words are spelled correctly, add them to the file called 'whitelist'."
exit 1
else
puts "No spelling errors found in built site."
end
end
desc "Spellcheck markdown files using Aspell"
task :spellcheckmarkdown do
errors = 0
files = FileList['**/*.md']
files.exclude("CONTRIBUTING.md")
files.exclude("HELP-WANTED.md")
files.exclude("credits.md")
files.each do |file|
results = run_spellcheck_markdown(file)
if (results.length > 0)
puts "Found spelling errors in #{file}:"
puts results
errors = errors + 1
end
end
if errors > 0
puts "Spelling errors found! If you believe these words are spelled correctly, add them to the file called 'whitelist'."
exit 1
else
puts "No spelling errors found in Markdown sources."
end
end