Class: Bindery::Formats::Epub

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

Overview

Builds an EPUB book file from the book description.

The EPUB Wikipedia entry provides a nice, concise overview of the EPUB format.

For more precise details:

Defined Under Namespace

Modules: BookMethods, ChapterMethods Classes: ManifestEntry

Constant Summary collapse

MimeTypes =
{
  '.jpg' => 'image/jpeg',
  '.png' => 'image/png',
  '.gif' => 'image/gif',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book) ⇒ Epub

Returns a new instance of Epub.



35
36
37
38
39
40
# File 'lib/bindery/formats/epub.rb', line 35

def initialize(book)
  self.book = book
  book.extend BookMethods
  book.chapters.each{|chapter| chapter.extend ChapterMethods}
  self.manifest_entries = []
end

Instance Attribute Details

#bookObject

Returns the value of attribute book.



33
34
35
# File 'lib/bindery/formats/epub.rb', line 33

def book
  @book
end

#manifest_entriesObject

Returns the value of attribute manifest_entries.



33
34
35
# File 'lib/bindery/formats/epub.rb', line 33

def manifest_entries
  @manifest_entries
end

Instance Method Details

#add_manifest_entry(file_name) ⇒ Object



197
198
199
200
# File 'lib/bindery/formats/epub.rb', line 197

def add_manifest_entry(file_name)
  xml_id, ext = File.base_parts(file_name.gsub('/', '-'))
  manifest_entries << ManifestEntry.new(file_name, xml_id, MimeTypes[ext])
end

#containerObject



69
70
71
72
73
74
75
76
77
# File 'lib/bindery/formats/epub.rb', line 69

def container
  %q{|<?xml version="1.0" encoding="UTF-8" ?>
     |<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
     |  <rootfiles>
     |    <rootfile full-path="book.opf" media-type="application/oebps-package+xml"/>
     |  </rootfiles>
     |</container>
     |}.strip_margin
end

#coverObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/bindery/formats/epub.rb', line 202

