Class: Repub::Epub::Content
Defined Under Namespace
Classes: ContentItem, Metadata
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
- #add_item(href, id = nil) ⇒ Object
-
#initialize(uid) ⇒ Content
constructor
A new instance of Content.
- #save(path = 'content.opf') ⇒ Object
-
#to_xml ⇒ Object
def add_stylesheet(href, id = nil) @manifest_items << ContentItem.new(id || “css_#+= 1”, href, ‘text/css’) end.
Constructor Details
#initialize(uid) ⇒ Content
Returns a new instance of Content.
9 10 11 12 13 14 |
# File 'lib/repub/epub/content.rb', line 9 def initialize(uid) @metadata = Metadata.new('Untitled', 'en', uid, Date.today.to_s) @manifest_items = [] @spine_items = [] @manifest_items << ContentItem.new('ncx', 'toc.ncx') end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
30 31 32 |
# File 'lib/repub/epub/content.rb', line 30 def @metadata end |
Instance Method Details
#add_item(href, id = nil) ⇒ Object
32 33 34 35 36 |
# File 'lib/repub/epub/content.rb', line 32 def add_item(href, id = nil) item = ContentItem.new(id || "item_#{@manifest_items.size}", href) @manifest_items << item @spine_items << item if item.document? end |
#save(path = 'content.opf') ⇒ Object
78 79 80 81 82 |
# File 'lib/repub/epub/content.rb', line 78 def save(path = 'content.opf') File.open(path, 'w') do |f| f << to_xml end end |
#to_xml ⇒ Object
def add_stylesheet(href, id = nil)
@manifest_items << ContentItem.new(id || "css_#{@css_counter += 1}", href, 'text/css')
end
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
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
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/repub/epub/content.rb', line 64 def to_xml out = '' builder = Builder::XmlMarkup.new(:target => out) builder.instruct! builder.package :xmlns => "http://www.idpf.org/2007/opf", 'unique-identifier' => "dcidid", 'version' => "2.0" do (builder) manifest_to_xml(@manifest_items, builder) spine_to_xml(@spine_items, builder) end out end |