Class: GEPUB::Metadata

Inherits:
Object
  • Object
show all
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.sort_as_meta }
  define_method(node + '_clear') {
    if !@content_nodes[node].nil?
      @content_nodes[node].each { |x| unregister_meta(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

DSLUtil::UNASSIGNED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InspectMixin

#inspect

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:



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
  @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_versionObject

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

      }
    }
    # 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

Yields:

  • (meta)


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: {}) 
  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, id: nil, title_type: nil, identifier_type: nil, display_seq: nil, file_as: nil, group_position: nil, role: nil, lang: nil, alternates: {}) {|meta| ... } ⇒ Object

Yields:

  • (meta)


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?
  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, 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

Yields:

  • (meta)


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: {}
                )
  meta = (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) ⇒ Object



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

def (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 = {}) ⇒ Object



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

def add_oldstyle_meta(content, attributes = {})
  meta = Meta.new('meta', content, self, attributes)
  (@oldstyle_meta ||= []) << meta
  meta
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

Yields:

  • (meta)


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: {})
  meta = (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, id: nil, title_type: nil, identifier_type: nil, display_seq: nil, file_as: nil, group_position: nil, role: nil, lang: nil, alternates: {}) {|meta| ... } ⇒ Object

Yields:

  • (meta)


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: {})
  meta = ('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) ⇒ 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].sort_as_meta[0]
  end
end

#ibooks_scroll_axisObject



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_versionObject



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

Returns:

  • (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 {
      |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
    }
    ('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

Returns:

  • (Boolean)


83
84
85
# File 'lib/gepub/metadata.rb', line 83

def lastmodified_updated?
  @lastmodified_updated
end

#main_titleObject

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_clearObject



124
125
126
127
128
129
# File 'lib/gepub/metadata.rb', line 124

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

#meta_listObject



120
121
122
# File 'lib/gepub/metadata.rb', line 120

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

#modified_nowObject



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

def modified_now
  lastmodified Time.now
end

#oldstyle_metaObject



108
109
110
# File 'lib/gepub/metadata.rb', line 108

def oldstyle_meta
  @oldstyle_meta.dup
end

#oldstyle_meta_clearObject



112
113
114
115
116
117
118
# File 'lib/gepub/metadata.rb', line 112

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

#register_meta(meta) ⇒ Object



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

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



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_orientationObject



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

Returns:

  • (Boolean)


289
290
291
# File 'lib/gepub/metadata.rb', line 289

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

#rendition_spreadObject



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
    meta = add_title(content, id: id, title_type: title_type)
    yield meta if block_given?
    meta
  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 {
        |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



234
235
236
237
238
239
# File 'lib/gepub/metadata.rb', line 234

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