2017-06-13 20:53:25 +08:00
|
|
|
use std::collections::BTreeMap;
|
2018-07-23 12:45:01 -05:00
|
|
|
use std::path::Path;
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2019-05-26 01:50:41 +07:00
|
|
|
use crate::utils;
|
2018-07-11 23:33:44 +10:00
|
|
|
|
2018-08-05 15:08:47 +08:00
|
|
|
use handlebars::{Context, Handlebars, Helper, HelperDef, Output, RenderContext, RenderError};
|
2019-06-11 09:26:24 -07:00
|
|
|
use pulldown_cmark::{html, Event, Parser};
|
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 {
|
2018-08-21 10:58:44 -05:00
|
|
|
fn call<'reg: 'rc, 'rc>(
|
|
|
|
|
&self,
|
2019-05-07 01:20:58 +07:00
|
|
|
_h: &Helper<'reg, 'rc>,
|
2020-01-24 11:01:44 +08:00
|
|
|
_r: &'reg Handlebars<'_>,
|
2019-05-07 01:20:58 +07:00
|
|
|
ctx: &'rc Context,
|
2020-01-24 11:01:44 +08:00
|
|
|
rc: &mut RenderContext<'reg, 'rc>,
|
2019-05-07 01:20:58 +07:00
|
|
|
out: &mut dyn Output,
|
2018-08-21 10:58:44 -05:00
|
|
|
) -> 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
|
2019-07-13 00:11:05 +08:00
|
|
|
let chapters = rc.evaluate(ctx, "@root/chapters").and_then(|c| {
|
|
|
|
|
serde_json::value::from_value::<Vec<BTreeMap<String, String>>>(c.as_json().clone())
|
2017-10-03 17:25:23 +05:45
|
|
|
.map_err(|_| RenderError::new("Could not decode the JSON data"))
|
|
|
|
|
})?;
|
2019-10-19 15:56:08 +08:00
|
|
|
let current_path = rc
|
2019-07-13 00:11:05 +08:00
|
|
|
.evaluate(ctx, "@root/path")?
|
|
|
|
|
.as_json()
|
2018-03-14 23:28:32 +08:00
|
|
|
.as_str()
|
2020-05-10 08:29:50 -07:00
|
|
|
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
|
2018-03-14 23:28:32 +08:00
|
|
|
.replace("\"", "");
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2019-10-19 15:56:08 +08:00
|
|
|
let current_section = rc
|
|
|
|
|
.evaluate(ctx, "@root/section")?
|
|
|
|
|
.as_json()
|
|
|
|
|
.as_str()
|
|
|
|
|
.map(str::to_owned)
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
let fold_enable = rc
|
|
|
|
|
.evaluate(ctx, "@root/fold_enable")?
|
|
|
|
|
.as_json()
|
|
|
|
|
.as_bool()
|
2020-05-10 08:29:50 -07:00
|
|
|
.ok_or_else(|| RenderError::new("Type error for `fold_enable`, bool expected"))?;
|
2019-10-19 15:56:08 +08:00
|
|
|
|
|
|
|
|
let fold_level = rc
|
|
|
|
|
.evaluate(ctx, "@root/fold_level")?
|
|
|
|
|
.as_json()
|
|
|
|
|
.as_u64()
|
2020-05-10 08:29:50 -07:00
|
|
|
.ok_or_else(|| RenderError::new("Type error for `fold_level`, u64 expected"))?;
|
2019-10-19 15:56:08 +08:00
|
|
|
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("<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() {
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("<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
|
|
|
|
2019-10-19 15:56:08 +08:00
|
|
|
let (section, level) = if let Some(s) = item.get("section") {
|
|
|
|
|
(s.as_str(), s.matches('.').count())
|
2016-03-17 22:31:28 +01:00
|
|
|
} else {
|
2019-10-19 15:56:08 +08:00
|
|
|
("", 1)
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-10 08:29:50 -07:00
|
|
|
let is_expanded =
|
|
|
|
|
if !fold_enable || (!section.is_empty() && current_section.starts_with(section)) {
|
|
|
|
|
// Expand if folding is disabled, or if the section is an
|
|
|
|
|
// ancestor or the current section itself.
|
2019-10-19 15:56:08 +08:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
// Levels that are larger than this would be folded.
|
|
|
|
|
level - 1 < fold_level as usize
|
2020-05-10 08:29:50 -07:00
|
|
|
};
|
2016-03-17 22:31:28 +01:00
|
|
|
|
|
|
|
|
if level > current_level {
|
2016-07-16 10:23:22 -04:00
|
|
|
while level > current_level {
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("<li>")?;
|
|
|
|
|
out.write("<ol class=\"section\">")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level += 1;
|
|
|
|
|
}
|
2019-10-19 15:56:08 +08:00
|
|
|
write_li_open_tag(out, is_expanded, false)?;
|
2016-03-17 22:31:28 +01:00
|
|
|
} else if level < current_level {
|
|
|
|
|
while level < current_level {
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("</ol>")?;
|
|
|
|
|
out.write("</li>")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level -= 1;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2019-10-19 15:56:08 +08:00
|
|
|
write_li_open_tag(out, is_expanded, false)?;
|
2016-03-17 22:31:28 +01:00
|
|
|
} else {
|
2019-10-19 15:56:08 +08:00
|
|
|
write_li_open_tag(out, is_expanded, item.get("section").is_none())?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2020-03-20 21:18:07 -05:00
|
|
|
|
|
|
|
|
// Part title
|
|
|
|
|
if let Some(title) = item.get("part") {
|
2020-06-24 21:13:02 -07:00
|
|
|
out.write("<li class=\"part-title\">")?;
|
2020-03-20 21:18:07 -05:00
|
|
|
out.write(title)?;
|
|
|
|
|
out.write("</li>")?;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2016-03-17 22:31:28 +01:00
|
|
|
|
|
|
|
|
// 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=\"")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
|
2020-03-24 23:52:24 +01:00
|
|
|
let tmp = Path::new(item.get("path").expect("Error: path should be Some(_)"))
|
2017-05-19 13:04:37 +02:00
|
|
|
.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
|
2019-10-19 15:56:08 +08:00
|
|
|
out.write(&utils::fs::path_to_root(¤t_path))?;
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write(&tmp)?;
|
|
|
|
|
out.write("\"")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
|
2020-03-24 23:52:24 +01:00
|
|
|
if path == ¤t_path {
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write(" class=\"active\"")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write(">")?;
|
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-08-05 15:08:47 +08:00
|
|
|
out.write("<strong aria-hidden=\"true\">")?;
|
|
|
|
|
out.write(§ion)?;
|
|
|
|
|
out.write("</strong> ")?;
|
2018-01-08 00:31:46 +08:00
|
|
|
}
|
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 {
|
2019-11-11 20:25:38 +01:00
|
|
|
Event::Code(_) | Event::Html(_) | Event::Text(_) => true,
|
2018-03-14 23:28:32 +08:00
|
|
|
_ => 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
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write(&markdown_parsed_name)?;
|
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 {
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("</a>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
2015-12-30 00:46:55 +01:00
|
|
|
|
2019-10-19 15:56:08 +08:00
|
|
|
// Render expand/collapse toggle
|
|
|
|
|
if let Some(flag) = item.get("has_sub_items") {
|
|
|
|
|
let has_sub_items = flag.parse::<bool>().unwrap_or_default();
|
|
|
|
|
if fold_enable && has_sub_items {
|
|
|
|
|
out.write("<a class=\"toggle\"><div>❱</div></a>")?;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("</li>")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
}
|
|
|
|
|
while current_level > 1 {
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("</ol>")?;
|
|
|
|
|
out.write("</li>")?;
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level -= 1;
|
2015-07-30 15:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-05 15:08:47 +08:00
|
|
|
out.write("</ol>")?;
|
2016-03-17 22:31:28 +01:00
|
|
|
Ok(())
|
2015-07-30 15:38:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-10-19 15:56:08 +08:00
|
|
|
|
|
|
|
|
fn write_li_open_tag(
|
|
|
|
|
out: &mut dyn Output,
|
|
|
|
|
is_expanded: bool,
|
|
|
|
|
is_affix: bool,
|
|
|
|
|
) -> Result<(), std::io::Error> {
|
2020-04-03 12:09:23 -07:00
|
|
|
let mut li = String::from("<li class=\"chapter-item ");
|
2019-10-19 15:56:08 +08:00
|
|
|
if is_expanded {
|
|
|
|
|
li.push_str("expanded ");
|
|
|
|
|
}
|
|
|
|
|
if is_affix {
|
|
|
|
|
li.push_str("affix ");
|
|
|
|
|
}
|
|
|
|
|
li.push_str("\">");
|
|
|
|
|
out.write(&li)
|
|
|
|
|
}
|