peregrin 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ require 'test_helper'
2
+
3
+ class Peregrin::Tests::OchookTest < Test::Unit::TestCase
4
+
5
+ def test_validates
6
+ # FIXME: tests for:
7
+ # - DirectoryNotFound
8
+ # - MissingManifest
9
+ # - IndexHTMLRootHasNoManifest
10
+ #
11
+ # ???
12
+ assert_nothing_raised {
13
+ Peregrin::Ochook.validate('test/fixtures/ochooks/basic')
14
+ }
15
+ end
16
+
17
+
18
+ def test_read
19
+ ook = Peregrin::Ochook.read('test/fixtures/ochooks/basic')
20
+ book = ook.to_book
21
+ assert_equal(1, book.components.length)
22
+ assert_equal("index.html", book.components.first.src)
23
+ assert_equal(['cover.png'], book.resources.collect { |res| res.src })
24
+ assert_equal("A Basic Ochook", book.property_for('title'))
25
+ assert_equal(1, book.chapters.size)
26
+
27
+ chp = book.chapters.first
28
+ assert_equal("A Basic Ochook", chp.title)
29
+ assert_equal("index.html", chp.src)
30
+ assert_equal("Part One", chp.children[0].title)
31
+ assert_equal("index.html#part1", chp.children[0].src)
32
+ assert_equal("Part Two", chp.children[1].title)
33
+ assert_equal("index.html#part2", chp.children[1].src)
34
+ end
35
+
36
+
37
+ def test_write_from_epub
38
+ epub = Peregrin::Epub.read('test/fixtures/epubs/alice.epub')
39
+ book = epub.to_book
40
+ ook = Peregrin::Ochook.new(book)
41
+ ook.write('test/output/alice_ochook')
42
+ assert_nothing_raised {
43
+ Peregrin::Ochook.validate('test/output/alice_ochook')
44
+ }
45
+ end
46
+
47
+
48
+ def test_to_book_cover_html
49
+ ook = Peregrin::Ochook.read("test/fixtures/ochooks/illustrated")
50
+ book = ook.to_book(:componentize => true)
51
+ cov_html = book.components.detect { |cmpt|
52
+ cmpt.src == "cover.html"
53
+ }.contents
54
+ doc = Nokogiri::HTML::Document.parse(cov_html)
55
+ assert_equal('cover.png', doc.at_xpath('/html/body/div/img')['src'])
56
+ end
57
+
58
+
59
+ def test_to_book_toc_html
60
+ ook = Peregrin::Ochook.read("test/fixtures/ochooks/illustrated")
61
+ book = ook.to_book(:componentize => true)
62
+ toc_html = book.components.detect { |cmpt| cmpt.src == "toc.html" }.contents
63
+ doc = Nokogiri::HTML::Document.parse(toc_html)
64
+ assert_equal(3, doc.xpath('/html/body/ol/li').size)
65
+ end
66
+
67
+
68
+ def test_to_book_loi_html
69
+ ook = Peregrin::Ochook.read("test/fixtures/ochooks/illustrated")
70
+ book = ook.to_book(:componentize => true)
71
+ loi_html = book.components.detect { |cmpt| cmpt.src == "loi.html" }.contents
72
+ doc = Nokogiri::HTML::Document.parse(loi_html)
73
+ assert_equal(2, doc.xpath('/html/body/ol/li').size)
74
+ end
75
+
76
+
77
+ def test_to_book_rel_links
78
+ ook = Peregrin::Ochook.read("test/fixtures/ochooks/illustrated")
79
+ book = ook.to_book(:componentize => true)
80
+ cmpt_html = book.components[3].contents
81
+ doc = Nokogiri::HTML::Document.parse(cmpt_html)
82
+ assert_equal(
83
+ "cover.html",
84
+ doc.at_xpath('/html/head/link[@rel="start"]')['href']
85
+ )
86
+ assert_equal(
87
+ "toc.html",
88
+ doc.at_xpath('/html/head/link[@rel="contents"]')['href']
89
+ )
90
+ assert_equal(
91
+ "index.html",
92
+ doc.at_xpath('/html/head/link[@rel="first"]')['href']
93
+ )
94
+ assert_equal(
95
+ "part002.html",
96
+ doc.at_xpath('/html/head/link[@rel="last"]')['href']
97
+ )
98
+ assert_equal(
99
+ "part001.html",
100
+ doc.at_xpath('/html/head/link[@rel="prev"]')['href']
101
+ )
102
+ end
103
+
104
+ end
@@ -0,0 +1,219 @@
1
+ require 'test_helper'
2
+
3
+ class Peregrin::Tests::ZhookTest < Test::Unit::TestCase
4
+
5
+ def test_validates
6
+ # File does not exist
7
+ assert_raise(Peregrin::Zhook::FileNotFound) {
8
+ Peregrin::Zhook.validate('test/fixtures/zhooks/invalid/missing.zhook')
9
+ }
10
+
11
+ # File extension is not .zhook
12
+ assert_raise(Peregrin::Zhook::WrongExtension) {
13
+ Peregrin::Zhook.validate('test/fixtures/zhooks/invalid/wrongext.zip')
14
+ }
15
+
16
+ # File is not a zip archive
17
+ assert_raise(Peregrin::Zhook::NotAZipArchive) {
18
+ Peregrin::Zhook.validate('test/fixtures/zhooks/invalid/notazip.zhook')
19
+ }
20
+
21
+ # Archive does not contain index.html
22
+ assert_raise(Peregrin::Zhook::MissingIndexHTML) {
23
+ Peregrin::Zhook.validate('test/fixtures/zhooks/invalid/noindex.zhook')
24
+ }
25
+
26
+ # Archive does not contain cover.png
27
+ assert_raise(Peregrin::Zhook::MissingCoverPNG) {
28
+ Peregrin::Zhook.validate('test/fixtures/zhooks/invalid/nocover.zhook')
29
+ }
30
+
31
+ # Index file has a HTML element with an id.
32
+ assert_raise(Peregrin::Zhook::IndexHTMLRootHasId) {
33
+ Peregrin::Zhook.validate('test/fixtures/zhooks/invalid/rootid.zhook')
34
+ }
35
+
36
+ # An actual valid .zhook
37
+ assert_nothing_raised {
38
+ Peregrin::Zhook.validate('test/fixtures/zhooks/flat.zhook')
39
+ }
40
+ end
41
+
42
+
43
+ def test_read
44
+ ook = Peregrin::Zhook.read('test/fixtures/zhooks/2level.zhook')
45
+ book = ook.to_book
46
+ assert_equal(1, book.components.length)
47
+ assert_equal("index.html", book.components.first.src)
48
+ assert_equal(['cover.png'], book.resources.collect { |res| res.src })
49
+ assert_equal("A Two-Level Zhook", book.property_for('title'))
50
+ assert_equal(1, book.chapters.size)
51
+ chp = book.chapters.first
52
+ assert_equal("A Two-Level Zhook", chp.title)
53
+ assert_equal("index.html", chp.src)
54
+ assert_equal("Part One", chp.children[0].title)
55
+ assert_equal("index.html#part1", chp.children[0].src)
56
+ assert_equal("Part Two", chp.children[1].title)
57
+ assert_equal("index.html#part2", chp.children[1].src)
58
+ end
59
+
60
+
61
+ def test_to_book_componentization
62
+ ook = Peregrin::Zhook.read('test/fixtures/zhooks/flat.zhook')
63
+ book = ook.to_book(:componentize => true)
64
+ assert_equal(
65
+ ["cover.html", "index.html", "part001.html", "part002.html", "toc.html"],
66
+ book.components.collect { |cmpt| cmpt.src }
67
+ )
68
+ assert_equal(3, book.chapters.size)
69
+ assert_equal("A Flat Zhook", book.chapters[0].title)
70
+ assert_equal("Part One", book.chapters[1].title)
71
+ assert_equal("Part Two", book.chapters[2].title)
72
+ end
73
+
74
+
75
+ def test_2_level_componentization
76
+ ook = Peregrin::Zhook.read('test/fixtures/zhooks/2level.zhook')
77
+ book = ook.to_book(:componentize => true)
78
+ assert_equal(1, book.chapters.size)
79
+ chp = book.chapters.first
80
+ assert_equal("A Two-Level Zhook", chp.title)
81
+ assert_equal("index.html", chp.src)
82
+ assert_equal("Part One", chp.children[0].title)
83
+ assert_equal("part001.html#part1", chp.children[0].src)
84
+ assert_equal("Part Two", chp.children[1].title)
85
+ assert_equal("part002.html#part2", chp.children[1].src)
86
+ end
87
+
88
+
89
+ def test_3_level_componentization
90
+ ook = Peregrin::Zhook.read('test/fixtures/zhooks/3level.zhook')
91
+ book = ook.to_book(:componentize => true)
92
+ assert_equal(1, book.chapters.size)
93
+ chp = book.chapters.first
94
+ assert_equal("A Three-Level Zhook", chp.title)
95
+ assert_equal("index.html", chp.src)
96
+ assert_equal("Part One", chp.children[0].title)
97
+ assert_equal("part001.html#part1", chp.children[0].src)
98
+ assert_equal("Part Two", chp.children[1].title)
99
+ assert_equal("part003.html#part2", chp.children[1].src)
100
+ assert_equal("Sub-part One Dot Two", chp.children[0].children[0].title)
101
+ assert_equal("part002.html", chp.children[0].children[0].src)
102
+ assert_equal("Sub-part Two Dot Two", chp.children[1].children[0].title)
103
+ assert_equal("part004.html", chp.children[1].children[0].src)
104
+ end
105
+
106
+
107
+ def test_stitching_components
108
+ book = Peregrin::Book.new
109
+ book.add_component(
110
+ "index.html",
111
+ %Q`
112
+ <html><head><title>Index</title>
113
+ <meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
114
+ </head><body>
115
+ <p>A para</p></body></html>
116
+ `
117
+ )
118
+ book.add_component(
119
+ "foo.html",
120
+ %Q`
121
+ <html><head><title>Foo</title>
122
+ <link rel="stylesheet" href="main.css" />
123
+ </head><body>
124
+ <hgroup><h1>Part Foo</h1><h2>Peregrin Took</h2></hgroup>
125
+ <cite>A cite tag</cite></body></html>
126
+ `
127
+ )
128
+ book.add_component("garply.html", %Q`<p>A floating para.</p>`)
129
+ ook = Peregrin::Zhook.new(book)
130
+ assert_equal(
131
+ whitewash(
132
+ %Q`
133
+ <!DOCTYPE html>
134
+ <html><head>
135
+ <title>Index</title>
136
+ <meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
137
+ <link rel="stylesheet" href="main.css">
138
+ </head><body>
139
+ <article>
140
+ <p>A para</p>
141
+ </article>
142
+ <article>
143
+ <hgroup><h1>Part Foo</h1><h2>Peregrin Took</h2></hgroup>
144
+ <cite>A cite tag</cite>
145
+ </article>
146
+ <article>
147
+ <p>A floating para.</p>
148
+ </article>
149
+ </body></html>`
150
+ ),
151
+ whitewash(ook.to_book.components.first.contents)
152
+ )
153
+ end
154
+
155
+
156
+ def test_consolidating_metadata
157
+ book = Peregrin::Book.new
158
+ book.add_component(
159
+ "index.html",
160
+ "<html><head><title>Foo</title>" +
161
+ "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=US-ASCII\">" +
162
+ "</head><body><p>Foo</p></body></html>"
163
+ )
164
+ book.add_property("title", "Foo")
165
+ book.add_property("creator", "Peregrin Took")
166
+ ook = Peregrin::Zhook.new(book)
167
+ assert_equal(
168
+ whitewash(%Q`
169
+ <!DOCTYPE html>
170
+ <html><head><title>Foo</title>
171
+ <meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
172
+ <meta name="title" content="Foo">
173
+ <meta name="creator" content="Peregrin Took">
174
+ </head><body><p>Foo</p></body></html>
175
+ `),
176
+ whitewash(ook.to_book.components.first.contents)
177
+ )
178
+ end
179
+
180
+
181
+ def test_write_from_epub
182
+ epub = Peregrin::Epub.read('test/fixtures/epubs/alice.epub')
183
+ book = epub.to_book
184
+ ook = Peregrin::Zhook.new(book)
185
+ ook.write('test/output/alice.zhook')
186
+ assert_nothing_raised {
187
+ Peregrin::Zhook.validate('test/output/alice.zhook')
188
+ }
189
+ end
190
+
191
+
192
+ def test_convert_jpg_cover_on_write
193
+ # Create an epub object, convert it to a book, and verify that the cover
194
+ # is a JPEG.
195
+ epub = Peregrin::Epub.read('test/fixtures/epubs/alice.epub')
196
+ book = epub.to_book
197
+ assert_equal(
198
+ "www.gutenberg.org@files@19033@19033-h@images@cover_th.jpg",
199
+ book.cover.src
200
+ )
201
+
202
+ # Write the book to file as a Zhook, which should convert the cover to PNG.
203
+ ook = Peregrin::Zhook.new(book)
204
+ ook.write('test/output/alice.zhook')
205
+
206
+ # Load the Zhook from file, and check that it has a cover.png.
207
+ ook2 = Peregrin::Zhook.read('test/output/alice.zhook')
208
+ book2 = ook2.to_book
209
+ assert_equal("cover.png", book2.cover.src)
210
+
211
+ # Validate the cover.png using ImageMagick's identify
212
+ IO.popen("identify -", "r+") { |io|
213
+ io.write(book2.read_resource(book2.cover))
214
+ io.close_write
215
+ assert_match(/^[^\s]+ PNG /, io.read)
216
+ }
217
+ end
218
+
219
+ end
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'peregrin'
3
+
4
+ module Peregrin::Tests
5
+
6
+ end
7
+
8
+
9
+
10
+ class Test::Unit::TestCase
11
+
12
+ def whitewash(str)
13
+ str.gsub(/\s+/, '')
14
+ end
15
+
16
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ class Peregrin::Tests::ComponentizerTest < Test::Unit::TestCase
4
+
5
+ def test_processing
6
+ cz = process_fixture("components1.html")
7
+ assert_equal(
8
+ [
9
+ '/html/body',
10
+ '/html/body/article[1]',
11
+ '/html/body/article[1]/article[1]',
12
+ '/html/body/article[1]/article[2]',
13
+ '/html/body/article[2]'
14
+ ],
15
+ cz.component_xpaths
16
+ )
17
+ end
18
+
19
+
20
+ def test_processing_where_body_should_be_empty
21
+ cz = process_fixture("components2.html")
22
+ assert_equal(
23
+ [
24
+ '/html/body/article[1]',
25
+ '/html/body/article[1]/article[1]',
26
+ '/html/body/article[1]/article[2]',
27
+ '/html/body/article[2]',
28
+ '/html/body/article[2]/article'
29
+ ],
30
+ cz.component_xpaths
31
+ )
32
+ end
33
+
34
+
35
+ def test_generate_component
36
+ cz = process_fixture("components1.html")
37
+ assert_equal(
38
+ whitewash(
39
+ "<!DOCTYPE html>" +
40
+ "<html><head><title>Components test 1</title></head><body>" +
41
+ "<article><h2>B</h2></article>" +
42
+ "</body></html>"
43
+ ),
44
+ whitewash(cz.generate_component('/html/body/article[1]').to_html)
45
+ )
46
+ end
47
+
48
+
49
+ def test_write_component
50
+ cz = process_fixture("components1.html")
51
+ tmp_path = "test/fixtures/componentizer/tmp.html"
52
+ cz.write_component("/html/body", tmp_path) { |doc| doc.to_xhtml }
53
+ assert_equal(
54
+ whitewash(
55
+ "<!DOCTYPE html>" +
56
+ '<html xmlns="http://www.w3.org/1999/xhtml"><head>' +
57
+ '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />' +
58
+ "<title>Components test 1</title></head>" +
59
+ "<body><h1>A</h1></body></html>"
60
+ ),
61
+ whitewash(IO.read(tmp_path))
62
+ )
63
+ ensure
64
+ File.unlink(tmp_path) if File.exists?(tmp_path)
65
+ end
66
+
67
+
68
+ protected
69
+
70
+ def process_fixture(filename)
71
+ fx = IO.read("test/fixtures/componentizer/#{filename}")
72
+ doc = Nokogiri::HTML::Document.parse(fx)
73
+ cz = Peregrin::Componentizer.new(doc)
74
+ cz.process(doc.at_xpath('/html/body'))
75
+ cz
76
+ end
77
+
78
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class Peregrin::Tests::OutlinerTest < Test::Unit::TestCase
4
+
5
+ def test_spec_1
6
+ load_spec_and_compare_out('spec1')
7
+ end
8
+
9
+
10
+ def test_spec_2
11
+ load_spec_and_compare_out('spec2') { |section, below|
12
+ if section.heading_text
13
+ section.heading_text
14
+ elsif section.node
15
+ "<i>Untitled #{section.node.name.upcase}</i>"
16
+ end
17
+ }
18
+ end
19
+
20
+
21
+ def test_spec_3a
22
+ load_spec_and_compare_out('spec3a')
23
+ end
24
+
25
+
26
+ def test_spec_3b
27
+ load_spec_and_compare_out('spec3b')
28
+ end
29
+
30
+
31
+ def test_spec_4
32
+ load_spec_and_compare_out('spec4')
33
+ end
34
+
35
+
36
+ protected
37
+
38
+ def load_spec_and_compare_out(spec_name, &blk)
39
+ src_file = File.new("test/fixtures/outliner/#{spec_name}.doc.html")
40
+ cmp_file = File.new("test/fixtures/outliner/#{spec_name}.out.html")
41
+ doc = Nokogiri::HTML::Document.parse(src_file)
42
+ outliner = Peregrin::Outliner.new(doc.root)
43
+ outliner.process(doc.root)
44
+ out = outliner.to_html(&blk)
45
+ cmp = cmp_file.read
46
+ assert_equal(whitewash(cmp), whitewash(out))
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peregrin
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 1
9
+ version: 1.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Joseph Pearson
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-06-28 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: zipruby
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: mime-types
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :runtime
55
+ version_requirements: *id003
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ prerelease: false
59
+ requirement: &id004 !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ type: :development
67
+ version_requirements: *id004
68
+ description: Peregrin converts EPUBs, Zhooks and Ochooks.
69
+ email: joseph@inventivelabs.com.au
70
+ executables:
71
+ - peregrin
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - README.md
76
+ - MIT-LICENSE
77
+ files:
78
+ - bin/peregrin
79
+ - lib/formats/epub.rb
80
+ - lib/formats/ochook.rb
81
+ - lib/formats/zhook.rb
82
+ - lib/peregrin/book.rb
83
+ - lib/peregrin/chapter.rb
84
+ - lib/peregrin/component.rb
85
+ - lib/peregrin/componentizer.rb
86
+ - lib/peregrin/outliner.rb
87
+ - lib/peregrin/property.rb
88
+ - lib/peregrin/resource.rb
89
+ - lib/peregrin/version.rb
90
+ - lib/peregrin/zip_patch.rb
91
+ - lib/peregrin.rb
92
+ - test/conversion_test.rb
93
+ - test/formats/epub_test.rb
94
+ - test/formats/ochook_test.rb
95
+ - test/formats/zhook_test.rb
96
+ - test/test_helper.rb
97
+ - test/utils/componentizer_test.rb
98
+ - test/utils/outliner_test.rb
99
+ - README.md
100
+ - MIT-LICENSE
101
+ has_rdoc: true
102
+ homepage: http://ochook.org/peregrin
103
+ licenses: []
104
+
105
+ post_install_message:
106
+ rdoc_options:
107
+ - --title
108
+ - Peregrin
109
+ - --main
110
+ - README.md
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project: nowarning
130
+ rubygems_version: 1.3.6
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Peregrin - ebook conversion
134
+ test_files: []
135
+