Class: Markdownator::Converters::Epub

Inherits:
Base
  • Object
show all
Defined in:
lib/markdownator/converters/epub.rb

Overview

Converts an EPUB into Markdown by reading the OPF spine order and running each XHTML chapter through the HTML converter.

Constant Summary collapse

CONTAINER_PATH =
"META-INF/container.xml"

Instance Method Summary collapse

Instance Method Details

#accepts?(_io, stream_info) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/markdownator/converters/epub.rb', line 10

def accepts?(_io, stream_info)
  matches?(stream_info, extensions: %w[epub], mimetypes: %w[application/epub+zip])
end

#convert(io, _stream_info, **_options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/markdownator/converters/epub.rb', line 14

def convert(io, _stream_info, **_options)
  Markdownator.require_optional("zip", feature: "EPUB conversion")
  Markdownator.require_optional("nokogiri", feature: "EPUB conversion")

  ::Zip::File.open_buffer(io) do |zip|
    opf_path = locate_opf(zip)
    raise FileConversionError, "EPUB is missing its OPF package document" if opf_path.nil?

    opf = Nokogiri::XML(read(zip, opf_path))
    opf.remove_namespaces!
    base = File.dirname(opf_path)
    title = opf.at_xpath("//metadata/title")&.text&.strip
    chapters = spine_documents(zip, opf, base)
    return Result.new(markdown: chapters.join("\n\n"), title: title)
  end
end