1
0
mirror of https://github.com/lise-henry/crowbook synced 2024-11-18 00:13:55 +01:00

Epub: nav.xhtml has full toc (but not yet toc.ncx)

This commit is contained in:
Elisabeth Henry 2016-02-24 22:31:24 +01:00
parent ab47a0d3bb
commit 02f296ff03
5 changed files with 36 additions and 26 deletions

@ -43,9 +43,11 @@ pub struct EpubRenderer<'a> {
impl<'a> EpubRenderer<'a> {
/// Creates a new Epub renderer
pub fn new(book: &'a Book) -> EpubRenderer<'a> {
let mut html = HtmlRenderer::new(book);
html.toc.numbered(true);
EpubRenderer {
book: book,
html: HtmlRenderer::new(book),
html: html,
toc: vec!(),
}
}
@ -59,6 +61,7 @@ impl<'a> EpubRenderer<'a> {
// Write chapters
for (i, &(n, ref v)) in self.book.chapters.iter().enumerate() {
self.html.filename = filenamer(i);
self.html.current_hide = false;
let book_numbering = self.book.get_i32("numbering").unwrap();
match n {
@ -261,13 +264,7 @@ impl<'a> EpubRenderer<'a> {
/// Render nav.xhtml
fn render_nav(&self) -> Result<String> {
let mut content = String::new();
for (x, ref title) in self.toc.iter().enumerate() {
let link = filenamer(x);
content.push_str(&format!("<li><a href = \"{}\">{}</a></li>\n",
link,
title));
}
let content = self.html.toc.render();
let template = mustache::compile_str(if self.book.get_i32("epub.version").unwrap() == 3 {epub3::NAV} else {NAV});
let data = self.book.get_mapbuilder("none")

@ -29,12 +29,13 @@ use mustache;
pub struct HtmlRenderer<'a> {
book: &'a Book,
toc: Toc,
pub toc: Toc,
link_number: u32,
pub footnotes: Vec<(String, String)>,
pub current_chapter: [i32;6],
pub current_numbering: i32,
pub current_hide: bool,
pub filename: String,
table_head: bool,
footnote_number: u32,
@ -56,7 +57,8 @@ impl<'a> HtmlRenderer<'a> {
footnote_number: 0,
footnotes: vec!(),
epub3: false,
verbatim: false
verbatim: false,
filename: String::new(),
}
}
@ -205,7 +207,11 @@ impl<'a> HtmlRenderer<'a> {
};
self.link_number += 1;
if n <= self.current_numbering {
self.toc.add(n, format!("#link-{}", self.link_number), s.clone());
self.toc.add(n,
format!("{}#link-{}",
self.filename,
self.link_number),
s.clone());
}
format!("<h{} id = \"link-{}\">{}</h{}>\n",
n, self.link_number, s, n)

@ -3,7 +3,8 @@ use std::iter;
/// A structure for manipulating Table Of Content
pub struct Toc {
elements: Vec<TocElement>
elements: Vec<TocElement>,
numbered: bool,
}
impl Toc {
@ -11,9 +12,15 @@ impl Toc {
pub fn new() -> Toc {
Toc {
elements: vec!(),
numbered: false,
}
}
/// Sets numbering of the Toc
pub fn numbered(&mut self, numbered: bool) {
self.numbered = numbered;
}
/// Adds an element
pub fn add(&mut self, level: i32, url: String, title: String) {
let element = TocElement::new(level, url, title);
@ -28,8 +35,9 @@ impl Toc {
let mut level = 0;
output.push_str(&self.render_vec(&mut x, &mut level));
for i in (0..level).rev() {
output.push_str(&format!("{}</ul>",
iter::repeat(' ').take(i as usize).collect::<String>()));
output.push_str(&format!("{}</{}>",
iter::repeat(' ').take(i as usize).collect::<String>(),
if self.numbered {"ol"} else {"ul"}));
}
output
}
@ -48,14 +56,16 @@ impl Toc {
if elem.level > *level {
for i in *level..elem.level {
content.push_str(&format!("{}<ul>\n",
iter::repeat(' ').take(i as usize).collect::<String>()));
content.push_str(&format!("{}{}\n",
iter::repeat(' ').take(i as usize).collect::<String>(),
if self.numbered {"<ol>"} else {"<ul>"}));
*level = elem.level;
}
} else if elem.level < *level {
for i in (elem.level..*level).rev() {
content.push_str(&format!("{}</ul>\n",
iter::repeat(' ').take(i as usize).collect::<String>()));
content.push_str(&format!("{}</{}>\n",
iter::repeat(' ').take(i as usize).collect::<String>(),
if self.numbered {"ol"} else {"ul"}));
}
*level = elem.level;
}
@ -65,8 +75,9 @@ impl Toc {
content.push_str(&self.render_vec(x, level));
for i in (elem.level..*level).rev() {
content.push_str(&format!("{}</ul>\n",
iter::repeat(' ').take(i as usize).collect::<String>()));
content.push_str(&format!("{}</{}>\n",
iter::repeat(' ').take(i as usize).collect::<String>(),
if self.numbered {"ol"} else {"ul"}));
}
*level = elem.level;
content.push_str(&format!("{}</li>\n", spaces));

@ -11,9 +11,7 @@
<body>
<div id="toc">
<h1 id="toc-title">{{title}}</h1>
<ol class="toc">
{{{{content}}}}
</ol>
{{{{content}}}}
</div>
</body>
</html>

@ -10,9 +10,7 @@
<body>
<nav epub:type = "toc" id="toc">
<h1 id="toc-title">{{title}}</h1>
<ol class="toc">
{{{content}}}
</ol>
{{{content}}}
</nav>
</body>
</html>