2 require 'rexml/parsers/pullparser'
8 STYLESHEET = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', "docbook.xsl"))
9 CALLOUT_PATH = File.join('images', 'callouts')
10 CALLOUT_FULL_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', CALLOUT_PATH))
13 XSLT_PROCESSOR = "xsltproc"
14 OUTPUT_DIR = ".epubtmp#{Time.now.to_f.to_s}"
15 MIMETYPE = "application/epub+zip"
20 attr_reader :output_dir
22 def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=nil, customization_layer=nil, embedded_fonts=[])
23 @docbook_file = docbook_file
24 @output_dir = output_dir
25 @meta_dir = File.join(@output_dir, META_DIR)
26 @oebps_dir = File.join(@output_dir, OEBPS_DIR)
27 @css_file = css_file ? File.expand_path(css_file) : css_file
28 @embedded_fonts = embedded_fonts
31 if customization_layer
32 @stylesheet = File.expand_path(customization_layer)
34 @stylesheet = STYLESHEET
37 unless File.exist?(@docbook_file)
38 raise ArgumentError.new("File #{@docbook_file} does not exist")
42 def render_to_file(output_file, verbose=false)
43 render_to_epub(output_file, verbose)
44 bundle_epub(output_file, verbose)
45 cleanup_files(@to_delete)
48 def self.invalid?(file)
49 # Obnoxiously, we can't just check for a non-zero output...
50 cmd = %Q(#{CHECKER} "#{file}")
51 output = `#{cmd} 2>&1`
56 STDERR.puts output if $DEBUG
62 def render_to_epub(output_file, verbose)
63 @collapsed_docbook_file = collapse_docbook()
65 chunk_quietly = "--stringparam chunk.quietly " + (verbose ? '0' : '1')
66 callout_path = "--stringparam callout.graphics.path #{CALLOUT_PATH}/"
67 callout_limit = "--stringparam callout.graphics.number.limit #{CALLOUT_LIMIT}"
68 callout_ext = "--stringparam callout.graphics.extension #{CALLOUT_EXT}"
69 html_stylesheet = "--stringparam html.stylesheet #{File.basename(@css_file)}" if @css_file
70 base = "--stringparam base.dir #{OEBPS_DIR}/"
71 unless @embedded_fonts.empty?
72 embedded_fonts = @embedded_fonts.map {|f| File.basename(f)}.join(',')
73 font = "--stringparam epub.embedded.fonts \"#{embedded_fonts}\""
75 meta = "--stringparam epub.metainf.dir #{META_DIR}/"
76 oebps = "--stringparam epub.oebps.dir #{OEBPS_DIR}/"
77 options = [chunk_quietly,
87 # Double-quote stylesheet & file to help Windows cmd.exe
88 db2epub_cmd = %Q(cd "#{@output_dir}" && #{XSLT_PROCESSOR} #{options} "#{@stylesheet}" "#{@collapsed_docbook_file}")
89 STDERR.puts db2epub_cmd if $DEBUG
90 success = system(db2epub_cmd)
91 raise "Could not render as .epub to #{output_file} (#{db2epub_cmd})" unless success
92 @to_delete << Dir["#{@meta_dir}/*"]
93 @to_delete << Dir["#{@oebps_dir}/*"]
96 def bundle_epub(output_file, verbose)
98 quiet = verbose ? "" : "-q"
99 mimetype_filename = write_mimetype()
100 meta = File.basename(@meta_dir)
101 oebps = File.basename(@oebps_dir)
102 images = copy_images()
105 callouts = copy_callouts()
106 # zip -X -r ../book.epub mimetype META-INF OEBPS
107 # Double-quote stylesheet & file to help Windows cmd.exe
108 zip_cmd = %Q(cd "#{@output_dir}" && #{ZIPPER} #{quiet} -X -r "#{File.expand_path(output_file)}" "#{mimetype_filename}" "#{meta}" "#{oebps}")
109 puts zip_cmd if $DEBUG
110 success = system(zip_cmd)
111 raise "Could not bundle into .epub file to #{output_file}" unless success
114 # Input must be collapsed because REXML couldn't find figures in files that
115 # were XIncluded or added by ENTITY
116 # http://sourceforge.net/tracker/?func=detail&aid=2750442&group_id=21935&atid=373747
118 # Double-quote stylesheet & file to help Windows cmd.exe
119 collapsed_file = File.join(File.expand_path(File.dirname(@docbook_file)),
120 '.collapsed.' + File.basename(@docbook_file))
121 entity_collapse_command = %Q(xmllint --loaddtd --noent -o "#{collapsed_file}" "#{@docbook_file}")
122 entity_success = system(entity_collapse_command)
123 raise "Could not collapse named entites in #{@docbook_file}" unless entity_success
125 xinclude_collapse_command = %Q(xmllint --xinclude -o "#{collapsed_file}" "#{collapsed_file}")
126 xinclude_success = system(xinclude_collapse_command)
127 raise "Could not collapse XIncludes in #{@docbook_file}" unless xinclude_success
129 @to_delete << collapsed_file
130 return collapsed_file
134 new_callout_images = []
136 calloutglob = "#{CALLOUT_FULL_PATH}/*#{CALLOUT_EXT}"
137 Dir.glob(calloutglob).each {|img|
138 img_new_filename = File.join(@oebps_dir, CALLOUT_PATH, File.basename(img))
140 # TODO: What to rescue for these two?
141 FileUtils.mkdir_p(File.dirname(img_new_filename))
142 FileUtils.cp(img, img_new_filename)
143 @to_delete << img_new_filename
144 new_callout_images << img
147 return new_callout_images
152 @embedded_fonts.each {|font_file|
153 font_new_filename = File.join(@oebps_dir, File.basename(font_file))
154 FileUtils.cp(font_file, font_new_filename)
155 new_fonts << font_file
162 css_new_filename = File.join(@oebps_dir, File.basename(@css_file))
163 FileUtils.cp(@css_file, css_new_filename)
168 image_references = get_image_refs()
170 image_references.each {|img|
171 # TODO: It'd be cooler if we had a filetype lookup rather than just
173 if img =~ /\.(svg|png|gif|jpe?g|xml)/i
174 img_new_filename = File.join(@oebps_dir, img)
175 img_full = File.join(File.expand_path(File.dirname(@docbook_file)), img)
177 # TODO: What to rescue for these two?
178 FileUtils.mkdir_p(File.dirname(img_new_filename))
179 puts(img_full + ": " + img_new_filename) if $DEBUG
180 FileUtils.cp(img_full, img_new_filename)
181 @to_delete << img_new_filename
182 new_images << img_full
189 mimetype_filename = File.join(@output_dir, "mimetype")
190 File.open(mimetype_filename, "w") {|f| f.print MIMETYPE}
191 @to_delete << mimetype_filename
192 return File.basename(mimetype_filename)
195 def cleanup_files(file_list)
196 file_list.flatten.each {|f|
198 FileUtils.rm_r(f, :force => true )
202 # Returns an Array of all of the (image) @filerefs in a document
204 parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
206 while parser.has_next?
208 if el.start_element? and (el[0] == "imagedata" or el[0] == "graphic")
209 image_refs << el[1]['fileref']
212 return image_refs.uniq
215 # Returns true if the document has code callouts
217 parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
218 while parser.has_next?
220 if el.start_element? and (el[0] == "calloutlist" or el[0] == "co")