Class: GEPUB::Metadata

Inherits:
Object
  • Object
show all
Includes:
DSLUtil, XMLUtil
Defined in:
lib/gepub/metadata.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.sort_as_meta }
  define_method(node + '_clear') {
    if !@content_nodes[node].nil?
      @content_nodes[node].each { |x| unregister_meta(x) };
      @content_nodes[node] = []
    end
  }

  define_method(node, ->(content=UNASSIGNED, id=nil) {
                  if unassigned?(content)
                    get_first_node(node)
                  else
                    send(node + "_clear")
                    (node, content, id)
                  end
                })

  define_method('add_' + node) {
    |content, id|
    (node, content, id)
  }
  
  define_method('set_' + node) {
    |content, id|
    warn "obsolete : set_#{node}. use #{node} instead."
    send(node + "_clear")
    (node, content, id)
  }
  
  define_method(node+'=') {
    |content|
    send(node + "_clear")
    (node, content, nil)
  }
}

Constants included from XMLUtil

XMLUtil::DC_NS, XMLUtil::OPF_NS

Constants included from DSLUtil

DSLUtil::UNASSIGNED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Yields:

  • (_self)

Yield Parameters:


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gepub/metadata.rb', line 60

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
  yield self if block_given?
end

Instance Attribute Details

#opf_versionObject

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
# 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).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

      }
    }
  }
end

Instance Method Details

#add_contributor(content, id = nil, role = nil) {|meta| ... } ⇒ Object

Yields:

  • (meta)

238
239
240
241
242
# File 'lib/gepub/metadata.rb', line 238

def add_contributor(content, id=nil, role=nil)
  meta = add_person('contributor', content, id, role)
  yield meta if block_given?
  meta
end

#add_creator(content, id = nil, role = 'aut') {|meta| ... } ⇒ Object

Yields:

  • (meta)

232
233
234
235
236
# File 'lib/gepub/metadata.rb', line 232

def add_creator(content, id = nil, role = 'aut')
  meta = add_person('creator', content, id, role)
  yield meta if block_given?
  meta
end

#add_date(date, id) ⇒ Object


193
194
195
# File 'lib/gepub/metadata.rb', line 193

def add_date(date, id)
  ('date', date, id, DateMeta)
end

#add_identifier(string, id = nil, type = nil) ⇒ Object


185
186
187
188
189
190
191
# File 'lib/gepub/metadata.rb', line 185

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)
  identifier.refine('identifier-type', type) unless type.nil?
  identifier
end

#add_metadata(name, content, id = nil, itemclass = Meta) {|meta| ... } ⇒ Object

Yields:

  • (meta)

205
206
207
208
209
210
# File 'lib/gepub/metadata.rb', line 205

def (name, content, id = nil, itemclass = Meta)
  meta = itemclass.new(name, content, self, { 'id' => id })
  (@content_nodes[name] ||= []) << meta
  yield meta if block_given?
  meta
end

#add_oldstyle_meta(content, attributes = {}) ⇒ Object


283
284
285
286
287
# File 'lib/gepub/metadata.rb', line 283

def add_oldstyle_meta(content, attributes = {})
  meta = Meta.new('meta', content, self, attributes)
  (@oldstyle_meta ||= []) << meta
  meta
end

#add_person(name, content, id = nil, role = nil) {|meta| ... } ⇒ Object

Yields:

  • (meta)

226
227
228
229
230
# File 'lib/gepub/metadata.rb', line 226

def add_person(name, content, id = nil, role = nil)
  meta = (name, content, id).refine('role', role)
  yield meta if block_given?
  meta
end

#add_title(content, id = nil, title_type = nil) {|meta| ... } ⇒ Object

Yields:

  • (meta)

212
213
214
215
216
# File 'lib/gepub/metadata.rb', line 212

def add_title(content, id = nil, title_type = nil)
  meta = ('title', content, id).refine('title-type', title_type)
  yield meta if block_given?
  meta
end

#get_first_node(node) ⇒ Object


179
180
181
182
183
# File 'lib/gepub/metadata.rb', line 179

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_axisObject


341
342
343
# File 'lib/gepub/metadata.rb', line 341

def ibooks_scroll_axis
  @ibooks_scroll_axis.content || ''      
end

#ibooks_scroll_axis=(val) ⇒ Object


345
346
347
348
349
350
351
# File 'lib/gepub/metadata.rb', line 345

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_versionObject


332
333
334
# File 'lib/gepub/metadata.rb', line 332

def ibooks_version
  @ibooks_version.content || ''
end

#ibooks_version=(val) ⇒ Object


336
337
338
339
# File 'lib/gepub/metadata.rb', line 336

def ibooks_version=(val)
  @ibooks_version = Meta.new('meta', val, self, { 'property' => 'ibooks:version' })
  (@content_nodes['meta'] ||= []) << @ibooks_version
end

#ibooks_vocaburaly_specified?Boolean

Returns:

  • (Boolean)

357
358
359
# File 'lib/gepub/metadata.rb', line 357

def ibooks_vocaburaly_specified?
  @ibooks_version.content || @ibooks_scroll_axis.content
end

#identifier_by_id(id) ⇒ Object


197
198
199
200
201
202
203
# File 'lib/gepub/metadata.rb', line 197

def identifier_by_id(id)
  @content_nodes['identifier'].each {
    |x|
    return x.content if x['id'] == id
  }
  return nil
end

