gepub 0.7.1 → 1.0.0beta1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/book_spec.rb CHANGED
@@ -115,7 +115,7 @@ describe GEPUB::Book do
115
115
  it 'returns main title' do
116
116
  book = GEPUB::Book.new()
117
117
  book.add_title 'sub title'
118
- book.add_title('the main title', nil, GEPUB::TITLE_TYPE::MAIN)
118
+ book.add_title('the main title', title_type: GEPUB::TITLE_TYPE::MAIN)
119
119
  expect(book.title.to_s).to eq('the main title')
120
120
  end
121
121
  end
@@ -136,7 +136,7 @@ describe GEPUB::Book do
136
136
  it 'returns titles in defined order' do
137
137
  book = GEPUB::Book.new()
138
138
  book.add_title 'sub title'
139
- book.add_title('the main title', nil, GEPUB::TITLE_TYPE::MAIN)
139
+ book.add_title('the main title', title_type: GEPUB::TITLE_TYPE::MAIN)
140
140
  expect(book.title_list[0].to_s).to eq('sub title')
141
141
  expect(book.title_list[1].to_s).to eq('the main title')
142
142
  end
data/spec/example_spec.rb CHANGED
@@ -75,7 +75,7 @@ describe 'GEPUB usage' do
75
75
  book.language = 'ja'
76
76
 
77
77
  # you can add metadata and its property using block
78
- book.add_title('GEPUBサンプル文書', nil, GEPUB::TITLE_TYPE::MAIN) {
78
+ book.add_title('GEPUBサンプル文書', title_type: GEPUB::TITLE_TYPE::MAIN) {
79
79
  |title|
80
80
  title.lang = 'ja'
81
81
  title.file_as = 'GEPUB Sample Book'
@@ -86,19 +86,19 @@ describe 'GEPUB usage' do
86
86
  'th' => 'GEPUB ตัวอย่าง (ญี่ปุ่น)')
87
87
  }
88
88
  # you can do the same thing using method chain
89
- book.add_title('これはあくまでサンプルです',nil, GEPUB::TITLE_TYPE::SUBTITLE).display_seq(2).add_alternates('en' => 'this book is just a sample.')
89
+ book.add_title('これはあくまでサンプルです',title_type: GEPUB::TITLE_TYPE::SUBTITLE).display_seq(2).add_alternates('en' => 'this book is just a sample.')
90
90
  book.add_creator('小嶋智') {
91
91
  |creator|
92
92
  creator.display_seq = 1
93
93
  creator.add_alternates('en' => 'KOJIMA Satoshi')
94
94
  }
95
- book.add_contributor('電書部').display_seq(1).add_alternates('en' => 'Denshobu')
96
- book.add_contributor('アサガヤデンショ').display_seq(2).add_alternates('en' => 'Asagaya Densho')
95
+ book.add_contributor('電書部', display_seq: 1, alternates: {'en' => 'Denshobu'})
96
+ book.add_contributor('アサガヤデンショ', display_seq: 2, alternates: { 'en' => 'Asagaya Densho' })
97
97
  book.add_contributor('湘南電書鼎談').display_seq(3).add_alternates('en' => 'Shonan Densho Teidan')
98
98
  book.add_contributor('電子雑誌トルタル').display_seq(4).add_alternates('en' => 'eMagazine Torutaru')
99
99
 
100
100
  imgfile = File.join(File.dirname(__FILE__), 'fixtures', 'testdata', 'image1.jpg')
101
- book.add_item('img/image1.jpg',imgfile).cover_image
101
+ book.add_item('img/image1.jpg',content: imgfile).cover_image
102
102
 
103
103
  # within ordered block, add_item will be added to spine.
