Class: GEPUB::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/gepub/book.rb

Overview

Book is the class to hold data in EPUB files.

It can generate and parse EPUB2/EPUB3 files.

If you want to generate a new EPUB file, consider using GEPUB::Builder instead of using Book directly. Builder is a wrapper class of Book specialized for generating EPUB.

Book delegates many methods to objects in other class, so you can’t find them in Book#methods or in ri/rdoc documentation. Their descriptions are below.

Package Attributes

Book#version (delegated to Package#version)

returns OPF version.

Book#version=, Book#set_version (delegated to Package#version=)

set OPF version

Book#unique_identifier (delegated to Package#unique_identifier)

return unique_identifier ID value. identifier itself can be get by Book#identifier

Metadata

Metadata items(e.g. title, creator, publisher, etc) are GEPUB::Meta objects.

Book#identifier (delegated to Package#identifier)

return GEPUB::Meta object of unique identifier.

Book#identifier=(identifier) (delegated to Package#identifier=)

set identifier (i.e. url, uuid, ISBN) as unique-identifier of EPUB.

Book#set_main_id(identifier, id = nil, type = nil) (delegated to Package#set_main_id)

same as identifier=, but can specify id (in the opf xml) and identifier type(i.e. URL, uuid, ISBN, etc)

Book#add_identifier(string, id, type=nil) (delegated to Metadata#add_identifier)

Set an identifier metadata. It it not unique-identifier in opf. Many EPUB files do not set identifier other than unique-identifier.

Book#add_title(content, id = nil, title_type = nil) (delegated to Metadata#add_title)

add title metadata. title_type candidates is defined in TITLE_TYPES.

Book#set_title(content, id = nil, title_type = nil) (delegated to Metadata#set_title)

clear all titles and then add title.

Book#title (delegated to Metadata)

returns ‘main’ title Meta object. ‘main’ title is determined by this order:

  1. title-type is ‘main’

  2. display-seq is smallest

  3. appears first in opf file

Book#title_list (delegated to Metadata)

returns titles list by display-seq or defined order. the title without display-seq is appear after titles with display-seq.

Book#add_creator(content, id = nil, role = ‘aut’) (delegated to Metadata#add_creator)

add creator.

Book#creator

returns ‘main’ creator Meta object. ‘main’ creator is determined as following:

  1. display-seq is smallest

  2. appears first in opf file

Book#creator_list (delegated to Metadata)

returns creators list by display-seq or defined order. the creators without display-seq is appear after creators with display-seq.

Book#add_contributor(content, id = nil, role = ‘aut’) (delegated to Metadata#add_contributor)

add contributor.

Book#contributor(content, id = nil, role = ‘aut’) (delegated to Metadata#contributor)

returns ‘main’ contributor. ‘main’ contributor determined as following:

  1. display-seq is smallest

  2. appears first in opf file

Book#contributors_list (delegated to Metadata)

returns contributors list by display-seq or defined order. the contributors without display-seq is appear after contributors with display-seq.

Book#set_lastmodified(date=nil) (delegated to Metadata#set_lastmodified)

set last modified date.if date is nil, it sets current time.

Book#lastmodified (delegated to Metadata#lastmodified)

returns Meta object contains last modified time.

setting and reading other metadata: publisher, language, coverage, date, description, format, relation, rights, source, subject, type (delegated to Metadata)

they all have methods like: publisher(which returns ‘main’ publisher), add_publisher(content, id) (which add publisher), set_publisher or publisher= (clears and set publisher), and publisher_list(returns publisher Meta object in display-seq order).

Constant Summary collapse

MIMETYPE =
'mimetype'
MIMETYPE_CONTENTS =
'application/epub+zip'
CONTAINER =
'META-INF/container.xml'
ROOTFILE_PATTERN =
/^.+\.opf$/
CONTAINER_NS =
'urn:oasis:names:tc:opendocument:xmlns:container'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = 'OEBPS/package.opf', attributes = {}) {|book| ... } ⇒ Book

creates new empty Book object. usually you do not need to specify any arguments.

Yields:

  • (book)


148
149
150
151
152
153
154
155
# File 'lib/gepub/book.rb', line 148

def initialize(path='OEBPS/package.opf', attributes = {})
  if File.extname(path) != '.opf'
    warn 'GEPUB::Book#new interface changed. You must supply path to package.opf as first argument. If you want to set title, please use GEPUB::Book#title='
  end
  @package = Package.new(path, attributes)
  @toc = []
  yield book if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



226
227
228
# File 'lib/gepub/book.rb', line 226

def method_missing(name,*args)
  @package.send(name, *args)
end

Class Method Details

.parse(io) ⇒ Object

Parses existing EPUB2/EPUB3 files from an IO object, and creates new Book object.

book = self.parse(File.new('some.epub'))


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
# File 'lib/gepub/book.rb', line 98

