class GEPUB::Metadata
Holds data in /package/metadata
Constants
- CONTENT_NODE_LIST
Attributes
opf_version[RW]
Public Class Methods
new(opf_version = '3.0',id_pool = Package::IDPool.new) { |self| ... }
click to toggle source
# File lib/gepub/metadata.rb, line 61 def initialize(opf_version = '3.0',id_pool = Package::IDPool.new) @id_pool = id_pool @metalist = {} @content_nodes = {} @oldstyle_meta = [] @opf_version = opf_version @namespaces = { 'xmlns:dc' => DC_NS } @namespaces['xmlns:opf'] = OPF_NS if @opf_version.to_f < 3.0 @default_layout = 'reflowable' @default_orientation = 'auto' @default_spread = 'auto' @layout = NilContent @orientation = NilContent @spread = NilContent @ibooks_version = NilContent @ibooks_scroll_axis = NilContent @lastmodified_updated = false yield self if block_given? end
parse(metadata_xml, opf_version = '3.0', id_pool = Package::IDPool.new)
click to toggle source
parse metadata element. metadata_xml should be Nokogiri::XML::Node object.
# File lib/gepub/metadata.rb, line 23 def self.parse(metadata_xml, opf_version = '3.0', id_pool = Package::IDPool.new) Metadata.new(opf_version, id_pool) { |metadata| metadata.instance_eval { @xml = metadata_xml @namespaces = @xml.namespaces CONTENT_NODE_LIST.each { |node| @content_nodes[node] = parse_node(DC_NS, node).sort_as_meta } @xml.xpath("#{ns_prefix(OPF_NS)}:meta[not(@refines) and @property]", @namespaces).each { |node| (@content_nodes['meta'] ||= []) << create_meta(node) } @oldstyle_meta = parse_opf2_meta meta_list.each { |metanode| case metanode['property'] when 'rendition:layout' @layout = metanode when 'rendition:orientation' @orientation = metanode when 'rendition:spread' @spread = metanode when 'ibooks:version' @ibooks_version = metanode when 'ibooks:scroll-axis' @ibooks_scroll_axis = metanode end } } # do not set @lastmodified_updated here } end
Public Instance Methods
add_contributor(content, deprecated_id = nil, deprecated_role = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) { |meta| ... }
click to toggle source
# File lib/gepub/metadata_add.rb, line 106 def add_contributor(content, deprecated_id = nil, deprecated_role = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) if deprecated_id warn 'second argument for add_contributor is deprecated. use id: instead' id = deprecated_id end if deprecated_role warn 'third argument for add_contributor is deprecated. use role: instead' role = deprecated_role end meta = add_person('contributor', content, id: id, title_type: title_type,identifier_type: identifier_type,display_seq: display_seq,file_as: file_as,group_position: group_position,role: role, lang: lang, alternates: alternates) yield meta if block_given? meta end
add_creator(content, deprecated_id = nil, deprecated_role = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) { |meta| ... }
click to toggle source
# File lib/gepub/metadata_add.rb, line 87 def add_creator(content, deprecated_id = nil, deprecated_role = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) if deprecated_id warn 'second argument for add_creator is deprecated. use id: instead' id = deprecated_id end if deprecated_role warn 'third argument for add_creator is deprecated. use role: instead' role = deprecated_role end role = 'aut' if role.nil? meta = add_person('creator', content, id: id, title_type: title_type,identifier_type: identifier_type,display_seq: display_seq,file_as: file_as,group_position: group_position,role: role, lang: lang, alternates: alternates) yield meta if block_given? meta end
add_date(date, deprecated_id = nil, id: nil)
click to toggle source
# File lib/gepub/metadata.rb, line 165 def add_date(date, deprecated_id = nil, id: nil) if deprecated_id warn "secound argument is deprecated. use id: keyword argument" id = deprecated_id end add_metadata('date', date, id: id, itemclass: DateMeta) end
add_identifier(string, id=nil, type=nil)
click to toggle source
# File lib/gepub/metadata.rb, line 157 def add_identifier(string, id=nil, type=nil) id = @id_pool.generate_key(:prefix => 'BookId') if id.nil? raise "id #{id} is already in use" if @id_pool[id] identifier = add_metadata('identifier', string, id: id) identifier.refine('identifier-type', type) unless type.nil? identifier end
add_metadata(name, content, id: nil, itemclass: Meta, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {} ) { |meta| ... }
click to toggle source
# File lib/gepub/metadata_add.rb, line 124 def add_metadata(name, content, id: nil, itemclass: Meta, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {} ) meta = add_metadata_internal(name, content, id: id, itemclass: itemclass) [{ value: title_type, name: 'title-type'},{ value: identifier_type, name: 'identifier-type'},{ value: display_seq, name: 'display-seq'},{ value: file_as, name: 'file-as'},{ value: group_position, name: 'group-position'},{ value: role, name: 'role'}].each do |refiner| if refiner[:value] meta.refine(refiner[:name], refiner[:value]) end end if lang meta.lang = lang end if alternates meta.add_alternates alternates end yield meta if block_given? meta end
add_metadata_internal(name, content, id: nil, itemclass: Meta)
click to toggle source
# File lib/gepub/metadata.rb, line 181 def add_metadata_internal(name, content, id: nil, itemclass: Meta) meta = itemclass.new(name, content, self, { 'id' => id }) (@content_nodes[name] ||= []) << meta meta end
add_oldstyle_meta(content, attributes = {})
click to toggle source
# File lib/gepub/metadata.rb, line 221 def add_oldstyle_meta(content, attributes = {}) meta = Meta.new('meta', content, self, attributes) (@oldstyle_meta ||= []) << meta meta end
add_person(name, content, deprecated_id = nil, deprecated_role = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) { |meta| ... }
click to toggle source
# File lib/gepub/metadata_add.rb, line 69 def add_person(name, content, deprecated_id = nil, deprecated_role = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) if deprecated_id warn 'second argument for add_person is deprecated. use id: instead' id = deprecated_id end if deprecated_role warn 'third argument for add_person is deprecated. use role: instead' role = deprecated_role end meta = add_metadata(name, content, id: id, title_type: title_type,identifier_type: identifier_type,display_seq: display_seq,file_as: file_as,group_position: group_position,role: role, lang: lang, alternates: alternates) yield meta if block_given? meta end
add_title(content, deprecated_id = nil, deprecated_title_type = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) { |meta| ... }
click to toggle source
# File lib/gepub/metadata_add.rb, line 51 def add_title(content, deprecated_id = nil, deprecated_title_type = nil, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) if deprecated_id warn 'second argument for add_title is deprecated. use id: instead' id = deprecated_id end if deprecated_title_type warn 'third argument for add_title is deprecated. use title_type: instead' title_type = deprecated_title_type end meta = add_metadata('title', content, id: id, title_type: title_type,identifier_type: identifier_type,display_seq: display_seq,file_as: file_as,group_position: group_position,role: role, lang: lang, alternates: alternates) yield meta if block_given? meta end
get_first_node(node)
click to toggle source
# File lib/gepub/metadata.rb, line 151 def get_first_node(node) if !@content_nodes[node].nil? && @content_nodes[node].size > 0 @content_nodes[node].sort_as_meta[0] end end
ibooks_scroll_axis()
click to toggle source
# File lib/gepub/metadata.rb, line 279 def ibooks_scroll_axis @ibooks_scroll_axis.content || '' end
ibooks_scroll_axis=(val)
click to toggle source
# File lib/gepub/metadata.rb, line 283 def ibooks_scroll_axis=(val) if ![:vertical, :horizontal, :default].member? val.to_sym raise 'ibooks_scroll_axis should be one of vertical, horizontal or default' end @ibooks_scroll_axis = Meta.new('meta', val, self, { 'property' => 'ibooks:scroll-axis' }) (@content_nodes['meta'] ||= []) << @ibooks_scroll_axis end
ibooks_version()
click to toggle source
# File lib/gepub/metadata.rb, line 270 def ibooks_version @ibooks_version.content || '' end
ibooks_version=(val)
click to toggle source
# File lib/gepub/metadata.rb, line 274 def ibooks_version=(val) @ibooks_version = Meta.new('meta', val, self, { 'property' => 'ibooks:version' }) (@content_nodes['meta'] ||= []) << @ibooks_version end
ibooks_vocaburaly_specified?()
click to toggle source
# File lib/gepub/metadata.rb, line 295 def ibooks_vocaburaly_specified? @ibooks_version.content || @ibooks_scroll_axis.content end
identifier_by_id(id)
click to toggle source
# File lib/gepub/metadata.rb, line 173 def identifier_by_id(id) (@content_nodes['identifier'] || []).each { |x| return x.content if x['id'] == id } return nil end
lastmodified(date=UNASSIGNED)
click to toggle source
# File lib/gepub/metadata.rb, line 187 def lastmodified(date=UNASSIGNED) if unassigned?(date) ret = (@content_nodes['meta'] ||=[]).select { |meta| meta['property'] == 'dcterms:modified' } ret.size == 0 ? nil : ret[0] else @lastmodified_updated = true date ||= Time.now date = DateTime.parse(date) if date.is_a? String (@content_nodes['meta'] ||= []).each { |meta| if (meta['property'] == 'dcterms:modified') @content_nodes['meta'].delete meta end } add_metadata('meta', date.to_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), itemclass: DateMeta)['property'] = 'dcterms:modified' end end
lastmodified=(date)
click to toggle source
# File lib/gepub/metadata.rb, line 212 def lastmodified=(date) lastmodified(date) end
lastmodified_updated?()
click to toggle source
# File lib/gepub/metadata.rb, line 81 def lastmodified_updated? @lastmodified_updated end
main_title()
click to toggle source
# File lib/gepub/metadata.rb, line 102 def main_title # should make it obsolete? title.to_s end
meta_clear()
click to toggle source
# File lib/gepub/metadata.rb, line 122 def meta_clear if !@content_nodes['meta'].nil? @content_nodes['meta'].each { |x| unregister_meta(x) }; @content_nodes['meta'] = [] end end
meta_list()
click to toggle source
# File lib/gepub/metadata.rb, line 118 def meta_list (@content_nodes['meta'] || []).sort_as_meta.dup end
modified_now()
click to toggle source
# File lib/gepub/metadata.rb, line 208 def modified_now lastmodified Time.now end
oldstyle_meta()
click to toggle source
# File lib/gepub/metadata.rb, line 106 def oldstyle_meta @oldstyle_meta.dup end
oldstyle_meta_clear()
click to toggle source
# File lib/gepub/metadata.rb, line 110 def oldstyle_meta_clear @oldstyle_meta.each { |meta| unregister_meta(meta) } @oldstyle_meta = [] end
register_meta(meta)
click to toggle source
# File lib/gepub/metadata.rb, line 228 def register_meta(meta) if !meta['id'].nil? raise "id '#{meta['id']}' is already in use." if @id_pool[meta['id']] @metalist[meta['id']] = meta @id_pool[meta['id']] = true end end
rendition_layout()
click to toggle source
# File lib/gepub/metadata.rb, line 243 def rendition_layout @layout.content || @default_layout end
rendition_layout=(val)
click to toggle source
# File lib/gepub/metadata.rb, line 247 def rendition_layout=(val) @layout = Meta.new('meta', val, self, { 'property' => 'rendition:layout' }) (@content_nodes['meta'] ||= []) << @layout end
rendition_orientation()
click to toggle source
# File lib/gepub/metadata.rb, line 252 def rendition_orientation @orientation.content || @default_orientation end
rendition_orientation=(val)
click to toggle source
# File lib/gepub/metadata.rb, line 256 def rendition_orientation=(val) @orientation = Meta.new('meta', val, self, { 'property' => 'rendition:orientation' }) (@content_nodes['meta'] ||= []) << @orientation end
rendition_specified?()
click to toggle source
# File lib/gepub/metadata.rb, line 291 def rendition_specified? @layout.content || @orientation.content || @spread.content end
rendition_spread()
click to toggle source
# File lib/gepub/metadata.rb, line 261 def rendition_spread @spread.content || @default_spread end
rendition_spread=(val)
click to toggle source
# File lib/gepub/metadata.rb, line 265 def rendition_spread=(val) @spread = Meta.new('meta', val, self, { 'property' => 'rendition:spread' }) (@content_nodes['meta'] ||= []) << @spread end
set_lastmodified(date=nil)
click to toggle source
# File lib/gepub/metadata.rb, line 216 def set_lastmodified(date=nil) warn "obsolete : set_lastmodified. use lastmodified instead." lastmodified(date) end
title(content=UNASSIGNED, id: nil, title_type: nil) { |meta| ... }
click to toggle source
# File lib/gepub/metadata.rb, line 133 def title(content=UNASSIGNED, id: nil, title_type: nil) if unassigned?(content) if !@content_nodes['title'].nil? @content_nodes['title'].each do |titlenode| return titlenode if titlenode.title_type.to_s == TITLE_TYPE::MAIN end end get_first_node('title') else title_clear meta = add_title(content, id: id, title_type: title_type) yield meta if block_given? meta end end
title=(content)
click to toggle source
# File lib/gepub/metadata.rb, line 129 def title=(content) title(content) end
to_xml(builder)
click to toggle source
# File lib/gepub/metadata.rb, line 85 def to_xml(builder) builder.metadata(@namespaces) { @content_nodes.each { |_name, list| list.each { |meta| meta.to_xml(builder, @id_pool, ns_prefix(DC_NS), nil, @opf_version) } } @oldstyle_meta.each { |node| node.to_xml(builder, @id_pool, nil) } } @xml end
unregister_meta(meta)
click to toggle source
# File lib/gepub/metadata.rb, line 236 def unregister_meta(meta) if meta['id'].nil? @metalist[meta['id']] = nil @id_pool[meta['id']] = nil end end
Private Instance Methods
collect_refiners(id)
click to toggle source
# File lib/gepub/metadata.rb, line 311 def collect_refiners(id) r = {} if !id.nil? @xml.xpath("//#{ns_prefix(OPF_NS)}:meta[@refines='##{id}']", @namespaces).each { |node| (r[node['property']] ||= []) << create_meta(node) } end r end
create_meta(node)
click to toggle source
# File lib/gepub/metadata.rb, line 307 def create_meta(node) Meta.new(node.name, node.content, self, attr_to_hash(node.attributes), collect_refiners(node['id'])) end
parse_node(ns, node)
click to toggle source
# File lib/gepub/metadata.rb, line 300 def parse_node(ns, node) @xml.xpath("#{ns_prefix(ns)}:#{node}", @namespaces).map { |n| create_meta(n) } end
parse_opf2_meta()
click to toggle source
# File lib/gepub/metadata.rb, line 322 def parse_opf2_meta @xml.xpath("#{ns_prefix(OPF_NS)}:meta[not(@refines) and not(@property)]", @namespaces).map { |node| create_meta(node) } end