mkbok 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/mkbok +173 -114
- data/lib/mkbok_version.rb +1 -1
- metadata +2 -2
data/bin/mkbok
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
2
3
|
|
3
4
|
require 'optparse'
|
4
5
|
require 'fileutils'
|
@@ -8,10 +9,10 @@ require 'yaml'
|
|
8
9
|
include FileUtils
|
9
10
|
|
10
11
|
options = {
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
"build"=> "pdf",
|
13
|
+
"lang" => "zh",
|
14
|
+
"config" => "latex/config.yml",
|
15
|
+
"template" => "latex/template.tex"
|
15
16
|
}
|
16
17
|
CONFIG_FILE = File.join('.mkbok.yml')
|
17
18
|
if File.exists? CONFIG_FILE
|
@@ -25,39 +26,43 @@ option_parser = OptionParser.new do |opts|
|
|
25
26
|
Usage: #{executable_name} [options]
|
26
27
|
"
|
27
28
|
# Create a switch
|
28
|
-
opts.on("-b","--build FORMAT","build") do |format|
|
29
|
-
|
30
|
-
|
29
|
+
opts.on("-b","--build FORMAT","build format:epub,pdf,html. seperated by ','") do |format|
|
30
|
+
formatOptions = format.split(",")
|
31
|
+
formatOptions.each do | fmt |
|
32
|
+
#puts fmt
|
33
|
+
unless ["pdf","epub","html"].include?(fmt)
|
34
|
+
raise ArgumentError,"FORMAT must be one of 'pdf,epub,html' format"
|
35
|
+
end
|
31
36
|
end
|
32
|
-
options[
|
37
|
+
options["outputformat"] = formatOptions
|
33
38
|
end
|
34
39
|
opts.on("-d","--debug","debug") do
|
35
|
-
options[
|
40
|
+
options["debug"] = true
|
36
41
|
end
|
37
42
|
# Create a flag
|
38
43
|
opts.on("-l","--lang LANG","language selection") do |lang|
|
39
44
|
unless lang=="zh" or lang=="en"
|
40
45
|
raise ArgumentError,"LANG must be in zh or en"
|
41
46
|
end
|
42
|
-
options[
|
47
|
+
options["lang"] = lang
|
43
48
|
end
|
44
49
|
opts.on("-c","--config CONFIG","config file") do |config|
|
45
50
|
unless File.exists? config
|
46
51
|
raise ArgumentError,"config file \"#{config}\" doesn't exist"
|
47
52
|
end
|
48
|
-
options[
|
53
|
+
options["config"] = config
|
49
54
|
end
|
50
55
|
opts.on("-t","--template template.tex","latex template file") do |template|
|
51
56
|
unless File.exists? template
|
52
57
|
raise ArgumentError,"template file \"#{template}\" doesn't exist"
|
53
58
|
end
|
54
|
-
options[
|
59
|
+
options["template"] = template
|
55
60
|
end
|
56
61
|
opts.on("-n","--name book name","book name") do |name|
|
57
62
|
unless name =~ /^[a-zA-Z0-9]+$/
|
58
63
|
raise ArgumentError,"name should be [a-zA-Z0-9]"
|
59
64
|
end
|
60
|
-
options[
|
65
|
+
options["name"] = name
|
61
66
|
end
|
62
67
|
end
|
63
68
|
|
@@ -67,9 +72,9 @@ option_parser.parse!
|
|
67
72
|
$here = Dir.getwd
|
68
73
|
$root = File.join($here)
|
69
74
|
$outDir = File.join($root, 'pdf')
|
70
|
-
options[
|
75
|
+
options["name"] = File.basename(Dir.getwd) unless options["name"]
|
71
76
|
|
72
|
-
puts options.inspect if options[
|
77
|
+
puts options.inspect if options["debug"]
|
73
78
|
|
74
79
|
def figures(&block)
|
75
80
|
begin
|
@@ -190,109 +195,163 @@ def post_pandoc(string, config, lang, chapter=true)
|
|
190
195
|
end
|
191
196
|
end
|
192
197
|
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
198
|
+
def generate_pdf(options)
|
199
|
+
$config = YAML.load_file(options["config"])
|
200
|
+
template = ERB.new(File.read(options["template"]))
|
201
|
+
languages = [options["lang"]]
|
202
|
+
missing = ['pandoc', 'xelatex'].reject{|command| command_exists?(command)}
|
203
|
+
unless missing.empty?
|
204
|
+
puts "Missing dependencies: #{missing.join(', ')}."
|
205
|
+
puts "Install these and try again."
|
206
|
+
exit
|
207
|
+
end
|
208
|
+
|
209
|
+
puts "Will generate pdf for the following languages using template #{options["template"]}:"
|
210
|
+
puts " #{languages}"
|
211
|
+
puts
|
212
|
+
|
213
|
+
figures do
|
214
|
+
languages.each do |lang|
|
215
|
+
config = $config['default'].merge($config[lang]) rescue $config['default']
|
216
|
+
|
217
|
+
puts "#{lang}:"
|
218
|
+
puts "\tParsing preface markdown... "
|
219
|
+
prefacemarkdown = Dir["#$root/#{lang}/*preface/*.markdown"].sort.map do |file|
|
220
|
+
puts "\t =>"+file
|
221
|
+
File.read(file)
|
222
|
+
end.join("\n\n")
|
223
|
+
|
224
|
+
preface = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
|
225
|
+
pipe.write(pre_pandoc(prefacemarkdown, config))
|
226
|
+
pipe.close_write
|
227
|
+
post_pandoc(pipe.read, config, lang, false)
|
228
|
+
end
|
229
|
+
|
230
|
+
puts "\n\tParsing main chapters markdown... "
|
231
|
+
found = false
|
232
|
+
chaptersmarkdown = Dir["#$root/#{lang}/*chapters/*.markdown"].sort.map do |file|
|
233
|
+
found = true
|
234
|
+
puts "\t =>"+file
|
235
|
+
File.read(file)
|
236
|
+
end.join("\n\n")
|
237
|
+
chaptersmarkdown = Dir["#$root/#{lang}/*/*.markdown"].sort.map do |file|
|
238
|
+
puts "\t =>"+file
|
239
|
+
File.read(file)
|
240
|
+
end.join("\n\n") unless found
|
241
|
+
# puts "done"
|
242
|
+
|
243
|
+
latex = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
|
244
|
+
pipe.write(pre_pandoc(chaptersmarkdown, config))
|
245
|
+
pipe.close_write
|
246
|
+
post_pandoc(pipe.read, config, lang)
|
247
|
+
end
|
248
|
+
# puts "done"
|
249
|
+
|
250
|
+
puts "\n\tParsing appendix markdown... "
|
251
|
+
appendixmarkdown = Dir["#$root/#{lang}/*appendix/*.markdown"].sort.map do |file|
|
252
|
+
puts "\t =>"+file
|
253
|
+
File.read(file)
|
254
|
+
end.join("\n\n")
|
255
|
+
|
256
|
+
appendix = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
|
257
|
+
pipe.write(pre_pandoc(appendixmarkdown, config))
|
258
|
+
pipe.close_write
|
259
|
+
post_pandoc(pipe.read, config, lang)
|
260
|
+
end
|
261
|
+
#puts "done"
|
262
|
+
|
263
|
+
print "\n\tCreating main.tex for #{lang}... "
|
264
|
+
dir = "latex/#{lang}"
|
265
|
+
mkdir_p(dir)
|
266
|
+
|
267
|
+
File.open("#{dir}/main.tex", 'w') do |file|
|
268
|
+
file.write(template.result(binding))
|
269
|
+
end
|
270
|
+
puts "done"
|
271
|
+
|
272
|
+
abort = false
|
273
|
+
|
274
|
+
puts "\tRunning XeTeX:"
|
275
|
+
cd($root)
|
276
|
+
3.times do |i|
|
277
|
+
print "\t\tPass #{i + 1}... "
|
278
|
+
IO.popen("xelatex -output-directory=\"#{dir}\" \"#{dir}/main.tex\" 2>&1",'r:UTF-16LE:UTF-8') do |pipe|
|
279
|
+
unless $DEBUG
|
280
|
+
if ~ /^!\s/
|
281
|
+
puts "failed with:\n\t\t\t#{$_.strip}"
|
282
|
+
puts "\tConsider running this again with --debug."
|
283
|
+
abort = true
|
284
|
+
end while pipe.gets and not abort
|
285
|
+
else
|
286
|
+
STDERR.print while pipe.gets rescue abort = true
|
287
|
+
end
|
288
|
+
end
|
289
|
+
break if abort
|
290
|
+
puts "done"
|
291
|
+
end
|
292
|
+
|
293
|
+
unless abort
|
294
|
+
print "\tMoving output to #{options["name"]}.#{lang}.pdf... "
|
295
|
+
mv("#{dir}/main.pdf", "#$root/#{options["name"]}.#{lang}.pdf")
|
296
|
+
puts "done"
|
297
|
+
else
|
298
|
+
print "\tConvert error, exit !\n"
|
299
|
+
exit 1
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
201
303
|
end
|
202
304
|
|
203
|
-
|
204
|
-
|
205
|
-
puts
|
206
|
-
|
207
|
-
figures do
|
208
|
-
languages.each do |lang|
|
209
|
-
config = $config['default'].merge($config[lang]) rescue $config['default']
|
210
|
-
|
211
|
-
puts "#{lang}:"
|
212
|
-
puts "\tParsing preface markdown... "
|
213
|
-
prefacemarkdown = Dir["#$root/#{lang}/*preface/*.markdown"].sort.map do |file|
|
214
|
-
puts "\t =>"+file
|
215
|
-
File.read(file)
|
216
|
-
end.join("\n\n")
|
217
|
-
|
218
|
-
preface = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
|
219
|
-
pipe.write(pre_pandoc(prefacemarkdown, config))
|
220
|
-
pipe.close_write
|
221
|
-
post_pandoc(pipe.read, config, lang, false)
|
222
|
-
end
|
223
|
-
|
224
|
-
puts "\n\tParsing main chapters markdown... "
|
225
|
-
found = false
|
226
|
-
chaptersmarkdown = Dir["#$root/#{lang}/*chapters/*.markdown"].sort.map do |file|
|
227
|
-
found = true
|
228
|
-
puts "\t =>"+file
|
229
|
-
File.read(file)
|
230
|
-
end.join("\n\n")
|
231
|
-
chaptersmarkdown = Dir["#$root/#{lang}/*/*.markdown"].sort.map do |file|
|
232
|
-
puts "\t =>"+file
|
233
|
-
File.read(file)
|
234
|
-
end.join("\n\n") unless found
|
235
|
-
# puts "done"
|
236
|
-
|
237
|
-
latex = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
|
238
|
-
pipe.write(pre_pandoc(chaptersmarkdown, config))
|
239
|
-
pipe.close_write
|
240
|
-
post_pandoc(pipe.read, config, lang)
|
241
|
-
end
|
242
|
-
# puts "done"
|
243
|
-
|
244
|
-
puts "\n\tParsing appendix markdown... "
|
245
|
-
appendixmarkdown = Dir["#$root/#{lang}/*appendix/*.markdown"].sort.map do |file|
|
246
|
-
puts "\t =>"+file
|
247
|
-
File.read(file)
|
248
|
-
end.join("\n\n")
|
249
|
-
|
250
|
-
appendix = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
|
251
|
-
pipe.write(pre_pandoc(appendixmarkdown, config))
|
252
|
-
pipe.close_write
|
253
|
-
post_pandoc(pipe.read, config, lang)
|
254
|
-
end
|
255
|
-
#puts "done"
|
305
|
+
def generate_ebook(options)
|
306
|
+
name = File.basename(Dir.getwd)
|
256
307
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
end
|
264
|
-
puts "done"
|
265
|
-
|
266
|
-
abort = false
|
267
|
-
|
268
|
-
puts "\tRunning XeTeX:"
|
269
|
-
cd($root)
|
270
|
-
3.times do |i|
|
271
|
-
print "\t\tPass #{i + 1}... "
|
272
|
-
IO.popen("xelatex -output-directory=\"#{dir}\" \"#{dir}/main.tex\" 2>&1",'r:UTF-16LE:UTF-8') do |pipe|
|
273
|
-
unless $DEBUG
|
274
|
-
if ~ /^!\s/
|
275
|
-
puts "failed with:\n\t\t\t#{$_.strip}"
|
276
|
-
puts "\tConsider running this again with --debug."
|
277
|
-
abort = true
|
278
|
-
end while pipe.gets and not abort
|
279
|
-
else
|
280
|
-
STDERR.print while pipe.gets rescue abort = true
|
281
|
-
end
|
282
|
-
end
|
283
|
-
break if abort
|
284
|
-
puts "done"
|
285
|
-
end
|
308
|
+
puts " "
|
309
|
+
puts "Will generate ebooks [#{options["outputformat"].join(',')}] for the following languages #{options["lang"]}"
|
310
|
+
puts " "
|
311
|
+
|
312
|
+
options["lang"].each do |lang|
|
313
|
+
puts "convert content for '#{lang}' language"
|
286
314
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
315
|
+
if lang == 'zh'
|
316
|
+
figure_title = '图'
|
317
|
+
else
|
318
|
+
figure_title = 'Figure'
|
319
|
+
end
|
320
|
+
|
321
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), lang))
|
322
|
+
book_content = ""
|
323
|
+
Dir[File.join(dir, '**', '*.markdown')].sort.each do |input|
|
324
|
+
puts "\tProcessing #{input}"
|
325
|
+
content = File.read(input)
|
326
|
+
content.gsub!(/Insert\s+(.*)(\.png)\s*\n?\s*#{figure_title}\s+(.*)/, '![\3](figures/\1-tn\2 "\3")')
|
327
|
+
book_content << content
|
328
|
+
end
|
329
|
+
|
330
|
+
File.open("#{name}.#{lang}.markdown", 'w') do |output|
|
331
|
+
output.write(book_content)
|
332
|
+
end
|
333
|
+
# pandoc -S -s --epub-metadata=metadata.xml -o sdcamp.zh.html --epub-stylesheet=epub/ProGit.css epub/title.txt sdcamp.zh.markdown
|
334
|
+
options["outputformat"].each do | format |
|
335
|
+
system('pandoc',
|
336
|
+
'--standalone',
|
337
|
+
'--table-of-contents',
|
338
|
+
'--epub-metadata', 'epub/metadata.xml',
|
339
|
+
'--epub-stylesheet', 'epub/book.css', # this doesn't work
|
340
|
+
'--output', "#{name}.#{lang}.#{format}",
|
341
|
+
"title.#{lang}.txt", # little strange, if this put under epub, pandoc reports error
|
342
|
+
"#{name}.#{lang}.markdown")
|
343
|
+
# pandoc -S --epub-metadata=metadata.xml -o progit.epub title.txt
|
344
|
+
puts("#{name}.#{lang}.#{format} is generated")
|
345
|
+
end
|
346
|
+
end
|
296
347
|
end
|
297
348
|
|
349
|
+
if options["outputformat"].include?("pdf")
|
350
|
+
#puts "pdf"
|
351
|
+
generate_pdf(options)
|
352
|
+
end
|
353
|
+
options["outputformat"].delete("pdf")
|
354
|
+
if options["outputformat"].count>0
|
355
|
+
generate_ebook(options)
|
356
|
+
end
|
298
357
|
|
data/lib/mkbok_version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mkbok
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02-
|
12
|
+
date: 2012-02-12 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: the ebook generate tools from markdown plain text
|
15
15
|
email:
|