Class: Epub::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/epub-reader/package.rb

Instance Method Summary collapse

Constructor Details

#initialize(rootfile, file) ⇒ Package

Returns a new instance of Package.



4
5
6
7
8
9
# File 'lib/epub-reader/package.rb', line 4

def initialize(rootfile, file)
  @rootfile = rootfile
  @file     = file
  @package  = get_package_content(file)
  @xml = Nokogiri::XML(@package).remove_namespaces!
end

Instance Method Details

#audiosObject



98
99
100
# File 'lib/epub-reader/package.rb', line 98

def audios
  resources.select{|resource| resource.attr('media-type').to_s.match(/^audio\/(mpeg|mp4)/)}
end

#contributorObject

TODO: equal to creator



55
56
57
# File 'lib/epub-reader/package.rb', line 55

def contributor
  contributors.size > 0 ? contributors.first.text : ""
end

#coverObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/epub-reader/package.rb', line 109

def cover
  begin
    cover_meta     = .css('[name="cover"]')
    meta_content   = cover_meta.size == 1 ? cover_meta.attr('content') : nil
    cover_content  = meta_content || manifest.css('[properties="cover-image"]').attr('id').to_s
    cover_path     = (cover_content.to_s.match(/\.(gif|jpe?g|png)/) ? cover_content : resources.css("##{cover_content}").attr('href').to_s)
    if cover_exist?(relative_content_path + cover_path)
      relative_content_path + cover_path
    elsif cover_exist?(relative_content_path + "Images/" + cover_path)
      relative_content_path + "Images/" + cover_path
    else
      ""
    end
  rescue
    ""
  end
end

#creatorObject

TODO: identify role TODO: identify file-as TODO: identify alternate-script TODO: identify display-seq



50
51
52
# File 'lib/epub-reader/package.rb', line 50

def creator
  creators.size > 0 ? creators.first.text : ""
end

#dateObject



59
60
61
62
# File 'lib/epub-reader/package.rb', line 59

def date
  d = .css('data')
  d.size > 0 ? d.text : ""
end

#fontsObject



94
95
96
# File 'lib/epub-reader/package.rb', line 94

def fonts
  resources.select{|resource| resource.attr('media-type').to_s.match(/application\/(vnd\.ms-opentype|font-woff)/)}
end

#htmlObject



82
83
84
# File 'lib/epub-reader/package.rb', line 82

def html
  resources.css('[media-type="application/xhtml+xml"]')
end

#identifierObject



32
33
34
# File 'lib/epub-reader/package.rb', line 32

def identifier
  identifiers.css("[id=#{unique_identifier}]").text
end

#imagesObject



78
79
80
# File 'lib/epub-reader/package.rb', line 78

def images
  resources.select{|resource| resource.attr('media-type').to_s.match(/^image\/(gif|jpeg|svg\+xml)/)}
end

#javascriptsObject



90
91
92
# File 'lib/epub-reader/package.rb', line 90

def javascripts
  resources.css('[media-type="text/javascript"]')
end

#languageObject



42
43
44
# File 'lib/epub-reader/package.rb', line 42

def language
  languages.first.text
end

#mediatypeObject



24
25
26
# File 'lib/epub-reader/package.rb', line 24

def mediatype
  @rootfile.attr('media-type')
end

#pathObject



15
16
17
# File 'lib/epub-reader/package.rb', line 15

def path
  @rootfile.attr('full-path').to_s
end

#rawObject



11
12
13
# File 'lib/epub-reader/package.rb', line 11

def raw
  @package.to_s
end

#reading_orderObject

TODO: to parse guide [optional/deprecated] bindings [optional]



131
132
133
134
135
136
# File 'lib/epub-reader/package.rb', line 131

def reading_order
  spine_items.map do |item|
    item_id = item.attr('idref').to_s
    manifest.css("##{item_id}") if item_id
  end
end

#relative_content_pathObject



19
20
21
22
# File 'lib/epub-reader/package.rb', line 19

def relative_content_path
  i   = path.rindex('/').to_i
  i > 0 ? path[0,i+1] : ""
end

#resourcesObject



74
75
76
# File 'lib/epub-reader/package.rb', line 74

def resources
  manifest.css('item')
end

#sourceObject



64
65
66
67
# File 'lib/epub-reader/package.rb', line 64

def source
  s = .css('source')
  s.size > 0 ? s.text : ""
end

#stylesheetsObject



86
87
88
# File 'lib/epub-reader/package.rb', line 86

def stylesheets
  resources.css('[media-type="text/css"]')
end

#titleObject

TODO: identify language TODO: identify subtitles



38
39
40
# File 'lib/epub-reader/package.rb', line 38

def title
  titles.first.text
end

#tocObject



102
103
104
105
106
107
# File 'lib/epub-reader/package.rb', line 102

def toc
  toc_item_id       = spine.attr("toc")
  toc_item_mimetype = "application/x-dtbncx+xml"
  toc_item_selector = toc_item_id ? "##{toc_item_id.to_s}" : '[media-type="#{toc_item_mimetype}"]'
  relative_content_path + resources.css(toc_item_selector).attr('href').to_s
end

#typeObject



69
70
71
72
# File 'lib/epub-reader/package.rb', line 69

def type
  t = .css('type')
  t.size > 0 ? t.text : ""
end

#versionObject



28
29
30
# File 'lib/epub-reader/package.rb', line 28

def version
  root.attr('version').to_s.to_i
end