104
104
  book.ordered {
@@ -110,5 +110,23 @@ describe 'GEPUB usage' do
110
110
  book.generate_epub(epubname)
111
111
  epubcheck(epubname)
112
112
  end
113
+
114
+ it 'should generate simple EPUB3 with some nil metadata' do
115
+ book = GEPUB::Book.new
116
+ book.identifier = 'http://example.jp/bookid_in_url'
117
+ book.title = 'GEPUB Sample Book'
118
+ book.creator = 'KOJIMA Satoshi'
119
+ book.language = 'ja'
120
+ book.publisher = nil
121
+
122
+ book.ordered do
123
+ item = book.add_item('name.xhtml')
124
+ item.add_content StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>')
125
+ end
126
+
127
+ epubname = File.join(File.dirname(__FILE__), 'example_test.epub')
128
+ book.generate_epub(epubname)
129
+ epubcheck(epubname)
130
+ end
113
131
  end
114
132
  end
@@ -0,0 +1,229 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/spec_helper.rb'
3
+ require 'rubygems'
4
+ require 'nokogiri'
5
+
6
+ describe GEPUB::Item do
7
+ it "should return atttributes" do
8
+ item = GEPUB::Item.new('theid', 'foo/bar.bar', 'application/xhtml+xml')
9
+ expect(item.itemid).to eq('theid')
10
+ expect(item.href).to eq('foo/bar.bar')
11
+ expect(item.mediatype).to eq('application/xhtml+xml')
12
+ end
13
+
14
+ it "should handle html" do
15
+ item = GEPUB::Item.new('id', 'text/foo.html')
16
+ expect(item.mediatype).to eq('application/xhtml+xml')
17
+ end
18
+
19
+ it "should handle xhtml" do
20
+ item = GEPUB::Item.new('id', 'text/foo.xhtml')
21
+ expect(item.mediatype).to eq('application/xhtml+xml')
22
+ end
23
+
24
+ it "should handle JPG" do
25
+ item = GEPUB::Item.new('id', 'img/foo.JPG')
26
+ expect(item.mediatype).to eq('image/jpeg')
27
+ end
28
+
29
+ it "should handle css" do
30
+ item = GEPUB::Item.new('id', 'img/foo.css')
31
+ expect(item.mediatype).to eq('text/css')
32
+ end
33
+
34
+ it "should handle javascript" do
35
+ item = GEPUB::Item.new('id', 'js/jQuery.js')
36
+ expect(item.mediatype).to eq('text/javascript')
37
+ end
38
+
39
+ end
40
+
41
+ describe GEPUB::Book do
42
+ before do
43
+ @book = GEPUB::Book.new('OEPBS/package.opf')
44
+ @book.title = 'thetitle'
45
+ @book.creator = "theauthor"
46
+ @book.contributor = "contributors contributors!"
47
+ @book.publisher = "thepublisher"
48
+ @book.date = "2010-05-05"
49
+ @book.identifier = "http://example.jp/foobar/"
50
+ @book.language = 'ja'
51
+ item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
52
+ item1.add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
53
+ @book.spine.push(item1)
54
+
55
+ item2 = @book.add_ordered_item('text/barbar.xhtml',
56
+ StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
57
+ 'c2')
58
+ item2.toc_text 'test chapter'
59
+
60
+ item3 = @book.add_item('text/handler.xhtml',
61
+ StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title><script>alert("foo");</script></head><body><p>this is scripted item</p></body></html>')
62
+ )
63
+ item3.add_property('scripted')
64
+ item3.is_handler_of('application/x-some-media-type')
65
+ @item3_id = item3.id
66
+
67
+ nav_string = <<EOF
68
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
69
+ <head></head>
70
+ <body>
71
+ <nav epub:type="toc" id="toc">
72
+ <h1>Table of contents</h1>
73
+ <ol>
74
+ <li><a href="foobar.xhtml">Chapter 1</a> </li>
75
+ <li> <a href="barbar.xhtml">Chapter 2</a></li>
76
+ </ol>
77
+ </nav>
78
+ </body>
79
+ </html>
80
+ EOF
81
+ item3 = @book.add_ordered_item('text/nav.xhtml', StringIO.new(nav_string), 'nav').add_property('nav')
82
+ end
83
+
84
+ it "should have title" do
85
+ expect(@book.title.to_s).to eq('thetitle')
86
+ end
87
+
88
+ it "should generate correct ncx" do
89
+ ncx = Nokogiri::XML.parse(@book.ncx_xml).root
90
+ expect(ncx.name).to eq('ncx')
91
+ expect(ncx.attributes['version'].value).to eq('2005-1')
92
+ ncx.namespaces['xmlns'] == 'http://www.daisy.org/z3986/2005/ncx/'
93
+ end
94
+
95
+ it "should have correct head in ncx" do
96
+ head = Nokogiri::XML.parse(@book.ncx_xml).at_xpath('/xmlns:ncx/xmlns:head')
97
+ expect(head).not_to be_nil
98
+ expect(head.at_xpath("xmlns:meta[@name='dtb:uid']")['content']).to eq("http://example.jp/foobar/")
99
+ expect(head.xpath("xmlns:meta[@name='dtb:depth']").size).to be > 0
100
+ expect(head.xpath("xmlns:meta[@name='dtb:totalPageCount']").size).to be > 0
101
+ expect(head.xpath("xmlns:meta[@name='dtb:maxPageNumber']").size).to be > 0
102
+ end
103
+
104
+ it "should have correct ncx doctitle" do
105
+ doctitle = Nokogiri::XML.parse(@book.ncx_xml).root
106
+
107
+ expect(doctitle.xpath('xmlns:docTitle').size).to be > 0
108
+ expect(doctitle.at_xpath('xmlns:docTitle/xmlns:text').text).to eq('thetitle')
109
+ end
110
+
111
+ it "should correct ncx navmap" do
112
+ ncx = Nokogiri::XML::parse(@book.ncx_xml).root
113
+
114
+ expect(ncx.xpath('xmlns:navMap').size).to be > 0
115
+ nav_point = ncx.at_xpath('xmlns:navMap/xmlns:navPoint')
116
+ expect(nav_point['id']).to eq('c2_')
117
+ expect(nav_point['playOrder']).to eq('1')
118
+
119
+ expect(nav_point.at_xpath('xmlns:navLabel/xmlns:text').content).to eq('test chapter')
120
+ nav_point.at_xpath('xmlns:content')['src'] == 'foobar2.html'
121
+
122
+ end
123
+
124
+ it "should create correct opf" do
125
+ opf = Nokogiri::XML.parse(@book.opf_xml).root
126
+ expect(opf.name).to eq('package')
127
+ expect(opf.namespaces['xmlns']).to eq('http://www.idpf.org/2007/opf')
128
+ expect(opf['version']).to eq('3.0')
129
+ expect(opf['unique-identifier']).to eq('BookId')
130
+ end
131
+
132
+ it "should have correct metadata in opf" do
133
+ opf = Nokogiri::XML.parse(@book.opf_xml).root
134
+ metadata = opf.xpath('xmlns:metadata').first
135
+ expect(metadata.at_xpath('dc:language', metadata.namespaces).content).to eq('ja')
136
+ #TODO: check metadata
137
+ end
138
+
139
+ it "should have correct manifest and spine in opf" do
140
+ opf = Nokogiri::XML.parse(@book.opf_xml).root
141
+
142
+ manifest = opf.at_xpath('xmlns:manifest')
143
+ expect(manifest.at_xpath('xmlns:item[@id="c1"]')['href']).to eq('text/foobar.xhtml')
144
+ expect(manifest.at_xpath('xmlns:item[@id="c1"]')['media-type']).to eq('application/xhtml+xml')
145
+
146
+ spine = opf.at_xpath('xmlns:spine')
147
+ expect(spine['toc']).to eq('ncx')
148
+ expect(spine.at_xpath('xmlns:itemref')['idref']).to eq('c1')
149
+ end
150
+
151
+ it "should have correct bindings in opf" do
152
+ opf = Nokogiri::XML.parse(@book.opf_xml).root
153
+ bindings = opf.at_xpath('xmlns:bindings')
154
+ expect(bindings.at_xpath('xmlns:mediaType')['handler']).to eq(@item3_id)
155
+ expect(bindings.at_xpath('xmlns:mediaType')['media-type']).to eq('application/x-some-media-type')
156
+ end
157
+
158
+ it "should have correct cover image id" do
159
+ item = @book.add_item("img/img.jpg").cover_image
160
+
161
+ opf = Nokogiri::XML.parse(@book.opf_xml).root
162
+
163
+ metadata = opf.at_xpath('xmlns:metadata')
164
+ meta = metadata.at_xpath("xmlns:meta[@name='cover']")
165
+ expect(meta['content']).to eq(item.itemid)
166
+ end
167
+
168
+ it "should generate correct epub" do
169
+ epubname = File.join(File.dirname(__FILE__), 'testepub.epub')
170
+ @book.generate_epub(epubname)
171
+ epubcheck(epubname)
172
+ end
173
+ it "should generate correct epub with buffer" do
174
+ epubname = File.join(File.dirname(__FILE__), 'testepub_buf.epub')
175
+ File.open(epubname, 'wb') {
176
+ |io|
177
+ io.write @book.generate_epub_stream.string
178
+ }
179
+ epubcheck(epubname)
180
+ end
181
+
182
+ it "should generate correct epub2.0" do
183
+ epubname = File.join(File.dirname(__FILE__), 'testepub2.epub')
184
+ @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '2.0'} )
185
+ @book.title = 'thetitle'
186
+ @book.creator = "theauthor"
187
+ @book.contributor = "contributors contributors!"
188
+ @book.publisher = "thepublisher"
189
+ @book.date = "2010-05-05"
190
+ @book.identifier = "http://example.jp/foobar/"
191
+ @book.language = 'ja'
192
+ item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
193
+ item1.add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
194
+ @book.spine.push(item1)
195
+ item2 = @book.add_ordered_item('text/barbar.xhtml',
196
+ StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
197
+ 'c2')
198
+ item2.toc_text 'test chapter'
199
+ @book.generate_epub(epubname)
200
+ epubcheck(epubname)
201
+ end
202
+ it 'should generate epub with extra file' do
203
+ epubname = File.join(File.dirname(__FILE__), 'testepub3.epub')
204
+ @book.add_optional_file('META-INF/foobar.xml', StringIO.new('<foo></foo>'))
205
+ @book.generate_epub(epubname)
206
+ epubcheck(epubname)
207
+ end
208
+
209
+ it 'should generate valid EPUB when @toc is empty' do
210
+ epubname = File.join(File.dirname(__FILE__), 'testepub4.epub')
211
+ @book = GEPUB::Book.new('OEPBS/package.opf', { 'version' => '3.0'} )
212
+ @book.title = 'thetitle'
213
+ @book.creator = "theauthor"
214
+ @book.contributor = "contributors contributors!"
215
+ @book.publisher = "thepublisher"
216
+ @book.date = "2015-05-05"
217
+ @book.identifier = "http://example.jp/foobar/"
218
+ @book.language = 'ja'
219
+ item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
220
+ item1.add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
221
+ @book.spine.push(item1)
222
+ item2 = @book.add_ordered_item('text/barbar.xhtml',
223
+ StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
224
+ 'c2')
225
+ @book.generate_epub(epubname)
226
+ epubcheck(epubname)
227
+
228
+ end
229
+ end
data/spec/gepub_spec.rb CHANGED
@@ -48,17 +48,16 @@ describe GEPUB::Book do
48
48
  @book.date = "2010-05-05"
