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

added option to display toc in the document for HTML and PDF output

This commit is contained in:
Elisabeth Henry 2016-02-24 20:51:39 +01:00
parent 8d763992ff
commit 0a0f21c6b5
7 changed files with 62 additions and 3 deletions

@ -7,6 +7,7 @@ lang: en
# Generic options (though not used with LaTeX)
## Number chapters and sections, but not below that
numbering: 2
display_toc: true
# Display foot notes as side notes
#side_notes: true

@ -47,6 +47,8 @@ output.odt:str # Output file name for ODT rendering
# Misc options
numbering:int:1 # The maximum heading levels to number (0: no numbering, 1: only chapters, ..., 6: all)
display_toc:bool:false # If true, display a table of content in the document
toc_name:str:Table of contents # Name of the table of contents if toc is displayed in line
autoclean:bool:true # Toggles cleaning of input markdown (not used for LaTeX)
verbose:bool:false # Toggle verbose mode
side_notes:bool:false # Display footnotes as side notes in HTML/Epub

@ -19,6 +19,7 @@ use escape::escape_html;
use token::Token;
use book::{Book, Number};
use error::{Error,Result};
use toc::Toc;
use mustache;
@ -28,6 +29,8 @@ use mustache;
pub struct HtmlRenderer<'a> {
book: &'a Book,
toc: Toc,
link_number: u32,
pub footnotes: Vec<(String, String)>,
pub current_chapter: [i32;6],
pub current_numbering: i32,
@ -44,6 +47,8 @@ impl<'a> HtmlRenderer<'a> {
pub fn new(book: &'a Book) -> HtmlRenderer<'a> {
HtmlRenderer {
book: book,
toc: Toc::new(),
link_number: 0,
current_chapter: [0, 0, 0, 0, 0, 0],
current_numbering: book.get_i32("numbering").unwrap(),
current_hide: false,
@ -104,12 +109,26 @@ impl<'a> HtmlRenderer<'a> {
}
content.push_str(&self.render_html(v));
}
let toc = self.toc.render();
// If display_toc, display the toc inline
if self.book.get_bool("display_toc").unwrap() {
content = format!("<h1>{}</h1>
<div id = \"toc\">
{}
</div>
{}",
self.book.get_str("toc_name").unwrap(),
&toc,
content);
}
let template = mustache::compile_str(try!(self.book.get_template("html.template")).as_ref());
let data = self.book.get_mapbuilder("none")
.insert_str("content", content)
.insert_str("style",
&try!(self.book.get_template("html.css")))
.insert_str("toc", toc)
.build();
let mut res:Vec<u8> = vec!();
@ -184,7 +203,12 @@ impl<'a> HtmlRenderer<'a> {
} else {
self.render_vec(vec)
};
format!("<h{}>{}</h{}>\n", n, s, n)
self.link_number += 1;
if n <= self.current_numbering {
self.toc.add(n, format!("#link-{}", self.link_number), s.clone());
}
format!("<h{} id = \"link-{}\">{}</h{}>\n",
n, self.link_number, s, n)
},
Token::Emphasis(ref vec) => format!("<em>{}</em>", self.render_vec(vec)),
Token::Strong(ref vec) => format!("<b>{}</b>", self.render_vec(vec)),

@ -58,6 +58,17 @@ impl<'a> LatexRenderer<'a> {
/// Render latex in a string
pub fn render_book(&mut self) -> Result<String> {
let mut content = String::from("");
// set tex numbering and toc display to book's parameters
let numbering = self.book.get_i32("numbering").unwrap() - 1;
content.push_str(&format!("\\setcounter{{tocdepth}}{{{}}}
\\setcounter{{secnumdepth}}{{{}}}\n",
numbering, numbering));
if self.book.get_bool("display_toc").unwrap() {
content.push_str("\\tableofcontents\n");
}
for &(n, ref v) in &self.book.chapters {
self.current_chapter = n;
content.push_str(&self.render_vec(v, true));

@ -60,7 +60,8 @@ impl Toc {
*level = elem.level;
}
let spaces:String = iter::repeat(' ').take(elem.level as usize).collect();
content.push_str(&format!("{}<li>{}\n", spaces, elem.title));
content.push_str(&format!("{}<li><a href = \"{}\">{}</a>\n",
spaces, elem.url, elem.title));
content.push_str(&self.render_vec(x, level));
for i in (elem.level..*level).rev() {

@ -124,6 +124,15 @@ impl Zipper {
/// generate a pdf file into given file name
pub fn generate_pdf(&mut self, command: &str, tex_file: &str, pdf_file: &str) -> Result<String> {
// first pass
let dir = try!(env::current_dir().map_err(|_| Error::Zipper("could not get current directory".to_owned())));
try!(env::set_current_dir(&self.path).map_err(|_| Error::Zipper("could not change current directory".to_owned())));
let _ = Command::new(command)
.arg(tex_file)
.output();
try!(env::set_current_dir(dir).map_err(|_| Error::Zipper("could not change back to old directory".to_owned())));
// second pass
let mut command = Command::new(command);
command.arg(tex_file);
self.run_command(command, pdf_file)

@ -11,15 +11,26 @@ body {
margin-left: 15%;
margin-right: 15%;
}
nav {
display: none;
}
#toc ul {
list-style-type: none;
}
</style>
</head>
</head>
<body>
<nav>
{{{toc}}}
</nav>
<header>
<h1 class="title">{{title}}</h1>
<h2 class="author">{{author}}</h2>
</header>
{{{content}}}
</body>
</html>