# File lib/gepub/package.rb, line 166 def primary_identifier(identifier, id = nil, type = nil) unique_identifier(id || @id_pool.generate_key(:prefix => 'BookId', :without_count => true)) @metadata.add_identifier identifier, unique_identifier, type end
class GEPUB::Package
Holds data in opf file.
Attributes
bindings[RW]
contents_prefix[RW]
epub_backward_compat[RW]
manifest[RW]
metadata[RW]
path[RW]
prefixes[RW]
spine[RW]
Public Class Methods
new(path='OEBPS/package.opf', attributes={}) { |self| ... }
click to toggle source
# File lib/gepub/package.rb, line 106 def initialize(path='OEBPS/package.opf', attributes={}) @path = path if File.extname(@path) != '.opf' if @path.size > 0 @path = [path,'package.opf'].join('/') end end @contents_prefix = File.dirname(@path).sub(/^\.$/,'') @contents_prefix = @contents_prefix + '/' if @contents_prefix.size > 0 @prefixes = {} @namespaces = {'xmlns' => OPF_NS } @attributes = attributes @attributes['version'] ||= '3.0' @id_pool = IDPool.new @metadata = Metadata.new(version) @manifest = Manifest.new(version) @spine = Spine.new(version) @bindings = Bindings.new @epub_backward_compat = true yield self if block_given? end
parse_opf(opf, path)
click to toggle source
parse OPF data. opf should be io or string object.
# File lib/gepub/package.rb, line 89 def self.parse_opf(opf, path) Package.new(path) { |package| package.instance_eval { @path = path @xml = Nokogiri::XML::Document.parse(opf) @namespaces = @xml.root.namespaces @attributes = attr_to_hash(@xml.root.attributes) @metadata = Metadata.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:metadata"), @attributes['version'], @id_pool) @manifest = Manifest.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:manifest"), @attributes['version'], @id_pool) @spine = Spine.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:spine"), @attributes['version'], @id_pool) @bindings = Bindings.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:bindings")) @prefixes = parse_prefixes(@attributes['prefix']) } } end
Public Instance Methods
[](x)
click to toggle source
# File lib/gepub/package.rb, line 145 def [](x) @attributes[x] end
[]=(k,v)
click to toggle source
# File lib/gepub/package.rb, line 149 def []=(k,v) @attributes[k] = v end
add_item(href, content:nil, id: nil, attributes: {}) { |item| ... }
click to toggle source
# File lib/gepub/package.rb, line 171 def add_item(href, content:nil, id: nil, attributes: {}) item = @manifest.add_item(id, href, nil, attributes) item.add_content(content) unless content.nil? @spine.push(item) if @ordered yield item if block_given? item end
add_ordered_item(href, content:nil, id: nil, attributes: {})
click to toggle source
# File lib/gepub/package.rb, line 186 def add_ordered_item(href, content:nil, id: nil, attributes: {}) raise 'do not call add_ordered_item within ordered block.' if @ordered item = add_item(href, attributes: attributes, id:id, content: content) @spine.push(item) item end
enable_ibooks_vocabulary()
click to toggle source
# File lib/gepub/package.rb, line 235 def enable_ibooks_vocabulary @prefixes['ibooks'] = 'http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/' end
enable_rendition()
click to toggle source
# File lib/gepub/package.rb, line 227 def enable_rendition @prefixes['rendition'] = 'http://www.idpf.org/vocab/rendition/#' end
ibooks_vocabulary_enabled?()
click to toggle source
# File lib/gepub/package.rb, line 239 def ibooks_vocabulary_enabled? @prefixes['ibooks'] == 'http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/' end
identifier(identifier=UNASSIGNED)
click to toggle source
# File lib/gepub/package.rb, line 154 def identifier(identifier=UNASSIGNED) if unassigned?(identifier) @metadata.identifier_by_id(unique_identifier) else self.identifier=(identifier) end end
identifier=(identifier)
click to toggle source
# File lib/gepub/package.rb, line 162 def identifier=(identifier) primary_identifier(identifier, nil, nil) end
items()
click to toggle source
# File lib/gepub/package.rb, line 200 def items @manifest.item_list end
opf_xml()
click to toggle source
# File lib/gepub/package.rb, line 243 def opf_xml if version.to_f < 3.0 || @epub_backward_compat spine.toc ||= 'ncx' if @metadata.oldstyle_meta.select { |meta| meta['name'] == 'cover' }.length == 0 @manifest.item_list.each { |_k, item| if item.properties && item.properties.member?('cover-image') @metadata.add_oldstyle_meta(nil, 'name' => 'cover', 'content' => item.id) end } end end if @metadata.rendition_specified? || @spine.rendition_specified? enable_rendition end if @metadata.ibooks_vocaburaly_specified? enable_ibooks_vocabulary end builder = Nokogiri::XML::Builder.new { |xml| if @prefixes.size == 0 @attributes.delete 'prefix' else @attributes['prefix'] = @prefixes.map { |k, v| "#{k}: #{v}" }.join(' ') end xml.package(@namespaces.merge(@attributes)) { @metadata.to_xml(xml) @manifest.to_xml(xml) @spine.to_xml(xml) @bindings.to_xml(xml) } } builder.to_xml(:encoding => 'utf-8') end
ordered() { || ... }
click to toggle source
# File lib/gepub/package.rb, line 179 def ordered raise 'need block.' if !block_given? @ordered = true yield @ordered = nil end
parse_prefixes(prefix)
click to toggle source
# File lib/gepub/package.rb, line 82 def parse_prefixes(prefix) return {} if prefix.nil? m = prefix.scan(/([\S]+): +(\S+)[\s]*/) Hash[*m.flatten] end
primary_identifier(identifier, id = nil, type = nil)
click to toggle source
rendition_enabled?()
click to toggle source
# File lib/gepub/package.rb, line 231 def rendition_enabled? @prefixes['rendition'] == 'http://www.idpf.org/vocab/rendition/#' end
set_version(val)
click to toggle source
# File lib/gepub/package.rb, line 215 def set_version(val) warn 'set_version is obsolete: use verion instead.' @attributes['version'] = val @metadata.opf_version = val @manifest.opf_version = val @spine.opf_version = val end
spine_items()
click to toggle source
# File lib/gepub/package.rb, line 193 def spine_items spine.itemref_list.map { |itemref| @manifest.item_list[itemref.idref] } end
version(val=UNASSIGNED)
click to toggle source
# File lib/gepub/package.rb, line 204 def version(val=UNASSIGNED) if unassigned?(val) @attributes['version'] else @attributes['version'] = val @metadata.opf_version = val @manifest.opf_version = val @spine.opf_version = val end end
version=(val)
click to toggle source
# File lib/gepub/package.rb, line 223 def version=(val) version(val) end