#lastmodified(date = UNASSIGNED) ⇒ Object


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/gepub/metadata.rb', line 244

def lastmodified(date=UNASSIGNED)
  if unassigned?(date)
    ret = (@content_nodes['meta'] ||=[]).select {
      |meta|
      meta['property'] == 'dcterms:modified'
    }
    ret.size == 0 ? nil : ret[0]
  else
    date ||= Time.now
    (@content_nodes['meta'] ||= []).each {
      |meta|
      if (meta['property'] == 'dcterms:modified')
        @content_nodes['meta'].delete meta
      end
    }
    ('meta', date.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), nil, DateMeta)['property'] = 'dcterms:modified'
  end
end

#lastmodified=(date) ⇒ Object


267
268
269
# File 'lib/gepub/metadata.rb', line 267

def lastmodified=(date)
  lastmodified(date)
end

#main_titleObject

should make it obsolete?


96
97
98
# File 'lib/gepub/metadata.rb', line 96

def main_title # should make it obsolete? 
  title.to_s
end

#meta_clearObject


154
155
156
157
158
159
# File 'lib/gepub/metadata.rb', line 154

def meta_clear
  if !@content_nodes['meta'].nil?
    @content_nodes['meta'].each { |x| unregister_meta(x) };
    @content_nodes['meta'] = []
  end
end

#meta_listObject


150
151
152
# File 'lib/gepub/metadata.rb', line 150

def meta_list
  (@content_nodes['meta'] || []).sort_as_meta.dup
end

#modified_nowObject


263
264
265
# File 'lib/gepub/metadata.rb', line 263

def modified_now
  lastmodified Time.now
end

#oldstyle_metaObject


100
101
102
# File 'lib/gepub/metadata.rb', line 100

def oldstyle_meta
  @oldstyle_meta.dup
end

#oldstyle_meta_clearObject


104
105
106
107
108
109
110
# File 'lib/gepub/metadata.rb', line 104

def oldstyle_meta_clear
  @oldstyle_meta.each {
    |meta|
    unregister_meta(meta)
  }
  @oldstyle_meta = []
end

#register_meta(meta) ⇒ Object


290
291
292
293
294
295
296
# File 'lib/gepub/metadata.rb', line 290

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_layoutObject


305
306
307
# File 'lib/gepub/metadata.rb', line 305

def rendition_layout
  @layout.content || @default_layout
end

#rendition_layout=(val) ⇒ Object


309
310
311
312
# File 'lib/gepub/metadata.rb', line 309

def rendition_layout=(val)
  @layout = Meta.new('meta', val, self, { 'property' => 'rendition:layout' })
  (@content_nodes['meta'] ||= []) << @layout
end

#rendition_orientationObject


314
315
316
# File 'lib/gepub/metadata.rb', line 314

def rendition_orientation
  @orientation.content || @default_orientation
end

#rendition_orientation=(val) ⇒ Object


318
319
320
321
# File 'lib/gepub/metadata.rb', line 318

def rendition_orientation=(val)
  @orientation = Meta.new('meta', val, self, { 'property' => 'rendition:orientation' })
  (@content_nodes['meta'] ||= []) << @orientation
end

#rendition_specified?Boolean

Returns:

  • (Boolean)

353
354
355
# File 'lib/gepub/metadata.rb', line 353

def rendition_specified?
  @layout.content || @orientation.content || @spread.content
end

#rendition_spreadObject


323
324
325
# File 'lib/gepub/metadata.rb', line 323

def rendition_spread
  @spread.content || @default_spread
end

#rendition_spread=(val) ⇒ Object


327
328
329
330
# File 'lib/gepub/metadata.rb', line 327

def rendition_spread=(val)
  @spread = Meta.new('meta', val, self, { 'property' => 'rendition:spread' })
  (@content_nodes['meta'] ||= []) << @spread
end

#set_lastmodified(date = nil) ⇒ Object


271
272
273
274
275
276
277
278
279
280
281
# File 'lib/gepub/metadata.rb', line 271

def set_lastmodified(date=nil)
  warn "obsolete : set_lastmodified. use lastmodified instead."
  date ||= Time.now
  (@content_nodes['meta'] ||= []).each {
    |meta|
    if (meta['property'] == 'dcterms:modified')
      @content_nodes['meta'].delete meta
    end
  }
  ('meta', date.utc.strftime('%Y-%m-%dT%H:%M:%SZ'), nil, DateMeta)['property'] = 'dcterms:modified'
end

#set_title(content, id = nil, title_type = nil) {|meta| ... } ⇒ Object

Yields:

  • (meta)

218
219
220
221
222
223
224
# File 'lib/gepub/metadata.rb', line 218

def set_title(content, id = nil, title_type = nil)
  warn "obsolete : set_title. use title instead."
  title_clear
  meta = add_title(content, id, title_type)
  yield meta if block_given?
  meta
end

#title(content = UNASSIGNED, id = nil, title_type = nil) ⇒ Object


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gepub/metadata.rb', line 161

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, title_type)
    yield meta if block_given?
    meta
  end
end

#to_xml(builder) ⇒ Object


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gepub/metadata.rb', line 79

def to_xml(builder) 
  builder.(@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) ⇒ Object


298
299
300
301
302
303
# File 'lib/gepub/metadata.rb', line 298

def unregister_meta(meta)
  if meta['id'].nil?
    @metalist[meta['id']] =  nil
    @id_pool[meta['id']] = nil
  end
end