Class: Peregrin::Epub

Inherits:
Object
  • Object
show all
Defined in:
lib/formats/epub.rb

Defined Under Namespace

Classes: FailureLoadingNAV, FailureLoadingNCX, FailureLoadingOCF, FailureLoadingOPF, FileNotFound, NotAZipArchive, ValidationError

Constant Summary collapse

FORMAT =
"EPUB"
NAMESPACES =
{
  :ocf => { 'ocf' => 'urn:oasis:names:tc:opendocument:xmlns:container' },
  :opf => { 'opf' => 'http://www.idpf.org/2007/opf' },
  :dc => { 'dc' => 'http://purl.org/dc/elements/1.1/' },
  :ncx => { 'ncx' => 'http://www.daisy.org/z3986/2005/ncx/' },
  :svg => { 'svg' => 'http://www.w3.org/2000/svg' },
  :nav => { 'nav' => 'http://www.w3.org/1999/xhtml'}
}
OCF_PATH =
"META-INF/container.xml"
HTML5_TAGNAMES =

FIXME: Which to divify? Which to leave as-is?

%w[section nav article aside hgroup header footer figure figcaption]
MIMETYPE_MAP =
{
  '.xhtml' => 'application/xhtml+xml',
  '.odt' => 'application/x-dtbook+xml',
  '.odt' => 'application/x-dtbook+xml',
  '.ncx' => 'application/x-dtbncx+xml',
  '.epub' => 'application/epub+zip'
}
OEBPS =
"OEBPS"
NCX =
'content'
OPF =
'content'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, epub_path = nil) ⇒ Epub

Returns a new instance of Epub.



53
54
55
56
57
58
# File 'lib/formats/epub.rb', line 53

def initialize(book, epub_path = nil)
  @book = book
  if epub_path
    load_from_path(epub_path)
  end
end

Class Method Details

.read(path) ⇒ Object



47
48
49
50
# File 'lib/formats/epub.rb', line 47

def self.read(path)
  book = Peregrin::Book.new
  new(book, path)
end

.validate(path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/formats/epub.rb', line 27

def self.validate(path)
  raise FileNotFound.new(path)  unless File.file?(path)
  begin
    zf = Zip::Archive.open(path)
  rescue => e
    raise NotAZipArchive.new(path)
  end

  begin
    book = Peregrin::Book.new
    epub = new(book)
    epub.send(:load_blueprints, zf)
  rescue => e
    raise e.class.new(path)
  end
ensure
  zf.close  if zf
end

Instance Method Details

#to_book(options = {}) ⇒ Object



72
73
74
# File 'lib/formats/epub.rb', line 72

def to_book(options = {})
  @book.deep_clone
end

#write(path) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/formats/epub.rb', line 61

def write(path)
  with_working_dir(path) {
    build_ocf
    build_ncx
    write_components
    build_opf
    zip_it_up(File.basename(path))
  }
end