Class: GEPUB::Book
- Inherits:
-
Object
- Object
- GEPUB::Book
- Includes:
- InspectMixin
- Defined in:
- lib/gepub/book.rb,
lib/gepub/book_add_item.rb
Overview
Book is the class to hold data in EPUB files.
It can generate and parse EPUB2/EPUB3 files.
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#title(content, id = nil, title_type = nil) (delegated to Metadata#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:
-
title-type is ‘main’
-
display-seq is smallest
-
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:
-
display-seq is smallest
-
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:
-
display-seq is smallest
-
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#lastmodified(date) (delegated to Metadata#lastmodified)
set last modified date. date is a Time, DateTime or string that can be parsed by DateTime#parse.
Book#modified_now (delegated to Metadata#modified_now)
set last modified date to 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), publisher= (clears and set publisher), and publisher_list(returns publisher Meta object in display-seq order).
Book#page_progression_direction= (delegated to Spine#page_progression_direction=)
set page-proression-direction attribute to spine.
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
-
.parse(path_or_io) ⇒ Object
Parses existing EPUB2/EPUB3 files from an IO object or a file path and creates new Book object.
- .rootfile_from_container(rootfile) ⇒ Object
Instance Method Summary collapse
-
#add_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil, id: nil, media_type: nil, fallback: nil, properties: nil, media_overlay: nil, toc_text: nil, property: nil, attributes: {}) ⇒ Object
add an item(i.e. html, images, audios, etc) to Book.
-
#add_optional_file(path, io_or_filename) ⇒ Object
Add an optional file to the container.
-
#add_ordered_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil, id: nil, media_type: nil, fallback: nil, properties: nil, media_overlay: nil, toc_text: nil, property: nil, attributes: {}) ⇒ Object
same as add_item, but the item will be added to spine of the EPUB.
-
#add_tocdata(toc_yaml) ⇒ Object
add tocdata like this : [ chapter1.xhtml, text: ‘Chapter 1’, level: 1 ] .
-
#cleanup ⇒ Object
cleanup and maintain consistency of metadata and items included in the Book object.
- #container_xml ⇒ Object
-
#generate_epub(path_to_epub) ⇒ Object
writes EPUB to file.
-
#generate_epub_stream ⇒ Object
generates and returns StringIO contains EPUB.
- #generate_nav_doc(title = 'Table of Contents') ⇒ Object
-
#get_handler_of(media_type) ⇒ Object
get handler item which defined in bindings for media type,.
-
#initialize(path = 'OEBPS/package.opf', attributes = {}, &block) ⇒ Book
constructor
creates new empty Book object.
- #nav_doc(title = 'Table of Contents') ⇒ Object
- #ncx_xml ⇒ Object
-
#optional_files ⇒ Object
Get optional(not required in EPUB specification) files in the container.
-
#ordered(&block) ⇒ Object
should call ordered() with block.
- #set_singleton_methods_to_item(item) ⇒ Object
- #write_landmarks(xml_doc, landmarks) ⇒ Object
-
#write_to_epub_container(epub) ⇒ Object
write EPUB to stream specified by the argument.
-
#write_toc(xml_doc, tocs) ⇒ Object
write toc.
Methods included from InspectMixin
Constructor Details
#initialize(path = 'OEBPS/package.opf', attributes = {}, &block) ⇒ Book
creates new empty Book object. usually you do not need to specify any arguments.
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/gepub/book.rb', line 118 def initialize(path='OEBPS/package.opf', attributes = {}, &block) 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 = [] @landmarks = [] if block block.arity < 1 ? instance_eval(&block) : block[self] end end |
Class Method Details
.parse(path_or_io) ⇒ Object
Parses existing EPUB2/EPUB3 files from an IO object or a file path and creates new Book object.
book = self.parse(File.new('some.epub'))
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/gepub/book.rb', line 100 def self.parse(path_or_io) files = {} package = nil package_path = nil book = nil Zip::File.open(path_or_io) do |zip_file| package, package_path = parse_container(zip_file, files) check_consistency_of_package(package, package_path) parse_files_into_package(files, package) book = Book.new(package.path) book.instance_eval { @package = package; @optional_files = files } end book end |
.rootfile_from_container(rootfile) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/gepub/book.rb', line 90 def self.rootfile_from_container(rootfile) doc = Nokogiri::XML::Document.parse(rootfile) ns = doc.root.namespaces defaultns = ns.select{ |_name, value| value == CONTAINER_NS }.to_a[0][0] doc.css("#{defaultns}|rootfiles > #{defaultns}|rootfile")[0]['full-path'] end |
Instance Method Details
#add_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil, id: nil, media_type: nil, fallback: nil, properties: nil, media_overlay: nil, toc_text: nil, property: 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.
7 8 9 10 11 12 |
# File 'lib/gepub/book_add_item.rb', line 7 def add_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil, id: nil,media_type: nil,fallback: nil,properties: nil,media_overlay: nil,toc_text: nil,property: nil, attributes: {}) content, id, attributes = handle_deprecated_add_item_arguments(deprecated_content, deprecated_id, deprecated_attributes, content, id, attributes) add_item_internal(href, content: content, item_attributes: { id: id,media_type: media_type,fallback: fallback,properties: properties,media_overlay: ,toc_text: toc_text,property: property }, attributes: attributes, ordered: false) end |
#add_optional_file(path, io_or_filename) ⇒ Object
Add an optional file to the container
137 138 139 140 141 142 143 144 |
# File 'lib/gepub/book.rb', line 137 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, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil, id: nil, media_type: nil, fallback: nil, properties: nil, media_overlay: nil, toc_text: nil, property: nil, attributes: {}) ⇒ Object
same as add_item, but the item will be added to spine of the EPUB.
15 16 17 18 19 20 |
# File 'lib/gepub/book_add_item.rb', line 15 def add_ordered_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content:nil, id: nil,media_type: nil,fallback: nil,properties: nil,media_overlay: nil,toc_text: nil,property: nil, attributes: {}) content, id, attributes = handle_deprecated_add_item_arguments(deprecated_content, deprecated_id, deprecated_attributes, content, id, attributes) add_item_internal(href, content: content, item_attributes: { id: id,media_type: media_type,fallback: fallback,properties: properties,media_overlay: ,toc_text: toc_text,property: property }, attributes: attributes, ordered: true) end |
#add_tocdata(toc_yaml) ⇒ Object
add tocdata like this : [ chapter1.xhtml, text: ‘Chapter 1’, level: 1 ] . if item corresponding to the link does not exists, error will be thrown.
254 255 256 257 258 259 260 261 262 263 |
# File 'lib/gepub/book.rb', line 254 def add_tocdata(toc_yaml) newtoc = [] toc_yaml.each do |toc_entry| href, id = toc_entry[:link].split('#') item = @package.manifest.item_by_href(href) throw "#{href} does not exist." if item.nil? newtoc.push({item: item, id: id, text: toc_entry[:text], level: toc_entry[:level] }) end @toc = @toc + newtoc end |
#cleanup ⇒ Object
cleanup and maintain consistency of metadata and items included in the Book object.
181 182 183 184 |
# File 'lib/gepub/book.rb', line 181 def cleanup cleanup_for_epub2 cleanup_for_epub3 end |
#container_xml ⇒ Object
240 241 242 243 244 245 246 247 248 249 |
# File 'lib/gepub/book.rb', line 240 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.
231 232 233 234 235 236 237 238 |
# File 'lib/gepub/book.rb', line 231 def generate_epub(path_to_epub) cleanup File.delete(path_to_epub) if File.exist?(path_to_epub) Zip::OutputStream::open(path_to_epub) { |epub| write_to_epub_container(epub) } end |
#generate_epub_stream ⇒ Object
generates and returns StringIO contains EPUB.
222 223 224 225 226 227 228 |
# File 'lib/gepub/book.rb', line 222 def generate_epub_stream cleanup Zip::OutputStream::write_buffer(StringIO.new) do |epub| write_to_epub_container(epub) end end |
#generate_nav_doc(title = 'Table of Contents') ⇒ Object
265 266 267 |
# File 'lib/gepub/book.rb', line 265 def generate_nav_doc(title = 'Table of Contents') add_item('nav.xhtml', id: 'nav', content: StringIO.new(nav_doc(title))).add_property('nav') end |
#get_handler_of(media_type) ⇒ Object
get handler item which defined in bindings for media type,
165 166 167 |
# File 'lib/gepub/book.rb', line 165 def get_handler_of(media_type) items[@package.bindings.handler_by_media_type[media_type]] end |
#nav_doc(title = 'Table of Contents') ⇒ Object
269 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/gepub/book.rb', line 269 def nav_doc(title = 'Table of Contents') # handle cascaded toc start_level = @toc && !@toc.empty? && @toc[0][:level] || 1 stacked_toc = {level: start_level, tocs: [] } @toc.inject(stacked_toc) do |current_stack, toc_entry| toc_entry_level = toc_entry[:level] || 1 if current_stack[:level] < toc_entry_level new_stack = { level: toc_entry_level, tocs: [], parent: current_stack} current_stack[:tocs].last[:child_stack] = new_stack current_stack = new_stack else while current_stack[:level] > toc_entry_level and !current_stack[:parent].nil? current_stack = current_stack[:parent] end end current_stack[:tocs].push toc_entry current_stack end # write toc def write_toc xml_doc, tocs return if tocs.empty? xml_doc.ol { tocs.each { |x| id = x[:id].nil? ? "" : "##{x[:id]}" toc_text = x[:text] toc_text = x[:item].href if toc_text.nil? or toc_text == '' xml_doc.li { xml_doc.a({'href' => x[:item].href + id} ,toc_text) if x[:child_stack] && x[:child_stack][:tocs].size > 0 write_toc(xml_doc, x[:child_stack][:tocs]) end } } } end def write_landmarks xml_doc, landmarks xml_doc.ol { landmarks.each { |landmark| id = landmark[:id].nil? ? "" : "##{x[:id]}" landmark_title = landmark[:title] xml_doc.li { xml_doc.a({'href' => landmark[:item].href + id, 'epub:type' => landmark[:type]}, landmark_title) } } } end # build nav builder = Nokogiri::XML::Builder.new { |doc| unless version.to_f < 3.0 doc.doc.create_internal_subset('html', nil, nil ) end doc.html('xmlns' => "http://www.w3.org/1999/xhtml",'xmlns:epub' => "http://www.idpf.org/2007/ops") { doc.head { doc.title title } doc.body { if !stacked_toc.empty? doc.nav('epub:type' => 'toc', 'id' => 'toc') { doc.h1 "#{title}" write_toc(doc, stacked_toc[:tocs]) } end if !@landmarks.empty? doc.nav('epub:type' => 'landmarks', 'id' => 'landmarks') { write_landmarks(doc, @landmarks) } end } } } builder.to_xml(:encoding => 'utf-8') end |
#ncx_xml ⇒ Object
346 347 348 349 350 351 352 353 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 |
# File 'lib/gepub/book.rb', line 346 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.('name' => 'dtb:uid', 'content' => "#{self.identifier}") xml.('name' => 'dtb:depth', 'content' => '1') xml.('name' => 'dtb:totalPageCount','content' => '0') xml.('name' => 'dtb:maxPageNumber', 'content' => '0') } xml.docTitle { xml.text_ "#{@package..title}" } count = 1 xml.navMap { @toc.each { |x| xml.navPoint('id' => "#{x[:item].itemid}_#{x[:id]}", '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_files ⇒ Object
Get optional(not required in EPUB specification) files in the container.
132 133 134 |
# File 'lib/gepub/book.rb', line 132 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.
175 176 177 |
# File 'lib/gepub/book.rb', line 175 def ordered(&block) @package.ordered(&block) end |
#set_singleton_methods_to_item(item) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/gepub/book.rb', line 146 def set_singleton_methods_to_item(item) toc = @toc = (class << item;self;end) .send(:define_method, :toc, Proc.new { toc }) landmarks = @landmarks .send(:define_method, :landmarks, Proc.new { landmarks }) bindings = @package.bindings .send(:define_method, :bindings, Proc.new { bindings }) end |
#write_landmarks(xml_doc, landmarks) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/gepub/book.rb', line 306 def write_landmarks xml_doc, landmarks xml_doc.ol { landmarks.each { |landmark| id = landmark[:id].nil? ? "" : "##{x[:id]}" landmark_title = landmark[:title] xml_doc.li { xml_doc.a({'href' => landmark[:item].href + id, 'epub:type' => landmark[:type]}, landmark_title) } } } end |
#write_to_epub_container(epub) ⇒ Object
write EPUB to stream specified by the argument.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/gepub/book.rb', line 187 def write_to_epub_container(epub) mod_time = Zip::DOSTime.now unless (last_mod = lastmodified).nil? tm = last_mod.content mod_time = Zip::DOSTime.local(tm.year, tm.month, tm.day, tm.hour, tm.min, tm.sec) end mimetype_entry = Zip::Entry.new(nil, 'mimetype', nil, nil, nil, nil, nil, nil, mod_time) epub.put_next_entry(mimetype_entry, nil, nil, Zip::Entry::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| zip_entry = Zip::Entry.new(nil, k, nil, nil, nil, nil, nil, nil, mod_time) epub.put_next_entry(zip_entry) epub << v.force_encoding('us-ascii') } end |
#write_toc(xml_doc, tocs) ⇒ Object
write toc
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/gepub/book.rb', line 289 def write_toc xml_doc, tocs return if tocs.empty? xml_doc.ol { tocs.each { |x| id = x[:id].nil? ? "" : "##{x[:id]}" toc_text = x[:text] toc_text = x[:item].href if toc_text.nil? or toc_text == '' xml_doc.li { xml_doc.a({'href' => x[:item].href + id} ,toc_text) if x[:child_stack] && x[:child_stack][:tocs].size > 0 write_toc(xml_doc, x[:child_stack][:tocs]) end } } } end |