mdbook/src/renderer/html_handlebars/helpers/toc.rs

151 lines
4.8 KiB
Rust
Raw Normal View History

2017-06-13 20:53:25 +08:00
use std::collections::BTreeMap;
2018-07-23 12:45:01 -05:00
use std::path::Path;
use crate::utils;
2018-08-05 15:08:47 +08:00
use handlebars::{Context, Handlebars, Helper, HelperDef, Output, RenderContext, RenderError};
use pulldown_cmark::{html, Event, Parser, Tag};
// Handlebars helper to construct TOC
#[derive(Clone, Copy)]
pub struct RenderToc {
pub no_section_label: bool,
}
impl HelperDef for RenderToc {
2018-08-21 10:58:44 -05:00
fn call<'reg: 'rc, 'rc>(
&self,
_h: &Helper<'reg, 'rc>,
_r: &'reg Handlebars,
ctx: &'rc Context,
rc: &mut RenderContext<'reg>,
out: &mut dyn Output,
2018-08-21 10:58:44 -05:00
) -> Result<(), RenderError> {
// get value from context data
// rc.get_path() is current json parent path, you should always use it like this
// param is the key of value you want to display
2018-08-05 15:08:47 +08:00
let chapters = rc.evaluate_absolute(ctx, "chapters", true).and_then(|c| {
serde_json::value::from_value::<Vec<BTreeMap<String, String>>>(c.clone())
.map_err(|_| RenderError::new("Could not decode the JSON data"))
})?;
let current = rc
2018-08-05 15:08:47 +08:00
.evaluate_absolute(ctx, "path", true)?
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");
2018-08-05 15:08:47 +08:00
out.write("<ol class=\"chapter\">")?;
let mut current_level = 1;
2017-06-13 20:53:25 +08:00
for item in chapters {
// Spacer
if item.get("spacer").is_some() {
2018-08-05 15:08:47 +08:00
out.write("<li class=\"spacer\"></li>")?;
continue;
}
let level = if let Some(s) = item.get("section") {
2017-06-27 09:08:58 +02:00
s.matches('.').count()
} else {
1
};
if level > current_level {
while level > current_level {
2018-08-05 15:08:47 +08:00
out.write("<li>")?;
out.write("<ol class=\"section\">")?;
current_level += 1;
}
2018-08-05 15:08:47 +08:00
out.write("<li>")?;
} else if level < current_level {
while level < current_level {
2018-08-05 15:08:47 +08:00
out.write("</ol>")?;
out.write("</li>")?;
current_level -= 1;
}
2018-08-05 15:08:47 +08:00
out.write("<li>")?;
} else {
2018-08-05 15:08:47 +08:00
out.write("<li")?;
if item.get("section").is_none() {
2018-08-05 15:08:47 +08:00
out.write(" class=\"affix\"")?;
}
2018-08-05 15:08:47 +08:00
out.write(">")?;
}
// Link
let path_exists = if let Some(path) = item.get("path") {
if !path.is_empty() {
2018-08-05 15:08:47 +08:00
out.write("<a href=\"")?;
let tmp = Path::new(item.get("path").expect("Error: path should be Some(_)"))
.with_extension("html")
.to_str()
.unwrap()
// Hack for windows who tends to use `\` as separator instead of `/`
.replace("\\", "/");
// Add link
2018-08-05 15:08:47 +08:00
out.write(&utils::fs::path_to_root(&current))?;
out.write(&tmp)?;
out.write("\"")?;
if path == &current {
2018-08-05 15:08:47 +08:00
out.write(" class=\"active\"")?;
}
2018-08-05 15:08:47 +08:00
out.write(">")?;
true
} else {
false
}
} else {
false
};
if !self.no_section_label {
// Section does not necessarily exist
if let Some(section) = item.get("section") {
2018-08-05 15:08:47 +08:00
out.write("<strong aria-hidden=\"true\">")?;
out.write(&section)?;
out.write("</strong> ")?;
}
}
if let Some(name) = item.get("name") {
// Render only inline code blocks
// filter all events that are not inline code blocks
let parser = Parser::new(name).filter(|event| match *event {
Event::Start(Tag::Code)
| Event::End(Tag::Code)
| Event::InlineHtml(_)
| Event::Text(_) => true,
_ => false,
});
// render markdown to html
let mut markdown_parsed_name = String::with_capacity(name.len() * 3 / 2);
html::push_html(&mut markdown_parsed_name, parser);
// write to the handlebars template
2018-08-05 15:08:47 +08:00
out.write(&markdown_parsed_name)?;
}
if path_exists {
2018-08-05 15:08:47 +08:00
out.write("</a>")?;
}
2018-08-05 15:08:47 +08:00
out.write("</li>")?;
}
while current_level > 1 {
2018-08-05 15:08:47 +08:00
out.write("</ol>")?;
out.write("</li>")?;
current_level -= 1;
}
2018-08-05 15:08:47 +08:00
out.write("</ol>")?;
Ok(())
}
}