Class: GEPUB::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/gepub/item.rb

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



21
22
23
24
25
26
27
28
29
30
# File 'lib/gepub/item.rb', line 21

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'] =  guess_mediatype if media_type.nil?
  @parent = parent
  @parent.register_item(self) unless @parent.nil?
  self
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



9
10
11
# File 'lib/gepub/item.rb', line 9

def content
  @content
end

Class Method Details

.create(parent, attributes = {}) ⇒ Object



10
11
12
13
# File 'lib/gepub/item.rb', line 10

def self.create(parent, attributes = {})
  Item.new(attributes['id'], attributes['href'], attributes['media-type'], parent,
           attributes.reject { |k,v| ['id','href','media-type'].member?(k) })
end

Instance Method Details

#[](x) ⇒ Object



49
50
51
# File 'lib/gepub/item.rb', line 49

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

#[]=(x, y) ⇒ Object



53
54
55
# File 'lib/gepub/item.rb', line 53

def []=(x,y)
  @attributes[x] = y
end

#add_content(io_or_filename) ⇒ Object

add content form io or file to the item



114
115
116
117
118
119
120
121
122
123
# File 'lib/gepub/item.rb', line 114

def add_content(io_or_filename)
  io = io_or_filename
  if io_or_filename.class == String
    io = File.new(io_or_filename)
  end
  io.binmode
  @content = io.read
  guess_content_property
  self
end

#add_property(property) ⇒ Object

add value to properties attribute.



58
59
60
61
# File 'lib/gepub/item.rb', line 58

def add_property(property)
  (@attributes['properties'] ||=[]) << property
  self
end

#add_raw_content(data) ⇒ Object

add content data to the item.



108
109
110
111
# File 'lib/gepub/item.rb', line 108

def add_raw_content(data)
  @content = data
  guess_content_property
end

#cover_imageObject

set ‘cover-image’ property to the Item. On generating EPUB, EPUB2-style cover image meta item will be added.



65
66
67
# File 'lib/gepub/item.rb', line 65

def cover_image
  add_property('cover-image')
end

#guess_content_propertyObject

guess and set content property from contents.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gepub/item.rb', line 75

def guess_content_property
  if File.extname(self.href) =~ /.x?html/
    @attributes['properties'] = (@attributes['properties'] || []).reject {
      |x| x == 'svg' || x == 'mathml' || x == 'switch' || x == 'remote-resources'
    }
    parsed = Nokogiri::XML::Document.parse(@content)
    ns_prefix =  parsed.namespaces.invert['http://www.w3.org/1999/xhtml']
    if ns_prefix.nil?
      prefix = ''
    else
      prefix = "#{ns_prefix}:"
    end
    videos = parsed.xpath("//#{prefix}video[starts-with(@src,'http')]")
    audios = parsed.xpath("//#{prefix}audio[starts-with(@src,'http')]")
    if 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
    if parsed.xpath("//#{prefix}script").size > 0
      self.add_property('scripted')
    end
  end
end

#guess_mediatypeObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gepub/item.rb', line 140

def guess_mediatype
  case File.extname(href)
  when /.(html|xhtml)/i
    'application/xhtml+xml'
  when /.css/i
    'text/css'
  when /.js/i
    'text/javascript'
  when /.(jpg|jpeg)/i
    'image/jpeg'
  when /.png/i
    'image/png'
  when /.gif/i
    'image/gif'
  when /.svg/i
    'image/svg+xml'
  when /.opf/i
    'application/oebps-package+xml'
  when /.ncx/i
    'application/x-dtbncx+xml'
  when /.(otf|ttf|ttc)/i
    'application/vnd.ms-opentype'
  when /.woff/i
    'application/font-woff'
  end
end

#itemidObject

get item’s id



40
41
42
# File 'lib/gepub/item.rb', line 40

def itemid
  id
end

#mediatypeObject

get mediatype of the item.



45
46
47
# File 'lib/gepub/item.rb', line 45

def mediatype
  media_type
end

set ‘nav’ property to the Item.



70
71
72
# File 'lib/gepub/item.rb', line 70

def nav
  add_property('nav')
end

#to_xml(builder, opf_version = '3.0') ⇒ Object

generate xml to supplied Nokogiri builder.



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gepub/item.rb', line 126

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