forked from progit/progit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
280 lines (242 loc) · 7.2 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
# encoding: UTF-8
require 'rake/clean'
require 'redcarpet'
$lang = ENV['language']
$lang ||= 'en'
namespace :epub do
TMP_DIR = File.join('epub', 'temp', $lang)
INDEX_FILEPATH = File.join(TMP_DIR, 'progit.html')
TARGET_FILEPATH = "progit-#{$lang}.epub"
SOURCE_FILES = FileList.new(File.join($lang, '0*', '*.markdown')).sort
CONVERTED_MK_FILES = SOURCE_FILES.pathmap(File.join(TMP_DIR, '%f'))
HTML_FILES = CONVERTED_MK_FILES.ext('html')
desc "generate EPUB ebook (add language=xx to build lang xx)"
task :generate => :check
task :generate => TARGET_FILEPATH
desc "check whether all the required tools are installed"
task :check do
begin
require 'maruku'
found_maruku = true
rescue LoadError
found_maruku = false
end
$ebook_convert_cmd = ENV['ebook_convert_path'].to_s
if $ebook_convert_cmd.empty?
$ebook_convert_cmd = `which ebook-convert`.chomp
end
if $ebook_convert_cmd.empty?
mac_osx_path = '/Applications/calibre.app/Contents/MacOS/ebook-convert'
$ebook_convert_cmd = mac_osx_path
end
found_calibre = File.executable?($ebook_convert_cmd)
if !found_maruku
puts 'EPUB generation requires the Maruku gem.'
puts ' On Ubuntu call "sudo apt-get install libmaruku-ruby".'
end
if !found_calibre
puts 'EPUB generation requires Calibre.'
puts ' On Ubuntu call "sudo apt-get install calibre".'
end
if !found_calibre || !found_maruku then exit 1 end
end
directory TMP_DIR
rule '.html' => '.mk' do |t|
require 'maruku'
mk_filename = t.source
html_filename = t.name
puts "Converting #{mk_filename} -> #{html_filename}"
mk_file = File.open(mk_filename, 'r') do |mk|
html_file = File.open(html_filename, 'w') do |html|
code = Maruku.new(mk.read.encode("UTF-8")).to_html
code.gsub!(/^(<h.) (id='[^']+?')/, '\1')
html << code
html << "\n"
end
end
end
src_for_converted = proc do |dst|
base_name = dst.pathmap('%n')
SOURCE_FILES.find { |s| s.pathmap('%n') == base_name }
end
rule '.mk' => src_for_converted do |t|
src_filename = t.source
dest_filename = t.name
puts "Processing #{src_filename} -> #{dest_filename}"
figures_dir = "../../../figures"
dest_file = File.open(dest_filename, 'w')
src_file = File.open(src_filename, 'r')
until src_file.eof?
line = src_file.readline
matches = line.match /^\s*Insert\s(.*)/
if matches
image_path = matches[1]
real_image_path = image_path.pathmap("#{figures_dir}/%X-tn%x")
next_line = src_file.readline.chomp
line = "![#{next_line}](#{real_image_path} \"#{next_line}\")\n"
end
dest_file << line
end
src_file.close
dest_file.close
end
file INDEX_FILEPATH => TMP_DIR
file INDEX_FILEPATH => HTML_FILES do
index_file = File.open(INDEX_FILEPATH, 'w') do |file|
file << '<?xml version="1.0" encoding="UTF-8"?>'
file << "\n"
file << '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" '
file << '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
file << "\n"
file << "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='#{$lang}'>"
file << '<head>'
file << '<title>Pro Git - professional version control</title>'
file << '</head>'
file << '<body>'
file << "\n"
HTML_FILES.each do |chapter_file|
file << File.open(chapter_file).read
file << "\n"
end
file << '</body></html>'
file << "\n"
end
end
file TARGET_FILEPATH => INDEX_FILEPATH do
opts = [
'--language', $lang,
'--authors', 'Scott Chacon',
'--comments', 'Licensed under the Creative Commons Attribution-Non Commercial-Share Alike 3.0 license',
'--cover', 'epub/title.png',
'--extra-css', 'epub/ProGit.css',
'--chapter', '//h:h1',
'--level1-toc', '//h:h1',
'--level2-toc', '//h:h2',
'--level3-toc', '//h:h3',
]
sh $ebook_convert_cmd, INDEX_FILEPATH, TARGET_FILEPATH, *opts
end
CLEAN.push(*CONVERTED_MK_FILES)
CLEAN.push(*HTML_FILES)
CLEAN << INDEX_FILEPATH
CLEAN << TMP_DIR
CLOBBER << TARGET_FILEPATH
end
namespace :pdf do
desc "generate a pdf"
task :generate do
system("bash makepdfs")
end
end
class StderrDecorator
def initialize(out)
@out = out
end
def <<(x)
@out << "#{x}"
if x.match /REXML/
raise ""
end
end
end
def test_lang(lang, out)
error_code = false
chapter_figure = {
"01-introduction" => 7,
"02-git-basics" => 2,
"03-git-branching" => 39,
"04-git-server" => 15,
"05-distributed-git" => 27,
"06-git-tools" => 1,
"07-customizing-git" => 3,
"08-git-and-other-scms" => 0,
"09-git-internals" => 4}
source_files = FileList.new(File.join(lang, '0*', '*.markdown')).sort
source_files.each do |mk_filename|
src_file = File.open(mk_filename, 'r')
figure_count = 0
until src_file.eof?
line = src_file.readline
matches = line.match /^#/
if matches
if line.match /^(#+).*#[[:blank:]]+$/
out<< "\nBadly formatted title in #{mk_filename}: #{line}\n"
error_code = true
end
end
if line.match /^\s*Insert\s(.*)/
figure_count = figure_count + 1
end
end
# This extraction is a bit contorted, because the pl translation renamed
# the files, so the match is done on the directories.
tab_fig_count = chapter_figure[File.basename(File.dirname(mk_filename))]
expected_figure_count = tab_fig_count ? tab_fig_count:0
if figure_count > expected_figure_count
out << "\nToo many figures declared in #{mk_filename}\n"
error_code = true
end
end
begin
mark = (source_files.map{|mk_filename| File.open(mk_filename, 'r'){
|mk| mk.read.encode("UTF-8")}}).join("\n\n")
require 'maruku'
code = Maruku.new(mark, :on_error => :raise, :error_stream => StderrDecorator.new(out))
rescue
print $!
error_code = true
end
error_code
end
$out = $stdout
namespace :ci do
desc "Parallel Continuous integration"
task :parallel_check do
require 'parallel'
langs = FileList.new('??')+FileList.new('??-??')
results = Parallel.map(langs) do |lang|
error_code = test_lang(lang, $out)
if error_code
print "processing #{lang} KO\n"
else
print "processing #{lang} OK\n"
end
error_code
end
fail "At least one language conversion failed" if results.any?
end
(FileList.new('??')+FileList.new('??-??')).each do |lang|
desc "testing " + lang
task (lang+"_check").to_sym do
error_code = test_lang(lang, $out)
fail "processing #{lang} KO\n" if error_code
print "processing #{lang} OK\n"
end
end
desc "Continuous Integration"
task :check do
require 'maruku'
langs = FileList.new('??')+FileList.new('??-??')
if ENV['debug'] && $lang
langs = [$lang]
else
excluded_langs = [
]
excluded_langs.each do |lang|
puts "excluding #{lang}: known to fail"
end
langs -= excluded_langs
end
errors = langs.map do |lang|
print "processing #{lang} "
error_code=test_lang(lang, $out)
if error_code
print "KO\n"
else
print "OK\n"
end
error_code
end
fail "At least one language conversion failed" if errors.any?
end
end