ruby-epub 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README +0 -0
  2. data/lib/ruby-epub.rb +118 -0
  3. metadata +57 -0
data/README ADDED
File without changes
@@ -0,0 +1,118 @@
1
+ require 'zip/zip'
2
+ require 'nokogiri'
3
+
4
+ class Epub
5
+ attr_accessor :title, :language, :publisher, :date, :rights, :creator, :description
6
+
7
+ def initialize(filename)
8
+ @zip = Zip::ZipFile.open(filename)
9
+ opf = get_opf(@zip)
10
+ get_metadata()
11
+ end
12
+
13
+ def get_opf(zipfile)
14
+ container_file = zipfile.get_input_stream("META-INF/container.xml")
15
+ container = Nokogiri::XML container_file
16
+ opf_path = container.at_css("rootfiles rootfile")['full-path']
17
+ tab_path = opf_path.split('/')
18
+ @base_path = ''
19
+ if tab_path.size > 1
20
+ @base_path = tab_path[0] + '/'
21
+ end
22
+ opf_file = zipfile.get_input_stream(opf_path)
23
+ @opf = Nokogiri::XML opf_file
24
+ @opf.remove_namespaces!
25
+ end
26
+
27
+ def get_metadata()
28
+ if @opf.search("//metadata/title")
29
+ @title = @opf.search("//metadata/title").first.content
30
+ end
31
+ if @opf.search("//metadata/language") && @opf.search("//metadata/language").first
32
+ @language = @opf.search("//metadata/language").first.content
33
+ end
34
+ if @opf.search("//metadata/publisher") && @opf.search("//metadata/publisher").first
35
+ @publisher = @opf.search("//metadata/publisher").first.content
36
+ end
37
+ if @opf.search("//metadata/date") && @opf.search("//metadata/date").first
38
+ @date = @opf.search("//metadata/date").first.content
39
+ end
40
+ if @opf.search("//metadata/rights") && @opf.search("//metadata/rights").first
41
+ @rights = @opf.search("//metadata/rights").first.content
42
+ end
43
+ if @opf.search("//metadata/creator") && @opf.search("//metadata/creator").first
44
+ @creator = @opf.search("//metadata/creator").first.content
45
+ end
46
+ if @opf.search("//metadata/description") && @opf.search("//metadata/description").first
47
+ @description = @opf.search("//metadata/description").first.content
48
+ end
49
+ end
50
+
51
+ def cover_image
52
+ content, img_name = cover_by_cover_id
53
+ if !content
54
+ content, img_name = cover_by_meta_cover
55
+ elsif !content
56
+ content, img_name = cover_image_by_html
57
+ end
58
+
59
+ if content
60
+ temp = Tempfile.new(['book_cover', File.extname(img_name)])
61
+ temp.binmode
62
+ temp.write(content.to_blob)
63
+ temp.flush
64
+ if(temp.size > 0)
65
+ return File.new(temp.path)
66
+ else
67
+ return nil
68
+ end
69
+ end
70
+ end
71
+
72
+ def cover_image_by_html
73
+ cover_item = @opf.search("//package/guide/reference[@type='cover']")
74
+ if cover_item && cover_item.first
75
+ cover_url = cover_item.first['href']
76
+ doc_cover = Nokogiri::HTML @zip.get_input_stream(@base_path + cover_url)
77
+ tab_path = cover_url.split('/')
78
+ html_path = ''
79
+ if tab_path.size > 1
80
+ html_path = tab_path[0] + '/'
81
+ end
82
+ img_src = doc_cover.xpath('//img').first
83
+ begin
84
+ @image_cover = @zip.get_input_stream(@base_path + html_path + img_src['src']) {|f| f.read}
85
+ return [@image_cover, img_src['src']]
86
+ rescue
87
+ return nil
88
+ end
89
+ end
90
+ end
91
+
92
+ def cover_by_meta_cover
93
+ img_id = @opf.search("//meta[@name='cover']")
94
+ if img_id && img_id.first
95
+ img_item = @opf.search("//manifest/item[@id='"+ img_id.first['content'] + "']")
96
+ if img_item && img_item.first
97
+ img_url = @base_path + img_item.first['href']
98
+ @image_cover = @zip.get_input_stream(img_url) {|f| f.read}
99
+ return [@image_cover, img_item.first['href']]
100
+ else
101
+ return nil
102
+ end
103
+ end
104
+ end
105
+
106
+ def cover_by_cover_id
107
+ img_item = @opf.search("//manifest/item[@id='cover']")
108
+ if img_item && img_item.first
109
+ img_url = img_item.first['href']
110
+ @image_cover = @zip.get_input_stream(img_url) {|f| f.read}
111
+ return [@image_cover, img_item.first['href']]
112
+ else
113
+ return nil
114
+ end
115
+ end
116
+
117
+
118
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-epub
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - banux
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-03 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rubyzip2
16
+ requirement: &73436840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *73436840
25
+ description: Extract epub metada
26
+ email: banux@helheim.net
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - README
32
+ - lib/ruby-epub.rb
33
+ homepage:
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.17
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Extract epub metada
57
+ test_files: []