49
49
  @book.identifier = "http://example.jp/foobar/"
50
50
  @book.language = 'ja'
51
- item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
52
- item1.add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
51
+ item1 = @book.add_item('text/foobar.xhtml',nil, id: 'c1', content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
53
52
  @book.spine.push(item1)
54
53
 
55
54
  item2 = @book.add_ordered_item('text/barbar.xhtml',
56
- StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
57
- 'c2')
58
- item2.toc_text 'test chapter'
55
+ content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
56
+ id: 'c2',
57
+ toc_text: 'test chapter')
59
58
 
60
59
  item3 = @book.add_item('text/handler.xhtml',
61
- StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title><script>alert("foo");</script></head><body><p>this is scripted item</p></body></html>')
60
+ content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title><script>alert("foo");</script></head><body><p>this is scripted item</p></body></html>')
62
61
  )
63
62
  item3.add_property('scripted')
64
63
  item3.is_handler_of('application/x-some-media-type')
@@ -78,10 +77,10 @@ describe GEPUB::Book do
78
77
  </body>
79
78
  </html>
80
79
  EOF
81
- item3 = @book.add_ordered_item('text/nav.xhtml', StringIO.new(nav_string), 'nav').add_property('nav')
80
+ item3 = @book.add_ordered_item('text/nav.xhtml', content: StringIO.new(nav_string), id: 'nav').add_property('nav')
82
81
  end
