Class: GEPUB::Package

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
DSLUtil, XMLUtil
Defined in:
lib/gepub/package.rb

Overview

Holds data in opf file.

Defined Under Namespace

Classes: IDPool

Constant Summary

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(path = 'OEBPS/package.opf', attributes = {}) {|_self| ... } ⇒ Package

Returns a new instance of Package

Yields:

  • (_self)

Yield Parameters:


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/gepub/package.rb', line 106

def initialize(path='OEBPS/package.opf', attributes={})
  @path = path
  if File.extname(@path) != '.opf'
    if @path.size > 0
      @path = [path,'package.opf'].join('/')
    end
  end
  @contents_prefix = File.dirname(@path).sub(/^\.$/,'')
  @contents_prefix = @contents_prefix + '/' if @contents_prefix.size > 0
  @prefixes = {}
  @namespaces = {'xmlns' => OPF_NS }
  @attributes = attributes
  @attributes['version'] ||= '3.0'
  @id_pool = IDPool.new
  @metadata = Metadata.new(version)
  @manifest = Manifest.new(version)
  @spine = Spine.new(version)
  @bindings = Bindings.new
  @epub_backward_compat = true
  yield self if block_given?
end

Instance Attribute Details

#bindingsObject

Returns the value of attribute bindings


10
11
12
# File 'lib/gepub/package.rb', line 10

def bindings
  @bindings
end

#contents_prefixObject

Returns the value of attribute contents_prefix


10
11
12
# File 'lib/gepub/package.rb', line 10

def contents_prefix
  @contents_prefix
end

#epub_backward_compatObject

Returns the value of attribute epub_backward_compat


10
11
12
# File 'lib/gepub/package.rb', line 10

def epub_backward_compat
  @epub_backward_compat
end

#manifestObject

Returns the value of attribute manifest


10
11
12
# File 'lib/gepub/package.rb', line 10

def manifest
  @manifest
end

#metadataObject

Returns the value of attribute metadata


10
11
12
# File 'lib/gepub/package.rb', line 10

def 
  @metadata
end

#pathObject

Returns the value of attribute path


10
11
12
# File 'lib/gepub/package.rb', line 10

def path
  @path
end

#prefixesObject

Returns the value of attribute prefixes


10
11
12
# File 'lib/gepub/package.rb', line 10

def prefixes
  @prefixes
end

#spineObject

Returns the value of attribute spine


10
11
12
# File 'lib/gepub/package.rb', line 10

def spine
  @spine
end

Class Method Details

.parse_opf(opf, path) ⇒ Object

parse OPF data. opf should be io or string object.


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gepub/package.rb', line 89

def self.parse_opf(opf, path)
  Package.new(path) {
    |package|
    package.instance_eval {
      @path = path
      @xml = Nokogiri::XML::Document.parse(opf)
      @namespaces = @xml.root.namespaces
      @attributes = attr_to_hash(@xml.root.attributes)
      @metadata = Metadata.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:metadata"), @attributes['version'], @id_pool)
      @manifest = Manifest.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:manifest"), @attributes['version'], @id_pool)
      @spine = Spine.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:spine"), @attributes['version'], @id_pool)
      @bindings = Bindings.parse(@xml.at_xpath("//#{ns_prefix(OPF_NS)}:bindings"))
      @prefixes = parse_prefixes(@attributes['prefix'])
    }
  }
end

Instance Method Details

#[](x) ⇒ Object


145
146
147
# File 'lib/gepub/package.rb', line 145

def [](x)
  @attributes[x]
end

#[]=(k, v) ⇒ Object


149
150
151
# File 'lib/gepub/package.rb', line 149

def []=(k,v)
  @attributes[k] = v
end

#add_item(href, content: nil, id: nil, attributes: {}) {|item| ... } ⇒ Object

Yields:

  • (item)

171
172
173
174
175
176
177
# File 'lib/gepub/package.rb', line 171

def add_item(href, content:nil, id: nil, attributes: {})
  item = @manifest.add_item(id, href, nil, attributes)
  item.add_content(content) unless content.nil?
  @spine.push(item) if @ordered
  yield item if block_given?
  item
end

#add_ordered_item(href, content: nil, id: nil, attributes: {}) ⇒ Object


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

def add_ordered_item(href, content:nil, id: nil, attributes: {})
  raise 'do not call add_ordered_item within ordered block.' if @ordered
  item = add_item(href, attributes: attributes, id:id, content: content)
  @spine.push(item)
  item
end

#enable_ibooks_vocabularyObject


