Class: GEPUB::Metadata
- Inherits:
-
Object
- Object
- GEPUB::Metadata
- Includes:
- DSLUtil, InspectMixin, XMLUtil
- 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, 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 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, 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, 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, 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, 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, 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 InspectMixin
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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gepub/metadata.rb', line 63 def initialize(opf_version = '3.0',id_pool = Package::IDPool.new) @id_pool = id_pool = {} @content_nodes = {} = [] @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.
23 24 25 |
# File 'lib/gepub/metadata.rb', line 23 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.
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 60 61 |
# File 'lib/gepub/metadata.rb', line 25 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) } = .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, id: nil, title_type: nil, identifier_type: nil, display_seq: nil, file_as: nil, group_position: nil, role: nil, lang: nil, alternates: {}) {|meta| ... } ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/gepub/metadata_add.rb', line 78 def add_contributor(content, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) = 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, 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 |
# File 'lib/gepub/metadata_add.rb', line 67 def add_creator(content, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) 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, id: nil) ⇒ Object
167 168 169 |
# File 'lib/gepub/metadata.rb', line 167 def add_date(date, id: nil) ('date', date, id: id, itemclass: DateMeta) end |
#add_identifier(string, id = nil, type = nil) ⇒ Object
159 160 161 162 163 164 165 |
# File 'lib/gepub/metadata.rb', line 159 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gepub/metadata_add.rb', line 88 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
179 180 181 182 183 |
# File 'lib/gepub/metadata.rb', line 179 def (name, content, id: nil, itemclass: Meta) = itemclass.new(name, content, self, { 'id' => id }) (@content_nodes[name] ||= []) << end |
#add_oldstyle_meta(content, attributes = {}) ⇒ Object
219 220 221 222 223 |
# File 'lib/gepub/metadata.rb', line 219 def (content, attributes = {}) = Meta.new('meta', content, self, attributes) ( ||= []) << end |
#add_person(name, content, id: nil, title_type: nil, identifier_type: nil, display_seq: nil, file_as: nil, group_position: nil, role: nil, lang: nil, alternates: {}) {|meta| ... } ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/gepub/metadata_add.rb', line 57 def add_person(name, content, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) = (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, id: nil, title_type: nil, identifier_type: nil, display_seq: nil, file_as: nil, group_position: nil, role: nil, lang: nil, alternates: {}) {|meta| ... } ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/gepub/metadata_add.rb', line 47 def add_title(content, id: nil, title_type: nil,identifier_type: nil,display_seq: nil,file_as: nil,group_position: nil,role: nil, lang: nil, alternates: {}) = ('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
153 154 155 156 157 |
# File 'lib/gepub/metadata.rb', line 153 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
277 278 279 |
# File 'lib/gepub/metadata.rb', line 277 def ibooks_scroll_axis @ibooks_scroll_axis.content || '' end |
#ibooks_scroll_axis=(val) ⇒ Object
281 282 283 284 285 286 287 |
# File 'lib/gepub/metadata.rb', line 281 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
268 269 270 |
# File 'lib/gepub/metadata.rb', line 268 def ibooks_version @ibooks_version.content || '' end |
#ibooks_version=(val) ⇒ Object
272 273 274 275 |
# File 'lib/gepub/metadata.rb', line 272 def ibooks_version=(val) @ibooks_version = Meta.new('meta', val, self, { 'property' => 'ibooks:version' }) (@content_nodes['meta'] ||= []) << @ibooks_version end |
#ibooks_vocaburaly_specified? ⇒ Boolean
293 294 295 |
# File 'lib/gepub/metadata.rb', line 293 def ibooks_vocaburaly_specified? @ibooks_version.content || @ibooks_scroll_axis.content end |
#identifier_by_id(id) ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/gepub/metadata.rb', line 171 def identifier_by_id(id) (@content_nodes['identifier'] || []).each { |x| return x.content if x['id'] == id } return nil end |
#lastmodified(date = UNASSIGNED) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/gepub/metadata.rb', line 185 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
210 211 212 |
# File 'lib/gepub/metadata.rb', line 210 def lastmodified=(date) lastmodified(date) end |
#lastmodified_updated? ⇒ Boolean
83 84 85 |
# File 'lib/gepub/metadata.rb', line 83 def lastmodified_updated? @lastmodified_updated end |
#main_title ⇒ Object
should make it obsolete?
104 105 106 |
# File 'lib/gepub/metadata.rb', line 104 def main_title # should make it obsolete? title.to_s end |
#meta_clear ⇒ Object
124 125 126 127 128 129 |
# File 'lib/gepub/metadata.rb', line 124 def if !@content_nodes['meta'].nil? @content_nodes['meta'].each { |x| (x) }; @content_nodes['meta'] = [] end end |
#meta_list ⇒ Object
120 121 122 |
# File 'lib/gepub/metadata.rb', line 120 def (@content_nodes['meta'] || [])..dup end |
#modified_now ⇒ Object
206 207 208 |
# File 'lib/gepub/metadata.rb', line 206 def modified_now lastmodified Time.now end |
#oldstyle_meta ⇒ Object
108 109 110 |
# File 'lib/gepub/metadata.rb', line 108 def .dup end |
#oldstyle_meta_clear ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/gepub/metadata.rb', line 112 def .each { || () } = [] end |
#register_meta(meta) ⇒ Object
226 227 228 229 230 231 232 |
# File 'lib/gepub/metadata.rb', line 226 def () if !['id'].nil? raise "id '#{meta['id']}' is already in use." if @id_pool[['id']] [['id']] = @id_pool[['id']] = true end end |
#rendition_layout ⇒ Object
241 242 243 |
# File 'lib/gepub/metadata.rb', line 241 def rendition_layout @layout.content || @default_layout end |
#rendition_layout=(val) ⇒ Object
245 246 247 248 |
# File 'lib/gepub/metadata.rb', line 245 def rendition_layout=(val) @layout = Meta.new('meta', val, self, { 'property' => 'rendition:layout' }) (@content_nodes['meta'] ||= []) << @layout end |
#rendition_orientation ⇒ Object
250 251 252 |
# File 'lib/gepub/metadata.rb', line 250 def rendition_orientation @orientation.content || @default_orientation end |
#rendition_orientation=(val) ⇒ Object
254 255 256 257 |
# File 'lib/gepub/metadata.rb', line 254 def rendition_orientation=(val) @orientation = Meta.new('meta', val, self, { 'property' => 'rendition:orientation' }) (@content_nodes['meta'] ||= []) << @orientation end |
#rendition_specified? ⇒ Boolean
289 290 291 |
# File 'lib/gepub/metadata.rb', line 289 def rendition_specified? @layout.content || @orientation.content || @spread.content end |
#rendition_spread ⇒ Object
259 260 261 |
# File 'lib/gepub/metadata.rb', line 259 def rendition_spread @spread.content || @default_spread end |
#rendition_spread=(val) ⇒ Object
263 264 265 266 |
# File 'lib/gepub/metadata.rb', line 263 def rendition_spread=(val) @spread = Meta.new('meta', val, self, { 'property' => 'rendition:spread' }) (@content_nodes['meta'] ||= []) << @spread end |
#set_lastmodified(date = nil) ⇒ Object
214 215 216 217 |
# File 'lib/gepub/metadata.rb', line 214 def set_lastmodified(date=nil) warn "obsolete : set_lastmodified. use lastmodified instead." lastmodified(date) end |
#title(content = UNASSIGNED, id: nil, title_type: nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/gepub/metadata.rb', line 135 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
131 132 133 |
# File 'lib/gepub/metadata.rb', line 131 def title=(content) title(content) end |
#to_xml(builder) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/gepub/metadata.rb', line 87 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) } } .each { |node| node.to_xml(builder, @id_pool, nil) } } @xml end |
#unregister_meta(meta) ⇒ Object
234 235 236 237 238 239 |
# File 'lib/gepub/metadata.rb', line 234 def () if ['id'].nil? [['id']] = nil @id_pool[['id']] = nil end end |