class EPUB::Directory

Constants

DEST_DIR
SRC_DIR
VERSION

epub-directory version

Public Class Methods

add(path) click to toggle source
# File lib/epub/directory.rb, line 20
def add(path)
  EPUB::Parser.parse(path).renditions.each do |rendition|
    add_rendition rendition
  end
end
add_rendition(rendition) click to toggle source
# File lib/epub/directory.rb, line 30
def add_rendition(rendition)
  Rendition.add rendition
end
build() click to toggle source
# File lib/epub/directory.rb, line 26
def build
  new.build
end
run(paths) click to toggle source
# File lib/epub/directory.rb, line 13
def run(paths)
  paths.each do |path|
    add path
  end
  build
end

Public Instance Methods

build() click to toggle source
# File lib/epub/directory.rb, line 35
def build
  DEST_DIR.mkpath
  opfs = []
  Pathname.glob("#{SRC_DIR}/**/*.opf").each do |opf_path|
    make_destination_directory opf_path
    build_opf opf_path
    build_html opf_path
    build_opds opf_path

    opfs << opf_path.to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  rescue => error
    warn "Error occurred while processing #{opf_path}"
    raise if $DEBUG
  end
  build_indices opfs
end
build_html(opf_path) click to toggle source
# File lib/epub/directory.rb, line 64
def build_html(opf_path)
  dest_path = opf_path.sub_ext(".html").to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  rendition = EPUB::Parser::Publication.new(opf_path.read).parse
  metadata = rendition.metadata
  page_title = "Information on #{metadata.title}(Release Identifier: #{metadata.release_identifier})"
  template_path = File.join(__dir__, "../../templates/release-identifier.haml")
  engine = Haml::Engine.new(File.read(template_path))
  engine.options[:format] = :html5
  dest_path.write engine.render(binding)
end
build_indices(opfs) click to toggle source
# File lib/epub/directory.rb, line 108
def build_indices(opfs)
  dest_path = DEST_DIR/"index.html"
  page_title = "EPUB Directory"
  items = opfs.lazy.collect {|path|
    {
      "uri" => Pathname.new(path).sub_ext(".html").to_path.sub(DEST_DIR.to_path, ""),
      "metadata" => EPUB::Parser::Publication.new(File.read(path)).parse.metadata
    }
  }
  template_path = File.join(__dir__, "../../templates/index.haml")
  engine = Haml::Engine.new(File.read(template_path))
  engine.options[:format] = :html5
  dest_path.write engine.render(binding)
end
build_json(opf_path) click to toggle source
# File lib/epub/directory.rb, line 104
def build_json(opf_path)
  raise NotImplementedError
end
build_opds(opf_path) click to toggle source
# File lib/epub/directory.rb, line 76
def build_opds(opf_path)
  dest_path = opf_path.sub_ext(".opds").to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  rendition = EPUB::Parser::Publication.new(opf_path.read).parse
  opds = RSS::Maker.make("atom:entry") {|maker|
    maker.items.new_item do |entry|
      entry.id = rendition.metadata.release_identifier
      entry.updated = rendition.metadata.modified.to_s
      entry.title = rendition.metadata.title
      # TODO: Filter authors by refines
      creators = rendition.metadata.creators + rendition.metadata.metas.select {|meta| ["dc:publisher", "dcterms:creator"].include? meta.property}
      creators.each do |creator|
        entry.authors.new_author do |author|
          author.name = creator.to_s
        end
      end
      entry.author = "Unknown" unless entry.author
      entry.summary = rendition.metadata.description
      entry.links.new_link do |link|
        link.rel = RSS::OPDS::RELATIONS["acquisition"] # TODO: Arrange by info.toml
        link.href = dest_path.basename.sub_ext(".epub").to_path # TODO: Arrange by info.toml
        link.type = "application/epub+zip"
      end
    end
  }
  dest_path.write opds
end
build_opf(opf_path) click to toggle source
# File lib/epub/directory.rb, line 58
def build_opf(opf_path)
  dest_path = opf_path.to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  dest_path.write opf_path.read
end
make_destination_directory(opf_path) click to toggle source
# File lib/epub/directory.rb, line 52
def make_destination_directory(opf_path)
  dest_path = opf_path.to_path.sub(SRC_DIR.to_path, DEST_DIR.to_path)
  dest_path = Pathname.new(dest_path)
  dest_path.dirname.mkpath
end