Skip to content

Commit

Permalink
Merge b1f2dde into f189a7a
Browse files Browse the repository at this point in the history
  • Loading branch information
skoji committed May 30, 2018
2 parents f189a7a + b1f2dde commit 6061366
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
13 changes: 6 additions & 7 deletions lib/gepub/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,15 @@ def set_singleton_methods_to_item(item)

# add an item(i.e. html, images, audios, etc) to Book.
# the added item will be referenced by the first argument in the EPUB container.
def add_item(href, io_or_filename = nil, id = nil, attributes = {})
item = @package.add_item(href,nil,id,attributes)
def add_item(href, attributes: {}, id: nil, content: nil)
item = @package.add_item(href, attributes: attributes, id: id, content: content)
set_singleton_methods_to_item(item)
item.add_content io_or_filename unless io_or_filename.nil?
item
end

# same as add_item, but the item will be added to spine of the EPUB.
def add_ordered_item(href, io_or_filename = nil, id = nil, attributes = {})
item = @package.add_ordered_item(href,io_or_filename,id,attributes)
def add_ordered_item(href, attributes: {}, id: nil, content: nil)
item = @package.add_ordered_item(href,attributes: attributes, id:id, content: content)
set_singleton_methods_to_item(item)
yield item if block_given?
item
Expand Down Expand Up @@ -279,7 +278,7 @@ def add_tocdata(toc_yaml)
end

def generate_nav_doc(title = 'Table of Contents')
add_item('nav.xhtml', StringIO.new(nav_doc(title)), 'nav').add_property('nav')
add_item('nav.xhtml', id: 'nav', content: StringIO.new(nav_doc(title))).add_property('nav')
end

def nav_doc(title = 'Table of Contents')
Expand Down Expand Up @@ -422,7 +421,7 @@ def cleanup_for_epub2
if (@toc.size == 0)
@toc << { :item => @package.manifest.item_list[@package.spine.itemref_list[0].idref] }
end
add_item('toc.ncx', StringIO.new(ncx_xml), 'ncx')
add_item('toc.ncx', id: 'ncx', content: StringIO.new(ncx_xml))
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions lib/gepub/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def primary_identifier(identifier, id = nil, type = nil)
@metadata.add_identifier identifier, unique_identifier, type
end

def add_item(href, io_or_filename = nil, id = nil, attributes = {})
def add_item(href, content:nil, id: nil, attributes: {})
item = @manifest.add_item(id, href, nil, attributes)
item.add_content(io_or_filename) unless io_or_filename.nil?
item.add_content(content) unless content.nil?
@spine.push(item) if @ordered
yield item if block_given?
item
Expand All @@ -183,11 +183,10 @@ def ordered
@ordered = nil
end

def add_ordered_item(href, io_or_filename = nil, id = nil, attributes = {})
def add_ordered_item(href, content:nil, id: nil, attributes: {})
raise 'do not call add_ordered_item within ordered block.' if @ordered
item = add_item(href, io_or_filename, id, attributes)
item = add_item(href, attributes: attributes, id:id, content: content)
@spine.push(item)

item
end

Expand Down
2 changes: 1 addition & 1 deletion lib/gepub/resource_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def create_one_file(val)
io = val[1]
end
name = "#{@dir_prefix}/#{name}" if !@dir_prefix.nil? && @dir_prefix.size > 0 && !name.start_with?('http')
@book.add_item(name, io)
@book.add_item(name, content: io)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
book.add_contributor('電子雑誌トルタル').display_seq(4).add_alternates('en' => 'eMagazine Torutaru')

imgfile = File.join(File.dirname(__FILE__), 'fixtures', 'testdata', 'image1.jpg')
book.add_item('img/image1.jpg',imgfile).cover_image
book.add_item('img/image1.jpg',content: imgfile).cover_image

# within ordered block, add_item will be added to spine.
book.ordered {
Expand Down
24 changes: 13 additions & 11 deletions spec/gepub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,18 @@
@book.date = "2010-05-05"
@book.identifier = "http://example.jp/foobar/"
@book.language = 'ja'
item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
item1 = @book.add_item('text/foobar.xhtml', id: 'c1')
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>'))
@book.spine.push(item1)

item2 = @book.add_ordered_item('text/barbar.xhtml',
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>'),
'c2')
id: 'c2',
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>'),
)
item2.toc_text 'test chapter'

item3 = @book.add_item('text/handler.xhtml',
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>')
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>')
)
item3.add_property('scripted')
item3.is_handler_of('application/x-some-media-type')
Expand All @@ -78,7 +79,7 @@
</body>
</html>
EOF
item3 = @book.add_ordered_item('text/nav.xhtml', StringIO.new(nav_string), 'nav').add_property('nav')
item3 = @book.add_ordered_item('text/nav.xhtml', id: 'nav', content: StringIO.new(nav_string)).add_property('nav')
end

it "should have titile" do
Expand Down Expand Up @@ -189,12 +190,12 @@
@book.date = "2010-05-05"
@book.identifier = "http://example.jp/foobar/"
@book.language = 'ja'
item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
item1 = @book.add_item('text/foobar.xhtml', id: 'c1')
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>'))
@book.spine.push(item1)
item2 = @book.add_ordered_item('text/barbar.xhtml',
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>'),
'c2')
id: 'c2',
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>'))
item2.toc_text 'test chapter'
@book.generate_epub(epubname)
epubcheck(epubname)
Expand All @@ -216,12 +217,13 @@
@book.date = "2015-05-05"
@book.identifier = "http://example.jp/foobar/"
@book.language = 'ja'
item1 = @book.add_item('text/foobar.xhtml',nil, 'c1')
item1 = @book.add_item('text/foobar.xhtml', id: 'c1')
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>'))
@book.spine.push(item1)
item2 = @book.add_ordered_item('text/barbar.xhtml',
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>'),
'c2')
id: 'c2',
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>')
)
@book.generate_epub(epubname)
epubcheck(epubname)

Expand Down

0 comments on commit 6061366

Please sign in to comment.