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:


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gepub/package.rb', line 102

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.


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gepub/package.rb', line 85

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


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

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

#[]=(k, v) ⇒ Object


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

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

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

Yields:

  • (item)

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

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

#add_ordered_item(href, io_or_filename = nil, id = nil, attributes = {}) ⇒ Object


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

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

#enable_ibooks_vocabularyObject


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

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

#enable_renditionObject


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

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

#ibooks_vocabulary_enabled?Boolean

Returns:

  • (Boolean)

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

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

#identifier(identifier = UNASSIGNED) ⇒ Object


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

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

#identifier=(identifier) ⇒ Object


158
159
160
# File 'lib/gepub/package.rb', line 158

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

#itemsObject


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

def items
  @manifest.item_list
end

#opf_xmlObject


240
241
242
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
# File 'lib/gepub/package.rb', line 240

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


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

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

#parse_prefixes(prefix) ⇒ Object


78
79
80
81
82
# File 'lib/gepub/package.rb', line 78

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


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

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)

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

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

#set_version(val) ⇒ Object


212
213
214
215
216
217
218
# File 'lib/gepub/package.rb', line 212

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


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

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

#version(val = UNASSIGNED) ⇒ Object


201
202
203
204
205
206
207
208
209
210
# File 'lib/gepub/package.rb', line 201

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


220
221
222
# File 'lib/gepub/package.rb', line 220

def version=(val)
  version(val)
end