235
236
237
# File 'lib/gepub/package.rb', line 235

def enable_ibooks_vocabulary
  @prefixes['ibooks'] = 'http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/'
end

#enable_renditionObject


227
228
229
# File 'lib/gepub/package.rb', line 227

def enable_rendition
  @prefixes['rendition'] = 'http://www.idpf.org/vocab/rendition/#'
end

#ibooks_vocabulary_enabled?Boolean

Returns:

  • (Boolean)

239
240
241
# File 'lib/gepub/package.rb', line 239

def ibooks_vocabulary_enabled?
  @prefixes['ibooks'] == 'http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0/'
end

#identifier(identifier = UNASSIGNED) ⇒ Object


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

def identifier(identifier=UNASSIGNED)
  if unassigned?(identifier)
    @metadata.identifier_by_id(unique_identifier)
  else
    self.identifier=(identifier)
  end
end

#identifier=(identifier) ⇒ Object


162
163
164
# File 'lib/gepub/package.rb', line 162

def identifier=(identifier)
  primary_identifier(identifier, nil, nil)
end

#itemsObject


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

def items
  @manifest.item_list
end

#opf_xmlObject


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/gepub/package.rb', line 243

def opf_xml
  if version.to_f < 3.0 || @epub_backward_compat
    spine.toc  ||= 'ncx'
    if @metadata.oldstyle_meta.select {
      |meta|
      meta['name'] == 'cover'
      }.length == 0
      
      @manifest.item_list.each {
        |_k, item|
        if item.properties && item.properties.member?('cover-image')
          @metadata.add_oldstyle_meta(nil, 'name' => 'cover', 'content' => item.id)
        end
      }
    end
  end
  if @metadata.rendition_specified? || @spine.rendition_specified? 
    enable_rendition
  end
  if @metadata.ibooks_vocaburaly_specified?
    enable_ibooks_vocabulary
  end

  builder = Nokogiri::XML::Builder.new {
    |xml|
    if @prefixes.size == 0
      @attributes.delete 'prefix'
    else
      @attributes['prefix'] = @prefixes.map { |k, v| "#{k}: #{v}" }.join(' ')
    end
    
    xml.package(@namespaces.merge(@attributes)) {
      @metadata.to_xml(xml)
      @manifest.to_xml(xml)
      @spine.to_xml(xml)
      @bindings.to_xml(xml)
    }
  }
  builder.to_xml(:encoding => 'utf-8')
end

#orderedObject


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

def ordered
  raise 'need block.' if !block_given?
  @ordered = true
  yield
  @ordered = nil
end

#parse_prefixes(prefix) ⇒ Object


82
83
84
85
86
# File 'lib/gepub/package.rb', line 82

def parse_prefixes(prefix)
  return {} if prefix.nil?
  m = prefix.scan(/([\S]+): +(\S+)[\s]*/)
  Hash[*m.flatten]
end

#primary_identifier(identifier, id = nil, type = nil) ⇒ Object


166
167
168
169
# File 'lib/gepub/package.rb', line 166

def primary_identifier(identifier, id = nil, type = nil)
  unique_identifier(id || @id_pool.generate_key(:prefix => 'BookId', :without_count => true))
  @metadata.add_identifier identifier, unique_identifier, type
end

#rendition_enabled?Boolean

Returns:

  • (Boolean)

231
232
233
# File 'lib/gepub/package.rb', line 231

def rendition_enabled?
  @prefixes['rendition'] == 'http://www.idpf.org/vocab/rendition/#'      
end

#set_version(val) ⇒ Object


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

def set_version(val)
  warn 'set_version is obsolete: use verion instead.'
  @attributes['version'] = val
  @metadata.opf_version = val
  @manifest.opf_version = val
  @spine.opf_version = val
end

#spine_itemsObject


193
194
195
196
197
198
# File 'lib/gepub/package.rb', line 193

def spine_items
  spine.itemref_list.map {
    |itemref|
    @manifest.item_list[itemref.idref]
  }
end

#version(val = UNASSIGNED) ⇒ Object


204
205
206
207
208
209
210
211
212
213
# File 'lib/gepub/package.rb', line 204

def version(val=UNASSIGNED)
  if unassigned?(val)
    @attributes['version']
  else
    @attributes['version'] = val
    @metadata.opf_version = val
    @manifest.opf_version = val
    @spine.opf_version = val
  end
end

#version=(val) ⇒ Object


223
224
225
# File 'lib/gepub/package.rb', line 223

def version=(val)
  version(val)
end