Class: GEPUB::Item
Overview
an Object to hold metadata and content of item in manifest.
following methods are created dynamically. #id, #id=, #set_id, #href, #href=, #set_href, #media_type, #media_type=, #set_media_type, #fallback, #fallback=, #set_fallback, #media_overlay, #media_overlay=, #set_media_overlay
Constant Summary collapse
- ATTRIBUTES =
['id', 'href', 'media-type', 'fallback', 'properties', 'media-overlay'].each { |name| methodbase = name.sub('-','_') define_method(methodbase + '=') { |val| @attributes[name] = val } define_method('set_' + methodbase) { |val| @attributes[name] = val; self } define_method(methodbase) { @attributes[name] } }
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
get
attribute
. -
#[]=(attribute, value) ⇒ Object
set
attribute
. -
#add_content(io_or_filename) ⇒ Object
add content from io or file to the item.
- #add_content_io(io) ⇒ Object
-
#add_property(property) ⇒ Object
add value to properties attribute.
-
#add_raw_content(data) ⇒ Object
add content data to the item.
-
#cover_image ⇒ Object
set ‘cover-image’ property to the Item.
-
#guess_content_property ⇒ Object
guess and set content property from contents.
-
#initialize(itemid, itemhref, itemmediatype = nil, parent = nil, attributes = {}) ⇒ Item
constructor
create Item.
-
#is_handler_of(media_type) ⇒ Object
set bindings: item is a handler for media_type.
-
#itemid ⇒ Object
get item’s id.
- #landmark(type:, title:, id: nil) ⇒ Object
-
#mediatype ⇒ Object
get mediatype of the item.
-
#nav ⇒ Object
set ‘nav’ property to the Item.
-
#to_xml(builder, opf_version = '3.0') ⇒ Object
generate xml to supplied Nokogiri builder.
-
#toc_text(text) ⇒ Object
set toc text to the item.
-
#toc_text_with_id(text, toc_id) ⇒ Object
set toc text with id to the item.
Methods included from InspectMixin
Constructor Details
#initialize(itemid, itemhref, itemmediatype = nil, parent = nil, attributes = {}) ⇒ Item
create Item.
if mediatype is not specified, it will be guessed from extension name. Item can’t guess media type for videos and audios, so you should specify one.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gepub/item.rb', line 23 def initialize(itemid, itemhref, itemmediatype = nil, parent = nil, attributes = {}) if attributes['properties'].class == String attributes['properties'] = attributes['properties'].split(' ') end @attributes = {'id' => itemid, 'href' => itemhref, 'media-type' => itemmediatype}.merge(attributes) @attributes['media-type'] = GEPUB::Mime.guess_mediatype(itemhref) if media_type.nil? @parent = parent @parent.register_item(self) unless @parent.nil? self end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
11 12 13 |
# File 'lib/gepub/item.rb', line 11 def content @content end |
Class Method Details
Instance Method Details
#[](attribute) ⇒ Object
get attribute
52 53 54 |
# File 'lib/gepub/item.rb', line 52 def [](attribute) @attributes[attribute] end |
#[]=(attribute, value) ⇒ Object
set attribute
57 58 59 |
# File 'lib/gepub/item.rb', line 57 def []=(attribute, value) @attributes[attribute] = value end |
#add_content(io_or_filename) ⇒ Object
add content from io or file to the item
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/gepub/item.rb', line 146 def add_content(io_or_filename) if io_or_filename.class == String File.open(io_or_filename, mode='r') do |f| add_content_io f end else add_content_io io_or_filename end self end |
#add_content_io(io) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/gepub/item.rb', line 157 def add_content_io(io) io.binmode @content = io.read if File.extname(self.href) =~ /x?html$/ @content.force_encoding('utf-8') end guess_content_property self end |
#add_property(property) ⇒ Object
add value to properties attribute.
62 63 64 65 |
# File 'lib/gepub/item.rb', line 62 def add_property(property) (@attributes['properties'] ||=[]) << property self end |
#add_raw_content(data) ⇒ Object
add content data to the item.
136 137 138 139 140 141 142 143 |
# File 'lib/gepub/item.rb', line 136 def add_raw_content(data) @content = data if File.extname(self.href) =~ /x?html$/ @content.force_encoding('utf-8') end guess_content_property self end |
#cover_image ⇒ Object
set ‘cover-image’ property to the Item. On generating EPUB, EPUB2-style cover image meta item will be added.
69 70 71 |
# File 'lib/gepub/item.rb', line 69 def cover_image add_property('cover-image') end |
#guess_content_property ⇒ Object
guess and set content property from contents.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/gepub/item.rb', line 102 def guess_content_property if File.extname(self.href) =~ /.x?html/ && @attributes['media-type'] === 'application/xhtml+xml' @attributes['properties'] ||= [] parsed = Nokogiri::XML::Document.parse(@content) return unless parsed.root.node_name === "html" ns_prefix = parsed.namespaces.invert['http://www.w3.org/1999/xhtml'] if ns_prefix.nil? prefix = '' else prefix = "#{ns_prefix}:" end images = parsed.xpath("//#{prefix}img[starts-with(@src,'http')]") videos = parsed.xpath("//#{prefix}video[starts-with(@src,'http')]") + parsed.xpath("//#{prefix}video/#{prefix}source[starts-with(@src,'http')]") audios = parsed.xpath("//#{prefix}audio[starts-with(@src,'http')]") + parsed.xpath("//#{prefix}audio/#{prefix}source[starts-with(@src,'http')]") if images.size > 0 || videos.size > 0 || audios.size > 0 self.add_property('remote-resources') end if parsed.xpath("//p:math", { 'p' => 'http://www.w3.org/1998/Math/MathML' }).size > 0 self.add_property('mathml') end if parsed.xpath("//s:svg", { 's' => 'http://www.w3.org/2000/svg' }).size > 0 self.add_property('svg') end if parsed.xpath("//epub:switch", { 'epub' => 'http://www.idpf.org/2007/ops' }).size > 0 self.add_property('switch') end scripts = parsed.xpath("//#{prefix}script") + parsed.xpath("//#{prefix}form") if scripts.size > 0 self.add_property('scripted') end end end |
#is_handler_of(media_type) ⇒ Object
set bindings: item is a handler for media_type
91 92 93 94 |
# File 'lib/gepub/item.rb', line 91 def is_handler_of media_type bindings.add(self.id, media_type) self end |
#itemid ⇒ Object
get item’s id
42 43 44 |
# File 'lib/gepub/item.rb', line 42 def itemid id end |
#landmark(type:, title:, id: nil) ⇒ Object
96 97 98 99 |
# File 'lib/gepub/item.rb', line 96 def landmark(type:, title:, id: nil) landmarks.push(:type => type, :title => title, :item => self, :id => id) self end |
#mediatype ⇒ Object
get mediatype of the item.
47 48 49 |
# File 'lib/gepub/item.rb', line 47 def mediatype media_type end |
#nav ⇒ Object
set ‘nav’ property to the Item.
74 75 76 |
# File 'lib/gepub/item.rb', line 74 def nav add_property('nav') end |
#to_xml(builder, opf_version = '3.0') ⇒ Object
generate xml to supplied Nokogiri builder.
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/gepub/item.rb', line 168 def to_xml(builder, opf_version = '3.0') attr = @attributes.dup if opf_version.to_f < 3.0 attr.reject!{ |k,_v| k == 'properties' } end if !attr['properties'].nil? attr['properties'] = attr['properties'].uniq.join(' ') if attr['properties'].size == 0 attr.delete 'properties' end end builder.item(attr) end |
#toc_text(text) ⇒ Object
set toc text to the item
79 80 81 82 |
# File 'lib/gepub/item.rb', line 79 def toc_text text toc.push(:item => self, :text => text, :id => nil) self end |
#toc_text_with_id(text, toc_id) ⇒ Object
set toc text with id to the item
85 86 87 88 |
# File 'lib/gepub/item.rb', line 85 def toc_text_with_id text, toc_id toc.push(:item => self, :text => text, :id => toc_id) self end |