Class: GEPUB::Metadata

Inherits:
Object
  • Object
show all
Includes:
XMLUtil
Defined in:
lib/gepub/metadata.rb

Overview

Holds data in /package/metadata

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| unregister_meta(x) };
      @content_nodes[node] = []
    end
  }

  define_method(node) {
    get_first_node(node)
  }

  define_method('add_' + node) {
    |content, id|
    (node, content, id)
  }
  
  define_method('set_' + node) {
    |content, id|
    send(node + "_clear")
    (node, content, id)
  }
  
  define_method(node+'=') {
    |content|
    send('set_' + node, content, nil)
  }
}

Constants included from XMLUtil

XMLUtil::DC_NS, XMLUtil::OPF_NS

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:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gepub/metadata.rb', line 42

def initialize(opf_version = '3.0',id_pool = Package::IDPool.new)
  @id_pool = id_pool
  @metalist = {}
  @content_nodes = {}
  @meta = {}
  @oldstyle_meta = []
  @opf_version = opf_version
  @namespaces = { 'xmlns:dc' =>  DC_NS }
  @namespaces['xmlns:opf'] = OPF_NS if @opf_version.to_f < 3.0 
  yield self if block_given?
end

Instance Attribute Details

#opf_versionObject

Returns the value of attribute opf_version.



16
17
18
# File 'lib/gepub/metadata.rb', line 16

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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gepub/metadata.rb', line 18

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|
        i = 0
        @content_nodes[node] = parse_node(DC_NS, node).sort_by {
          |v|
          [v.refiner('display-seq').to_s.to_i || 2 ** (0.size * 8 - 2) - 1, i += 1]
        }              
      }
      @xml.xpath("#{ns_prefix(OPF_NS)}:meta[not(@refines) and @property]", @namespaces).each {
        |node|
        @meta[node['property']] = create_meta(node)
      }

      @oldstyle_meta = parse_opf2_meta
    }
  }
end

Instance Method Details

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

Yields:

  • (meta)


184
185
186
187
188
# File 'lib/gepub/metadata.rb', line 184

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)


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

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



141
142
143
# File 'lib/gepub/metadata.rb', line 141

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

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



134
135
136
137
138
139
# File 'lib/gepub/metadata.rb', line 134

def add_identifier(string, id, type=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) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



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

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

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



209
210
211
212
213
# File 'lib/gepub/metadata.rb', line 209

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)


172
173
174
175
176
# File 'lib/gepub/metadata.rb', line 172

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)


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

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



128
129
130
131
132
# File 'lib/gepub/metadata.rb', line 128

def get_first_node(node)
  if !@content_nodes[node].nil? && @content_nodes[node].size > 0
    @content_nodes[node][0]
  end
end

#identifier_by_id(id) ⇒ Object



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

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

#lastmodifiedObject



190
191
192
193
194
195
196
# File 'lib/gepub/metadata.rb', line 190

def lastmodified
  ret = (@content_nodes['meta'] ||=[]).select {
    |meta|
    meta['property'] == 'dcterms:modified'
  }
  ret.size == 0 ? nil : ret[0]
end

#main_titleObject

should make it obsolete?



71
72
73
# File 'lib/gepub/metadata.rb', line 71

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

#oldstyle_metaObject



75
76
77
# File 'lib/gepub/metadata.rb', line 75

def oldstyle_meta
  @oldstyle_meta.dup
end

#oldstyle_meta_clearObject



79
80
81
82
83
84
85
# File 'lib/gepub/metadata.rb', line 79

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

#register_meta(meta) ⇒ Object



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

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

#set_lastmodified(date = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/gepub/metadata.rb', line 198

def set_lastmodified(date=nil)
  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)


165
166
167
168
169
170
# File 'lib/gepub/metadata.rb', line 165

def set_title(content, id = nil, title_type = nil)
  title_clear
  meta = add_title(content, id, title_type)
  yield meta if block_given?
  meta
end

#titleObject



118
119
120
121
122
123
124
125
126
# File 'lib/gepub/metadata.rb', line 118

def title
  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')
end

#to_xml(builder) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gepub/metadata.rb', line 54

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



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

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