(feat) update to handlebars 1.0

This commit is contained in:
Ning Sun 2018-08-05 15:08:47 +08:00
parent 322e8fcf77
commit 4cc3a1333b
No known key found for this signature in database
GPG Key ID: 2A61AA38AB6BD92E
4 changed files with 51 additions and 48 deletions

7
Cargo.lock generated
View File

@ -279,7 +279,7 @@ dependencies = [
[[package]] [[package]]
name = "handlebars" name = "handlebars"
version = "0.32.4" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -290,6 +290,7 @@ dependencies = [
"regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -511,7 +512,7 @@ dependencies = [
"elasticlunr-rs 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "elasticlunr-rs 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.11 (registry+https://github.com/rust-lang/crates.io-index)",
"error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"handlebars 0.32.4 (registry+https://github.com/rust-lang/crates.io-index)", "handlebars 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1435,7 +1436,7 @@ dependencies = [
"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
"checksum futf 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7c9c1ce3fa9336301af935ab852c437817d14cd33690446569392e65170aac3b" "checksum futf 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7c9c1ce3fa9336301af935ab852c437817d14cd33690446569392e65170aac3b"
"checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797" "checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797"
"checksum handlebars 0.32.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d89ec99d1594f285d4590fc32bac5f75cdab383f1123d504d27862c644a807dd" "checksum handlebars 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df2594295f9b79788875e0102718eb2eeea55f26ee18bee134c22db5c80e9d3e"
"checksum html5ever 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a49d5001dd1bddf042ea41ed4e0a671d50b1bf187e66b349d7ec613bdce4ad90" "checksum html5ever 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a49d5001dd1bddf042ea41ed4e0a671d50b1bf187e66b349d7ec613bdce4ad90"
"checksum html5ever 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b04478cf718862650a0bf66acaf8f2f8c906fbc703f35c916c1f4211b069a364" "checksum html5ever 0.22.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b04478cf718862650a0bf66acaf8f2f8c906fbc703f35c916c1f4211b069a364"
"checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540" "checksum httparse 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7b6288d7db100340ca12873fd4d08ad1b8f206a9457798dfb17c018a33fee540"

View File

@ -17,7 +17,7 @@ exclude = ["book-example/*"]
[dependencies] [dependencies]
clap = "2.24" clap = "2.24"
chrono = "0.4" chrono = "0.4"
handlebars = "0.32" handlebars = "1.0"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
error-chain = "0.12" error-chain = "0.12"

View File

@ -1,7 +1,7 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::path::Path; use std::path::Path;
use handlebars::{Context, Handlebars, Helper, RenderContext, RenderError, Renderable}; use handlebars::{Context, Handlebars, Helper, Output, RenderContext, RenderError, Renderable};
use serde_json; use serde_json;
use utils; use utils;
@ -45,16 +45,16 @@ impl Target {
} }
} }
fn find_chapter(rc: &mut RenderContext, target: Target) -> Result<Option<StringMap>, RenderError> { fn find_chapter(ctx: &Context, rc: &mut RenderContext, target: Target) -> Result<Option<StringMap>, RenderError> {
debug!("Get data from context"); debug!("Get data from context");
let chapters = rc.evaluate_absolute("chapters", true).and_then(|c| { let chapters = rc.evaluate_absolute(ctx, "chapters", true).and_then(|c| {
serde_json::value::from_value::<Vec<StringMap>>(c.clone()) serde_json::value::from_value::<Vec<StringMap>>(c.clone())
.map_err(|_| RenderError::new("Could not decode the JSON data")) .map_err(|_| RenderError::new("Could not decode the JSON data"))
})?; })?;
let base_path = rc let base_path = rc
.evaluate_absolute("path", true)? .evaluate_absolute(ctx, "path", true)?
.as_str() .as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", ""); .replace("\"", "");
@ -84,14 +84,16 @@ fn find_chapter(rc: &mut RenderContext, target: Target) -> Result<Option<StringM
fn render( fn render(
_h: &Helper, _h: &Helper,
r: &Handlebars, r: &Handlebars,
ctx: &Context,
rc: &mut RenderContext, rc: &mut RenderContext,
out: &mut Output,
chapter: &StringMap, chapter: &StringMap,
) -> Result<(), RenderError> { ) -> Result<(), RenderError> {
trace!("Creating BTreeMap to inject in context"); trace!("Creating BTreeMap to inject in context");
let mut context = BTreeMap::new(); let mut context = BTreeMap::new();
let base_path = rc let base_path = rc
.evaluate_absolute("path", false)? .evaluate_absolute(ctx, "path", false)?
.as_str() .as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", ""); .replace("\"", "");
@ -122,28 +124,29 @@ fn render(
_h.template() _h.template()
.ok_or_else(|| RenderError::new("Error with the handlebars template")) .ok_or_else(|| RenderError::new("Error with the handlebars template"))
.and_then(|t| { .and_then(|t| {
let mut local_rc = rc.with_context(Context::wraps(&context)?); let mut local_rc = rc.new_for_block();
t.render(r, &mut local_rc) let local_ctx = Context::wraps(&context)?;
t.render(r, &local_ctx, &mut local_rc, out)
})?; })?;
Ok(()) Ok(())
} }
pub fn previous(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { pub fn previous(_h: &Helper, r: &Handlebars, ctx: &Context, rc: &mut RenderContext, out: &mut Output) -> Result<(), RenderError> {
trace!("previous (handlebars helper)"); trace!("previous (handlebars helper)");
if let Some(previous) = find_chapter(rc, Target::Previous)? { if let Some(previous) = find_chapter(ctx, rc, Target::Previous)? {
render(_h, r, rc, &previous)?; render(_h, r, ctx, rc, out, &previous)?;
} }
Ok(()) Ok(())
} }
pub fn next(_h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { pub fn next(_h: &Helper, r: &Handlebars, ctx: &Context, rc: &mut RenderContext, out: &mut Output) -> Result<(), RenderError> {
trace!("next (handlebars helper)"); trace!("next (handlebars helper)");
if let Some(next) = find_chapter(rc, Target::Next)? { if let Some(next) = find_chapter(ctx, rc, Target::Next)? {
render(_h, r, rc, &next)?; render(_h, r, ctx, rc, out, &next)?;
} }
Ok(()) Ok(())

View File

@ -3,7 +3,7 @@ use std::path::Path;
use utils; use utils;
use handlebars::{Handlebars, Helper, HelperDef, RenderContext, RenderError}; use handlebars::{Context, Handlebars, Helper, HelperDef, Output, RenderContext, RenderError};
use pulldown_cmark::{html, Event, Parser, Tag}; use pulldown_cmark::{html, Event, Parser, Tag};
use serde_json; use serde_json;
@ -14,28 +14,28 @@ pub struct RenderToc {
} }
impl HelperDef for RenderToc { impl HelperDef for RenderToc {
fn call(&self, _h: &Helper, _: &Handlebars, rc: &mut RenderContext) -> Result<(), RenderError> { fn call<'reg:'rc, 'rc>(&self, _h: &Helper, _: &Handlebars, ctx: &Context, rc: &mut RenderContext, out: &mut Output) -> Result<(), RenderError> {
// get value from context data // get value from context data
// rc.get_path() is current json parent path, you should always use it like this // 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 // param is the key of value you want to display
let chapters = rc.evaluate_absolute("chapters", true).and_then(|c| { let chapters = rc.evaluate_absolute(ctx, "chapters", true).and_then(|c| {
serde_json::value::from_value::<Vec<BTreeMap<String, String>>>(c.clone()) serde_json::value::from_value::<Vec<BTreeMap<String, String>>>(c.clone())
.map_err(|_| RenderError::new("Could not decode the JSON data")) .map_err(|_| RenderError::new("Could not decode the JSON data"))
})?; })?;
let current = rc let current = rc
.evaluate_absolute("path", true)? .evaluate_absolute(ctx, "path", true)?
.as_str() .as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", ""); .replace("\"", "");
rc.writer.write_all(b"<ol class=\"chapter\">")?; out.write("<ol class=\"chapter\">")?;
let mut current_level = 1; let mut current_level = 1;
for item in chapters { for item in chapters {
// Spacer // Spacer
if item.get("spacer").is_some() { if item.get("spacer").is_some() {
rc.writer.write_all(b"<li class=\"spacer\"></li>")?; out.write("<li class=\"spacer\"></li>")?;
continue; continue;
} }
@ -47,30 +47,30 @@ impl HelperDef for RenderToc {
if level > current_level { if level > current_level {
while level > current_level { while level > current_level {
rc.writer.write_all(b"<li>")?; out.write("<li>")?;
rc.writer.write_all(b"<ol class=\"section\">")?; out.write("<ol class=\"section\">")?;
current_level += 1; current_level += 1;
} }
rc.writer.write_all(b"<li>")?; out.write("<li>")?;
} else if level < current_level { } else if level < current_level {
while level < current_level { while level < current_level {
rc.writer.write_all(b"</ol>")?; out.write("</ol>")?;
rc.writer.write_all(b"</li>")?; out.write("</li>")?;
current_level -= 1; current_level -= 1;
} }
rc.writer.write_all(b"<li>")?; out.write("<li>")?;
} else { } else {
rc.writer.write_all(b"<li")?; out.write("<li")?;
if item.get("section").is_none() { if item.get("section").is_none() {
rc.writer.write_all(b" class=\"affix\"")?; out.write(" class=\"affix\"")?;
} }
rc.writer.write_all(b">")?; out.write(">")?;
} }
// Link // Link
let path_exists = if let Some(path) = item.get("path") { let path_exists = if let Some(path) = item.get("path") {
if !path.is_empty() { if !path.is_empty() {
rc.writer.write_all(b"<a href=\"")?; out.write("<a href=\"")?;
let tmp = Path::new(item.get("path").expect("Error: path should be Some(_)")) let tmp = Path::new(item.get("path").expect("Error: path should be Some(_)"))
.with_extension("html") .with_extension("html")
@ -80,16 +80,15 @@ impl HelperDef for RenderToc {
.replace("\\", "/"); .replace("\\", "/");
// Add link // Add link
rc.writer out.write(&utils::fs::path_to_root(&current))?;
.write_all(&utils::fs::path_to_root(&current).as_bytes())?; out.write(&tmp)?;
rc.writer.write_all(tmp.as_bytes())?; out.write("\"")?;
rc.writer.write_all(b"\"")?;
if path == &current { if path == &current {
rc.writer.write_all(b" class=\"active\"")?; out.write(" class=\"active\"")?;
} }
rc.writer.write_all(b">")?; out.write(">")?;
true true
} else { } else {
false false
@ -101,9 +100,9 @@ impl HelperDef for RenderToc {
if !self.no_section_label { if !self.no_section_label {
// Section does not necessarily exist // Section does not necessarily exist
if let Some(section) = item.get("section") { if let Some(section) = item.get("section") {
rc.writer.write_all(b"<strong aria-hidden=\"true\">")?; out.write("<strong aria-hidden=\"true\">")?;
rc.writer.write_all(section.as_bytes())?; out.write(&section)?;
rc.writer.write_all(b"</strong> ")?; out.write("</strong> ")?;
} }
} }
@ -124,22 +123,22 @@ impl HelperDef for RenderToc {
html::push_html(&mut markdown_parsed_name, parser); html::push_html(&mut markdown_parsed_name, parser);
// write to the handlebars template // write to the handlebars template
rc.writer.write_all(markdown_parsed_name.as_bytes())?; out.write(&markdown_parsed_name)?;
} }
if path_exists { if path_exists {
rc.writer.write_all(b"</a>")?; out.write("</a>")?;
} }
rc.writer.write_all(b"</li>")?; out.write("</li>")?;
} }
while current_level > 1 { while current_level > 1 {
rc.writer.write_all(b"</ol>")?; out.write("</ol>")?;
rc.writer.write_all(b"</li>")?; out.write("</li>")?;
current_level -= 1; current_level -= 1;
} }
rc.writer.write_all(b"</ol>")?; out.write("</ol>")?;
Ok(()) Ok(())
} }
} }