Skip to content

Commit 03cbf4b

Browse files
committed
Extract code from Rakefile to lib/
1 parent caab257 commit 03cbf4b

File tree

5 files changed

+166
-123
lines changed

5 files changed

+166
-123
lines changed

Rakefile

+10-123
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ rescue LoadError => e
1010
exit -1
1111
end
1212

13-
require 'pathname'
14-
1513
HOST = 'www.ruby-lang.org'
1614
LANGUAGES = %w[bg de en es fr id it ja ko pl pt ru tr vi zh_cn zh_tw]
1715
CONFIG = "_config.yml"
@@ -113,147 +111,36 @@ task :test => [:check]
113111

114112
namespace :check do
115113

116-
def read_yaml(filename)
117-
require 'yaml'
118-
match_data = File.read(filename).match(/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m)
119-
data = YAML.load(match_data[1]) if match_data
120-
121-
data || {}
122-
end
123-
124-
def author_variable_defined?(filename)
125-
read_yaml(filename).has_key?('author')
126-
end
127-
128-
def lang_variable_defined?(filename)
129-
read_yaml(filename).has_key?('lang')
130-
end
131-
132-
def pub_date_utc(filename)
133-
date = read_yaml(filename)['date']
134-
135-
date ? date.getutc.strftime('%Y/%m/%d') : nil
136-
end
137-
138-
def glob(pattern)
139-
Pathname.glob(pattern).reject {|path| path.expand_path.to_s =~ %r{\A#{Regexp.escape(Bundler.bundle_path.to_s)}/} }.map(&:to_s)
140-
end
141-
142114
desc "Check for missing author variables in news posts"
143115
task :author do
144-
print "Checking for missing author variables in news posts..."
145-
146-
md_files = glob("**/_posts/*.md")
147-
148-
author_missing = md_files.select {|fn| !author_variable_defined?(fn) }
149-
if author_missing.empty?
150-
puts " ok"
151-
else
152-
puts "\nNo author variable defined in:"
153-
puts author_missing.map {|s| " #{s}\n"}.join
154-
155-
raise
156-
end
116+
require_relative "lib/linter"
117+
Linter.new.check_author
157118
end
158119

159120
desc "Check for missing lang variables in markdown files"
160121
task :lang do
161-
print "Checking for missing lang variables in markdown files..."
162-
163-
md_files = glob("**/*.md")
164-
skip_patterns = [/README.md/, %r{[^/]*/examples/}, %r{\A_includes/}]
165-
166-
skip_patterns.each do |pattern|
167-
md_files.delete_if {|fn| fn =~ pattern }
168-
end
169-
170-
lang_missing = md_files.select {|fn| !lang_variable_defined?(fn) }
171-
if lang_missing.empty?
172-
puts " ok"
173-
else
174-
puts "\nNo lang variable defined in:"
175-
puts lang_missing.map {|s| " #{s}\n"}.join
176-
177-
raise
178-
end
122+
require_relative "lib/linter"
123+
Linter.new.check_lang
179124
end
180125

181126
desc "Check publication dates (UTC) for consistency with filename"
182127
task :pubdates do
183-
print "Checking for date mismatch in posts (filename / YAML front matter)..."
184-
185-
posts = glob("**/_posts/*.md")
186-
187-
date_mismatch = []
188-
posts.each do |post|
189-
filename_date = File.basename(post).split('-',4)[0..2].join('/')
190-
yaml_date = pub_date_utc(post)
191-
192-
date_mismatch << post if yaml_date && yaml_date != filename_date
193-
end
194-
195-
if date_mismatch.empty?
196-
puts " ok"
197-
else
198-
puts "\nDate mismatch in:"
199-
puts date_mismatch.map {|s| " #{s}\n"}.join
200-
201-
raise
202-
end
128+
require_relative "lib/linter"
129+
Linter.new.check_pubdates
203130
end
204131

205132
localport = 9292
206133

207134
desc "Check for broken links on http://localhost:#{localport}/"
208135
task :links do
209-
gem 'spidr', '~> 0.6'
210-
require 'spidr'
211-
212-
url_map = Hash.new { |hash,key| hash[key] = [] }
213-
214-
Spidr.site("http://localhost:#{localport}/") do |agent|
215-
LANGUAGES.each do |lang|
216-
agent.enqueue("http://localhost:#{localport}/#{lang}/")
217-
end
218-
219-
agent.every_link do |origin,dest|
220-
url_map[dest] << origin
221-
end
222-
223-
agent.every_page do |page|
224-
if page.code == 404
225-
origin = url_map[page.url].last
226-
dest = page.url.request_uri
227-
228-
external = URI::HTTP.build(
229-
:host => HOST,
230-
:path => page.url.path,
231-
:query => page.url.query
232-
)
233-
234-
if Net::HTTP.get_response(external).code == '404'
235-
puts "Old Broken Link: #{origin} -> #{dest}"
236-
else
237-
puts "New Broken Link: #{origin} -> #{dest}"
238-
end
239-
240-
raise
241-
end
242-
end
243-
end
136+
require_relative "lib/link_checker"
137+
LinkChecker.new.check(localport: localport, languages: LANGUAGES, host: HOST)
244138
end
245139

246140
desc 'Validate _site markup with validate-website'
247141
task :markup => :build do
248-
require 'jekyll'
249-
options = Jekyll.configuration
250-
Dir.chdir('_site') do
251-
system("validate-website-static",
252-
"--verbose",
253-
"--exclude", "examples",
254-
"--site", "#{options['url']}/")
255-
exit($?.exitstatus)
256-
end
142+
require_relative "lib/markup_checker"
143+
MarkupChecker.new.check
257144
end
258145
end
259146

_config.yml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ exclude:
1717
- Rakefile
1818
- README.md
1919
- unicorn.rb
20+
- lib
2021
- vendor
2122

2223
url: https://www.ruby-lang.org

lib/link_checker.rb

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
gem "spidr", "~> 0.6"
2+
require "spidr"
3+
4+
class LinkChecker
5+
6+
# Check for broken links on http://localhost:localport/
7+
def check(localport:, languages:, host:)
8+
9+
url_map = Hash.new { |hash,key| hash[key] = [] }
10+
11+
Spidr.site("http://localhost:#{localport}/") do |agent|
12+
languages.each do |lang|
13+
agent.enqueue("http://localhost:#{localport}/#{lang}/")
14+
end
15+
16+
agent.every_link do |origin,dest|
17+
url_map[dest] << origin
18+
end
19+
20+
agent.every_page do |page|
21+
if page.code == 404
22+
origin = url_map[page.url].last
23+
dest = page.url.request_uri
24+
25+
external = URI::HTTP.build(
26+
:host => host,
27+
:path => page.url.path,
28+
:query => page.url.query
29+
)
30+
31+
if Net::HTTP.get_response(external).code == '404'
32+
puts "Old Broken Link: #{origin} -> #{dest}"
33+
else
34+
puts "New Broken Link: #{origin} -> #{dest}"
35+
end
36+
37+
raise
38+
end
39+
end
40+
end
41+
end
42+
end

lib/linter.rb

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
require "bundler/setup"
2+
require "pathname"
3+
require 'yaml'
4+
5+
6+
class Linter
7+
8+
# Check for missing lang variables in markdown files
9+
def check_lang
10+
print "Checking for missing lang variables in markdown files..."
11+
12+
md_files = glob("**/*.md")
13+
skip_patterns = [/README.md/, %r{[^/]*/examples/}, %r{\A_includes/}]
14+
15+
skip_patterns.each do |pattern|
16+
md_files.delete_if {|fn| fn =~ pattern }
17+
end
18+
19+
lang_missing = md_files.select {|fn| !lang_variable_defined?(fn) }
20+
if lang_missing.empty?
21+
puts " ok"
22+
else
23+
puts "\nNo lang variable defined in:"
24+
puts lang_missing.map {|s| " #{s}\n"}.join
25+
26+
raise
27+
end
28+
end
29+
30+
# Check for missing author variables in news posts
31+
def check_author
32+
print "Checking for missing author variables in news posts..."
33+
34+
md_files = glob("**/_posts/*.md")
35+
36+
author_missing = md_files.select {|fn| !author_variable_defined?(fn) }
37+
if author_missing.empty?
38+
puts " ok"
39+
else
40+
puts "\nNo author variable defined in:"
41+
puts author_missing.map {|s| " #{s}\n"}.join
42+
43+
raise
44+
end
45+
end
46+
47+
# Check publication dates (UTC) for consistency with filename
48+
def check_pubdates
49+
print "Checking for date mismatch in posts (filename / YAML front matter)..."
50+
51+
posts = glob("**/_posts/*.md")
52+
53+
date_mismatch = []
54+
posts.each do |post|
55+
filename_date = File.basename(post).split('-',4)[0..2].join('/')
56+
yaml_date = pub_date_utc(post)
57+
58+
date_mismatch << post if yaml_date && yaml_date != filename_date
59+
end
60+
61+
if date_mismatch.empty?
62+
puts " ok"
63+
else
64+
puts "\nDate mismatch in:"
65+
puts date_mismatch.map {|s| " #{s}\n"}.join
66+
67+
raise
68+
end
69+
end
70+
71+
private
72+
73+
def read_yaml(filename)
74+
match_data = File.read(filename).match(/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m)
75+
data = YAML.load(match_data[1]) if match_data
76+
77+
data || {}
78+
end
79+
80+
def author_variable_defined?(filename)
81+
read_yaml(filename).has_key?('author')
82+
end
83+
84+
def lang_variable_defined?(filename)
85+
read_yaml(filename).has_key?('lang')
86+
end
87+
88+
def pub_date_utc(filename)
89+
date = read_yaml(filename)['date']
90+
91+
date ? date.getutc.strftime('%Y/%m/%d') : nil
92+
end
93+
94+
def glob(pattern)
95+
Pathname.glob(pattern).reject {|path| path.expand_path.to_s =~ %r{\A#{Regexp.escape(Bundler.bundle_path.to_s)}/} }.map(&:to_s)
96+
end
97+
end

lib/markup_checker.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "jekyll"
2+
3+
class MarkupChecker
4+
5+
# Validate _site markup with validate-website
6+
def check
7+
options = Jekyll.configuration
8+
Dir.chdir("_site") do
9+
system("validate-website-static",
10+
"--verbose",
11+
"--exclude", "examples",
12+
"--site", "#{options['url']}/")
13+
exit($?.exitstatus)
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)