83
82
 
84
- it "should have titile" do
83
+ it "should have title" do
85
84
  expect(@book.title.to_s).to eq('thetitle')
86
85
  end
87
86
 
@@ -189,12 +188,12 @@ EOF
189
188
  @book.date = "2010-05-05"
190
189
  @book.identifier = "http://example.jp/foobar/"
191
190
  @book.language = 'ja'
192
- item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
191
+ item1 = @book.add_item('text/foobar.xhtml',id: 'c1')
193
192
  item1.add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
194
193
  @book.spine.push(item1)
195
194
  item2 = @book.add_ordered_item('text/barbar.xhtml',
196
- StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
197
- 'c2')
195
+ content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
196
+ id: 'c2')
198
197
  item2.toc_text 'test chapter'
199
198
  @book.generate_epub(epubname)
200
199
  epubcheck(epubname)
@@ -216,12 +215,13 @@ EOF
216
215
  @book.date = "2015-05-05"
217
216
  @book.identifier = "http://example.jp/foobar/"
218
217
  @book.language = 'ja'
219
- item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
218
+ item1 = @book.add_item('text/foobar.xhtml',id: 'c1')
220
219
  item1.add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c1</title></head><body><p>the first page</p></body></html>'))
221
220
  @book.spine.push(item1)
