Class: GEPUB::Metadata

Inherits:
Object
  • Object
show all
Includes:
DSLUtil, 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, 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

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

Yields:

  • (meta)

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
  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| ... } ⇒ Object

Yields:

  • (meta)

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?
  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) ⇒ Object


159
160
161
162
163
164
165
# File 'lib/gepub/metadata.rb', line 159

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


151
152
153
154
155
156
157
# File 'lib/gepub/metadata.rb', line 151

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)

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: {}
)
	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


175
176
177
178
179
# File 'lib/gepub/metadata.rb', line 175

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


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

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| ... } ⇒ Object

Yields:

  • (meta)

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

Yields:

  • (meta)

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


145
146
147
148
149
# File 'lib/gepub/metadata.rb', line 145

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


278
279
280
# File 'lib/gepub/metadata.rb', line 278

def ibooks_scroll_axis
  @ibooks_scroll_axis.content || ''      
end

#ibooks_scroll_axis=(val) ⇒ Object


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

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


269
270
271
# File 'lib/gepub/metadata.rb', line 269

def ibooks_version
  @ibooks_version.content || ''
end

#ibooks_version=(val) ⇒ Object


273
274
275
276
# File 'lib/gepub/metadata.rb', line 273

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

#ibooks_vocaburaly_specified?Boolean


294
295
296
# File 'lib/gepub/metadata.rb', line 294

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

#identifier_by_id(id) ⇒ Object


167
168
169
170
171
172
173
# File 'lib/gepub/metadata.rb', line 167

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

#lastmodified(date = UNASSIGNED) ⇒ Object


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/gepub/metadata.rb', line 181

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'), itemclass: DateMeta)['property'] = 'dcterms:modified'
  end
end

#lastmodified=(date) ⇒ Object


204
205
206
# File 'lib/gepub/metadata.rb', line 204

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


116
117
118
119
120
121
# File 'lib/gepub/metadata.rb', line 116

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

#meta_listObject


112
113
114
# File 'lib/gepub/metadata.rb', line 112

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

#modified_nowObject


200
201
202
# File 'lib/gepub/metadata.rb', line 200

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


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

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


242
243
244
# File 'lib/gepub/metadata.rb', line 242

def rendition_layout
  @layout.content || @default_layout
end

#rendition_layout=(val) ⇒ Object


246
247
248
249
# File 'lib/gepub/metadata.rb', line 246

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

#rendition_orientationObject


251
252
253
# File 'lib/gepub/metadata.rb', line 251

def rendition_orientation
  @orientation.content || @default_orientation
end

#rendition_orientation=(val) ⇒ Object


255
256
257
258
# File 'lib/gepub/metadata.rb', line 255

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

#rendition_specified?Boolean


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

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

#rendition_spreadObject


260
261
262
# File 'lib/gepub/metadata.rb', line 260

def rendition_spread
  @spread.content || @default_spread
end

#rendition_spread=(val) ⇒ Object


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

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

#set_lastmodified(date = nil) ⇒ Object


208
209
210
211
212
213
214
215
216
217
218
# File 'lib/gepub/metadata.rb', line 208

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'), itemclass: DateMeta)['property'] = 'dcterms:modified'
end

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


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gepub/metadata.rb', line 127

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


123
124
125
# File 'lib/gepub/metadata.rb', line 123

def title=(content)
  title(content)
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


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

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