Skip to content

Commit

Permalink
Anchor fragments for sections in toc.html & NCX.
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph committed Jul 24, 2010
1 parent 74ebb5f commit 2e49ab8
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/epub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def build_oebps_container
def build_ncx
x = 0
curse = lambda { |xml, section|
if cmpt = url_for_component_child(section.node || section.heading)
if cmpt = url_for_section(section)
xml.navPoint(:id => "navPoint#{x+=1}", :playOrder => x) {
xml.navLabel { xml.text_(section.heading_text) }
xml.content(:src => cmpt)
Expand Down Expand Up @@ -142,10 +142,15 @@ def write_components
@component_paths['toc'] = working_path(OEBPS, "toc.html")
outline_html = outliner.to_html { |section, below|
heading = section.heading_text
heading ||= '<br class="anon" />' unless below.empty?
if heading
heading = '<a href="'+url_for_section(section)+'">'+heading+'</a>'
elsif !below.empty?
heading = '<br class="anon" />'
end
heading
}
componentizer.write_component(
Nokogiri::HTML::Document.parse(outliner.to_html).root,
Nokogiri::HTML::Document.parse(outline_html).root,
@component_paths['toc'],
&xhtmlize
)
Expand Down Expand Up @@ -297,12 +302,15 @@ def componentizer
end


def url_for_component_child(node)
while node && node.respond_to?(:parent)
if c = componentizer.components.index(node)
return "part#{c+1}.html"
def url_for_section(section)
sid = section.heading['id'] || section.node['id']
sid = "#"+sid if sid && !sid.empty?
n = section.node || section.heading
while n && n.respond_to?(:parent)
if cmptIndex = componentizer.components.index(n)
return "part#{cmptIndex+1}.html#{sid}"
end
node = node.parent
n = n.parent
end
nil
end
Expand Down

0 comments on commit 2e49ab8

Please sign in to comment.