2015-07-30 15:38:21 +02:00
|
|
|
use std::path::Path;
|
2016-12-23 08:10:42 +00:00
|
|
|
use std::collections::{VecDeque, BTreeMap};
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2016-11-03 01:58:42 +01:00
|
|
|
use serde_json;
|
2017-02-15 22:34:37 -05:00
|
|
|
use handlebars::{Handlebars, HelperDef, RenderError, RenderContext, Helper};
|
2016-03-27 18:22:17 +02:00
|
|
|
use pulldown_cmark::{Parser, html, Event, Tag};
|
2015-07-30 15:38:21 +02:00
|
|
|
|
|
|
|
|
// Handlebars helper to construct TOC
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
|
pub struct RenderToc;
|
|
|
|
|
|
|
|
|
|
impl HelperDef for RenderToc {
|
2017-02-15 22:34:37 -05:00
|
|
|
fn call(&self, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> {
|
2015-07-30 15:38:21 +02:00
|
|
|
|
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
|
2017-02-15 22:34:37 -05:00
|
|
|
let chapters = rc.context().navigate(rc.get_path(), &VecDeque::new(), "chapters").to_owned();
|
|
|
|
|
let current = rc.context().navigate(rc.get_path(), &VecDeque::new(), "path").to_string().replace("\"", "");
|
2016-03-17 22:31:28 +01:00
|
|
|
try!(rc.writer.write("<ul class=\"chapter\">".as_bytes()));
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
// Decode json format
|
2016-11-03 01:58:42 +01:00
|
|
|
let decoded: Vec<BTreeMap<String, String>> = serde_json::from_str(&chapters.to_string()).unwrap();
|
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
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
for item in decoded {
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
// Spacer
|
|
|
|
|
if let Some(_) = item.get("spacer") {
|
|
|
|
|
try!(rc.writer.write("<li class=\"spacer\"></li>".as_bytes()));
|
|
|
|
|
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") {
|
|
|
|
|
s.len() / 2
|
|
|
|
|
} else {
|
|
|
|
|
1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if level > current_level {
|
2016-07-16 10:23:22 -04:00
|
|
|
while level > current_level {
|
|
|
|
|
try!(rc.writer.write("<li>".as_bytes()));
|
|
|
|
|
try!(rc.writer.write("<ul class=\"section\">".as_bytes()));
|
|
|
|
|
current_level += 1;
|
|
|
|
|
}
|
2016-03-17 22:31:28 +01:00
|
|
|
try!(rc.writer.write("<li>".as_bytes()));
|
|
|
|
|
} else if level < current_level {
|
|
|
|
|
while level < current_level {
|
|
|
|
|
try!(rc.writer.write("</ul>".as_bytes()));
|
|
|
|
|
try!(rc.writer.write("</li>".as_bytes()));
|
2016-07-16 10:23:22 -04:00
|
|
|
current_level -= 1;
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
try!(rc.writer.write("<li>".as_bytes()));
|
|
|
|
|
} else {
|
|
|
|
|
try!(rc.writer.write("<li".as_bytes()));
|
|
|
|
|
if let None = item.get("section") {
|
|
|
|
|
try!(rc.writer.write(" class=\"affix\"".as_bytes()));
|
2015-07-31 15:06:08 +02:00
|
|
|
}
|
|
|
|
|
try!(rc.writer.write(">".as_bytes()));
|
2016-03-17 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Link
|
|
|
|
|
let path_exists = if let Some(path) = item.get("path") {
|
|
|
|
|
if !path.is_empty() {
|
|
|
|
|
try!(rc.writer.write("<a href=\"".as_bytes()));
|
|
|
|
|
|
|
|
|
|
// Add link
|
|
|
|
|
try!(rc.writer.write(Path::new(item.get("path")
|
|
|
|
|
.expect("Error: path should be Some(_)"))
|
|
|
|
|
.with_extension("html")
|
|
|
|
|
.to_str()
|
|
|
|
|
.unwrap()
|
2016-04-25 17:02:47 +02:00
|
|
|
// Hack for windows who tends to use `\` as separator instead of `/`
|
|
|
|
|
.replace("\\", "/")
|
2016-03-17 22:31:28 +01:00
|
|
|
.as_bytes()));
|
|
|
|
|
|
|
|
|
|
try!(rc.writer.write("\"".as_bytes()));
|
|
|
|
|
|
|
|
|
|
if path == ¤t {
|
|
|
|
|
try!(rc.writer.write(" class=\"active\"".as_bytes()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try!(rc.writer.write(">".as_bytes()));
|
|
|
|
|
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
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
// Section does not necessarily exist
|
|
|
|
|
if let Some(section) = item.get("section") {
|
|
|
|
|
try!(rc.writer.write("<strong>".as_bytes()));
|
|
|
|
|
try!(rc.writer.write(section.as_bytes()));
|
|
|
|
|
try!(rc.writer.write("</strong> ".as_bytes()));
|
|
|
|
|
}
|
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
|
|
|
|
|
let parser = Parser::new(&name).filter(|event| {
|
|
|
|
|
match event {
|
2016-04-25 17:02:47 +02:00
|
|
|
&Event::Start(Tag::Code) |
|
|
|
|
|
&Event::End(Tag::Code) => true,
|
2016-03-17 22:31:28 +01:00
|
|
|
&Event::InlineHtml(_) => true,
|
|
|
|
|
&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
|
|
|
|
|
try!(rc.writer.write(markdown_parsed_name.as_bytes()));
|
|
|
|
|
}
|
2015-12-30 00:46:55 +01:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
if path_exists {
|
|
|
|
|
try!(rc.writer.write("</a>".as_bytes()));
|
|
|
|
|
}
|
2015-12-30 00:46:55 +01:00
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
try!(rc.writer.write("</li>".as_bytes()));
|
2015-07-30 15:38:21 +02:00
|
|
|
|
2016-07-16 10:23:22 -04:00
|
|
|
}
|
|
|
|
|
while current_level > 1 {
|
|
|
|
|
try!(rc.writer.write("</ul>".as_bytes()));
|
|
|
|
|
try!(rc.writer.write("</li>".as_bytes()));
|
|
|
|
|
current_level -= 1;
|
2015-07-30 15:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-17 22:31:28 +01:00
|
|
|
try!(rc.writer.write("</ul>".as_bytes()));
|
|
|
|
|
Ok(())
|
2015-07-30 15:38:21 +02:00
|
|
|
}
|
|
|
|
|
}
|