222
221
  item2 = @book.add_ordered_item('text/barbar.xhtml',
223
- StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
224
- 'c2')
222
+ content: StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>second page, whith is test chapter.</p></body></html>'),
223
+ id: 'c2')
224
+ @book.spine.push(item2)
225
225
  @book.generate_epub(epubname)
226
226
  epubcheck(epubname)
227
227
 
@@ -94,15 +94,15 @@ describe GEPUB::Metadata do
94
94
 
95
95
  it 'should write and read title with type' do
96
96
  metadata = GEPUB::Metadata.new
97
- metadata.add_title('The Main Title', 'maintitle', GEPUB::TITLE_TYPE::MAIN)
97
+ metadata.add_title('The Main Title', id: 'maintitle', title_type: GEPUB::TITLE_TYPE::MAIN)
98
98
  expect(metadata.title.to_s).to eq('The Main Title')
99
99
  expect(metadata.title.title_type.to_s).to eq('main')
100
100
  end
101
101
 
102
102
  it 'should write and read multipletitle with type' do
103
103
  metadata = GEPUB::Metadata.new
104
- metadata.add_title('The Main Title', 'maintitle', GEPUB::TITLE_TYPE::MAIN)
105
- metadata.add_title('The Book Series', 'series', GEPUB::TITLE_TYPE::COLLECTION).group_position(1)
104
+ metadata.add_title('The Main Title', id: 'maintitle', title_type: GEPUB::TITLE_TYPE::MAIN)
105
+ metadata.add_title('The Book Series', id: 'series', title_type: GEPUB::TITLE_TYPE::COLLECTION).group_position(1)
106
106
  expect(metadata.title.to_s).to eq('The Main Title')