def self.parse(io)
  files = {}
  package = nil
  package_path = nil
  Zip::ZipInputStream::open_buffer(io) {
    |zis|
    while entry = zis.get_next_entry
      if !entry.directory?
        files[entry.name] = zis.read
        case entry.name
        when MIMETYPE then
          if files[MIMETYPE] != MIMETYPE_CONTENTS
            warn "#{MIMETYPE} is not valid: should be #{MIMETYPE_CONTENTS} but was #{files[MIMETYPE]}"
          end
          files.delete(MIMETYPE)
        when CONTAINER then
          package_path = rootfile_from_container(files[CONTAINER])
          files.delete(CONTAINER)
        when ROOTFILE_PATTERN then
          package = Package.parse_opf(files[entry.name], entry.name)
          files.delete(entry.name)
        end
      end
    end

    if package.nil?
      raise 'this container do not cotains publication information file'
    end
    
    if package_path != package.path
      warn 'inconsistend EPUB file: container says opf is #{package_path}, but actually #{package.path}'
    end
    
    files.each {
      |k, content|
      item = package.manifest.item_by_href(k.sub(/^#{package.contents_prefix}/,''))
      if !item.nil?
        files.delete(k)
        item.add_raw_content(content)
      end
    }
    book = Book.new(package.path)
    book.instance_eval { @package = package; @optional_files = files }
    book
  }
end

.rootfile_from_container(rootfile) ⇒ Object



88
89
90
91
92
93
# File 'lib/gepub/book.rb', line 88

def self.rootfile_from_container(rootfile)
  doc = Nokogiri::XML::Document.parse(rootfile)
  ns = doc.root.namespaces
  defaultns = ns.select{ |name, value| value == CONTAINER_NS }.keys[0]
  doc.css("#{defaultns}|rootfiles > #{defaultns}|rootfile")[0]['full-path']
end

Instance Method Details

#add_item(href, io_or_filename = nil, id = nil, attributes = {}) ⇒ Object

add an item(i.e. html, images, audios, etc) to Book. the added item will be referenced by the first argument in the EPUB container.



205
206
207
208
209
210
# File 'lib/gepub/book.rb', line 205

def add_item(href, io_or_filename = nil, id = nil, attributes = {})
  item = @package.add_item(href,nil,id,attributes)
  set_sigleton_methods_to_item(item)
  item.add_content io_or_filename unless io_or_filename.nil?
  item
end

#add_nav(item, text, id = nil) ⇒ Object

add navigation text (which will appear on navigation document or table of contents) to an item. DEPRECATED: please use Item#toc_text or Item#toc_text_with_id, or Builder#heading



176
177
178
179
# File 'lib/gepub/book.rb', line 176

def add_nav(item, text, id = nil)
  warn 'add_nav is deprecated: please use Item#toc_text'
  @toc.push({ :item => item, :text => text, :id => id})      
end

#add_optional_file(path, io_or_filename) ⇒ Object

Add an optional file to the container



164
165
166
167
168
169
170
171
# File 'lib/gepub/book.rb', line 164

def add_optional_file(path, io_or_filename)
  io = io_or_filename
  if io_or_filename.class == String
    io = File.new(io_or_filename)
  end
  io.binmode
  (@optional_files ||= {})[path] = io.read
end

#add_ordered_item(href, io_or_filename = nil, id = nil, attributes = {}) {|item| ... } ⇒ Object

same as add_item, but the item will be added to spine of the EPUB.

Yields:

  • (item)


213
214
215
216
217
218
# File 'lib/gepub/book.rb', line 213

def add_ordered_item(href, io_or_filename = nil, id = nil, attributes = {})
  item = @package.add_ordered_item(href,io_or_filename,id,attributes)
  set_sigleton_methods_to_item(item)
  yield item if block_given?
  item
end

#cleanupObject

clenup and maintain consistency of metadata and items included in the Book object.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/gepub/book.rb', line 238

def cleanup
  if version.to_f < 3.0 || @package.epub_backward_compat
    if @package.manifest.item_list.select {
      |x,item|
      item.media_type == 'application/x-dtbncx+xml'
    }.size == 0
      if (@toc.size == 0)
        @toc << { :item => @package.manifest.item_list[@package.spine.itemref_list[0].idref] }
      end
      add_item('toc.ncx', StringIO.new(ncx_xml), 'ncx')
    end
  end

  if version.to_f >=3.0
    @package..set_lastmodified
    
    if @package.manifest.item_list.select {
      |href, item|
      (item.properties||[]).member? 'nav'
      }.size == 0
      generate_nav_doc
    end
    
    @package.spine.remove_with_idlist @package.manifest.item_list.map {
      |href, item|
      item.fallback
    }.reject(&:nil?)

  end
end

#container_xmlObject



315
316
317
318
319
320
321
322
323
324
# File 'lib/gepub/book.rb', line 315

def container_xml
  <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
  <rootfiles>
<rootfile full-path="#{@package.path}" media-type="application/oebps-package+xml"/>
  </rootfiles>
</container>
EOF
end

#generate_epub(path_to_epub) ⇒ Object

writes EPUB to file. if file exists, it will be overwritten.



306
307
308
309
310
311
312
313
# File 'lib/gepub/book.rb', line 306

def generate_epub(path_to_epub)
  cleanup
  File.delete(path_to_epub) if File.exist?(path_to_epub)
  Zip::ZipOutputStream::open(path_to_epub) {
    |epub|
    write_to_epub_container(epub)
  }
end

#generate_epub_streamObject

generates and returns StringIO contains EPUB.



297
298
299
300
301
302
303
# File 'lib/gepub/book.rb', line 297

def generate_epub_stream
  cleanup
  Zip::ZipOutputStream::write_buffer {
    |epub|
    write_to_epub_container(epub)
  }
end

#generate_nav_doc(title = 'Table of Contents') ⇒ Object



326
327
328
# File 'lib/gepub/book.rb', line 326

def generate_nav_doc(title = 'Table of Contents')
  add_item('nav.html', StringIO.new(nav_doc(title)), 'nav').add_property('nav')
end

#get_handler_of(media_type) ⇒ Object

get handler item which defined in bindings for media type,



222
223
224
# File 'lib/gepub/book.rb', line 222

def get_handler_of(media_type)
  items[@package.bindings.handler_by_media_type[media_type]]
end


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/gepub/book.rb', line 330

def nav_doc(title = 'Table of Contents')
  builder = Nokogiri::XML::Builder.new {
    |doc|
    doc.html('xmlns' => "http://www.w3.org/1999/xhtml",'xmlns:epub' => "http://www.idpf.org/2007/ops") {
      doc.head { doc.text ' ' }
      doc.body {
        doc.nav('epub:type' => 'toc', 'id' => 'toc') {
          doc.h1 "#{title}"
          doc.ol {
            @toc.each {
              |x|
              id = x[:id].nil? ? "" : "##{x[:id]}"
              doc.li {
                doc.a({'href' => x[:item].href + id} ,x[:text])
              }
            }
          }
        }
      }
    }
  }
  builder.to_xml(:encoding => 'utf-8')
end

#ncx_xmlObject



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/gepub/book.rb', line 354

def ncx_xml
  builder = Nokogiri::XML::Builder.new {
    |xml|
    xml.ncx('xmlns' => 'http://www.daisy.org/z3986/2005/ncx/', 'version' => '2005-1') {
      xml.head {
        xml.meta('name' => 'dtb:uid', 'content' => "#{self.identifier}") 
        xml.meta('name' => 'dtb:depth', 'content' => '1')
        xml.meta('name' => 'dtb:totalPageCount','content' => '0')
        xml.meta('name' => 'dtb:maxPageNumber', 'content' => '0')
      }
      xml.docTitle {
        xml.text_ "#{@package..title}"
      }
      count = 1
      xml.navMap {
        @toc.each {
          |x|
          xml.navPoint('id' => "#{x[:item].itemid}", 'playOrder' => "#{count}") {
            xml.navLabel {
              xml.text_  "#{x[:text]}"
            }
            if x[:id].nil?
              xml.content('src' => "#{x[:item].href}")
            else
              xml.content('src' => "#{x[:item].href}##{x[:id]}")
            end
          }
          count += 1
        }
      }
    }
  }
  builder.to_xml(:encoding => 'utf-8')
end

#optional_filesObject

Get optional(not required in EPUB specification) files in the container.



159
160
161
# File 'lib/gepub/book.rb', line 159

def optional_files
  @optional_files || {}
end

#ordered(&block) ⇒ Object

should call ordered() with block. within the block, all item added by add_item will be added to spine also.



232
233
234
# File 'lib/gepub/book.rb', line 232

def ordered(&block)
  @package.ordered(&block)
end

#set_sigleton_methods_to_item(item) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/gepub/book.rb', line 181

def set_sigleton_methods_to_item(item)
  toc = @toc
  metaclass = (class << item;self;end)
  metaclass.send(:define_method, :toc_text,
                                Proc.new { |text|
                                  toc.push(:item => item, :text => text, :id => nil)
                                  item
                 })
  metaclass.send(:define_method, :toc_text_with_id,
                                Proc.new { |text, id|
                                  toc.push(:item => item, :text => text, :id => id)
                                  item
                 })
  bindings = @package.bindings
  metaclass.send(:define_method, :is_handler_of,
                 Proc.new { |media_type|
                   bindings.add(item.id, media_type)
                   item
                 })
                           
end

#write_to_epub_container(epub) ⇒ Object

write EPUB to stream specified by the argument.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/gepub/book.rb', line 270

def write_to_epub_container(epub)
  epub.put_next_entry('mimetype', '', '', Zip::ZipEntry::STORED)
  epub << "application/epub+zip"

  entries = {}
  optional_files.each {
    |k, content|
    entries[k] = content
  }

  entries['META-INF/container.xml'] = container_xml
  entries[@package.path] = opf_xml
  @package.manifest.item_list.each {
    |k, item|
    if item.content != nil
      entries[@package.contents_prefix + item.href] = item.content
    end
  }

  entries.sort_by { |k,v| k }.each {
    |k,v|
    epub.put_next_entry(k)
    epub << v.force_to_bin
  }
end