dbtoepub-0+svn9759.orig/0000755000175000017500000000000012150502452012672 5ustar dldldbtoepub-0+svn9759.orig/bin/0000755000175000017500000000000012150502452013442 5ustar dldldbtoepub-0+svn9759.orig/bin/dbtoepub0000755000175000017500000000515411443517330015206 0ustar dldl#!/usr/bin/env ruby # This program converts DocBook documents into .epub files. # # Usage: dbtoepub [OPTIONS] [DocBook Files] # # .epub is defined by the IDPF at www.idpf.org and is made up of 3 standards: # - Open Publication Structure (OPS) # - Open Packaging Format (OPF) # - Open Container Format (OCF) # # Specific options: # -c, --css [FILE] Use FILE for CSS on generated XHTML. # -d, --debug Show debugging output. # -f, --font [OTF FILE] Embed OTF FILE in .epub. # -h, --help Display usage info. # -s, --stylesheet [XSL FILE] Use XSL FILE as a customization # layer (imports epub/docbook.xsl). # -v, --verbose Make output verbose. lib = File.expand_path(File.join(File.dirname(__FILE__), 'lib')) $LOAD_PATH.unshift(lib) if File.exist?(lib) require 'fileutils' require 'optparse' require 'tmpdir' require 'docbook' verbose = false debug = false css_file = nil otf_files = [] customization_layer = nil output_file = nil #$DEBUG=true # Set up the OptionParser opts = OptionParser.new opts.banner = "Usage: #{File.basename($0)} [OPTIONS] [DocBook Files] #{File.basename($0)} converts DocBook and
s into to .epub files. .epub is defined by the IDPF at www.idpf.org and is made up of 3 standards: - Open Publication Structure (OPS) - Open Packaging Format (OPF) - Open Container Format (OCF) Specific options:" opts.on("-c", "--css [FILE]", "Use FILE for CSS on generated XHTML.") {|f| css_file = f} opts.on("-d", "--debug", "Show debugging output.") {debug = true; verbose = true} opts.on("-f", "--font [OTF FILE]", "Embed OTF FILE in .epub.") {|f| otf_files << f} opts.on("-h", "--help", "Display usage info.") {puts opts.to_s; exit 0} opts.on("-o", "--output [OUTPUT FILE]", "Output ePub file as OUTPUT FILE.") {|f| output_file = f} opts.on("-s", "--stylesheet [XSL FILE]", "Use XSL FILE as a customization layer (imports epub/docbook.xsl).") {|f| customization_layer = f} opts.on("-v", "--verbose", "Make output verbose.") {verbose = true} db_files = opts.parse(ARGV) if db_files.size == 0 puts opts.to_s exit 0 end db_files.each {|docbook_file| dir = File.expand_path(File.join(Dir.tmpdir, ".epubtmp#{Time.now.to_f.to_s}")) FileUtils.mkdir_p(dir) e = DocBook::Epub.new(docbook_file, dir, css_file, customization_layer, otf_files) if output_file epub_file = output_file else epub_file = File.basename(docbook_file, ".xml") + ".epub" end puts "Rendering DocBook file #{docbook_file} to #{epub_file}" if verbose e.render_to_file(epub_file) } dbtoepub-0+svn9759.orig/bin/lib/0000755000175000017500000000000011443517262014221 5ustar dldldbtoepub-0+svn9759.orig/bin/lib/docbook.rb0000755000175000017500000002003411443517262016170 0ustar dldlrequire 'fileutils' require 'rexml/parsers/pullparser' module DocBook class Epub CHECKER = "epubcheck" STYLESHEET = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', "docbook.xsl")) CALLOUT_PATH = File.join('images', 'callouts') CALLOUT_FULL_PATH = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', CALLOUT_PATH)) CALLOUT_LIMIT = 15 CALLOUT_EXT = ".png" XSLT_PROCESSOR = "xsltproc" OUTPUT_DIR = ".epubtmp#{Time.now.to_f.to_s}" MIMETYPE = "application/epub+zip" META_DIR = "META-INF" OEBPS_DIR = "OEBPS" ZIPPER = "zip" attr_reader :output_dir def initialize(docbook_file, output_dir=OUTPUT_DIR, css_file=nil, customization_layer=nil, embedded_fonts=[]) @docbook_file = docbook_file @output_dir = output_dir @meta_dir = File.join(@output_dir, META_DIR) @oebps_dir = File.join(@output_dir, OEBPS_DIR) @css_file = css_file ? File.expand_path(css_file) : css_file @embedded_fonts = embedded_fonts @to_delete = [] if customization_layer @stylesheet = File.expand_path(customization_layer) else @stylesheet = STYLESHEET end unless File.exist?(@docbook_file) raise ArgumentError.new("File #{@docbook_file} does not exist") end end def render_to_file(output_file, verbose=false) render_to_epub(output_file, verbose) bundle_epub(output_file, verbose) cleanup_files(@to_delete) end def self.invalid?(file) # Obnoxiously, we can't just check for a non-zero output... cmd = %Q(#{CHECKER} "#{file}") output = `#{cmd} 2>&1` if $?.to_i == 0 return false else STDERR.puts output if $DEBUG return output end end private def render_to_epub(output_file, verbose) @collapsed_docbook_file = collapse_docbook() chunk_quietly = "--stringparam chunk.quietly " + (verbose ? '0' : '1') callout_path = "--stringparam callout.graphics.path #{CALLOUT_PATH}/" callout_limit = "--stringparam callout.graphics.number.limit #{CALLOUT_LIMIT}" callout_ext = "--stringparam callout.graphics.extension #{CALLOUT_EXT}" html_stylesheet = "--stringparam html.stylesheet #{File.basename(@css_file)}" if @css_file base = "--stringparam base.dir #{OEBPS_DIR}/" unless @embedded_fonts.empty? embedded_fonts = @embedded_fonts.map {|f| File.basename(f)}.join(',') font = "--stringparam epub.embedded.fonts \"#{embedded_fonts}\"" end meta = "--stringparam epub.metainf.dir #{META_DIR}/" oebps = "--stringparam epub.oebps.dir #{OEBPS_DIR}/" options = [chunk_quietly, callout_path, callout_limit, callout_ext, base, font, meta, oebps, html_stylesheet, ].join(" ") # Double-quote stylesheet & file to help Windows cmd.exe db2epub_cmd = %Q(cd "#{@output_dir}" && #{XSLT_PROCESSOR} #{options} "#{@stylesheet}" "#{@collapsed_docbook_file}") STDERR.puts db2epub_cmd if $DEBUG success = system(db2epub_cmd) raise "Could not render as .epub to #{output_file} (#{db2epub_cmd})" unless success @to_delete << Dir["#{@meta_dir}/*"] @to_delete << Dir["#{@oebps_dir}/*"] end def bundle_epub(output_file, verbose) quiet = verbose ? "" : "-q" mimetype_filename = write_mimetype() meta = File.basename(@meta_dir) oebps = File.basename(@oebps_dir) images = copy_images() csses = copy_csses() fonts = copy_fonts() callouts = copy_callouts() # zip -X -r ../book.epub mimetype META-INF OEBPS # Double-quote stylesheet & file to help Windows cmd.exe zip_cmd = %Q(cd "#{@output_dir}" && #{ZIPPER} #{quiet} -X -r "#{File.expand_path(output_file)}" "#{mimetype_filename}" "#{meta}" "#{oebps}") puts zip_cmd if $DEBUG success = system(zip_cmd) raise "Could not bundle into .epub file to #{output_file}" unless success end # Input must be collapsed because REXML couldn't find figures in files that # were XIncluded or added by ENTITY # http://sourceforge.net/tracker/?func=detail&aid=2750442&group_id=21935&atid=373747 def collapse_docbook # Double-quote stylesheet & file to help Windows cmd.exe collapsed_file = File.join(File.expand_path(File.dirname(@docbook_file)), '.collapsed.' + File.basename(@docbook_file)) entity_collapse_command = %Q(xmllint --loaddtd --noent -o "#{collapsed_file}" "#{@docbook_file}") entity_success = system(entity_collapse_command) raise "Could not collapse named entites in #{@docbook_file}" unless entity_success xinclude_collapse_command = %Q(xmllint --xinclude -o "#{collapsed_file}" "#{collapsed_file}") xinclude_success = system(xinclude_collapse_command) raise "Could not collapse XIncludes in #{@docbook_file}" unless xinclude_success @to_delete << collapsed_file return collapsed_file end def copy_callouts new_callout_images = [] if has_callouts? calloutglob = "#{CALLOUT_FULL_PATH}/*#{CALLOUT_EXT}" Dir.glob(calloutglob).each {|img| img_new_filename = File.join(@oebps_dir, CALLOUT_PATH, File.basename(img)) # TODO: What to rescue for these two? FileUtils.mkdir_p(File.dirname(img_new_filename)) FileUtils.cp(img, img_new_filename) @to_delete << img_new_filename new_callout_images << img } end return new_callout_images end def copy_fonts new_fonts = [] @embedded_fonts.each {|font_file| font_new_filename = File.join(@oebps_dir, File.basename(font_file)) FileUtils.cp(font_file, font_new_filename) new_fonts << font_file } return new_fonts end def copy_csses if @css_file css_new_filename = File.join(@oebps_dir, File.basename(@css_file)) FileUtils.cp(@css_file, css_new_filename) end end def copy_images image_references = get_image_refs() new_images = [] image_references.each {|img| # TODO: It'd be cooler if we had a filetype lookup rather than just # extension if img =~ /\.(svg|png|gif|jpe?g|xml)/i img_new_filename = File.join(@oebps_dir, img) img_full = File.join(File.expand_path(File.dirname(@docbook_file)), img) # TODO: What to rescue for these two? FileUtils.mkdir_p(File.dirname(img_new_filename)) puts(img_full + ": " + img_new_filename) if $DEBUG FileUtils.cp(img_full, img_new_filename) @to_delete << img_new_filename new_images << img_full end } return new_images end def write_mimetype mimetype_filename = File.join(@output_dir, "mimetype") File.open(mimetype_filename, "w") {|f| f.print MIMETYPE} @to_delete << mimetype_filename return File.basename(mimetype_filename) end def cleanup_files(file_list) file_list.flatten.each {|f| # Yikes FileUtils.rm_r(f, :force => true ) } end # Returns an Array of all of the (image) @filerefs in a document def get_image_refs parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file)) image_refs = [] while parser.has_next? el = parser.pull if el.start_element? and (el[0] == "imagedata" or el[0] == "graphic") image_refs << el[1]['fileref'] end end return image_refs.uniq end # Returns true if the document has code callouts def has_callouts? parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file)) while parser.has_next? el = parser.pull if el.start_element? and (el[0] == "calloutlist" or el[0] == "co") return true end end return false end end end dbtoepub-0+svn9759.orig/bin/xslt/0000755000175000017500000000000011000777641014443 5ustar dldldbtoepub-0+svn9759.orig/bin/xslt/obfuscate.xsl0000644000175000017500000000113011000777641017141 0ustar dldl dbtoepub-0+svn9759.orig/README0000644000175000017500000000704511020602012013544 0ustar dldl---------------------------------------------------------------------- README file for the DocBook XSL Stylesheets ---------------------------------------------------------------------- These are XSL stylesheets for transforming DocBook XML document instances into .epub format. .epub is an open standard of the The International Digital Publishing Forum (IDPF), a the trade and standards association for the digital publishing industry. An alpha-quality reference implementation (dbtoepub) for a DocBook to .epub converter (written in Ruby) is available under bin/. From http://idpf.org What is EPUB, .epub, OPS/OCF & OEB? ".epub" is the file extension of an XML format for reflowable digital books and publications. ".epub" is composed of three open standards, the Open Publication Structure (OPS), Open Packaging Format (OPF) and Open Container Format (OCF), produced by the IDPF. "EPUB" allows publishers to produce and send a single digital publication file through distribution and offers consumers interoperability between software/hardware for unencrypted reflowable digital books and other publications. The Open eBook Publication Structure or "OEB", originally produced in 1999, is the precursor to OPS. ---------------------------------------------------------------------- .epub Constraints ---------------------------------------------------------------------- .epub does not support all of the image formats that DocBook supports. When an image is available in an accepted format, it will be used. The accepted @formats are: 'GIF','GIF87a','GIF89a','JPEG','JPG','PNG','SVG' A mime-type for the image will be guessed from the file extension, which may not work if your file extensions are non-standard. Non-supported elements: * * , , , with text/XML @filerefs * * in lists (generic XHTML rendering inability) * (just make your programlistings siblings, rather than descendents of paras) ---------------------------------------------------------------------- dbtoepub Reference Implementation ---------------------------------------------------------------------- An alpha-quality DocBook to .epub conversion program, dbtoepub, is provided in bin/dbtoepub. This tool requires: - 'xsltproc' in your PATH - 'zip' in your PATH - Ruby 1.8.4+ Windows compatibility has not been extensively tested; bug reports encouraged. [See http://www.zlatkovic.com/libxml.en.html and http://unxutils.sourceforge.net/] $ dbtoepub --help Usage: dbtoepub [OPTIONS] [DocBook Files] dbtoepub converts DocBook and
s into to .epub files. .epub is defined by the IDPF at www.idpf.org and is made up of 3 standards: - Open Publication Structure (OPS) - Open Packaging Format (OPF) - Open Container Format (OCF) Specific options: -d, --debug Show debugging output. -h, --help Display usage info -v, --verbose Make output verbose ---------------------------------------------------------------------- Validation ---------------------------------------------------------------------- The epubcheck project provides limited validation for .epub documents. See http://code.google.com/p/epubcheck/ for details. ---------------------------------------------------------------------- Copyright information ---------------------------------------------------------------------- See the accompanying file named COPYING. dbtoepub-0+svn9759.orig/docbook.xsl0000644000175000017500000022314712140404707015056 0ustar dldl 1 2 book toc,title 4 ERROR: the $base.dir param must not include the directory in its path. Exiting. / ncxtoc htmltoc 0 .png 1 1 1 1 0 Note namesp. cut stripped namespace before processing Note namesp. cut processing stripped document ID ' ' not found in document. Formatting from urn: : urn:isbn: urn:issn: _ 2.0 cover 1.0 application/oebps-package+xml 2005-1 cover dtb:uid : : © cover Cover toc Table of Contents yes yes no application/x-dtbncx+xml application/xhtml+xml text/css css application/xhtml+xml image/gif image/gif image/png image/png image/jpeg image/jpeg image/jpeg image/jpeg image/svg+xml image/svg+xml WARNING: mediaobjectco almost certainly will not render as expected in .epub! application/xhtml+xml (missing alt) text-align: middle 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 No insertfile extension available. No insertfile extension available. Use a different processor (with extensions) or turn on $use.extensions and $textinsert.extension (see docs for more). Cover text/css img { max-width: 100%; } -toc font/opentype WARNING: OpenType fonts should be supplied! ( ) 6 clear: both 1 1 1 2 3 4 5 6 5 4 3 2 1 title