epub-parser 0.1.1 → 0.1.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.
- data/README.markdown +2 -0
- data/Rakefile +23 -21
- data/bin/epubinfo +3 -3
- data/epub-parser.gemspec +6 -2
- data/features/epubinfo.feature +6 -0
- data/features/step_definitions/epubinfo_steps.rb +5 -0
- data/features/support/env.rb +1 -0
- data/lib/epub/parser/content_document.rb +2 -2
- data/lib/epub/parser/publication.rb +0 -1
- data/lib/epub/parser/version.rb +1 -1
- data/lib/epub/publication/package/manifest.rb +1 -1
- data/lib/epub/publication/package/metadata.rb +19 -8
- data/test/fixtures/book/OPS/%E6%97%A5%E6%9C%AC%E8%AA%9E.xhtml +10 -0
- data/test/fixtures/book/OPS/containing space.xhtml +10 -0
- data/test/fixtures/book/OPS/containing%20space.xhtml +10 -0
- data/test/fixtures/book/OPS//343/203/253/343/203/274/343/203/210/343/203/225/343/202/241/343/202/244/343/203/253.opf +14 -0
- data/test/fixtures/book/OPS//346/227/245/346/234/254/350/252/236.xhtml +10 -0
- data/test/test_parser_publication.rb +19 -6
- data/test/test_publication.rb +16 -0
- data/wiki/Home.md +114 -0
- data/wiki/Item.md +80 -0
- metadata +60 -41
data/README.markdown
CHANGED
data/Rakefile
CHANGED
@@ -12,18 +12,22 @@ task :test => 'test:default'
|
|
12
12
|
namespace :test do
|
13
13
|
task :default => [:build, :test]
|
14
14
|
|
15
|
+
desc 'Run all tests'
|
16
|
+
task :all => [:build, :test, :cucumber]
|
17
|
+
|
18
|
+
desc 'Build test fixture EPUB file'
|
19
|
+
task :build => :clean do
|
20
|
+
input_dir = 'test/fixtures/book'
|
21
|
+
sh "epzip #{input_dir}"
|
22
|
+
end
|
23
|
+
|
15
24
|
Rake::TestTask.new do |task|
|
16
25
|
task.test_files = FileList['test/**/test_*.rb']
|
17
26
|
task.warning = true
|
18
27
|
task.options = '--no-show-detail-immediately --verbose'
|
19
28
|
end
|
20
29
|
|
21
|
-
|
22
|
-
task :build do
|
23
|
-
input_dir = 'test/fixtures/book'
|
24
|
-
FileList["#{input_dir}/**/*"]
|
25
|
-
sh "epzip #{input_dir}"
|
26
|
-
end
|
30
|
+
Cucumber::Rake::Task.new
|
27
31
|
end
|
28
32
|
|
29
33
|
|
@@ -31,21 +35,19 @@ YARD::Rake::YardocTask.new do |task|
|
|
31
35
|
task.files = %w[- wiki/*.md]
|
32
36
|
end
|
33
37
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
end
|
38
|
+
namespace :gem do
|
39
|
+
desc "Build epub-parser-#{EPUB::Parser::VERSION}.gem into the pkg directory."
|
40
|
+
task :build => :yard do
|
41
|
+
Bundler::GemHelper.new.build_gem
|
42
|
+
end
|
40
43
|
|
41
|
-
desc "Build and install epub-parser-#{EPUB::Parser::VERSION}.gem into system gems."
|
42
|
-
task :install => :yard do
|
43
|
-
|
44
|
-
end
|
44
|
+
desc "Build and install epub-parser-#{EPUB::Parser::VERSION}.gem into system gems."
|
45
|
+
task :install => :yard do
|
46
|
+
Bundler::GemHelper.new.install_gem
|
47
|
+
end
|
45
48
|
|
46
|
-
desc "Create tag v#{EPUB::Parser::VERSION} and build and push epub-parser-#{EPUB::Parser::VERSION}.gem to Rubygems"
|
47
|
-
task :release => :yard do
|
48
|
-
|
49
|
+
desc "Create tag v#{EPUB::Parser::VERSION} and build and push epub-parser-#{EPUB::Parser::VERSION}.gem to Rubygems"
|
50
|
+
task :release => :yard do
|
51
|
+
Bundler::GemHelper.new.release_gem
|
52
|
+
end
|
49
53
|
end
|
50
|
-
|
51
|
-
Cucumber::Rake::Task.new
|
data/bin/epubinfo
CHANGED
@@ -26,10 +26,10 @@ unless file
|
|
26
26
|
abort
|
27
27
|
end
|
28
28
|
|
29
|
-
book = EPUB::Parser.parse
|
29
|
+
book = EPUB::Parser.parse(file)
|
30
30
|
data = {'Title' => [book.title]}
|
31
|
-
data.merge!(book.
|
32
|
-
data['Unique identifier'] = [book.
|
31
|
+
data.merge!(book.metadata.to_hash)
|
32
|
+
data['Unique identifier'] = [book.metadata.unique_identifier]
|
33
33
|
data['EPUB Version'] = [book.package.version]
|
34
34
|
if options[:format] == :line
|
35
35
|
key_width = data.keys.map {|k| k.length}.max + 3
|
data/epub-parser.gemspec
CHANGED
@@ -16,9 +16,13 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.files = `git ls-files`.split("\n")
|
18
18
|
.push('test/fixtures/book/OPS/ルートファイル.opf')
|
19
|
+
.push('test/fixtures/book/OPS/日本語.xhtml')
|
20
|
+
.push(Dir['wiki/*.md'])
|
19
21
|
.push(Dir['doc/*'])
|
20
|
-
s.files.
|
21
|
-
|
22
|
+
s.files.reject! do |fn|
|
23
|
+
['"test/fixtures/book/OPS/\343\203\253\343\203\274\343\203\210\343\203\225\343\202\241\343\202\244\343\203\253.opf"', '"test/fixtures/book/OPS/\346\227\245\346\234\254\350\252\236.xhtml"'].include? fn
|
24
|
+
end
|
25
|
+
s.test_files = s.files & Dir['{test,spec,features}/**/*.{rb,feature}']
|
22
26
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
27
|
s.require_paths = ["lib"]
|
24
28
|
s.has_rdoc = 'yard'
|
@@ -0,0 +1,6 @@
|
|
1
|
+
Feature: We can see information about EPUB file
|
2
|
+
|
3
|
+
Scenario: See info about existing EPUB file
|
4
|
+
Given the file "test/fixtures/book.epub" exists
|
5
|
+
When I successfully run `bundle exec epubinfo /home/ikeda/ruby/projects/epub-parser/test/fixtures/book.epub`
|
6
|
+
Then the stdout should contain "The New French Cuisine Masters"
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'aruba/cucumber'
|
@@ -21,7 +21,7 @@ module EPUB
|
|
21
21
|
document.search('/xhtml:html/xhtml:body//xhtml:nav', EPUB::NAMESPACES).collect {|elem| parse_navigation elem}
|
22
22
|
end
|
23
23
|
|
24
|
-
# @param [Nokogiri::XML::Element]
|
24
|
+
# @param [Nokogiri::XML::Element] element nav element
|
25
25
|
# @return [EPUB::ContentDocument::Navigation::Nav] nav Nav object
|
26
26
|
def parse_navigation(element)
|
27
27
|
nav = EPUB::ContentDocument::Navigation::Nav.new
|
@@ -33,7 +33,7 @@ module EPUB
|
|
33
33
|
|
34
34
|
private
|
35
35
|
|
36
|
-
# @param [Nokogiri::XML::Element]
|
36
|
+
# @param [Nokogiri::XML::Element] element nav element
|
37
37
|
# @return [String] heading heading text
|
38
38
|
def find_heading(element)
|
39
39
|
heading = element.xpath('./xhtml:h1|xhtml:h2|xhtml:h3|xhtml:h4|xhtml:h5|xhtml:h6|xhtml:hgroup', EPUB::NAMESPACES).first
|
data/lib/epub/parser/version.rb
CHANGED
@@ -61,7 +61,7 @@ module EPUB
|
|
61
61
|
def read
|
62
62
|
rootfile = Addressable::URI.parse(manifest.package.book.ocf.container.rootfile.full_path)
|
63
63
|
Zip::Archive.open(manifest.package.book.epub_file) {|zip|
|
64
|
-
path = rootfile + href.request_uri
|
64
|
+
path = Addressable::URI.unescape(rootfile + href.normalize.request_uri)
|
65
65
|
zip.fopen(path.to_s).read
|
66
66
|
}
|
67
67
|
end
|
@@ -46,7 +46,7 @@ module EPUB
|
|
46
46
|
metas.select {|meta| meta.primary_expression?}
|
47
47
|
end
|
48
48
|
|
49
|
-
module
|
49
|
+
module Refinee
|
50
50
|
PROPERTIES = %w[ alternate-script display-seq file-as group-position identifier-type meta-auth role title-type ]
|
51
51
|
|
52
52
|
attr_writer :refiners
|
@@ -59,13 +59,13 @@ module EPUB
|
|
59
59
|
met = voc.gsub(/-/, '_')
|
60
60
|
attr_writer met
|
61
61
|
define_method met do
|
62
|
-
refiners.
|
62
|
+
refiners.selector {|refiner| refiner.property == voc}.first
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
67
|
class DCMES
|
68
|
-
include
|
68
|
+
include Refinee
|
69
69
|
|
70
70
|
attr_accessor :content, :id, :lang, :dir
|
71
71
|
|
@@ -85,14 +85,19 @@ module EPUB
|
|
85
85
|
end
|
86
86
|
|
87
87
|
class Meta
|
88
|
-
include
|
88
|
+
include Refinee
|
89
89
|
|
90
|
-
attr_accessor :property, :
|
90
|
+
attr_accessor :property, :id, :scheme, :content
|
91
|
+
attr_reader :refines
|
92
|
+
|
93
|
+
def refines=(refinee)
|
94
|
+
@refines = refinee
|
95
|
+
refinee.refiners << self
|
96
|
+
end
|
91
97
|
|
92
98
|
def refines?
|
93
99
|
! refines.nil?
|
94
100
|
end
|
95
|
-
|
96
101
|
alias subexpression? refines?
|
97
102
|
|
98
103
|
def primary_expression?
|
@@ -105,9 +110,15 @@ module EPUB
|
|
105
110
|
end
|
106
111
|
|
107
112
|
class Link
|
108
|
-
include
|
113
|
+
include Refinee
|
109
114
|
|
110
|
-
attr_accessor :href, :rel, :id, :
|
115
|
+
attr_accessor :href, :rel, :id, :media_type
|
116
|
+
attr_reader :refines
|
117
|
+
|
118
|
+
def refines=(refinee)
|
119
|
+
@refines = refinee
|
120
|
+
refinee.refiners << self
|
121
|
+
end
|
111
122
|
end
|
112
123
|
end
|
113
124
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE html>
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
|
4
|
+
<head>
|
5
|
+
<title>Containing Space</title>
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<p>This document contains space in file name.</p>
|
9
|
+
</body>
|
10
|
+
</html>
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE html>
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
|
4
|
+
<head>
|
5
|
+
<title>Containing%20Space</title>
|
6
|
+
</head>
|
7
|
+
<body>
|
8
|
+
<p>This document contains "%20" in file name.</p>
|
9
|
+
</body>
|
10
|
+
</html>
|
@@ -88,11 +88,25 @@
|
|
88
88
|
<item id="external-css"
|
89
89
|
href="http://example.net/stylesheets/common.css"
|
90
90
|
media-type="text/css"/>
|
91
|
+
<item id="containing-space"
|
92
|
+
href="containing space.xhtml"
|
93
|
+
media-type="application/xhtml+xml"/>
|
94
|
+
<item id="containing-encoded-space"
|
95
|
+
href="containing%20space.xhtml"
|
96
|
+
media-type="application/xhtml+xml"/>
|
97
|
+
<item id="japanese-filename"
|
98
|
+
href="日本語.xhtml"
|
99
|
+
media-type="application/xhtml+xml"/>
|
100
|
+
<item id="encoded-japanese-filename"
|
101
|
+
href="%E6%97%A5%E6%9C%AC%E8%AA%9E.xhtml"
|
102
|
+
media-type="application/xhtml+xml"/>
|
91
103
|
</manifest>
|
92
104
|
<spine>
|
93
105
|
<itemref idref="nav"/>
|
94
106
|
<itemref idref="manifest-item-1"/>
|
95
107
|
<itemref idref="manifest-item-2"/>
|
108
|
+
<itemref idref="containing-encoded-space"/>
|
109
|
+
<itemref idref="encoded-japanese-filename"/>
|
96
110
|
</spine>
|
97
111
|
<guide>
|
98
112
|
<reference type="cover" href="cover.xhtml#start" title="カバー"/>
|
@@ -27,8 +27,15 @@ class TestParserPublication < Test::Unit::TestCase
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def test_has_prefixes_for_vocabulary
|
30
|
-
|
31
|
-
|
30
|
+
expected = {'foaf' => 'http://xmlns.com/foaf/spec/',
|
31
|
+
'dbp' => 'http://dbpedia.org/ontology/'}
|
32
|
+
assert_equal expected, @package.prefix
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_has_empty_hash_as_prefix_when_no_prefix_attribute
|
36
|
+
parser = EPUB::Parser::Publication.new('<package></package>', '')
|
37
|
+
package = parser.parse_package
|
38
|
+
assert_empty package.prefix
|
32
39
|
end
|
33
40
|
|
34
41
|
class TestParseMetadata < TestParserPublication
|
@@ -71,8 +78,8 @@ class TestParserPublication < Test::Unit::TestCase
|
|
71
78
|
@manifest = @parser.parse_manifest
|
72
79
|
end
|
73
80
|
|
74
|
-
def
|
75
|
-
assert_equal
|
81
|
+
def test_manifest_has_16_items
|
82
|
+
assert_equal 16, @manifest.items.length
|
76
83
|
end
|
77
84
|
|
78
85
|
def test_item_has_relative_path_as_href_attribute
|
@@ -93,6 +100,12 @@ class TestParserPublication < Test::Unit::TestCase
|
|
93
100
|
assert_equal 'html', doc.root.name
|
94
101
|
end
|
95
102
|
|
103
|
+
def test_item_unescape_href_when_reading_file
|
104
|
+
item = EPUB::Parser.parse('test/fixtures/book.epub').package.manifest['containing-encoded-space']
|
105
|
+
doc = Nokogiri.HTML(item.read)
|
106
|
+
assert_equal 'Containing Space', (doc/'title').first.content
|
107
|
+
end
|
108
|
+
|
96
109
|
def test_iri_of_item_is_case_sensitive
|
97
110
|
manifest = EPUB::Parser.parse('test/fixtures/book.epub').package.manifest
|
98
111
|
|
@@ -151,8 +164,8 @@ class TestParserPublication < Test::Unit::TestCase
|
|
151
164
|
@spine = @parser.parse_spine
|
152
165
|
end
|
153
166
|
|
154
|
-
def
|
155
|
-
expected = %w[nav manifest-item-1 manifest-item-2].map {|idref|
|
167
|
+
def atest_each_itemref_yields_itemref_in_order_on_spine_element
|
168
|
+
expected = %w[nav manifest-item-1 manifest-item-2 containing-space japanese-filename].map {|idref|
|
156
169
|
itemref = EPUB::Publication::Package::Spine::Itemref.new
|
157
170
|
itemref.id = nil
|
158
171
|
itemref.spine = @spine
|
data/test/test_publication.rb
CHANGED
@@ -13,4 +13,20 @@ class TestPublication < Test::Unit::TestCase
|
|
13
13
|
package.metadata = another_metadata
|
14
14
|
assert_nil metadata.package
|
15
15
|
end
|
16
|
+
|
17
|
+
class TestMetadata < TestPublication
|
18
|
+
def test_meta_refines_setter_connect_refinee_to_the_meta
|
19
|
+
refiner = EPUB::Publication::Package::Metadata::Meta.new
|
20
|
+
refinee = EPUB::Publication::Package::Metadata::Meta.new
|
21
|
+
refiner.refines = refinee
|
22
|
+
assert_same refinee.refiners.first, refiner
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_link_refines_setter_connect_refinee_to_the_link
|
26
|
+
refiner = EPUB::Publication::Package::Metadata::Link.new
|
27
|
+
refinee = EPUB::Publication::Package::Metadata::Meta.new
|
28
|
+
refiner.refines = refinee
|
29
|
+
assert_same refinee.refiners.first, refiner
|
30
|
+
end
|
31
|
+
end
|
16
32
|
end
|
data/wiki/Home.md
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
EPUB Parser gem parses EPUB 3 book loosely.
|
2
|
+
|
3
|
+
Installation
|
4
|
+
============
|
5
|
+
|
6
|
+
gem install epub-parser
|
7
|
+
|
8
|
+
Usage
|
9
|
+
=====
|
10
|
+
|
11
|
+
As a command-line tool
|
12
|
+
----------------------
|
13
|
+
|
14
|
+
epubinfo path/to/book.epub
|
15
|
+
|
16
|
+
To see help:
|
17
|
+
|
18
|
+
epubinfo -h
|
19
|
+
|
20
|
+
As a library
|
21
|
+
------------
|
22
|
+
|
23
|
+
Use `EPUB::Parser.parse` at first:
|
24
|
+
|
25
|
+
require 'epub/parser'
|
26
|
+
|
27
|
+
book = EPUB::Parser.parse '/path/to/book.epub'
|
28
|
+
|
29
|
+
This book object can yield page by spine's order(spine defines the order to read that the author determines):
|
30
|
+
|
31
|
+
book.each_page_on_spine do |page|
|
32
|
+
# do something...
|
33
|
+
end
|
34
|
+
|
35
|
+
`page` above is a [`EPUB::Publication::Package::Manifest::Item`][1] object and you can call `#href` to see where is the page file:
|
36
|
+
|
37
|
+
[1]:http://rubydoc.info/gems/epub-parser/EPUB/Publication/Package/Manifest/Item
|
38
|
+
|
39
|
+
book.each_page_on_spine do |page|
|
40
|
+
file = page.href # => path/to/page/in/zip/archive
|
41
|
+
html = Zip::Archive.open('/path/to/book.epub') {|zip|
|
42
|
+
zip.fopen(file.to_s).read
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
And `Item` provides syntax suger `#read` for above:
|
47
|
+
|
48
|
+
html = page.read
|
49
|
+
doc = Nokogiri.HTML html
|
50
|
+
# do something with Nokogiri as always
|
51
|
+
|
52
|
+
For several utilities of Item, see [[Item]] page.
|
53
|
+
|
54
|
+
By the way, although `book` above is a `EPUB::Book` object, all features are provided by `EPUB` module. Therefore YourBook class can include the features of EPUB:
|
55
|
+
|
56
|
+
require 'epub'
|
57
|
+
|
58
|
+
class YourBook < ActiveRecord::Base
|
59
|
+
include EPUB
|
60
|
+
end
|
61
|
+
|
62
|
+
book = EPUB::Parser.parse(
|
63
|
+
'uploaded-book.epub',
|
64
|
+
:class => YourBook # *************** pass YourBook class
|
65
|
+
)
|
66
|
+
book.instance_of? YourBook # => true
|
67
|
+
book.required = 'value for required field'
|
68
|
+
book.save!
|
69
|
+
book.each_page_on_spine do |epage|
|
70
|
+
page = YouBookPage.create(
|
71
|
+
:some_attr => 'some attr',
|
72
|
+
:content => epage.read,
|
73
|
+
:another_attr => 'another attr'
|
74
|
+
)
|
75
|
+
book.pages << page
|
76
|
+
end
|
77
|
+
|
78
|
+
You are also able to find YourBook object for the first:
|
79
|
+
|
80
|
+
book = YourBook.find params[:id]
|
81
|
+
ret = EPUB::Parser.parse(
|
82
|
+
'uploaded-book.epub',
|
83
|
+
:book => book # ******************* pass your book instance
|
84
|
+
) # => book
|
85
|
+
ret == book # => true; this API is not good I feel... Welcome suggestion!
|
86
|
+
# do something with your book
|
87
|
+
|
88
|
+
More documents comming soon..., hopefully :)
|
89
|
+
|
90
|
+
Requirements
|
91
|
+
============
|
92
|
+
|
93
|
+
* libxml2 and libxslt for Nokogiri gem
|
94
|
+
|
95
|
+
Note
|
96
|
+
====
|
97
|
+
|
98
|
+
This library is still in work.
|
99
|
+
Only a few features are implemented and APIs might be changed in the future.
|
100
|
+
Note that.
|
101
|
+
|
102
|
+
Currently implemented:
|
103
|
+
|
104
|
+
* container.xml of [EPUB Open Container Format (OCF) 3.0][]
|
105
|
+
* [EPUB Publications 3.0][]
|
106
|
+
|
107
|
+
[EPUB Open Container Format (OCF) 3.0]:http://idpf.org/epub/30/spec/epub30-ocf.html#sec-container-metainf-container.xml
|
108
|
+
[EPUB Publications 3.0]:http://idpf.org/epub/30/spec/epub30-publications.html
|
109
|
+
|
110
|
+
License
|
111
|
+
=======
|
112
|
+
|
113
|
+
This library is distributed under the term of the MIT Licence.
|
114
|
+
See [MIT-LICENSE](/epub/parser/blobs/master/MIT-LICENSE) file for more info.
|
data/wiki/Item.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
[[Home]] > **[[Item]]**
|
2
|
+
|
3
|
+
Overview
|
4
|
+
========
|
5
|
+
|
6
|
+
When manipulating resources (XHTML, images, audio...) in EPUB, [`EPUB::Publication::Manifest::Item`](http://rubydoc.info/gems/epub-parser/EPUB/Publication/Package/Manifest/Item) object will be used.
|
7
|
+
And objects which `EPUB#each_page_on_spine` yields are also instances of this class.
|
8
|
+
|
9
|
+
Here's the tutorial of this class.
|
10
|
+
|
11
|
+
Getting Items
|
12
|
+
=============
|
13
|
+
|
14
|
+
Getting the `Item` object you want is due to other classes, mainly `EPUB` module:
|
15
|
+
|
16
|
+
book = EPUB::Parser.parse 'book.epub'
|
17
|
+
book.resouces # => all items including XHTMLs, CSSs, images, audios and so on
|
18
|
+
book.cover_image # => item representing cover image file
|
19
|
+
book.each_page_by_spine do |page|
|
20
|
+
page # => item in spine(order of "page" the author determined, often XHTML file)
|
21
|
+
end
|
22
|
+
book.package.manifest.navs # => navigation items(XHTML files including <nav> element)
|
23
|
+
book.package.manifest['item-id'] # => item referenced by the ID "item-id"
|
24
|
+
|
25
|
+
For the last two examples, knowledge for EPUB structure is required.
|
26
|
+
|
27
|
+
Using Items
|
28
|
+
===========
|
29
|
+
|
30
|
+
Once you've got an `Item`, it provides informations about the item(file).
|
31
|
+
|
32
|
+
item.id # => the ID of the item
|
33
|
+
item.media_type # => media type like application/xhtml+xml
|
34
|
+
item.href # => Addressable::URI object which represents the IRI of the item
|
35
|
+
item.properties # => array of properties
|
36
|
+
item.fallback # => see the next section for details
|
37
|
+
item.fallback_chain # => ditto.
|
38
|
+
item.using_fallback_chain # => ditto.
|
39
|
+
|
40
|
+
And `Item` also provides some methods which helps you handle the item.
|
41
|
+
|
42
|
+
For example, for XHTML:
|
43
|
+
|
44
|
+
item.read # => content of the item
|
45
|
+
Nokogiri.HTML(item.read) #=> Nokogiri::HTML::Document object
|
46
|
+
|
47
|
+
For image:
|
48
|
+
|
49
|
+
uri = 'data:' + item.media_type + '; base64,' + Base64.encode64(item.read)
|
50
|
+
img = %Q!<img src="#{uri}" alt="#{item.id}">!
|
51
|
+
|
52
|
+
Fallback Chain
|
53
|
+
==============
|
54
|
+
|
55
|
+
Some items have `fallback` attribute, which provides the item to be used when reading system(your app) cannot handle with given item for some reason(for example, media type not supported).
|
56
|
+
|
57
|
+
Of course, you can get it by calling `fallback` method:
|
58
|
+
|
59
|
+
item.fallback # => fallback `Item` or nil
|
60
|
+
|
61
|
+
Also you can use `use_fallback_chain` not to check if you can accept item or not for every item:
|
62
|
+
|
63
|
+
item.use_fallback_chain :supported => 'image/png' do |png|
|
64
|
+
# do something with PNG image
|
65
|
+
end
|
66
|
+
|
67
|
+
If item's media type is, for instance, 'image/x-eps', the fallback is used.
|
68
|
+
If the fallback item's media type is 'image/png', `png` variable means the item, if not, "fallback of fallback" will be checked.
|
69
|
+
Finally you can use the item you want, or `EPUB::MediaType::UnsupportedError` exception will be raised(if no item you can accept found).
|
70
|
+
Therefore, you should `rescue` clause:
|
71
|
+
|
72
|
+
# :unsupported option can also be used
|
73
|
+
# fallback chain will be followed until EPUB's Core Media Types found or UnsupportedError raised
|
74
|
+
begin
|
75
|
+
item.use_fallback_chain :unsupported => 'application/pdf' do |page|
|
76
|
+
# do something with item with core media type
|
77
|
+
end
|
78
|
+
rescue EPUB::MediaType::UnsupportedError => evar
|
79
|
+
# error handling
|
80
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: epub-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubygems-test
|
16
|
-
requirement: &
|
16
|
+
requirement: &11881060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *11881060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &11879480 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *11879480
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: pry
|
38
|
-
requirement: &
|
38
|
+
requirement: &11892900 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *11892900
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: pry-doc
|
49
|
-
requirement: &
|
49
|
+
requirement: &11890900 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *11890900
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: test-unit-full
|
60
|
-
requirement: &
|
60
|
+
requirement: &11888960 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *11888960
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
|
-
requirement: &
|
71
|
+
requirement: &11887240 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *11887240
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: thin
|
82
|
-
requirement: &
|
82
|
+
requirement: &11919280 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *11919280
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: yard
|
93
|
-
requirement: &
|
93
|
+
requirement: &11916000 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *11916000
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: gem-man
|
104
|
-
requirement: &
|
104
|
+
requirement: &11914440 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *11914440
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: ronn
|
115
|
-
requirement: &
|
115
|
+
requirement: &11913320 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,10 +120,10 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *11913320
|
124
124
|
- !ruby/object:Gem::Dependency
|
125
125
|
name: epzip
|
126
|
-
requirement: &
|
126
|
+
requirement: &11936480 !ruby/object:Gem::Requirement
|
127
127
|
none: false
|
128
128
|
requirements:
|
129
129
|
- - ! '>='
|
@@ -131,10 +131,10 @@ dependencies:
|
|
131
131
|
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
|
-
version_requirements: *
|
134
|
+
version_requirements: *11936480
|
135
135
|
- !ruby/object:Gem::Dependency
|
136
136
|
name: epubcheck
|
137
|
-
requirement: &
|
137
|
+
requirement: &11935400 !ruby/object:Gem::Requirement
|
138
138
|
none: false
|
139
139
|
requirements:
|
140
140
|
- - ! '>='
|
@@ -142,10 +142,10 @@ dependencies:
|
|
142
142
|
version: '0'
|
143
143
|
type: :development
|
144
144
|
prerelease: false
|
145
|
-
version_requirements: *
|
145
|
+
version_requirements: *11935400
|
146
146
|
- !ruby/object:Gem::Dependency
|
147
147
|
name: epub_validator
|
148
|
-
requirement: &
|
148
|
+
requirement: &11934700 !ruby/object:Gem::Requirement
|
149
149
|
none: false
|
150
150
|
requirements:
|
151
151
|
- - ! '>='
|
@@ -153,10 +153,10 @@ dependencies:
|
|
153
153
|
version: '0'
|
154
154
|
type: :development
|
155
155
|
prerelease: false
|
156
|
-
version_requirements: *
|
156
|
+
version_requirements: *11934700
|
157
157
|
- !ruby/object:Gem::Dependency
|
158
158
|
name: aruba
|
159
|
-
requirement: &
|
159
|
+
requirement: &11934000 !ruby/object:Gem::Requirement
|
160
160
|
none: false
|
161
161
|
requirements:
|
162
162
|
- - ! '>='
|
@@ -164,10 +164,10 @@ dependencies:
|
|
164
164
|
version: '0'
|
165
165
|
type: :development
|
166
166
|
prerelease: false
|
167
|
-
version_requirements: *
|
167
|
+
version_requirements: *11934000
|
168
168
|
- !ruby/object:Gem::Dependency
|
169
169
|
name: enumerabler
|
170
|
-
requirement: &
|
170
|
+
requirement: &11933380 !ruby/object:Gem::Requirement
|
171
171
|
none: false
|
172
172
|
requirements:
|
173
173
|
- - ! '>='
|
@@ -175,10 +175,10 @@ dependencies:
|
|
175
175
|
version: '0'
|
176
176
|
type: :runtime
|
177
177
|
prerelease: false
|
178
|
-
version_requirements: *
|
178
|
+
version_requirements: *11933380
|
179
179
|
- !ruby/object:Gem::Dependency
|
180
180
|
name: zipruby
|
181
|
-
requirement: &
|
181
|
+
requirement: &11932600 !ruby/object:Gem::Requirement
|
182
182
|
none: false
|
183
183
|
requirements:
|
184
184
|
- - ! '>='
|
@@ -186,10 +186,10 @@ dependencies:
|
|
186
186
|
version: '0'
|
187
187
|
type: :runtime
|
188
188
|
prerelease: false
|
189
|
-
version_requirements: *
|
189
|
+
version_requirements: *11932600
|
190
190
|
- !ruby/object:Gem::Dependency
|
191
191
|
name: nokogiri
|
192
|
-
requirement: &
|
192
|
+
requirement: &11930860 !ruby/object:Gem::Requirement
|
193
193
|
none: false
|
194
194
|
requirements:
|
195
195
|
- - ! '>='
|
@@ -197,10 +197,10 @@ dependencies:
|
|
197
197
|
version: '0'
|
198
198
|
type: :runtime
|
199
199
|
prerelease: false
|
200
|
-
version_requirements: *
|
200
|
+
version_requirements: *11930860
|
201
201
|
- !ruby/object:Gem::Dependency
|
202
202
|
name: addressable
|
203
|
-
requirement: &
|
203
|
+
requirement: &11959220 !ruby/object:Gem::Requirement
|
204
204
|
none: false
|
205
205
|
requirements:
|
206
206
|
- - ! '>='
|
@@ -208,7 +208,7 @@ dependencies:
|
|
208
208
|
version: '0'
|
209
209
|
type: :runtime
|
210
210
|
prerelease: false
|
211
|
-
version_requirements: *
|
211
|
+
version_requirements: *11959220
|
212
212
|
description: Parse EPUB 3 book loosely
|
213
213
|
email:
|
214
214
|
- KitaitiMakoto@gmail.com
|
@@ -226,6 +226,9 @@ files:
|
|
226
226
|
- Rakefile
|
227
227
|
- bin/epubinfo
|
228
228
|
- epub-parser.gemspec
|
229
|
+
- features/epubinfo.feature
|
230
|
+
- features/step_definitions/epubinfo_steps.rb
|
231
|
+
- features/support/env.rb
|
229
232
|
- lib/epub.rb
|
230
233
|
- lib/epub/book.rb
|
231
234
|
- lib/epub/constants.rb
|
@@ -256,8 +259,11 @@ files:
|
|
256
259
|
- schemas/epub-xhtml-30.sch
|
257
260
|
- schemas/ocf-container-30.rnc
|
258
261
|
- test/fixtures/book/META-INF/container.xml
|
262
|
+
- test/fixtures/book/OPS/%E6%97%A5%E6%9C%AC%E8%AA%9E.xhtml
|
259
263
|
- test/fixtures/book/OPS/CASE-SENSITIVE.xhtml
|
260
264
|
- test/fixtures/book/OPS/case-sensitive.xhtml
|
265
|
+
- test/fixtures/book/OPS/containing space.xhtml
|
266
|
+
- test/fixtures/book/OPS/containing%20space.xhtml
|
261
267
|
- test/fixtures/book/OPS/nav.xhtml
|
262
268
|
- test/fixtures/book/mimetype
|
263
269
|
- test/helper.rb
|
@@ -268,6 +274,9 @@ files:
|
|
268
274
|
- test/test_parser_publication.rb
|
269
275
|
- test/test_publication.rb
|
270
276
|
- test/fixtures/book/OPS/ルートファイル.opf
|
277
|
+
- test/fixtures/book/OPS/日本語.xhtml
|
278
|
+
- wiki/Home.md
|
279
|
+
- wiki/Item.md
|
271
280
|
- doc/method_list.html
|
272
281
|
- doc/EPUB.html
|
273
282
|
- doc/file.README.html
|
@@ -296,7 +305,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
296
305
|
version: '0'
|
297
306
|
segments:
|
298
307
|
- 0
|
299
|
-
hash:
|
308
|
+
hash: -1959705838620689710
|
300
309
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
301
310
|
none: false
|
302
311
|
requirements:
|
@@ -305,12 +314,22 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
305
314
|
version: '0'
|
306
315
|
segments:
|
307
316
|
- 0
|
308
|
-
hash:
|
317
|
+
hash: -1959705838620689710
|
309
318
|
requirements: []
|
310
319
|
rubyforge_project:
|
311
320
|
rubygems_version: 1.8.8
|
312
321
|
signing_key:
|
313
322
|
specification_version: 3
|
314
323
|
summary: EPUB 3 Parser
|
315
|
-
test_files:
|
324
|
+
test_files:
|
325
|
+
- features/epubinfo.feature
|
326
|
+
- features/step_definitions/epubinfo_steps.rb
|
327
|
+
- features/support/env.rb
|
328
|
+
- test/helper.rb
|
329
|
+
- test/test_epub.rb
|
330
|
+
- test/test_parser.rb
|
331
|
+
- test/test_parser_content_document.rb
|
332
|
+
- test/test_parser_ocf.rb
|
333
|
+
- test/test_parser_publication.rb
|
334
|
+
- test/test_publication.rb
|
316
335
|
has_rdoc: yard
|