Class: GEPUB::Metadata
- Inherits:
-
Object
- Object
- GEPUB::Metadata
- Defined in:
- lib/gepub/metadata.rb,
lib/gepub/metadata_add.rb
Overview
Holds data in /package/metadata
Defined Under Namespace
Classes: NilContent
Constant Summary collapse
- CONTENT_NODE_LIST =
['identifier', 'title', 'language', 'contributor', 'creator', 'coverage', 'date','description','format','publisher','relation','rights','source','subject','type'].each { |node| define_method(node + '_list') { @content_nodes[node].dup. } define_method(node + '_clear') { if !@content_nodes[node].nil? @content_nodes[node].each { |x| (x) }; @content_nodes[node] = [] end } next if node == 'title' define_method(node, ->(content=UNASSIGNED, deprecated_id=nil, id:nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) { if unassigned?(content) get_first_node(node) else if deprecated_id warn "secound argument is deprecated. use id: keyword argument" id = deprecated_id end send(node + "_clear") (node, 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) end }) define_method(node+'=') { |content| send(node + "_clear") return if content.nil? if node == 'date' add_date(content) else (node, content) end } next if ["identifier", "date", "creator", "contributor"].include?(node) define_method('add_' + node) { |content, id| (node, content, id: id) } }
Constants included from XMLUtil
XMLUtil::DC_NS, XMLUtil::OPF_NS
Constants included from DSLUtil
Instance Attribute Summary collapse
-
#opf_version ⇒ Object
Returns the value of attribute opf_version.
Class Method Summary collapse
-
.parse(metadata_xml, opf_version = '3.0', id_pool = Package::IDPool.new) ⇒ Object
parse metadata element.
Instance Method Summary collapse
- #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| ... } ⇒ Object
- #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| ... } ⇒ Object
- #add_date(date, deprecated_id = nil, id: nil) ⇒ Object
- #add_identifier(string, id = nil, type = nil) ⇒ Object
- #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| ... } ⇒ Object
- #add_metadata_internal(name, content, id: nil, itemclass: Meta) ⇒ Object
- #add_oldstyle_meta(content, attributes = {}) ⇒ Object
- #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| ... } ⇒ Object
- #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| ... } ⇒ Object
- #get_first_node(node) ⇒ Object
- #ibooks_scroll_axis ⇒ Object
- #ibooks_scroll_axis=(val) ⇒ Object
- #ibooks_version ⇒ Object
- #ibooks_version=(val) ⇒ Object
- #ibooks_vocaburaly_specified? ⇒ Boolean
- #identifier_by_id(id) ⇒ Object
-
#initialize(opf_version = '3.0', id_pool = Package::IDPool.new) {|_self| ... } ⇒ Metadata
constructor
A new instance of Metadata.
- #lastmodified(date = UNASSIGNED) ⇒ Object
- #lastmodified=(date) ⇒ Object
- #lastmodified_updated? ⇒ Boolean
-
#main_title ⇒ Object
should make it obsolete?.
- #meta_clear ⇒ Object
- #meta_list ⇒ Object
- #modified_now ⇒ Object
- #oldstyle_meta ⇒ Object
- #oldstyle_meta_clear ⇒ Object
- #register_meta(meta) ⇒ Object
- #rendition_layout ⇒ Object
- #rendition_layout=(val) ⇒ Object
- #rendition_orientation ⇒ Object
- #rendition_orientation=(val) ⇒ Object
- #rendition_specified? ⇒ Boolean
- #rendition_spread ⇒ Object
- #rendition_spread=(val) ⇒ Object
- #set_lastmodified(date = nil) ⇒ Object
- #title(content = UNASSIGNED, id: nil, title_type: nil) ⇒ Object
- #title=(content) ⇒ Object
- #to_xml(builder) ⇒ Object
- #unregister_meta(meta) ⇒ Object
Methods included from XMLUtil
#attr_to_hash, #ns_prefix, #raw_prefix
Constructor Details
#initialize(opf_version = '3.0', id_pool = Package::IDPool.new) {|_self| ... } ⇒ Metadata
Returns a new instance of Metadata.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 |
Instance Attribute Details
#opf_version ⇒ Object
Returns the value of attribute opf_version.
21 22 23 |
# File 'lib/gepub/metadata.rb', line 21 def opf_version @opf_version end |
Class Method Details
.parse(metadata_xml, opf_version = '3.0', id_pool = Package::IDPool.new) ⇒ Object
parse metadata element. metadata_xml should be Nokogiri::XML::Node object.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/gepub/metadata.rb', line 23 def self.parse(, opf_version = '3.0', id_pool = Package::IDPool.new) Metadata.new(opf_version, id_pool) { || .instance_eval { @xml = @namespaces = @xml.namespaces CONTENT_NODE_LIST.each { |node| @content_nodes[node] = parse_node(DC_NS, node). } @xml.xpath("#{ns_prefix(OPF_NS)}:meta[not(@refines) and @property]", @namespaces).each { |node| (@content_nodes['meta'] ||= []) << (node) } @oldstyle_meta = .each { || case ['property'] when 'rendition:layout' @layout = when 'rendition:orientation' @orientation = when 'rendition:spread' @spread = when 'ibooks:version' @ibooks_version = when 'ibooks:scroll-axis' @ibooks_scroll_axis = end } } # do not set @lastmodified_updated here } end |
Instance Method Details
#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| ... } ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gepub/metadata_add.rb', line 104 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 = 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 if block_given? 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| ... } ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gepub/metadata_add.rb', line 85 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? = 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 if block_given? end |
#add_date(date, deprecated_id = nil, id: nil) ⇒ Object
165 166 167 168 169 170 171 |
# 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 ('date', date, id: id, itemclass: DateMeta) end |
#add_identifier(string, id = nil, type = nil) ⇒ Object
157 158 159 160 161 162 163 |
# 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 = ('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| ... } ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/gepub/metadata_add.rb', line 122 def (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: {} ) = (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] .refine(refiner[:name], refiner[:value]) end end if lang .lang = lang end if alternates .add_alternates alternates end yield if block_given? end |
#add_metadata_internal(name, content, id: nil, itemclass: Meta) ⇒ Object
181 182 183 184 185 |
# File 'lib/gepub/metadata.rb', line 181 def (name, content, id: nil, itemclass: Meta) = itemclass.new(name, content, self, { 'id' => id }) (@content_nodes[name] ||= []) << end |
#add_oldstyle_meta(content, attributes = {}) ⇒ Object
221 222 223 224 225 |
# File 'lib/gepub/metadata.rb', line 221 def (content, attributes = {}) = Meta.new('meta', content, self, attributes) (@oldstyle_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| ... } ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gepub/metadata_add.rb', line 67 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 = (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 if block_given? 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| ... } ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gepub/metadata_add.rb', line 49 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 = ('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 if block_given? end |
#get_first_node(node) ⇒ Object
151 152 153 154 155 |
# 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].[0] end end |
#ibooks_scroll_axis ⇒ Object
279 280 281 |
# File 'lib/gepub/metadata.rb', line 279 def ibooks_scroll_axis @ibooks_scroll_axis.content || '' end |
#ibooks_scroll_axis=(val) ⇒ Object
283 284 285 286 287 288 289 |
# 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 ⇒ Object
270 271 272 |
# File 'lib/gepub/metadata.rb', line 270 def ibooks_version @ibooks_version.content || '' end |
#ibooks_version=(val) ⇒ Object
274 275 276 277 |
# 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? ⇒ Boolean
295 296 297 |
# File 'lib/gepub/metadata.rb', line 295 def ibooks_vocaburaly_specified? @ibooks_version.content || @ibooks_scroll_axis.content end |
#identifier_by_id(id) ⇒ Object
173 174 175 176 177 178 179 |
# 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) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/gepub/metadata.rb', line 187 def lastmodified(date=UNASSIGNED) if unassigned?(date) ret = (@content_nodes['meta'] ||=[]).select { || ['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 { || if (['property'] == 'dcterms:modified') @content_nodes['meta'].delete end } ('meta', date.to_time.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), itemclass: DateMeta)['property'] = 'dcterms:modified' end end |
#lastmodified=(date) ⇒ Object
212 213 214 |
# File 'lib/gepub/metadata.rb', line 212 def lastmodified=(date) lastmodified(date) end |
#lastmodified_updated? ⇒ Boolean
81 82 83 |
# File 'lib/gepub/metadata.rb', line 81 def lastmodified_updated? @lastmodified_updated end |
#main_title ⇒ Object
should make it obsolete?
102 103 104 |
# File 'lib/gepub/metadata.rb', line 102 def main_title # should make it obsolete? title.to_s end |
#meta_clear ⇒ Object
122 123 124 125 126 127 |
# File 'lib/gepub/metadata.rb', line 122 def if !@content_nodes['meta'].nil? @content_nodes['meta'].each { |x| (x) }; @content_nodes['meta'] = [] end end |
#meta_list ⇒ Object
118 119 120 |
# File 'lib/gepub/metadata.rb', line 118 def (@content_nodes['meta'] || [])..dup end |
#modified_now ⇒ Object
208 209 210 |
# File 'lib/gepub/metadata.rb', line 208 def modified_now lastmodified Time.now end |
#oldstyle_meta ⇒ Object
106 107 108 |
# File 'lib/gepub/metadata.rb', line 106 def @oldstyle_meta.dup end |
#oldstyle_meta_clear ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/gepub/metadata.rb', line 110 def @oldstyle_meta.each { || () } @oldstyle_meta = [] end |
#register_meta(meta) ⇒ Object
228 229 230 231 232 233 234 |
# File 'lib/gepub/metadata.rb', line 228 def () if !['id'].nil? raise "id '#{['id']}' is already in use." if @id_pool[['id']] @metalist[['id']] = @id_pool[['id']] = true end end |
#rendition_layout ⇒ Object
243 244 245 |
# File 'lib/gepub/metadata.rb', line 243 def rendition_layout @layout.content || @default_layout end |
#rendition_layout=(val) ⇒ Object
247 248 249 250 |
# 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 ⇒ Object
252 253 254 |
# File 'lib/gepub/metadata.rb', line 252 def rendition_orientation @orientation.content || @default_orientation end |
#rendition_orientation=(val) ⇒ Object
256 257 258 259 |
# 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? ⇒ Boolean
291 292 293 |
# File 'lib/gepub/metadata.rb', line 291 def rendition_specified? @layout.content || @orientation.content || @spread.content end |
#rendition_spread ⇒ Object
261 262 263 |
# File 'lib/gepub/metadata.rb', line 261 def rendition_spread @spread.content || @default_spread end |
#rendition_spread=(val) ⇒ Object
265 266 267 268 |
# 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) ⇒ Object
216 217 218 219 |
# 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) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# 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 = add_title(content, id: id, title_type: title_type) yield if block_given? end end |
#title=(content) ⇒ Object
129 130 131 |
# File 'lib/gepub/metadata.rb', line 129 def title=(content) title(content) end |
#to_xml(builder) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/gepub/metadata.rb', line 85 def to_xml(builder) builder.(@namespaces) { @content_nodes.each { |_name, list| list.each { || .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) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/gepub/metadata.rb', line 236 def () if ['id'].nil? @metalist[['id']] = nil @id_pool[['id']] = nil end end |