Class: GEPUB::Package

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

#inspect

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:



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

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.



12
13
14
# File 'lib/gepub/package.rb', line 12

def bindings
  @bindings
end

#contents_prefixObject

Returns the value of attribute contents_prefix.



12
13
14
# File 'lib/gepub/package.rb', line 12

def contents_prefix
  @contents_prefix
end

#epub_backward_compatObject

Returns the value of attribute epub_backward_compat.



12
13
14
# File 'lib/gepub/package.rb', line 12

def epub_backward_compat
  @epub_backward_compat
end

#manifestObject

Returns the value of attribute manifest.



12
13
14
# File 'lib/gepub/package.rb', line 12

def manifest
  @manifest
end

#metadataObject

Returns the value of attribute metadata.



12
13
14
# File 'lib/gepub/package.rb', line 12

def 
  @metadata
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/gepub/package.rb', line 12

def path
  @path
end

#prefixesObject

Returns the value of attribute prefixes.



12
13
14
# File 'lib/gepub/package.rb', line 12

def prefixes
  @prefixes
end

#spineObject

Returns the value of attribute spine.



12
13
14
# File 'lib/gepub/package.rb', line 12

def spine
  @spine
end

Class Method Details

.parse_opf(opf, path) ⇒ Object

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



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

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

#[](attribute) ⇒ Object

get attribute



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

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

#[]=(attribute, value) ⇒ Object

set attribute



153
154
155
# File 'lib/gepub/package.rb', line 153

def []=(attribute, value)
  @attributes[attribute] = value
end

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

Yields:

  • (item)


174
175
176
177
178
179
180
# File 'lib/gepub/package.rb', line 174

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



189
190
191
192
193
194
# File 'lib/gepub/package.rb', line 189

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



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

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

#enable_renditionObject



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

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

#ibooks_vocabulary_enabled?Boolean

Returns:

  • (Boolean)


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

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

#identifier(identifier = UNASSIGNED) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/gepub/package.rb', line 157

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

#identifier=(identifier) ⇒ Object



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

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

#itemsObject



203
204
205
# File 'lib/gepub/package.rb', line 203

def items
  @manifest.item_list
end

#opf_xmlObject



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
283
284
285
# File 'lib/gepub/package.rb', line 246

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



182
183
184
185
186
187
# File 'lib/gepub/package.rb', line 182

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

#parse_prefixes(prefix) ⇒ Object



84
85
86
87
88
# File 'lib/gepub/package.rb', line 84

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



169
170
171
172
# File 'lib/gepub/package.rb', line 169

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)


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

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

#set_version(val) ⇒ Object



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

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

#spine_itemsObject



196
197
198
199
200
201
# File 'lib/gepub/package.rb', line 196

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

#version(val = UNASSIGNED) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/gepub/package.rb', line 207

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



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

def version=(val)
  version(val)
end