Class: GEPUB::Package
- Inherits:
-
Object
show all
- Includes:
- XMLUtil
- Defined in:
- lib/gepub/package.rb
Overview
Defined Under Namespace
Classes: IDPool
Constant Summary
Constants included
from XMLUtil
XMLUtil::DC_NS, XMLUtil::OPF_NS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](x) ⇒ Object
-
#[]=(k, v) ⇒ Object
-
#add_item(href, io_or_filename = nil, id = nil, attributes = {}) {|item| ... } ⇒ Object
-
#add_ordered_item(href, io_or_filename = nil, id = nil, attributes = {}) ⇒ Object
-
#author ⇒ Object
-
#author=(val) ⇒ Object
-
#epub_version ⇒ Object
-
#epub_version=(val) ⇒ Object
-
#identifier ⇒ Object
-
#identifier=(identifier) ⇒ Object
-
#initialize(path = 'OEBPS/package.opf', attributes = {}) {|_self| ... } ⇒ Package
constructor
A new instance of Package.
-
#items ⇒ Object
-
#locale ⇒ Object
-
#locale=(val) ⇒ Object
-
#method_missing(name, *args) ⇒ Object
-
#opf_xml ⇒ Object
-
#ordered ⇒ Object
-
#set_main_id(identifier, id = nil, type = nil) ⇒ Object
-
#set_version(val) ⇒ Object
-
#specify_cover_image(item) ⇒ Object
-
#spine_items ⇒ Object
-
#version ⇒ Object
-
#version=(val) ⇒ Object
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.
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/gepub/package.rb', line 70
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
@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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/gepub/package.rb', line 155
def method_missing(name, *args)
return @manifest.send(name.to_sym, *args) if [:item_by_href].member? name
Metadata::CONTENT_NODE_LIST.each {
|x|
case name.to_s
when x, "#{x}_list", "set_#{x}", "#{x}=", "add_#{x}"
return @metadata.send(name, *args)
end
}
super
end
|
Instance Attribute Details
#bindings ⇒ Object
Returns the value of attribute bindings.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def bindings
@bindings
end
|
#contents_prefix ⇒ Object
Returns the value of attribute contents_prefix.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def contents_prefix
@contents_prefix
end
|
#epub_backward_compat ⇒ Object
Returns the value of attribute epub_backward_compat.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def epub_backward_compat
@epub_backward_compat
end
|
#manifest ⇒ Object
Returns the value of attribute manifest.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def manifest
@manifest
end
|
Returns the value of attribute metadata.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def metadata
@metadata
end
|
#path ⇒ Object
Returns the value of attribute path.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def path
@path
end
|
#spine ⇒ Object
Returns the value of attribute spine.
8
9
10
|
# File 'lib/gepub/package.rb', line 8
def spine
@spine
end
|
Class Method Details
.parse_opf(opf, path) ⇒ Object
parse OPF data. opf should be io or string object.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/gepub/package.rb', line 54
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"))
}
}
end
|
Instance Method Details
#[](x) ⇒ Object
99
100
101
|
# File 'lib/gepub/package.rb', line 99
def [](x)
@attributes[x]
end
|
#[]=(k, v) ⇒ Object
103
104
105
|
# File 'lib/gepub/package.rb', line 103
def []=(k,v)
@attributes[k] = v
end
|
#add_item(href, io_or_filename = nil, id = nil, attributes = {}) {|item| ... } ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/gepub/package.rb', line 121
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
136
137
138
139
140
141
142
|
# File 'lib/gepub/package.rb', line 136
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
|
#author ⇒ Object
172
173
174
175
|
# File 'lib/gepub/package.rb', line 172
def author
warn '#author is deprecated. please use #creator'
@metadata.creator
end
|
#author=(val) ⇒ Object
167
168
169
170
|
# File 'lib/gepub/package.rb', line 167
def author=(val)
warn 'author= is deprecated. please use #creator'
@metadata.creator= val
end
|
#epub_version ⇒ Object
212
213
214
215
|
# File 'lib/gepub/package.rb', line 212
def epub_version
warn 'epub_version is deprecated. please use #version'
version
end
|
#epub_version=(val) ⇒ Object
207
208
209
210
|
# File 'lib/gepub/package.rb', line 207
def epub_version=(val)
warn 'epub_version= is deprecated. please use #version='
@attributes['version'] = val
end
|
#identifier ⇒ Object
108
109
110
|
# File 'lib/gepub/package.rb', line 108
def identifier
@metadata.identifier_by_id(unique_identifier)
end
|
#identifier=(identifier) ⇒ Object
112
113
114
|
# File 'lib/gepub/package.rb', line 112
def identifier=(identifier)
set_main_id(identifier, nil, 'URL')
end
|
#items ⇒ Object
151
152
153
|
# File 'lib/gepub/package.rb', line 151
def items
@manifest.item_list
end
|
#locale ⇒ Object
187
188
189
190
|
# File 'lib/gepub/package.rb', line 187
def locale
warn '#locale is deprecated. please use #language'
@metadata.language
end
|
#locale=(val) ⇒ Object
182
183
184
185
|
# File 'lib/gepub/package.rb', line 182
def locale=(val)
warn 'locale= is deprecated. please use #language='
@metadata.language = val
end
|
#opf_xml ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/gepub/package.rb', line 217
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
builder = Nokogiri::XML::Builder.new {
|xml|
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
|
#ordered ⇒ Object
129
130
131
132
133
134
|
# File 'lib/gepub/package.rb', line 129
def ordered
raise 'need block.' if !block_given?
@ordered = true
yield
@ordered = nil
end
|
#set_main_id(identifier, id = nil, type = nil) ⇒ Object
116
117
118
119
|
# File 'lib/gepub/package.rb', line 116
def set_main_id(identifier, id = nil, type = nil)
set_unique_identifier(id || @id_pool.generate_key(:prefix => 'BookId', :without_count => true))
@metadata.add_identifier identifier, unique_identifier, type
end
|
#set_version(val) ⇒ Object
196
197
198
199
200
201
|
# File 'lib/gepub/package.rb', line 196
def set_version(val)
@attributes['version'] = val
@metadata.opf_version = val
@manifest.opf_version = val
@spine.opf_version = val
end
|
#specify_cover_image(item) ⇒ Object
177
178
179
180
|
# File 'lib/gepub/package.rb', line 177
def specify_cover_image(item)
warn 'specify_cover_image is deprecated. please use Item#cover_image'
item.cover_image
end
|
#spine_items ⇒ Object
144
145
146
147
148
149
|
# File 'lib/gepub/package.rb', line 144
def spine_items
spine.itemref_list.map {
|itemref|
@manifest.item_list[itemref.idref]
}
end
|
#version ⇒ Object
192
193
194
|
# File 'lib/gepub/package.rb', line 192
def version
@attributes['version']
end
|
#version=(val) ⇒ Object
203
204
205
|
# File 'lib/gepub/package.rb', line 203
def version=(val)
set_version(val)
end
|