-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakeebooks
executable file
·96 lines (80 loc) · 3.24 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/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
# $ ruby makeebooks en ru
# or
# $ FORMAT=mobi ruby makeebooks en ru
#
# Build e-book in 'epub' format for russian only
# $ FORMAT=epub ruby makeebooks ru
#
# Конвертирование изображений из svg в png (тредуется inkscape)
# for i in *; do inkscape $i --export-png=../png/`echo $i | awk -F. '{print($1)}'`.png --export-area-drawing --export-dpi=180; done
#
# Для того чтобы пропустить конвертирование изображений используйте флаг
# --skip-img
require 'rubygems'
require 'rdiscount'
require 'redcloth'
if ARGV.length == 0
puts "you need to specify at least one language. For example: makeebooks en"
exit
end
skip_converting = ARGV.include?('--skip-img')
ARGV.delete("--skip-img")
image_magick_converting = ARGV.include?('--im')
ARGV.delete("--im")
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"
unless skip_converting
puts "convert images from '/images/#{lang}/svg/' to '/images/#{lang}/png/'"
puts '---------------------------------------------------------------'
Dir.mkdir("images/#{lang}/png/") unless Dir.exist?("images/#{lang}/png/")
Dir.glob("images/#{lang}/svg/*.svg") do |svg_image|
system("inkscape #{svg_image} --export-png=#{svg_image.gsub('svg', 'png')} --export-area-drawing --export-dpi=180")
end
end
if image_magick_converting
Dir.mkdir("images/#{lang}/png/") unless Dir.exist?("images/#{lang}/png/")
Dir.glob("images/#{lang}/svg/*.svg") do |svg_image|
system("convert #{svg_image} #{svg_image.gsub('svg', 'png')}")
end
end
book_content = %(<html xmlns="http://www.w3.org/1999/xhtml"><head><title>openEHR Architecture Overview</title></head><body>)
dir = File.expand_path(File.join(File.dirname(__FILE__), lang))
Dir[File.join(dir, '**', '*.markdown'), File.join(dir, '**', '*.textile')].sort.each do |input|
type = case input
when /.*\.(markdown|md)$/ then
:markdown
when /.*\.(textile)$/ then
:textile
end
puts "processing #{type} file #{input}"
content = File.read(input)
content.gsub!(/!(.*)!/, "!images/#{lang}/png/" + '\1' + "!")
case type
when :markdown then
book_content << RDiscount.new(content).to_html
when :textile then
book_content << RedCloth.new(content).to_html
end
end
book_content << "</body></html>"
File.open("openehr.#{lang}.html", 'w') do |output|
output.write(book_content)
end
system('ebook-convert', "openehr.#{lang}.html", "openehr.#{lang}.#{format}",
'--authors', 'T Beale, S Heard',
'--comments', "Copyright and Licenses for the use of openEHR materials http://www.openehr.org/free_commercial_use.htm",
'--level1-toc', '//h:h1',
'--level2-toc', '//h:h2',
'--level3-toc', '//h:h3',
'--language', lang)
end