| 1 |
require 'fileutils' |
| 2 |
require 'rexml/parsers/pullparser' |
| •3 |
require 'tmpdir' |
| 4 |
|
| 5 |
module DocBook |
| 6 |
|
| 7 |
class Epub |
| 8 |
CHECKER = "epubcheck" |
| •9 |
STYLESHEET_PATH = File.join('usr/share/xml/docbook/stylesheet', 'docbook-xsl') |
| 10 |
STYLESHEET_NS_PATH = File.join('usr/share/xml/docbook/stylesheet', 'docbook-xsl-ns') |
| 11 |
if File.directory?(File.expand_path(File.join(File::Separator, STYLESHEET_PATH))) |
| 12 |
STYLESHEET = File.expand_path(File.join(File::Separator, STYLESHEET_PATH, 'epub', "docbook.xsl")) |
| 13 |
elsif File.directory?(File.expand_path(File.join(File::Separator, STYLESHEET_NS_PATH))) |
| 14 |
STYLESHEET = File.expand_path(File.join(File::Separator, STYLESHEET_NS_PATH, 'epub', "docbook.xsl")) |
| 15 |
else |
| 16 |
fail "Stylesheet path not found. Use the -s switch." |
| 17 |
end |
| 18 |
fail "Stylesheet #{STYLESHEET} not found. Use the -s switch." unless File.exist?(STYLESHEET) |
| 19 |
CALLOUT_PATH = File.join('images', 'callouts') |
| •20 |
CALLOUT_FULL_PATH = File.expand_path(File.join(File::Separator, STYLESHEET_PATH, CALLOUT_PATH)) |
| 21 |
CALLOUT_LIMIT = 15 |
| 22 |
CALLOUT_EXT = ".png" |
| 23 |
XSLT_PROCESSOR = "xsltproc" |
| •24 |
OUTPUT_DIR = File.expand_path(File.join(Dir::tmpdir, ".epubtmp#{Time.now.to_f.to_s}")) |
| 25 |
MIMETYPE = "application/epub+zip" |
| 26 |
META_DIR = "META-INF" |
| 27 |
OEBPS_DIR = "OEBPS" |
| 28 |
ZIPPER = "zip" |
| 29 |
|
| 30 |
attr_reader :output_dir |
| 31 |
|
| 32 |
def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=nil, customization_layer=nil, embedded_fonts=[]) |
| 33 |
@docbook_file = docbook_file |
| 34 |
@output_dir = output_dir |
| 35 |
@meta_dir = File.join(@output_dir, META_DIR) |
| 36 |
@oebps_dir = File.join(@output_dir, OEBPS_DIR) |
| 37 |
@css_file = css_file ? File.expand_path(css_file) : css_file |
| 38 |
@embedded_fonts = embedded_fonts |
| •39 |
@to_delete = [OUTPUT_DIR] |
| 40 |
|
| 41 |
if customization_layer |
| 42 |
@stylesheet = File.expand_path(customization_layer) |
| 43 |
else |
| 44 |
@stylesheet = STYLESHEET |
| 45 |
end |
| 46 |
|
| 47 |
unless File.exist?(@docbook_file) |
| 48 |
raise ArgumentError.new("File #{@docbook_file} does not exist") |
| 49 |
end |
| 50 |
end |
| 51 |
|
| 52 |
def render_to_file(output_file, verbose=false) |
| 53 |
render_to_epub(output_file, verbose) |
| 54 |
bundle_epub(output_file, verbose) |
| 55 |
cleanup_files(@to_delete) |
| 56 |
end |
| 57 |
|
| 58 |
def self.invalid?(file) |
| 59 |
|
| 60 |
cmd = %Q(#{CHECKER} "#{file}") |
| 61 |
output = `#{cmd} 2>&1` |
| 62 |
|
| 63 |
if $?.to_i == 0 |
| 64 |
return false |
| 65 |
else |
| 66 |
STDERR.puts output if $DEBUG |
| 67 |
return output |
| 68 |
end |
| 69 |
end |
| 70 |
|
| 71 |
private |
| 72 |
def render_to_epub(output_file, verbose) |
| 73 |
@collapsed_docbook_file = collapse_docbook() |
| 74 |
|
| 75 |
chunk_quietly = "--stringparam chunk.quietly " + (verbose ? '0' : '1') |
| 76 |
callout_path = "--stringparam callout.graphics.path #{CALLOUT_PATH}/" |
| 77 |
callout_limit = "--stringparam callout.graphics.number.limit #{CALLOUT_LIMIT}" |
| 78 |
callout_ext = "--stringparam callout.graphics.extension #{CALLOUT_EXT}" |
| 79 |
html_stylesheet = "--stringparam html.stylesheet #{File.basename(@css_file)}" if @css_file |
| 80 |
base = "--stringparam base.dir #{OEBPS_DIR}/" |
| 81 |
unless @embedded_fonts.empty? |
| 82 |
embedded_fonts = @embedded_fonts.map {|f| File.basename(f)}.join(',') |
| 83 |
font = "--stringparam epub.embedded.fonts \"#{embedded_fonts}\"" |
| 84 |
end |
| 85 |
meta = "--stringparam epub.metainf.dir #{META_DIR}/" |
| 86 |
oebps = "--stringparam epub.oebps.dir #{OEBPS_DIR}/" |
| 87 |
options = [chunk_quietly, |
| 88 |
callout_path, |
| 89 |
callout_limit, |
| 90 |
callout_ext, |
| 91 |
base, |
| 92 |
font, |
| 93 |
meta, |
| 94 |
oebps, |
| 95 |
html_stylesheet, |
| 96 |
].join(" ") |
| 97 |
|
| 98 |
db2epub_cmd = %Q(cd "#{@output_dir}" && #{XSLT_PROCESSOR} #{options} "#{@stylesheet}" "#{@collapsed_docbook_file}") |
| 99 |
STDERR.puts db2epub_cmd if $DEBUG |
| 100 |
success = system(db2epub_cmd) |
| 101 |
raise "Could not render as .epub to #{output_file} (#{db2epub_cmd})" unless success |
| 102 |
@to_delete << Dir["#{@meta_dir}/*"] |
| 103 |
@to_delete << Dir["#{@oebps_dir}/*"] |
| 104 |
end |
| 105 |
|
| 106 |
def bundle_epub(output_file, verbose) |
| 107 |
|
| 108 |
quiet = verbose ? "" : "-q" |
| 109 |
mimetype_filename = write_mimetype() |
| 110 |
meta = File.basename(@meta_dir) |
| 111 |
oebps = File.basename(@oebps_dir) |
| 112 |
images = copy_images() |
| 113 |
csses = copy_csses() |
| 114 |
fonts = copy_fonts() |
| 115 |
callouts = copy_callouts() |
| 116 |
|
| 117 |
|
| 118 |
zip_cmd = %Q(cd "#{@output_dir}" && #{ZIPPER} #{quiet} -X -r "#{File.expand_path(output_file)}" "#{mimetype_filename}" "#{meta}" "#{oebps}") |
| 119 |
puts zip_cmd if $DEBUG |
| 120 |
success = system(zip_cmd) |
| 121 |
raise "Could not bundle into .epub file to #{output_file}" unless success |
| 122 |
end |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
def collapse_docbook |
| 128 |
|
| 129 |
collapsed_file = File.join(File.expand_path(File.dirname(@docbook_file)), |
| 130 |
'.collapsed.' + File.basename(@docbook_file)) |
| 131 |
entity_collapse_command = %Q(xmllint --loaddtd --noent -o "#{collapsed_file}" "#{@docbook_file}") |
| 132 |
entity_success = system(entity_collapse_command) |
| 133 |
raise "Could not collapse named entites in #{@docbook_file}" unless entity_success |
| 134 |
|
| 135 |
xinclude_collapse_command = %Q(xmllint --xinclude -o "#{collapsed_file}" "#{collapsed_file}") |
| 136 |
xinclude_success = system(xinclude_collapse_command) |
| 137 |
raise "Could not collapse XIncludes in #{@docbook_file}" unless xinclude_success |
| 138 |
|
| 139 |
@to_delete << collapsed_file |
| 140 |
return collapsed_file |
| 141 |
end |
| 142 |
|
| 143 |
def copy_callouts |
| 144 |
new_callout_images = [] |
| 145 |
if has_callouts? |
| 146 |
calloutglob = "#{CALLOUT_FULL_PATH}/*#{CALLOUT_EXT}" |
| 147 |
Dir.glob(calloutglob).each {|img| |
| 148 |
img_new_filename = File.join(@oebps_dir, CALLOUT_PATH, File.basename(img)) |
| 149 |
|
| 150 |
|
| 151 |
FileUtils.mkdir_p(File.dirname(img_new_filename)) |
| 152 |
FileUtils.cp(img, img_new_filename) |
| 153 |
@to_delete << img_new_filename |
| 154 |
new_callout_images << img |
| 155 |
} |
| 156 |
end |
| 157 |
return new_callout_images |
| 158 |
end |
| 159 |
|
| 160 |
def copy_fonts |
| 161 |
new_fonts = [] |
| 162 |
@embedded_fonts.each {|font_file| |
| 163 |
font_new_filename = File.join(@oebps_dir, File.basename(font_file)) |
| 164 |
FileUtils.cp(font_file, font_new_filename) |
| 165 |
new_fonts << font_file |
| 166 |
} |
| 167 |
return new_fonts |
| 168 |
end |
| 169 |
|
| 170 |
def copy_csses |
| 171 |
if @css_file |
| 172 |
css_new_filename = File.join(@oebps_dir, File.basename(@css_file)) |
| 173 |
FileUtils.cp(@css_file, css_new_filename) |
| 174 |
end |
| 175 |
end |
| 176 |
|
| 177 |
def copy_images |
| 178 |
image_references = get_image_refs() |
| 179 |
new_images = [] |
| 180 |
image_references.each {|img| |
| 181 |
|
| 182 |
|
| 183 |
if img =~ /\.(svg|png|gif|jpe?g|xml)/i |
| 184 |
img_new_filename = File.join(@oebps_dir, img) |
| 185 |
img_full = File.join(File.expand_path(File.dirname(@docbook_file)), img) |
| 186 |
|
| 187 |
|
| 188 |
FileUtils.mkdir_p(File.dirname(img_new_filename)) |
| 189 |
puts(img_full + ": " + img_new_filename) if $DEBUG |
| 190 |
FileUtils.cp(img_full, img_new_filename) |
| 191 |
@to_delete << img_new_filename |
| 192 |
new_images << img_full |
| 193 |
end |
| 194 |
} |
| 195 |
return new_images |
| 196 |
end |
| 197 |
|
| 198 |
def write_mimetype |
| 199 |
mimetype_filename = File.join(@output_dir, "mimetype") |
| 200 |
File.open(mimetype_filename, "w") {|f| f.print MIMETYPE} |
| 201 |
@to_delete << mimetype_filename |
| 202 |
return File.basename(mimetype_filename) |
| 203 |
end |
| 204 |
|
| 205 |
def cleanup_files(file_list) |
| 206 |
file_list.flatten.each {|f| |
| 207 |
|
| 208 |
FileUtils.rm_r(f, :force => true ) |
| 209 |
} |
| 210 |
end |
| 211 |
|
| 212 |
|
| 213 |
def get_image_refs |
| 214 |
parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file)) |
| 215 |
image_refs = [] |
| 216 |
while parser.has_next? |
| 217 |
el = parser.pull |
| 218 |
if el.start_element? and (el[0] == "imagedata" or el[0] == "graphic") |
| 219 |
image_refs << el[1]['fileref'] |
| 220 |
end |
| 221 |
end |
| 222 |
return image_refs.uniq |
| 223 |
end |
| 224 |
|
| 225 |
|
| 226 |
def has_callouts? |
| 227 |
parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file)) |
| 228 |
while parser.has_next? |
| 229 |
el = parser.pull |
| 230 |
if el.start_element? and (el[0] == "calloutlist" or el[0] == "co") |
| 231 |
return true |
| 232 |
end |
| 233 |
end |
| 234 |
return false |
| 235 |
end |
| 236 |
end |
| 237 |
end |