def cover
  xm = Builder::XmlMarkup.new(:indent => 2)
  xm.instruct!
  xm.declare!(:DOCTYPE, :html, :PUBLIC, '-//W3C//DTD XHTML 1.1//END', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd')
  xm.html('xmlns'=>'http://www.w3.org/1999/xhtml') { # ??? xml:lang attribute?
    xm.head {
      xm.title "#{book.title}: Cover"
      xm.meta('http-equiv'=>'Content-Type', 'content'=>'application/xhtml+xml; charset=utf-8')
    }
    xm.body {
      xm.div('style'=>'text-align: center; page-break-after: always;') {
        if book.cover
          xm.img('src'=>"images/#{book.cover}", 'alt'=>book.title, 'style'=>'height: 100%; max-width: 100%;')
        else
          xm.h1 book.title
          xm.h2 book.subtitle if book.subtitle
          xm.h3 "by #{book.author}" if book.author
        end
      }
    }
  }
end

#generateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bindery/formats/epub.rb', line 42

def generate
  File.delete(book.epub_output_file) if File.exist?(book.epub_output_file)
  Zip::ZipFile.open(book.epub_output_file, Zip::ZipFile::CREATE) do |zipfile|
    # FIXME: The mimetype file is supposed to be the first one in the Zip directory, but that doesn't seem to be happening.
    zipfile.write_uncompressed_file 'mimetype', mimetype
    zipfile.mkdir 'META-INF'
    zipfile.write_file 'META-INF/container.xml', container
    
    # also frontmatter, backmatter
    book.chapters.each do |chapter|
      write_chapter(chapter, zipfile)
    end
    
    zipfile.mkdir 'css'
    zipfile.write_file 'css/book.css', stylesheet

    zipfile.write_file 'book.opf', opf
    zipfile.write_file 'book.ncx', ncx
  end
end

#ident_options(opts) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/bindery/formats/epub.rb', line 259

def ident_options(opts)
  if book.isbn
    return opts.merge('id'=>'BookId') if opts['opf:scheme'] == 'ISBN'
  else
    return opts.merge('id'=>'BookId') if opts['opf:scheme'] == 'URL'
  end
  opts
end

#include_images(doc, zipfile) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bindery/formats/epub.rb', line 180

def include_images(doc, zipfile)
  # TODO: where else can images appear? Style sheets?
  zipfile.mkdir('images') unless zip_dir_exists?(zipfile, 'images')
  doc.css('img').each do |img|
    url = img['src']
    img_fn = make_image_file_name(zipfile, url)
    # TODO: These images should be cached somewhere for multi-format runs
    open(url, 'r') do |is|
      zipfile.get_output_stream(img_fn) do |os|
        os.write is.read
      end
    end
    add_manifest_entry(img_fn)
    img['src'] = img_fn
  end
end

#make_image_file_name(zipfile, url) ⇒ Object



277
278
279
280
281
282
283
284
285
286
# File 'lib/bindery/formats/epub.rb', line 277

def make_image_file_name(zipfile, url)
  stem, ext = File.base_parts(url)
  filename = "images/#{stem}#{ext}"
  n = 0
  while zip_file_exists?(zipfile, filename)
    n += 1
    filename = "#{stem}_#{n}#{ext}"
  end
  filename
end

#mimetypeObject



63
64
65
66
67
# File 'lib/bindery/formats/epub.rb', line 63

def mimetype
  # the mimetype file must be the first file in the archive
  # it must be ASCII, uncompressed, and unencrypted
  'application/epub+zip'
end

#ncxObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/bindery/formats/epub.rb', line 116

def ncx
  xm = Builder::XmlMarkup.new(:indent => 2)
  xm.instruct!
  xm.declare!(:DOCTYPE, :ncx, :PUBLIC, '-//NISO//DTD ncx 2005-1//EN', 'http://www.daisy.org/z3986/2005/ncx-2005-1.dtd')
  xm.ncx('version'=>'2005-1', 'xml:lang'=>'en', 'xmlns'=>'http://www.daisy.org/z3986/2005/ncx/') {
    xm.head {
      xm.meta 'name'=>'dtb:uid', 'content'=>book.ident
      xm.meta 'name'=>'dtb:depth', 'content'=>book.depth
      xm.meta 'name'=>'dtb:totalPageCount', 'content'=>0
      xm.meta 'name'=>'dtb:maxPageNumber', 'content'=>0
    }
    
    xm.docTitle {
      xm.text book.full_title
    }
    
    xm.docAuthor {
      xm.text book.author
    }
    
    xm.navMap {
      play_order = 1
      
      # also frontmatter, backmatter
      book.chapters.each do |chapter|
        xm.navPoint('class'=>'chapter', 'id'=>chapter.epub_id, 'playOrder'=>play_order) {
          xm.navLabel {
            xm.text chapter.title
          }
          xm.content 'src'=>chapter.epub_output_file
        }
        play_order += 1
      end
    }
  }
end

#opfObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bindery/formats/epub.rb', line 79

def opf
  xm = Builder::XmlMarkup.new(:indent => 2)
  xm.instruct!
  xm.package('version'=>'2.0', 'xmlns'=>'http://www.idpf.org/2007/opf', 'unique-identifier'=>'BookId') {
    
    xm.('xmlns:dc'=>'http://purl.org/dc/elements/1.1/', 'xmlns:opf'=>'http://www.idpf.org/2007/opf') {
      # required elements
      xm.dc :title, book.full_title
      xm.dc :language, book.language
      xm.dc :identifier, book.url, ident_options('opf:scheme'=>'URL') if book.url
      xm.dc :identifier, book.isbn, ident_options('opf:scheme'=>'ISBN') if book.isbn            
      
      # optional elements
      xm.dc :creator, book.author, 'opf:role'=>'aut' if book.author
    }
    
    xm.manifest {
      book.chapters.each{|chapter| xm.item 'id'=>chapter.epub_id, 'href'=>chapter.epub_output_file, 'media-type'=>'application/xhtml+xml'}
      # also frontmatter, backmatter
      xm.item 'id'=>'stylesheet', 'href'=>'css/book.css', 'media-type'=>'text/css'
      manifest_entries.each do |entry|
        xm.item 'id'=>entry.xml_id, 'href'=>entry.file_name, 'media-type'=>entry.mime_type
      end
      # xm.item 'id'=>'myfont', 'href'=>'css/myfont.otf', 'media-type'=>'application/x-font-opentype'
      xm.item 'id'=>'ncx', 'href'=>'book.ncx', 'media-type'=>'application/x-dtbncx+xml'
    }
    
    xm.spine('toc'=>'ncx') {
      book.chapters.each{|chapter| xm.itemref 'idref'=>chapter.epub_id}
    }
    
    # xm.guide {
    #   xm.reference 'type'='loi', 'title'=>'List of Illustrations', 'href'=>'appendix.html#figures'
    # }
  }
end

#stylesheetObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/bindery/formats/epub.rb', line 225

def stylesheet
  # This is a start, but needs work.
  %q{|@page {
     |  margin-top: 0.8em;
     |  margin-bottom: 0.8em;}
     |
     |body {
     |  margin-left: 1em;
     |  margin-right: 1em;
     |  padding: 0;}
     |
     |h2 {
     |  padding-top:0;
     |  display:block;}
     |
     |p {
     |  margin-top: 0.3em;
     |  margin-bottom: 0.3em;
     |  text-indent: 1.0em;
     |  text-align: justify;}
     |
     |body > p:first-child {text-indent: 0}
     |div.text p:first-child {text-indent: 0}
     |
     |blockquote p, li p {
     |  text-indent: 0.0em;
     |  text-align: left;}
     |
     |div.chapter {padding-top: 3.0em;}
     |div.part {padding-top: 3.0em;}
     |h3.section_title {text-align: center;}
     |}.strip_margin
end

#write_chapter(chapter, zipfile) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/bindery/formats/epub.rb', line 153

def write_chapter(chapter, zipfile)
  save_options = Nokogiri::XML::Node::SaveOptions
  File.open(chapter.file, 'r:UTF-8') do |ch_in|
    doc = Nokogiri.HTML(ch_in.read)
    include_images(doc, zipfile) if chapter.include_images?
    zipfile.get_output_stream(chapter.epub_output_file) do |ch_out|
      if chapter.body_only?
        # FIXME: must HTML-escape the chapter title
        ch_out.write %{|<?xml version="1.0" encoding="UTF-8" ?>
                       |<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
                       |<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
                       |<head>
                       |  <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
                       |  <title>#{chapter.title}</title>
                       |  <link rel="stylesheet" href="css/book.css" type="text/css" />
                       |</head>
                       |}.strip_margin
        ch_out.write doc.at('body').serialize(:save_with => (save_options::AS_XHTML | save_options::NO_DECLARATION))
        ch_out.write %{|</html>
                       |}.strip_margin
      else
        ch_out.write doc.serialize(:save_with => save_options::AS_XHTML)
      end
    end
  end
end

#zip_dir_exists?(zipfile, dirname) ⇒ Boolean

Returns:

  • (Boolean)


268
269
270
271
# File 'lib/bindery/formats/epub.rb', line 268

def zip_dir_exists?(zipfile, dirname)
  dirname = "#{dirname}/" unless dirname =~ %r{/$}
  zipfile.entries.any?{|e| e.directory? && e.name == dirname}
end

#zip_file_exists?(zipfile, filename) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/bindery/formats/epub.rb', line 273

def zip_file_exists?(zipfile, filename)
  zipfile.entries.any?{|e| e.name == filename}
end