107
107
  expect(metadata.title.title_type.to_s).to eq('main')
108
108
 
@@ -113,14 +113,14 @@ describe GEPUB::Metadata do
113
113
 
114
114
  it 'should handle alternate-script metadata of creator, not using method chain' do
115
115
  metadata = GEPUB::Metadata.new
116
- metadata.add_creator('TheCreator', 'author', 'aut').display_seq(1).file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
116
+ metadata.add_creator('TheCreator', id: 'author', role: 'aut').display_seq(1).file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
117
117
  expect(metadata.creator.to_s).to eq('TheCreator')
118
118
  expect(metadata.creator.to_s('ja')).to eq('作成者')
119
119
  end
120
120
 
121
121
  it 'should handle alternate-script metadata of creator, not using method chain' do
122
122
  metadata = GEPUB::Metadata.new
123
- m = metadata.add_creator('TheCreator', 'author', 'aut')
123
+ m = metadata.add_creator('TheCreator', id: 'author', role: 'aut')
124
124
  m.display_seq = 1
125
125
  m.file_as = 'Creator, The'
126
126
  m.add_alternates({ 'ja-JP' => '作成者' })
@@ -131,7 +131,7 @@ describe GEPUB::Metadata do
131
131
 
132
132
  it 'should detect duplicate id' do
133
133
  metadata = GEPUB::Metadata.new
134
- metadata.add_creator('TheCreator', 'id', 'aut')
134
+ metadata.add_creator('TheCreator', id: 'id', role: 'aut')
135
135
  expect { metadata.add_title('TheTitle', 'id') }.to raise_error(RuntimeError, "id 'id' is already in use.")
136
136
  end
137
137
 
@@ -187,7 +187,7 @@ describe GEPUB::Metadata do
187
187
 
188
188
  it 'should generate metadata with creator refiner' do
189
189
  metadata = GEPUB::Metadata.new
