Class: Rpub::Epub::Content

Inherits:
XmlFile
  • Object
show all
Defined in:
lib/rpub/epub/content.rb

Constant Summary collapse

MEDIA_TYPES =
{
  'png' => 'image/png',
  'gif' => 'image/gif',
  'jpg' => 'image/jpeg',
  'svg' => 'image/svg+xml'
}

Instance Attribute Summary collapse

Attributes inherited from XmlFile

#xml

Instance Method Summary collapse

Methods inherited from XmlFile

#to_s

Constructor Details

#initialize(book) ⇒ Content

Returns a new instance of Content.



13
14
15
16
# File 'lib/rpub/epub/content.rb', line 13

def initialize(book)
  @book = book
  super()
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



4
5
6
# File 'lib/rpub/epub/content.rb', line 4

def book
  @book
end

Instance Method Details

#renderObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rpub/epub/content.rb', line 18

def render
  xml.instruct!
  xml.declare! :DOCTYPE, :package, :PUBLIC,  '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
  xml.package 'xmlns' => 'http://www.idpf.org/2007/opf', 'unique-identifier' => 'BookId', 'version' => '2.0' do
    xml. 'xmlns:dc' => 'http://purl.org/dc/elements/1.1/', 'xmlns:opf' => 'http://www.idpf.org/2007/opf' do
      xml.dc :language,    book.language
      xml.dc :title,       book.title
      xml.dc :creator,     book.creator, 'opf:role' => 'aut'
      xml.dc :publisher,   book.publisher
      xml.dc :subject,     book.subject
      xml.dc :identifier,  book.uid, :id => 'BookId'
      xml.dc :rights,      book.rights
      xml.dc :description, book.description

      if book.has_cover?
        xml.meta :name => 'cover', :content => 'cover-image'
      end
    end

    xml.manifest do
      xml.item 'id' => 'ncx', 'href' => 'toc.ncx', 'media-type' => 'application/x-dtbncx+xml'
      xml.item 'id' => 'css', 'href' => 'styles.css', 'media-type' => 'text/css'

      if book.has_fonts?
        book.fonts.each do |font|
          xml.item 'id' => File.basename(font), 'href' => font, 'media-type' => 'font/opentype'
        end
      end

      if book.has_cover?
        xml.item 'id' => 'cover', 'href' => 'cover.html', 'media-type' => 'application/xhtml+xml'
        xml.item 'id' => 'cover-image', 'href' => book.cover_image, 'media-type' => guess_media_type(book.cover_image)
      end

      book.images.each do |image|
        xml.item 'id' => File.basename(image), 'href' => image, 'media-type' => guess_media_type(image)
      end

      if book.has_toc?
        xml.item 'id' => 'toc', 'href' => 'toc.html', 'media-type' => 'application/xhtml+xml'
      end

      book.chapters.each do |chapter|
        xml.item 'id' => chapter.xml_id, 'href' => chapter.filename, 'media-type' => 'application/xhtml+xml'
      end
    end

    xml.spine 'toc' => 'ncx' do
      if book.has_cover?
        xml.itemref 'idref' => 'cover', 'linear' => 'no'
      end
      book.chapters.each do |chapter|
        xml.itemref 'idref' => chapter.xml_id
      end
    end

    if book.has_cover? || book.has_toc?
      xml.guide do
        if book.has_cover?
          xml.reference :type => 'cover', :title => 'Cover', :href => 'cover.html'
        end
        if book.has_toc?
          xml.reference :type => 'toc',   :title => 'Table of Contents', :href => 'toc.html'
        end
      end
    end
  end
end