Skip to content

Commit

Permalink
implements landmarks (fixes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
skoji committed Nov 6, 2018
1 parent 633a28d commit 04a7090
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
24 changes: 22 additions & 2 deletions lib/gepub/book.rb
Expand Up @@ -124,6 +124,7 @@ def initialize(path='OEBPS/package.opf', attributes = {}, &block)
end
@package = Package.new(path, attributes)
@toc = []
@landmarks = []
if block
block.arity < 1 ? instance_eval(&block) : block[self]
end
Expand Down Expand Up @@ -151,9 +152,12 @@ def set_singleton_methods_to_item(item)
metaclass.send(:define_method, :toc, Proc.new {
toc
})
landmarks = @landmarks
metaclass.send(:define_method, :landmarks, Proc.new {
landmarks
})
bindings = @package.bindings
metaclass.send(:define_method, :bindings,
Proc.new {
metaclass.send(:define_method, :bindings, Proc.new {
bindings
})

Expand Down Expand Up @@ -293,6 +297,19 @@ def write_toc xml_doc, tocs
}
}
end
def write_landmarks xml_doc, landmarks
xml_doc.ol {
landmarks.each {
|landmark|
id = landmark[:id].nil? ? "" : "##{x[:id]}"
landmark_title = landmark[:title]
type = landmark[:type]
xml_doc.li {
xml_doc.a({'href' => landmark[:item].href + id, 'epub:type' => landmark[:type]}, landmark_title)
}
}
}
end
# build nav
builder = Nokogiri::XML::Builder.new {
|doc|
Expand All @@ -303,6 +320,9 @@ def write_toc xml_doc, tocs
doc.h1 "#{title}"
write_toc(doc, stacked_toc[:tocs])
}
doc.nav('epub:type' => 'landmarks', 'id' => 'landmarks') {
write_landmarks(doc, @landmarks)
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/gepub/item.rb
Expand Up @@ -89,7 +89,8 @@ def is_handler_of media_type
self
end

def landmarks(type:, title:)
def landmark(type:, title:, id: nil)
landmarks.push(:type => type, :title => title, :item => self, :id => id)
self
end

Expand Down
20 changes: 9 additions & 11 deletions spec/example_spec.rb
Expand Up @@ -162,8 +162,8 @@

# within ordered block, add_item will be added to spine.
book.ordered {
book.add_item('text/cover.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>cover</title></head><body><h1>the book title</h1><img src="../img/image1.jpg" /></body></html>')).landmarks(type: 'cover', title: '表紙')
book.add_item('text/chap1.xhtml').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>')).toc_text('Chapter 1').landmarks(type: 'bodymatter', title: '本文')
book.add_item('text/cover.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>cover</title></head><body><h1>the book title</h1><img src="../img/image1.jpg" /></body></html>')).landmark(type: 'cover', title: '表紙')
book.add_item('text/chap1.xhtml').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>')).toc_text('Chapter 1').landmark(type: 'bodymatter', title: '本文')
book.add_item('text/chap1-1.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c2</title></head><body><p>the second page</p></body></html>')) # do not appear on table of contents
book.add_item('text/chap2.xhtml').add_content(StringIO.new('<html xmlns="http://www.w3.org/1999/xhtml"><head><title>c3</title></head><body><p>the third page</p></body></html>')).toc_text('Chapter 2')
}
Expand All @@ -173,20 +173,18 @@
xml = Nokogiri::XML::Document.parse book.nav_doc

# check toc
tocs = xml.at_xpath("//xmlns:nav[@epub:type='toc']").xpath("//xmlns:li")
tocs = xml.xpath("//xmlns:nav[@epub:type='toc']/xmlns:ol/xmlns:li")
expect(tocs.size).to eq 2
expect(tocs[0].content.strip).to eq 'Chapter 1'
expect(tocs[1].content.strip).to eq 'Chapter 2'

# check landmarks
landmarks = xml.at_xpath("//xmlns:nav[@epub:type='landmarks']").xpath("//xmlns:ol/xmlns:li/xmlns:a")
expect(landmarks.size).to eq 3
expect(landmarks[0]['@epub:type']).to eq 'cover'
expect(landmarks[0]['href']).to eq 'cover.xhtml'
expect(landmarks[1]['@epub:type']).to eq 'toc'
expect(landmarks[1]['href']).to eq '#toc'
expect(landmarks[2]['@epub:type']).to eq 'bodymatter'
expect(landmarks[2]['href']).to eq 'chapt.xhtml'
landmarks = xml.xpath("//xmlns:nav[@epub:type='landmarks']/xmlns:ol/xmlns:li/xmlns:a")
expect(landmarks.size).to eq 2
expect(landmarks[0]['epub:type']).to eq 'cover'
expect(landmarks[0]['href']).to eq 'text/cover.xhtml'
expect(landmarks[1]['epub:type']).to eq 'bodymatter'
expect(landmarks[1]['href']).to eq 'text/chap1.xhtml'
book.generate_epub(epubname)
epubcheck(epubname)
end
Expand Down

0 comments on commit 04a7090

Please sign in to comment.