-
Notifications
You must be signed in to change notification settings - Fork 60
/
makeebooks
executable file
·78 lines (67 loc) · 2.41 KB
/
makeebooks
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
#!/usr/bin/env ruby
# encoding: utf-8
# This script convers markdown book to one of the serveral e-book
# formats supported with calibre (http://calibre-ebook.com)
#
# Samples:
#
# Build e-book for amazon kindle for english and russian languages
# $ make-ebook en ru
# or
# $ FORMAT=mobi make-ebook en ru
#
# Build e-book in 'epub' format for russian only
# $ FORMAT=epub make-ebook ru
require 'rubygems'
require 'rdiscount'
require 'fileutils'
#require 'ruby-debug'
include FileUtils
$root = File.expand_path(File.dirname(__FILE__))
if ARGV.length == 0
puts "you need to specify at least one language. For example: makeebooks en"
exit
end
format = ENV['FORMAT'] || 'mobi'
puts "using .#{format} (you can change it via FORMAT environment variable. try 'mobi' or 'epub')"
ARGV.each do |lang|
puts "convert content for '#{lang}' language"
if lang == 'ko'
figure_title = '그림'
else
figure_title = 'Figure'
end
book_content = %(<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Programming Erlang: Software for a Concurrent World</title></head><body>)
dir = File.expand_path(File.join(File.dirname(__FILE__), lang))
Dir[File.join(dir, '**', '*.md')].sort.each do |input|
puts "processing #{input}"
content = File.read(input, encoding: 'utf-8')
content.gsub!(/Insert\s+(.*)(\.png)\s*\n?\s*#{figure_title}\s+(.*)/, '![\3](figures/\1-tn\2 "\3")')
book_content << RDiscount.new(content).to_html
end
book_content << "</body></html>"
mkdir_p("#$root/book/")
File.open("#$root/book/erlangbook.#{lang}.html", 'w') do |output|
output.write(book_content)
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'
unless File.exists?(mac_osx_path)
puts 'ERROR: install calibri app'
exit 1
end
$ebook_convert_cmd = mac_osx_path
end
system($ebook_convert_cmd, "#$root/book/erlangbook.#{lang}.html", "#$root/book/erlangbook.#{lang}.#{format}",
#'--cover', 'ebooks/cover.png',
'--authors', 'Joe Armstrong',
'--comments', "licensed under the Creative Commons Attribution-Non Commercial-Share Alike 3.0 license",
'--level1-toc', '//h:h1',
'--level2-toc', '//h:h2',
'--level3-toc', '//h:h3',
'--language', lang)
end