eeepub 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,6 +6,21 @@ EeePub is a Ruby ePub generator.
6
6
  Usage
7
7
  -------
8
8
 
9
+ ### Basic
10
+
11
+ epub = EeePub::Basic.new(
12
+ :title => 'simple',
13
+ :id => {'URL' => 'http://example.com/book/foo'},
14
+ :uid => 'http://example.com/book/foo'
15
+ )
16
+ epub.files << '/path/to/foo.html'
17
+ epub.files << '/path/to/bar.html'
18
+ epub.nav << {:label => '1. foo', :content => 'foo.html'}
19
+ epub.nav << {:label => '2. bar', :content => 'bar.html'}
20
+ epub.save('sample.epub')
21
+
22
+ ### Raw
23
+
9
24
  Create NCX:
10
25
 
11
26
  EeePub::NCX.new(
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/examples/basic.rb ADDED
@@ -0,0 +1,18 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'rubygems'
3
+ require 'eeepub'
4
+
5
+ dir = File.join(File.dirname(__FILE__), 'files')
6
+
7
+ epub = EeePub::Basic.new(
8
+ :title => 'simple',
9
+ :id => {'URL' => 'http://example.com/book/foo'},
10
+ :uid => 'http://example.com/book/foo'
11
+ )
12
+ epub.files << File.join(dir, 'foo.html')
13
+ epub.files << File.join(dir, 'bar.html')
14
+ epub.nav << {:label => '1. foo', :content => 'foo.html'}
15
+ epub.nav << {:label => '2. bar', :content => 'bar.html'}
16
+ epub.save('sample.epub')
17
+
18
+ puts "complete! => 'sample.epub'"
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
4
+ <head>
5
+ <title></title>
6
+ </head>
7
+ <body>
8
+ <h1>bar</h1>
9
+ </body>
10
+ </html>
@@ -0,0 +1,10 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
4
+ <head>
5
+ <title></title>
6
+ </head>
7
+ <body>
8
+ <h1>foo</h1>
9
+ </body>
10
+ </html>
@@ -0,0 +1,44 @@
1
+ require 'tmpdir'
2
+ require 'fileutils'
3
+
4
+ module EeePub
5
+ class Basic
6
+ attr_accessor :title, :id, :uid, :files, :nav
7
+
8
+ def initialize(values)
9
+ values.each do |k, v|
10
+ self.send(:"#{k}=", v)
11
+ end
12
+ @files ||= []
13
+ @nav ||= []
14
+ end
15
+
16
+ def save(filename)
17
+ dir = Dir.mktmpdir
18
+
19
+ files.each do |file|
20
+ FileUtils.cp(file, dir)
21
+ end
22
+
23
+ EeePub::NCX.new(
24
+ :uid => uid,
25
+ :title => title,
26
+ :nav => nav
27
+ ).save(File.join(dir, 'toc.ncx'))
28
+
29
+ EeePub::OPF.new(
30
+ :title => title,
31
+ :identifier => id,
32
+ :manifest => files.map{|i| File.basename(i)},
33
+ :ncx => 'toc.ncx'
34
+ ).save(File.join(dir, 'content.opf'))
35
+
36
+ EeePub::OCF.new(
37
+ :dir => dir,
38
+ :container => 'content.opf'
39
+ ).save(filename)
40
+
41
+ FileUtils.rm_rf(dir)
42
+ end
43
+ end
44
+ end
@@ -1,10 +1,7 @@
1
- require 'erb' # TODO 消す
2
1
  require 'builder'
3
2
 
4
3
  module EeePub
5
4
  class ContainerItem
6
- include ERB::Util
7
-
8
5
  class << self
9
6
  def default_value(name, default)
10
7
  instance_variable_name = "@#{name}"
@@ -38,14 +35,6 @@ module EeePub
38
35
  out
39
36
  end
40
37
 
41
- def erb(filename)
42
- ERB.new(
43
- File.read(File.join(TEMPLATE_DIR, filename)),
44
- nil,
45
- '-'
46
- )
47
- end
48
-
49
38
  def save(filepath)
50
39
  File.open(filepath, 'w') do |file|
51
40
  file << self.to_xml
data/lib/eeepub/opf.rb CHANGED
@@ -43,21 +43,10 @@ module EeePub
43
43
  end
44
44
 
45
45
  def spine
46
- unless @spine
47
- items = manifest.map do |i|
48
- case i
49
- when String
50
- id = i
51
- media_type = guess_media_type(i)
52
- when Hash
53
- id = i[:id]
54
- media_type = i[:media_type] || guess_media_type(i[:href])
55
- end
56
- media_type == 'application/xhtml+xml' ? id : nil
57
- end
58
- @spine = items.compact
59
- end
60
- @spine
46
+ @spine ||
47
+ complete_manifest.
48
+ select { |i| i[:media_type] == 'application/xhtml+xml' }.
49
+ map { |i| i[:id]}
61
50
  end
62
51
 
63
52
  def build_xml(builder)
@@ -96,17 +85,9 @@ module EeePub
96
85
  end
97
86
 
98
87
  def build_manifest(builder)
99
- items = manifest + [{:id => 'ncx', :href => ncx}]
100
88
  builder.manifest do
101
- items.each do |i|
102
- case i
103
- when Hash
104
- builder.item :id => i[:id], :href => i[:href], 'media-type' => i[:media_type] || guess_media_type(i[:href])
105
- when String
106
- builder.item :id => i, :href => i, 'media-type' => guess_media_type(i)
107
- else
108
- raise 'manifest item must be Hash or String'
109
- end
89
+ complete_manifest.each do |i|
90
+ builder.item :id => i[:id], :href => i[:href], 'media-type' => i[:media_type]
110
91
  end
111
92
  end
112
93
  end
@@ -118,5 +99,38 @@ module EeePub
118
99
  end
119
100
  end
120
101
  end
102
+
103
+ def complete_manifest
104
+ item_id_cache = {}
105
+
106
+ result = manifest.map do |i|
107
+ case i
108
+ when String
109
+ id = create_unique_item_id(i, item_id_cache)
110
+ href = i
111
+ media_type = guess_media_type(i)
112
+ when Hash
113
+ id = i[:id] || create_unique_item_id(i[:href], item_id_cache)
114
+ href = i[:href]
115
+ media_type = i[:media_type] || guess_media_type(i[:href])
116
+ end
117
+ {:id => id, :href => href, :media_type => media_type}
118
+ end
119
+
120
+ result += [{:id => 'ncx', :href => ncx, :media_type => 'application/x-dtbncx+xml'}] if ncx
121
+ result
122
+ end
123
+
124
+ def create_unique_item_id(filename, id_cache)
125
+ basename = File.basename(filename)
126
+ unless id_cache[basename]
127
+ id_cache[basename] = 0
128
+ name = basename
129
+ else
130
+ name = "#{basename}-#{id_cache[basename]}"
131
+ end
132
+ id_cache[basename] += 1
133
+ name
134
+ end
121
135
  end
122
136
  end
data/lib/eeepub.rb CHANGED
@@ -2,3 +2,4 @@ require 'eeepub/container_item'
2
2
  require 'eeepub/opf'
3
3
  require 'eeepub/ocf'
4
4
  require 'eeepub/ncx'
5
+ require 'eeepub/basic'
@@ -17,36 +17,6 @@ describe "EeePub::OPF" do
17
17
  @opf.language.should == 'en'
18
18
  end
19
19
 
20
- describe 'spec of identifier' do
21
- context 'specify as Array' do
22
- before { @opf.identifier = [{:scheme => 'ISBN', :value => '978-4-00-310101-8'}] }
23
- it 'should return value' do
24
- @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8'}]
25
- end
26
- end
27
-
28
- context 'specify as Hash' do
29
- before { @opf.identifier = {:scheme => 'ISBN', :value => '978-4-00-310101-8'} }
30
- it 'should return value' do
31
- @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
32
- end
33
- end
34
-
35
- context 'specify as Hash(scheme => value)' do
36
- before { @opf.identifier = {'ISBN' => '978-4-00-310101-8'} }
37
- it 'should return value' do
38
- @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
39
- end
40
- end
41
-
42
- context 'specify as String' do
43
- before { @opf.identifier = '978-4-00-310101-8' }
44
- it 'should return value' do
45
- @opf.identifier.should == [{:value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
46
- end
47
- end
48
- end
49
-
50
20
  it 'should export as xml' do
51
21
  doc = Nokogiri::XML(@opf.to_xml)
52
22
  doc.at('package').attribute('unique-identifier').value.should == @opf.unique_identifier
@@ -89,9 +59,58 @@ describe "EeePub::OPF" do
89
59
  spine.should_not be_nil
90
60
  spine = spine.search('itemref')
91
61
  spine.size.should == 2
92
- spine.each_with_index do |itemref, index|
93
- expect = @opf.spine[index]
94
- itemref.attribute('idref').value.should == expect
62
+ spine[0].attribute('idref').value.should == 'foo.html'
63
+ spine[1].attribute('idref').value.should == 'bar.html'
64
+ end
65
+
66
+ describe 'spec of identifier' do
67
+ context 'specify as Array' do
68
+ before { @opf.identifier = [{:scheme => 'ISBN', :value => '978-4-00-310101-8'}] }
69
+ it 'should return value' do
70
+ @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8'}]
71
+ end
72
+ end
73
+
74
+ context 'specify as Hash' do
75
+ before { @opf.identifier = {:scheme => 'ISBN', :value => '978-4-00-310101-8'} }
76
+ it 'should return value' do
77
+ @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
78
+ end
79
+ end
80
+
81
+ context 'specify as Hash(scheme => value)' do
82
+ before { @opf.identifier = {'ISBN' => '978-4-00-310101-8'} }
83
+ it 'should return value' do
84
+ @opf.identifier.should == [{:scheme => 'ISBN', :value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
85
+ end
86
+ end
87
+
88
+ context 'specify as String' do
89
+ before { @opf.identifier = '978-4-00-310101-8' }
90
+ it 'should return value' do
91
+ @opf.identifier.should == [{:value => '978-4-00-310101-8', :id => @opf.unique_identifier}]
92
+ end
93
+ end
94
+ end
95
+
96
+ describe 'spec of create_unique_item_id' do
97
+ it 'should return unique item id' do
98
+ id_cache = {}
99
+ @opf.create_unique_item_id('foo/bar/test.html', id_cache).should == 'test.html'
100
+ @opf.create_unique_item_id('foo/bar/test.html', id_cache).should == 'test.html-1'
101
+ @opf.create_unique_item_id('foo/bar/TEST.html', id_cache).should == 'TEST.html'
102
+ @opf.create_unique_item_id('foo/bar/test.html.1', id_cache).should == 'test.html.1'
103
+ end
104
+ end
105
+
106
+ context 'ncx is nil' do
107
+ before do
108
+ @opf.ncx = nil
109
+ end
110
+
111
+ it 'should not set ncx to manifest' do
112
+ doc = Nokogiri::XML(@opf.to_xml)
113
+ doc.search('manifest/item[id=ncx]').should be_empty
95
114
  end
96
115
  end
97
116
 
@@ -180,10 +199,8 @@ describe "EeePub::OPF" do
180
199
  spine.should_not be_nil
181
200
  spine = spine.search('itemref')
182
201
  spine.size.should == 2
183
- spine.each_with_index do |itemref, index|
184
- expect = @opf.spine[index]
185
- itemref.attribute('idref').value.should == expect
186
- end
202
+ spine[0].attribute('idref').value.should == 'a'
203
+ spine[1].attribute('idref').value.should == 'b'
187
204
  end
188
205
  end
189
206
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
7
+ - 3
8
8
  - 0
9
- version: 0.2.0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jugyo
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-05 00:00:00 +09:00
17
+ date: 2010-05-06 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -55,8 +55,11 @@ files:
55
55
  - README.md
56
56
  - Rakefile
57
57
  - VERSION
58
- - examples/simple.rb
58
+ - examples/basic.rb
59
+ - examples/files/bar.html
60
+ - examples/files/foo.html
59
61
  - lib/eeepub.rb
62
+ - lib/eeepub/basic.rb
60
63
  - lib/eeepub/container_item.rb
61
64
  - lib/eeepub/ncx.rb
62
65
  - lib/eeepub/ocf.rb
@@ -102,4 +105,4 @@ test_files:
102
105
  - spec/eeepub/opf_spec.rb
103
106
  - spec/eeepub_spec.rb
104
107
  - spec/spec_helper.rb
105
- - examples/simple.rb
108
+ - examples/basic.rb
data/examples/simple.rb DELETED
@@ -1,53 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + '/../lib')
2
- require 'rubygems'
3
- require 'eeepub'
4
- require 'fileutils'
5
-
6
- dir = 'simple'
7
- FileUtils.rm_rf(dir)
8
- FileUtils.mkdir(dir)
9
-
10
- epub_name = 'simple.epub'
11
- FileUtils.rm_f(epub_name)
12
-
13
- # Create sample html
14
- ['foo', 'bar'].each do |name|
15
- File.open(File.join(dir, "#{name}.html"), 'w') do |f|
16
- f << <<-HTML
17
- <?xml version="1.0" encoding="UTF-8"?>
18
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
19
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
20
- <head>
21
- <title></title>
22
- </head>
23
- <body>
24
- <h1>#{name}</h1>
25
- </body>
26
- </html>
27
- HTML
28
- end
29
- end
30
-
31
- # Create NCX
32
- EeePub::NCX.new(
33
- :uid => 'xxxx',
34
- :title => 'simple',
35
- :nav => [
36
- {:label => '1. foo', :content => 'foo.html'},
37
- {:label => '2. bar', :content => 'bar.html'}
38
- ]
39
- ).save(File.join(dir, 'toc.ncx'))
40
-
41
- # Create OPF
42
- EeePub::OPF.new(
43
- :title => 'simple',
44
- :identifier => {'ISBN' => '0-0000000-0-0'},
45
- :manifest => ['foo.html', 'bar.html'],
46
- :ncx => 'toc.ncx'
47
- ).save(File.join(dir, 'content.opf'))
48
-
49
- # Create OCF
50
- EeePub::OCF.new(
51
- :dir => dir,
52
- :container => 'content.opf'
53
- ).save(epub_name)