forked from ruby/www.ruby-lang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
262 lines (204 loc) · 5.94 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
# encoding: utf-8
require 'rubygems'
begin
require 'bundler/setup'
rescue LoadError => e
warn e.message
warn "Run `gem install bundler` to install Bundler"
exit -1
end
require 'pathname'
HOST = 'www.ruby-lang.org'
LANGUAGES = %w[bg de en es fr id it ja ko pl pt ru tr vi zh_cn zh_tw]
CONFIG = "_config.yml"
task :default => [:build]
desc "Build the Jekyll site"
task :build do
require "lanyon"
Lanyon.build
end
namespace :build do
def build_subpage(lang)
require "yaml"
require "lanyon"
exclude_config = YAML.load_file(CONFIG)["exclude"]
exclude_langs = (LANGUAGES - [lang]).map {|x| "#{x}/" }
exclude = exclude_config + exclude_langs
Lanyon.build(exclude: exclude)
end
desc "Build the Jekyll site (`lang' language part only)"
task :lang do
puts 'Please specify one of the valid language codes:'
puts LANGUAGES.join(', ') << '.'
end
LANGUAGES.each do |lang|
task lang.to_sym do
build_subpage(lang)
end
end
end
desc "Serve the Jekyll site locally"
task :serve do
sh "rackup config.ru"
end
namespace :new_post do
def create_template(lang)
url_title = 'short-title'
title = 'Post Title'
now = Time.now.utc
datetime = now.strftime("%Y-%m-%d %H:%M:%S %z")
date = now.strftime("%Y-%m-%d")
filename = "#{date}-#{url_title}.md"
path = File.join(lang, 'news', '_posts', filename)
content = <<-TEMPLATE.gsub(/^ */, '')
---
layout: news_post
title: "#{title}"
author: "Unknown Author"
translator:
date: #{datetime}
lang: #{lang}
---
Content.
TEMPLATE
$stderr.print "Creating template `#{path}'... "
begin
if File.exist?(path)
warn "Could not create template, `#{path}' already exists."
else
File.open(path, 'w') {|f| f.write content }
warn 'done.'
end
rescue => e
warn e.message
end
end
desc "Create a news post template for language `lang'"
task :lang do
puts 'Please specify one of the valid language codes:'
puts LANGUAGES.join(', ') << '.'
end
LANGUAGES.each do |lang|
task lang.to_sym do
create_template(lang)
end
end
end
desc "Alias for `check'"
task :test => [:check]
namespace :check do
def read_yaml(filename)
require 'yaml'
match_data = File.read(filename).match(/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m)
data = YAML.load(match_data[1]) if match_data
data || {}
end
def author_variable_defined?(filename)
read_yaml(filename).has_key?('author')
end
def lang_variable_defined?(filename)
read_yaml(filename).has_key?('lang')
end
def pub_date_utc(filename)
date = read_yaml(filename)['date']
date ? date.getutc.strftime('%Y/%m/%d') : nil
end
def glob(pattern)
Pathname.glob(pattern).reject {|path| path.expand_path.to_s =~ %r{\A#{Regexp.escape(Bundler.bundle_path.to_s)}/} }.map(&:to_s)
end
desc "Check for missing author variables in news posts"
task :author do
print "Checking for missing author variables in news posts..."
md_files = glob("**/_posts/*.md")
author_missing = md_files.select {|fn| !author_variable_defined?(fn) }
if author_missing.empty?
puts " ok"
else
puts "\nNo author variable defined in:"
puts author_missing.map {|s| " #{s}\n"}.join
raise
end
end
desc "Check for missing lang variables in markdown files"
task :lang do
print "Checking for missing lang variables in markdown files..."
md_files = glob("**/*.md")
skip_patterns = [/README.md/, %r{[^/]*/examples/}, %r{\A_includes/}]
skip_patterns.each do |pattern|
md_files.delete_if {|fn| fn =~ pattern }
end
lang_missing = md_files.select {|fn| !lang_variable_defined?(fn) }
if lang_missing.empty?
puts " ok"
else
puts "\nNo lang variable defined in:"
puts lang_missing.map {|s| " #{s}\n"}.join
raise
end
end
desc "Check publication dates (UTC) for consistency with filename"
task :pubdates do
print "Checking for date mismatch in posts (filename / YAML front matter)..."
posts = glob("**/_posts/*.md")
date_mismatch = []
posts.each do |post|
filename_date = File.basename(post).split('-',4)[0..2].join('/')
yaml_date = pub_date_utc(post)
date_mismatch << post if yaml_date && yaml_date != filename_date
end
if date_mismatch.empty?
puts " ok"
else
puts "\nDate mismatch in:"
puts date_mismatch.map {|s| " #{s}\n"}.join
raise
end
end
localport = 9292
desc "Check for broken links on http://localhost:#{localport}/"
task :links do
gem 'spidr', '~> 0.6'
require 'spidr'
url_map = Hash.new { |hash,key| hash[key] = [] }
Spidr.site("http://localhost:#{localport}/") do |agent|
LANGUAGES.each do |lang|
agent.enqueue("http://localhost:#{localport}/#{lang}/")
end
agent.every_link do |origin,dest|
url_map[dest] << origin
end
agent.every_page do |page|
if page.code == 404
origin = url_map[page.url].last
dest = page.url.request_uri
external = URI::HTTP.build(
:host => HOST,
:path => page.url.path,
:query => page.url.query
)
if Net::HTTP.get_response(external).code == '404'
puts "Old Broken Link: #{origin} -> #{dest}"
else
puts "New Broken Link: #{origin} -> #{dest}"
end
raise
end
end
end
end
desc 'Validate _site markup with validate-website'
task :markup => :build do
require 'jekyll'
options = Jekyll.configuration
Dir.chdir('_site') do
system("validate-website-static",
"--verbose",
"--exclude", "examples",
"--site", "#{options['url']}/")
exit($?.exitstatus)
end
end
end
desc "Run some tests (lang, author, pubdates)"
task :check => ['check:lang', 'check:author', 'check:pubdates']
task :ci => [:test, :build]