class Eepub::Epub

Constants

XMLNS

Attributes

path[R]
title[W]

Public Class Methods

new(path) click to toggle source
# File lib/eepub/epub.rb, line 22
def initialize(path)
  @path = path
end

Public Instance Methods

save!(to: path) click to toggle source
# File lib/eepub/epub.rb, line 38
def save!(to: path)
  FileUtils.cp path, to unless path == to

  Zip::File.open to do |zip|
    rootfile_entry = find_rootfile_entry(zip)
    rootfile_doc   = parse_xml(rootfile_entry)

    get_title_element(rootfile_doc).text = title

    zip.get_output_stream rootfile_entry.name, &rootfile_doc.method(:write)
    zip.commit
  end
end
title() click to toggle source
# File lib/eepub/epub.rb, line 28
def title
  @title ||= Zip::File.open(path) {|zip|
    rootfile_doc = parse_xml(find_rootfile_entry(zip))

    get_title_element(rootfile_doc).text
  }
end

Private Instance Methods

find_rootfile_entry(zip) click to toggle source
# File lib/eepub/epub.rb, line 54
def find_rootfile_entry(zip)
  container_doc = parse_xml(zip.find_entry('META-INF/container.xml'))
  rootfile_path = REXML::XPath.first(container_doc, '//container:rootfile/@full-path', XMLNS.slice('container')).value

  zip.find_entry(rootfile_path)
end
get_title_element(doc) click to toggle source
# File lib/eepub/epub.rb, line 66
def get_title_element(doc)
  REXML::XPath.first(doc, '//dc:title', XMLNS.slice('dc'))
end
parse_xml(entry) click to toggle source
# File lib/eepub/epub.rb, line 61
def parse_xml(entry)
  content = entry.get_input_stream.read
  REXML::Document.new(content)
end