class DocbookXslWrapper::Epub

Attributes

options[R]
xml[R]

Public Class Methods

new(options) click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 9
def initialize(options)
  @options = options
  @xml     = Nokogiri::XML(File.open(@options.docbook, 'rb'))
end

Public Instance Methods

create() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 14
def create
  # Nokogiri doesn't create directories, so we do it manually
  Dir.mkdir(File.join(options.destination, meta_inf_directory))
  Dir.mkdir(File.join(options.destination, oebps_directory))

  render_to_epub
  bundle_epub
end

Private Instance Methods

bundle_epub() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 79
def bundle_epub
  copy_media_files_to_epub_dir
  create_mimetype_file if options.format == 'epub' # EPUB3 stylesheet creates this automatically

  quiet = options.verbose ? '' : '-q'
  # Double-quote stylesheet & file to help Windows cmd.exe
  zip_cmd = %Q(cd "#{options.destination}" && zip #{quiet} -X -r  "#{File.expand_path(options.output)}" "mimetype" "#{meta_inf_directory}" "#{oebps_directory}")

  puts zip_cmd if options.debug
  success = system(zip_cmd)
  raise "Could not bundle into .epub file to #{options.output}" unless success
end
copy_callouts() click to toggle source

TODO: This method is not being called for the moment.…needs to be tested

# File lib/docbook_xsl_wrapper/epub.rb, line 174
def copy_callouts
  return unless has_callouts?

  images = Array.new
  calloutglob = "#{options.callout_full_path}/*#{options.callout_ext}"
  Dir.glob(calloutglob).each do |image|
    new_filename = File.join(oebps_path, options.callout_path, File.basename(image))

    # TODO: What to rescue for these two?
    FileUtils.mkdir_p(File.dirname(new_filename))
    FileUtils.cp(image, new_filename)
    images << image
  end
  images
end
copy_css() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 118
def copy_css
  return unless options.css

  FileUtils.cp(options.css, File.join(options.destination, oebps_directory, File.basename(options.css)))
end
copy_fonts() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 107
def copy_fonts
  return if options.fonts.empty?

  font_directory = File.join(options.destination, oebps_directory, 'fonts')
  Dir.mkdir(font_directory)

  options.fonts.each do |font|
    FileUtils.cp(font, File.join(font_directory, File.basename(font)))
  end
end
copy_image(image) click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 145
def copy_image(image)
  source      = File.join(File.dirname(options.docbook), image)
  destination = File.join(options.destination, oebps_directory, image)

  FileUtils.mkdir_p(File.dirname(destination))

  puts "Copying image: #{source} to #{destination}" if options.debug
  FileUtils.cp(source, destination)
end
copy_images() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 124
def copy_images
  xml_image_references.each do |image|
    copy_image(image)
  end
end
copy_media_files_to_epub_dir() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 98
def copy_media_files_to_epub_dir
  copy_fonts
  copy_css
  copy_images

  # Callouts disabled in this release until more testing can be done
  #copy_callouts
end
create_mimetype_file() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 92
def create_mimetype_file
  filename = File.join(options.destination, "mimetype")
  File.open(filename, "w") {|f| f.print "application/epub+zip"}
  File.basename(filename)
end
css() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 75
def css
  ['html.stylesheet', File.join(File.basename(options.css), '/')]
end
docbook_xsl_path() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 46
def docbook_xsl_path
  case options.format
  when 'epub3'
    File.join(GEM_PATH, 'xsl', 'epub3', 'chunk.xsl')
  else
    File.join(GEM_PATH, 'xsl', 'epub', 'docbook.xsl')
  end
end
fonts() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 71
def fonts
  ['epub.embedded.fonts', options.fonts.map {|f| File.basename(f)}.join(',')]
end
has_callouts?() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 190
def has_callouts?
  parser = REXML::Parsers::PullParser.new(File.new(@collapsed_docbook_file))
  while parser.has_next?
    element = parser.pull
    return true if element.start_element? and (element[0] == "calloutlist" or element[0] == "co")
  end
  false
end
is_valid_image?(image) click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 140
def is_valid_image?(image)
  return true if File.extname(image).match(/\.(jpe?g|png|gif|svg|xml)\z/i)
  false
end
meta_inf_directory() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 163
def meta_inf_directory
  'META-INF'
end
oebps_directory() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 155
def oebps_directory
  'OEBPS'
end
oebps_path() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 159
def oebps_path
  @oebps_path ||= File.join(options.destination, oebps_directory)
end
params() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 55
def params
  params_list = [
    'chunk.quietly', "#{verbosity}",
    'chunk.first.sections', 1,
    'othercredit.like.author.enabled', 1,
    'chapter.autolabel', 0,
    'section.autolabel', 0,
    'part.autolabel', 0,
    'base.dir', File.join(options.destination, '/'),
  ]
  params_list.concat(css) if options.css
  params_list.concat(fonts) unless options.fonts.empty?

  Nokogiri::XSLT.quote_params(params_list)
end
render_to_epub() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 25
def render_to_epub
  errors = xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
  raise "Could not render as .epub to #{options.output}\n\n(#{errors})" unless errors.empty?
end
stylesheet() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 39
def stylesheet
  xsl = docbook_xsl_path
  xsl = options.stylesheet unless options.stylesheet.empty?

  Nokogiri::XSLT(File.open(xsl, 'rb'))
end
verbosity() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 167
def verbosity
  return 0 if options.verbose == true
  return 1
end
xml_image_references() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 130
def xml_image_references
  refs = Array.new
  xml.xpath('//xmlns:imagedata', '//xmlns:graphic', 'xmlns' => 'http://docbook.org/ns/docbook').each do |node|
    img = node.attribute('fileref').value
    refs << img if is_valid_image?(img)
  end

  refs.uniq
end
xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error() click to toggle source
# File lib/docbook_xsl_wrapper/epub.rb, line 30
def xslt_transform_and_rescue_because_it_currently_throws_unknown_runtime_error
  begin
    errors = stylesheet.transform(xml, params)
  rescue
    errors = ''
  end
  errors
end