1
  E   ˆl–äY>”ª_)%+€rã/©‹Fÿ�þD)Y²ÊÓB²2ùüÿÂƒyð¿Á_�uÐb&=LyZËcõÁÀ "×    require 'fileutils'
require 'rexml/parsers/pullparser'
require 'tmpdir'

module DocBook

  class Epub
    CHECKER = "epubcheck"
    STYLESHEET_PATH = File.join('usr/share/xml/docbook/stylesheet', 'docbook-xsl')
    STYLESHEET_NS_PATH = File.join('usr/share/xml/docbook/stylesheet', 'docbook-xsl-ns')
    if File.directory?(File.expand_path(File.join(File::Separator, STYLESHEET_PATH)))
      STYLESHEET = File.expand_path(File.join(File::Separator, STYLESHEET_PATH, 'epub', "docbook.xsl"))
    elsif File.directory?(File.expand_path(File.join(File::Separator, STYLESHEET_NS_PATH)))
      STYLESHEET = File.expand_path(File.join(File::Separator, STYLESHEET_NS_PATH, 'epub', "docbook.xsl"))
    else
      fail "Stylesheet path not found. Use the -s switch."
    end
    fail "Stylesheet #{STYLESHEET} not found. Use the -s switch." unless File.exist?(STYLESHEET)
    CALLOUT_PATH = File.join('images', 'callouts')
    CALLOUT_FULL_PATH = File.expand_path(File.join(File::Separator, STYLESHEET_PATH, CALLOUT_PATH))
    CALLOUT_LIMIT = 15
    CALLOUT_EXT = ".png"
    XSLT_PROCESSOR = "xsltproc"
    OUTPUT_DIR = File.expand_path(File.join(Dir::tmpdir, ".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 = [OUTPUT_DIR]
      
      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)
      r