190
- metadata.add_creator('TheCreator', nil, 'aut').display_seq(1).file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
190
+ metadata.add_creator('TheCreator', role: 'aut').display_seq(1).file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
191
191
  builder = Nokogiri::XML::Builder.new { |xml|
192
192
  xml.package('xmlns' => "http://www.idpf.org/2007/opf",'version' => "3.0",'unique-identifier' => "pub-id",'xml:lang' => "ja") {
193
193
  metadata.to_xml(xml)
@@ -205,7 +205,7 @@ describe GEPUB::Metadata do
205
205
 
206
206
  it 'should generate metadata with old style meta tag' do
207
207
  metadata = GEPUB::Metadata.new
208
- metadata.add_creator('TheCreator', nil, 'aut').display_seq(1).file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
208
+ metadata.add_creator('TheCreator', role: 'aut').display_seq(1).file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
209
209
  metadata.add_oldstyle_meta(nil, { 'name' => 'cover', 'content' => 'cover.jpg' })
210
210
  builder = Nokogiri::XML::Builder.new { |xml|
211
211
  xml.package('xmlns' => "http://www.idpf.org/2007/opf",'version' => "3.0",'unique-identifier' => "pub-id",'xml:lang' => "ja") {
@@ -0,0 +1,191 @@
1
+ require_relative '../lib/gepub/item.rb'
2
+ attrs = GEPUB::Item::ATTRIBUTES.select do |attr|
3
+ attr != 'href'
4
+ end.map do |attr|
5
+ attr.sub('-', '_')
6
+ end
7
+ attrs << "toc_text"
8
+ attrs_arguments_string = attrs.map { |attr| "#{attr}: nil" }.join(',')
9
+ attrs_internal_string = "{ " + attrs.map { |attr| "#{attr}: #{attr}"}.join(',') + " }"
10
+ File.write(File.join(File.dirname(__FILE__), "../lib/gepub/book_add_item.rb"), <<EOF)
11
+ module GEPUB
12
+ class Book
13
+ # add an item(i.e. html, images, audios, etc) to Book.
14
+ # the added item will be referenced by the first argument in the EPUB container.
15
+ def add_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content: nil,
16
+ #{attrs_arguments_string},
17
+ attributes: {})
18
+ content, id, attributes = handle_deprecated_add_item_arguments(deprecated_content, deprecated_id, deprecated_attributes, content, id, attributes)
19
+ add_item_internal(href, content: content, item_attributes: #{attrs_internal_string}, attributes: attributes, ordered: false)
20
+ end
21
+
22
+ # same as add_item, but the item will be added to spine of the EPUB.
23
+ def add_ordered_item(href, deprecated_content = nil, deprecated_id = nil, deprecated_attributes = nil, content:nil,
24
+ #{attrs_arguments_string},
25
+ attributes: {})
26
+ content, id, attributes = handle_deprecated_add_item_arguments(deprecated_content, deprecated_id, deprecated_attributes, content, id, attributes)
27
+ add_item_internal(href, content: content, item_attributes: #{attrs_internal_string}, attributes: attributes, ordered: true)
28
+ end
29
+ end
30
+ end
31
+ EOF
32
+
33
+ require_relative '../lib/gepub/dsl_util.rb'
34
+ require_relative '../lib/gepub/meta.rb'
35
+
36
+ refiners = GEPUB::Meta::REFINERS.map do |refiner|
37
+ refiner.sub('-', '_')
38
+ end
39
+
40
+ refiners_arguments_string = refiners.map { |refiner| "#{refiner}: nil" }.join(',')
41
+ refiners_arguments_set_string = refiners.map { |refiner| "#{refiner}: #{refiner}" }.join(',')
42
+ refiners_string = "[" + GEPUB::Meta::REFINERS.map { |refiner| "{ value: #{refiner.sub('-', '_')}, name: '#{refiner}'}" }.join(",") + "]"
43
+
44
+ meta_attr_arguments_string = "lang: nil, alternates: {}"
45
+ meta_attr_arguments_set_string = "lang: lang, alternates: alternates"
46
+
47
+ File.write(File.join(File.dirname(__FILE__), "../lib/gepub/metadata_add.rb"), <<EOF)
48
+ module GEPUB
49
+ class Metadata
50
+ CONTENT_NODE_LIST = ['identifier', 'title', 'language', 'contributor', 'creator', 'coverage', 'date','description','format','publisher','relation','rights','source','subject','type'].each {
51
+ |node|
52
+ define_method(node + '_list') { @content_nodes[node].dup.sort_as_meta }
53
+ define_method(node + '_clear') {
54
+ if !@content_nodes[node].nil?
55
+ @content_nodes[node].each { |x| unregister_meta(x) };
56
+ @content_nodes[node] = []
57
+ end
58
+ }
59
+
60
+ next if node == 'title'
61
+
62
+ define_method(node, ->(content=UNASSIGNED, deprecated_id=nil, id:nil,
63
+ #{refiners_arguments_string},
64
+ #{meta_attr_arguments_string}) {
65
+ if unassigned?(content)
66
+ get_first_node(node)
67
+ else
68
+ if deprecated_id
69
+ warn "secound argument is deprecated. use id: keyword argument"
70
+ id = deprecated_id
71
+ end
72
+ send(node + "_clear")
73
+ add_metadata(node, content, id: id, #{refiners_arguments_set_string}, #{meta_attr_arguments_set_string})
74
+ end
75
+ })
76
+
77
+ define_method(node+'=') {
78
+ |content|
79
+ send(node + "_clear")
80
+ return if content.nil?
81
+ if node == 'date'
82
+ add_date(content)
83
+ else
84
+ add_metadata(node, content)
85
+ end
86
+ }
87
+
88
+ next if ["identifier", "date", "creator", "contributor"].include?(node)
89
+
90
+ define_method('add_' + node) {
91
+ |content, id|
92
+ add_metadata(node, content, id: id)
93
+ }
94
+ }
95
+
96
+ def add_title(content, deprecated_id = nil, deprecated_title_type = nil, id: nil,
97
+ #{refiners_arguments_string},
98
+ #{meta_attr_arguments_string})
99
+ if deprecated_id
100
+ warn 'second argument for add_title is deprecated. use id: instead'
101
+ id = deprecated_id
102
+ end
103
+ if deprecated_title_type
104
+ warn 'third argument for add_title is deprecated. use title_type: instead'
105
+ title_type = deprecated_title_type
106
+ end
107
+ meta = add_metadata('title', content, id: id,
108
+ #{refiners_arguments_set_string},
109
+ #{meta_attr_arguments_set_string})
110
+ yield meta if block_given?
111
+ meta
112
+ end
113
+
114
+ def add_person(name, content, deprecated_id = nil, deprecated_role = nil, id: nil,
115
+ #{refiners_arguments_string},
116
+ #{meta_attr_arguments_string})
117
+ if deprecated_id
118
+ warn 'second argument for add_person is deprecated. use id: instead'
119
+ id = deprecated_id
120
+ end
121
+ if deprecated_role
122
+ warn 'third argument for add_person is deprecated. use role: instead'
123
+ role = deprecated_role
124
+ end
125
+ meta = add_metadata(name, content, id: id,
126
+ #{refiners_arguments_set_string},
127
+ #{meta_attr_arguments_set_string})
128
+ yield meta if block_given?
129
+ meta
130
+ end
131
+
132
+ def add_creator(content, deprecated_id = nil, deprecated_role = nil, id: nil,
133
+ #{refiners_arguments_string},
134
+ #{meta_attr_arguments_string})
135
+ if deprecated_id
136
+ warn 'second argument for add_creator is deprecated. use id: instead'
137
+ id = deprecated_id
138
+ end
139
+ if deprecated_role
140
+ warn 'third argument for add_creator is deprecated. use role: instead'
141
+ role = deprecated_role
142
+ end
143
+ role = 'aut' if role.nil?
144
+ meta = add_person('creator', content, id: id,
145
+ #{refiners_arguments_set_string},
146
+ #{meta_attr_arguments_set_string})
147
+ yield meta if block_given?
148
+ meta
149
+ end
150
+
151
+ def add_contributor(content, deprecated_id = nil, deprecated_role = nil, id: nil,
152
+ #{refiners_arguments_string},
153
+ #{meta_attr_arguments_string})
154
+ if deprecated_id
155
+ warn 'second argument for add_contributor is deprecated. use id: instead'
156
+ id = deprecated_id
157
+ end
158
+ if deprecated_role
159
+ warn 'third argument for add_contributor is deprecated. use role: instead'
160
+ role = deprecated_role
161
+ end
162
+ meta = add_person('contributor', content, id: id,
163
+ #{refiners_arguments_set_string},
164
+ #{meta_attr_arguments_set_string})
165
+ yield meta if block_given?
166
+ meta
167
+ end
168
+
169
+ def add_metadata(name, content, id: nil, itemclass: Meta,
170
+ #{refiners_arguments_string},
171
+ #{meta_attr_arguments_string}
172
+ )
173
+ meta = add_metadata_internal(name, content, id: id, itemclass: itemclass)
174
+ #{refiners_string}.each do |refiner|
175
+ if refiner[:value]
176
+ meta.refine(refiner[:name], refiner[:value])
177
+ end
178
+ end
179
+ if lang
180
+ meta.lang = lang
181
+ end
182
+ if alternates
183
+ meta.add_alternates alternates
184
+ end
185
+ yield meta if block_given?
186
+ meta
187
+ end
188
+ end
189
+ end
190
+ EOF
191
+