Class: Repub::Epub::Content
Defined Under Namespace
Classes: ContentItem, Metadata
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(uid) ⇒ Content
Returns a new instance of Content.
9
10
11
12
13
14
15
16
17
|
# File 'lib/repub/epub/content.rb', line 9
def initialize(uid)
@metadata = Metadata.new('Untitled', 'en', uid, Date.today.to_s)
@css_counter = 0
@img_counter = 0
@html_counter = 0
@manifest_items = []
@spine_items = []
@manifest_items << ContentItem.new('ncx', 'toc.ncx', 'application/x-dtbncx+xml')
end
|
Instance Attribute Details
Returns the value of attribute metadata.
33
34
35
|
# File 'lib/repub/epub/content.rb', line 33
def metadata
@metadata
end
|
Instance Method Details
#add_document(href, id = nil) ⇒ Object
59
60
61
62
63
|
# File 'lib/repub/epub/content.rb', line 59
def add_document(href, id = nil)
manifest_item = ContentItem.new(id || "item_#{@html_counter += 1}", href, 'application/xhtml+xml')
@manifest_items << manifest_item
@spine_items << manifest_item
end
|
#add_image(href, id = nil) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/repub/epub/content.rb', line 43
def add_image(href, id = nil)
image_type = case(href.strip.downcase)
when /.*\.(jpeg|jpg)$/
'image/jpeg'
when /.*\.png$/
'image/png'
when /.*\.gif$/
'image/gif'
when /.*\.svg$/
'image/svg+xml'
else
raise 'Unsupported image type'
end
@manifest_items << ContentItem.new(id || "img_#{@img_counter += 1}", href, image_type)
end
|
#add_page_template(href = 'page-template.xpgt', id = 'pt') ⇒ Object
35
36
37
|
# File 'lib/repub/epub/content.rb', line 35
def add_page_template(href = 'page-template.xpgt', id = 'pt')
@manifest_items << ContentItem.new(id, href, 'application/vnd.adobe-page-template+xml')
end
|
#add_stylesheet(href, id = nil) ⇒ Object
39
40
41
|
# File 'lib/repub/epub/content.rb', line 39
def add_stylesheet(href, id = nil)
@manifest_items << ContentItem.new(id || "css_#{@css_counter += 1}", href, 'text/css')
end
|
#save(path = 'content.opf') ⇒ Object
79
80
81
82
83
|
# File 'lib/repub/epub/content.rb', line 79
def save(path = 'content.opf')
File.open(path, 'w') do |f|
f << to_xml
end
end
|
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/repub/epub/content.rb', line 65
def to_xml
out = ''
builder = Builder::XmlMarkup.new(:target => out, :indent => 4)
builder.instruct!
builder.package :xmlns => "http://www.idpf.org/2007/opf",
'unique-identifier' => "dcidid",
'version' => "2.0" do
metadata_to_xml(builder)
manifest_to_xml(@manifest_items, builder)
spine_to_xml(@spine_items, builder)
end
out
end
|