2015-07-30 15:38:21 +02:00
|
|
|
use std::path::Path;
|
2017-06-13 20:53:25 +08:00
|
|
|
use std::collections::BTreeMap;
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2018-07-11 23:33:44 +10:00
|
|
|
use utils;
|
|
|
|
|
|
2016-11-03 01:58:42 +01:00
|
|
|
use serde_json;
|
2017-10-03 17:25:23 +05:45
|
|
|
use handlebars::{Handlebars, Helper, HelperDef, RenderContext, RenderError};
|
|
|
|
|
use pulldown_cmark::{html, Event, Parser, Tag};
|
2015-07-30 15:38:21 +02:00
|
|
|
|
|
|
|
|
// Handlebars helper to construct TOC
|
|
|
|
|
#[derive(Clone, Copy)]
|
2018-01-08 00:31:46 +08:00
|
|
|
pub struct RenderToc {
|
2018-03-14 23:28:32 +08:00
|
|
|
pub no_section_label: bool,
|
2018-01-08 00:31:46 +08:00
|
|
|
}
|
2015-07-30 15:38:21 +02:00
|
|
|
|
|
|
|
|
impl HelperDef for RenderToc {
|
2017-02-15 22:34:37 -05:00
|
|
|
fn call(&self, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
2016-03-17 22:31:28 +01:00
|
|
|
// 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-03-14 23:28:32 +08:00
|
|
|
let chapters = rc.evaluate_absolute("chapters", true).and_then(|c| {
|
2017-10-03 17:25:23 +05:45
|
|
|
serde_json::value::from_value::<Vec<BTreeMap<String, String>>>(c.clone())
|
|
|
|
|
.map_err(|_| RenderError::new("Could not decode the JSON data"))
|
|
|
|
|
})?;
|
2018-03-14 23:28:32 +08:00
|
|
|
let current = rc.evaluate_absolute("path", true)?
|
|
|
|
|
.as_str()
|
|
|
|
|
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
|
|
|
|
|
.replace("\"", "");
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2018-01-15 14:26:53 +01:00
|
|
|
rc.writer.write_all(b"<ol class=\"chapter\">")?;
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
let mut current_level = 1;
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2017-06-13 20:53:25 +08:00
|
|
|
for item in chapters {
|
2016-03-17 22:31:28 +01:00
|
|
|
// Spacer
|
2017-02-15 22:01:26 -05:00
|
|
|
if item.get("spacer").is_some() {
|
2017-10-03 17:25:23 +05:45
|
|
|
rc.writer.write_all(b"<li class=\"spacer\"></li>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
continue;
|
2015-09-11 20:52:55 +02:00
|
|
|
}
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
let level = if let Some(s) = item.get("section") {
|
2017-06-27 09:08:58 +02:00
|
|
|
s.matches('.').count()
|
2016-03-17 22:31:28 +01:00
|
|
|
} else {
|
|
|
|
|
1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if level > current_level {
|
2016-07-16 10:23:22 -04:00
|
|
|
while level > current_level {
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"<li>")?;
|
2018-01-15 14:26:53 +01:00
|
|
|
rc.writer.write_all(b"<ol class=\"section\">")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level += 1;
|
|
|
|
|
}
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"<li>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
} else if level < current_level {
|
|
|
|
|
while level < current_level {
|
2018-01-15 14:26:53 +01:00
|
|
|
rc.writer.write_all(b"</ol>")?;
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"</li>")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level -= 1;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"<li>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
} else {
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"<li")?;
|
2017-02-15 22:01:26 -05:00
|
|
|
if item.get("section").is_none() {
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b" class=\"affix\"")?;
|
2015-07-31 15:06:08 +02:00
|
|
|
}
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b">")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Link
|
|
|
|
|
let path_exists = if let Some(path) = item.get("path") {
|
|
|
|
|
if !path.is_empty() {
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"<a href=\"")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
|
2017-05-19 13:04:37 +02:00
|
|
|
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("\\", "/");
|
2016-03-17 22:31:28 +01:00
|
|
|
|
|
|
|
|
// Add link
|
2018-07-11 23:33:44 +10:00
|
|
|
rc.writer.write_all(&utils::fs::path_to_root(¤t).as_bytes())?;
|
2017-05-19 13:04:37 +02:00
|
|
|
rc.writer.write_all(tmp.as_bytes())?;
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"\"")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
|
|
|
|
|
if path == ¤t {
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b" class=\"active\"")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b">")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
2015-07-30 15:38:21 +02:00
|
|
|
} else {
|
|
|
|
|
false
|
2016-03-17 22:31:28 +01:00
|
|
|
};
|
2015-09-11 20:52:55 +02:00
|
|
|
|
2018-01-08 00:31:46 +08:00
|
|
|
if !self.no_section_label {
|
|
|
|
|
// Section does not necessarily exist
|
|
|
|
|
if let Some(section) = item.get("section") {
|
2018-01-15 14:26:53 +01:00
|
|
|
rc.writer.write_all(b"<strong aria-hidden=\"true\">")?;
|
2018-01-08 00:31:46 +08:00
|
|
|
rc.writer.write_all(section.as_bytes())?;
|
|
|
|
|
rc.writer.write_all(b"</strong> ")?;
|
|
|
|
|
}
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2015-12-30 00:46:55 +01:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
if let Some(name) = item.get("name") {
|
|
|
|
|
// Render only inline code blocks
|
|
|
|
|
|
|
|
|
|
// filter all events that are not inline code blocks
|
2017-05-05 08:41:50 +08:00
|
|
|
let parser = Parser::new(name).filter(|event| match *event {
|
2018-03-14 23:28:32 +08:00
|
|
|
Event::Start(Tag::Code)
|
|
|
|
|
| Event::End(Tag::Code)
|
|
|
|
|
| Event::InlineHtml(_)
|
|
|
|
|
| Event::Text(_) => true,
|
|
|
|
|
_ => false,
|
|
|
|
|
});
|
2016-03-17 22:31:28 +01:00
|
|
|
|
|
|
|
|
// 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
|
2017-05-19 13:04:37 +02:00
|
|
|
rc.writer.write_all(markdown_parsed_name.as_bytes())?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2015-12-30 00:46:55 +01:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
if path_exists {
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"</a>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2015-12-30 00:46:55 +01:00
|
|
|
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"</li>")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
}
|
|
|
|
|
while current_level > 1 {
|
2018-01-15 14:26:53 +01:00
|
|
|
rc.writer.write_all(b"</ol>")?;
|
2017-06-27 09:08:58 +02:00
|
|
|
rc.writer.write_all(b"</li>")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level -= 1;
|
2015-07-30 15:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
2018-01-15 14:26:53 +01:00
|
|
|
rc.writer.write_all(b"</ol>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
Ok(())
|
2015-07-30 15:38:21 +02:00
|
|
|
}
|
|
|
|
|
}
|