-
Notifications
You must be signed in to change notification settings - Fork 39
/
Rakefile
362 lines (307 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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
require 'colorize'
require 'yaml'
task :test_posts do
class LinterLine
attr_reader :file, :raw, :line_num, :content
def initialize(file, raw, line_num)
@file = file
@raw = raw
@line_num = line_num
@content = raw.chomp
end
end
class LinterError
attr_reader :file, :line, :message
def initialize(file, line, message)
@file = file
@line = line
@message = message
end
end
class Linter
def initialize(dir, glob, ignore = [])
@dir = dir
@glob = glob
@ignore = ignore
end
def run
errors = []
puts "Running linter checks on #{@dir} on #{@glob}".blue
posts = find_posts
posts.each do |post|
errors += check_front_matter(post)
errors += check_for_internal_urls(post)
errors += check_for_external_urls(post)
end
errors.each do |error|
if error.line
puts "#{error.file}:#{error.line.line_num} -> #{error.message}".red
else
puts "#{error.file} -> #{error.message}".red
end
end
abort if errors.length > 0
end
def check_front_matter(post)
errors = []
categories = YAML.load(File.open('./_config.yml') { |f| f.read })['category_list'].keys
authors = YAML.load(File.open('./_data/authors.yml') { |f| f.read }).map(&:first).map(&:last)
if ['.md'].include? File.extname(post)
f_m = YAML.load(File.open(post) { |f| f.read }[/---(.*?)---/m, 1])
if f_m['category'].nil?
errors << LinterError.new(post, nil, 'Post must have a category')
else
unless categories.include? f_m['category']
errors << LinterError.new(post, nil, 'Post category is not valid')
end
end
if f_m['layout'].nil?
errors << LinterError.new(post, nil, 'Post must have a layout')
else
unless f_m['layout'] == 'post'
errors << LinterError.new(post, nil, 'Post layout must be "post"')
end
end
if f_m['authors'].nil?
errors << LinterError.new(post, nil, 'Post must have authors')
else
unless f_m['authors'].is_a?(Array)
errors << LinterError.new(post, nil, 'Post authors must be an Array')
end
end
unless f_m['about_authors'].nil?
unless f_m['about_authors'].is_a?(Array)
errors << LinterError.new(post, nil, 'Post about_authors must be an Array')
end
unless (f_m['about_authors'] - authors).empty?
errors << LinterError.new(post, nil, 'Post about_authors ids must exist on \'_data/authors.yml\'')
end
end
if f_m['header_image'].nil?
errors << LinterError.new(post, nil, 'Post must have a header_image')
else
unless File.file?('./assets/img/pages/headers/' + f_m['header_image'])
errors << LinterError.new(post, nil, 'Post header_image doesn\'t exist')
end
end
if !f_m['tags'].nil? && !f_m['tags'].is_a?(Array)
errors << LinterError.new(post, nil, 'Post tags must be an Array')
end
if f_m['date'].nil?
errors << LinterError.new(post, nil, 'Post must have a date')
else
unless (Date.strptime(f_m['date'].to_s, '%Y-%m-%d') rescue nil)
errors << LinterError.new(post, nil, 'Post date is not valid')
end
end
if f_m['title'].nil?
errors << LinterError.new(post, nil, 'Post must have a title')
end
if !f_m['header_position'].nil? && !(['top','center','bottom'].include? f_m['header_position'])
errors << LinterError.new(post, nil, 'Post header_position must be "top", "center" or "bottom"')
end
end
errors
end
def check_for_internal_urls(file)
errors = []
if ['.md'].include? File.extname(file)
lines = read_file_lines(file)
lines.each do |line|
if line.content =~ /(\[.*\]\(.*developer.epages.com[^ {}\(\)]+\))/
errors << LinterError.new(file, line, 'Linking to an internal URL. Use relative path')
end
end
end
errors
end
def check_for_external_urls(file)
errors = []
if ['.md'].include? File.extname(file)
lines = read_file_lines(file)
lines.each do |line|
if line.content =~ /(\[.*?\]\(https?(?!.*developer.epages.com.*)[^ {}\(\)]+\)(?!{:target="_blank"}))/
errors << LinterError.new(file, line, 'Linking to an external URL without using {:target="_blank"}')
end
end
end
errors
end
def find_posts
Dir.glob(File.join(@dir, @glob))
.select { |file| not @ignore.any? { |ign| file.start_with? ign } }
.select { |file| File.file?(file) }
end
def read_file_lines(file)
File.foreach(file).with_index.map { |raw, line_num| LinterLine.new(file, raw, line_num + 1) }
end
end
Linter.new('./_posts/', '**/*.*').run
end
task :test_files do
class LinterLine
attr_reader :file, :raw, :line_num, :content
def initialize(file, raw, line_num)
@file = file
@raw = raw
@line_num = line_num
@content = raw.chomp
end
end
class LinterError
attr_reader :file, :line, :message
def initialize(file, line, message)
@file = file
@line = line
@message = message
end
end
class Linter
def initialize(dir, glob, ignore = [])
@dir = dir
@glob = glob
@ignore = ignore
end
def run
errors = []
puts "Running linter checks on #{@dir} on #{@glob}".blue
files = find_files
files.each do |file|
errors += check_line_endings(file)
errors += check_trailing_whitespaces(file)
errors += check_indentation(file)
errors += check_filename(file)
end
errors += check_sidebar('_data/beyond_essence.yml')
errors.each do |error|
if error.line
puts "#{error.file}:#{error.line.line_num} -> #{error.message}".red
else
puts "#{error.file} -> #{error.message}".red
end
end
abort if errors.length > 0
end
def check_line_endings(file)
errors = []
if ['.html', '.scss', '.js', '.yml', '.md'].include? File.extname(file)
lines = read_file_lines(file)
lines.each do |line|
if line.raw.end_with? "\r\n"
errors << LinterError.new(file, line, 'Found windows line ending (CRLF)')
end
if line.raw.end_with? "\r"
errors << LinterError.new(file, line, 'Found old Macintosh line ending (LF)')
end
end
end
errors
end
def check_trailing_whitespaces(file)
errors = []
if ['.html', '.scss', '.js', '.yml'].include? File.extname(file)
lines = read_file_lines(file)
lines.each do |line|
if line.content =~ /\s+$/
errors << LinterError.new(file, line, 'Found trailing whitespaces')
end
end
end
errors
end
def check_indentation(file)
errors = []
if ['.html', '.scss', '.js', '.yml', '.md', '.rb'].include? File.extname(file)
lines = read_file_lines(file)
lines.each do |line|
if line.content =~ /^\s*\t+\s*/
errors << LinterError.new(file, line, 'Contains tabs in indentation')
end
end
end
errors
end
def check_filename(file)
errors = []
unless file =~ /^[a-z0-9\-\_\.\/]+$/
errors << LinterError.new(file, nil, 'Filenames must only contain lower case letters, numbers, dashes, underscores or points')
end
errors
end
def check_sidebar(file)
errors = []
# Check if it's a valid yaml file
begin
YAML.parse(File.open(file))
rescue
errors << LinterError.new(file, nil, 'Not a valid YAML format')
end
unless file =~ /^[a-z0-9\-\_\.\/]+$/
errors << LinterError.new(file, nil, 'Filenames must only contain lower case letters, numbers, dashes, underscores or points')
end
errors
end
def find_files
Dir.glob(File.join(@dir, @glob))
.select { |file| not @ignore.any? { |ign| file.start_with? ign } }
.select { |file| File.file?(file) }
end
def read_file_lines(file)
File.foreach(file).with_index.map { |raw, line_num| LinterLine.new(file, raw, line_num + 1) }
end
end
Linter.new('.', '*/**/*.*', ['./_site',
'./.bundle',
'./.git',
'./.sass_cache',
'./.tweet-cache',
'./assets/fonts',
'./vendor']).run
end
task :test_html do
require 'html-proofer'
url_ignore = [/.*apps.*/,
/.*signup/,
/.*terms-and-conditions.*/,
/.*beyond-docs.*/]
file_ignore = [/.*instagram.*/]
options = { disable_external: true,
url_ignore: url_ignore,
empty_alt_ignore: true,
file_ignore: file_ignore,
check_html: true,
allow_hash_href: true }
HTMLProofer.check_directory('./_site', options).run
end
task :test do
sh 'bundle exec jekyll build'
sh 'rake test_ci'
end
task :test_ci do
sh 'rake test_html'
sh 'rake test_files'
sh 'rake test_posts'
end
task :write do
require 'date'
write_file = '_config_write.yml'
date_from = Date.new(2015)
date_to = Date.today.prev_month.prev_month
File.delete(write_file) if File.exist?(write_file)
File.new(write_file, 'w')
exclude = { 'exclude' => YAML.load_file('_config.yml')['exclude'],
'sass' => { 'sass_dir' => '_sass',
'style' => 'expanded' } }
exclude['exclude'].push '_pages/categories/'
exclude['exclude'].push '_pages/api/categories/'
exclude['exclude'].push '_pages/404.html'
exclude['exclude'].push '_pages/about.md'
exclude['exclude'].push '_pages/devjobs.html'
exclude['exclude'].push '_pages/index.html'
exclude['exclude'].push '_pages/search.html'
exclude['exclude'].push '_pages/terms-and-conditions.md'
exclude['exclude'].push *(date_from.year..date_to.prev_year.year).map { |d| "_posts/#{d}" }.uniq
exclude['exclude'].push *(Date.new(date_to.year)..date_to).map { |d| "_posts/#{d.year}/#{d.year}-#{'%02d' % d.month}-*" }.uniq
File.open(write_file, 'w+') { |f| f.puts exclude.to_yaml }
sh 'bundle exec jekyll serve --config _config.yml,_config_write.yml --host 0.0.0.0'
end