From 2d11eb05fe6e8c0c6ef8356833b03b014cb41ace Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Sat, 13 Jul 2019 00:11:05 +0800 Subject: [PATCH] (feat) update handlebars to 2.0 --- Cargo.lock | 16 +++++++++++++--- Cargo.toml | 2 +- .../html_handlebars/helpers/navigation.rs | 10 ++++++---- src/renderer/html_handlebars/helpers/theme.rs | 7 ++++--- src/renderer/html_handlebars/helpers/toc.rs | 7 ++++--- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 01a15fcb..5e94ff57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -376,9 +376,10 @@ dependencies = [ [[package]] name = "handlebars" -version = "1.1.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "pest 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -389,6 +390,14 @@ dependencies = [ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hashbrown" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "heck" version = "0.3.1" @@ -632,7 +641,7 @@ dependencies = [ "elasticlunr-rs 2.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", - "handlebars 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "handlebars 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "iron 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1690,7 +1699,8 @@ dependencies = [ "checksum futf 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7c9c1ce3fa9336301af935ab852c437817d14cd33690446569392e65170aac3b" "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" "checksum getopts 0.2.19 (registry+https://github.com/rust-lang/crates.io-index)" = "72327b15c228bfe31f1390f93dd5e9279587f0463836393c9df719ce62a3e450" -"checksum handlebars 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d82e5750d8027a97b9640e3fefa66bbaf852a35228e1c90790efd13c4b09c166" +"checksum handlebars 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "df044dd42cdb7e32f28557b661406fc0f2494be75199779998810dbc35030e0d" +"checksum hashbrown 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e1de41fb8dba9714efd92241565cdff73f78508c95697dd56787d3cba27e2353" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum html5ever 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a49d5001dd1bddf042ea41ed4e0a671d50b1bf187e66b349d7ec613bdce4ad90" "checksum html5ever 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5ce65ac8028cf5a287a7dbf6c4e0a6cf2dcf022ed5b167a81bae66ebf599a8b7" diff --git a/Cargo.toml b/Cargo.toml index a75c47dd..23aa0add 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ chrono = "0.4" clap = "2.24" env_logger = "0.6" error-chain = "0.12" -handlebars = { version = "1.0", default-features = false, features = ["no_dir_source"] } +handlebars = { version = "2.0", default-features = false, features = ["no_dir_source"] } itertools = "0.8" lazy_static = "1.0" log = "0.4" diff --git a/src/renderer/html_handlebars/helpers/navigation.rs b/src/renderer/html_handlebars/helpers/navigation.rs index 4d162af7..48b8f12d 100644 --- a/src/renderer/html_handlebars/helpers/navigation.rs +++ b/src/renderer/html_handlebars/helpers/navigation.rs @@ -51,13 +51,14 @@ fn find_chapter( ) -> Result, RenderError> { debug!("Get data from context"); - let chapters = rc.evaluate_absolute(ctx, "chapters", true).and_then(|c| { - serde_json::value::from_value::>(c.clone()) + let chapters = rc.evaluate(ctx, "@root/chapters").and_then(|c| { + serde_json::value::from_value::>(c.as_json().clone()) .map_err(|_| RenderError::new("Could not decode the JSON data")) })?; let base_path = rc - .evaluate_absolute(ctx, "path", true)? + .evaluate(ctx, "@root/path")? + .as_json() .as_str() .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .replace("\"", ""); @@ -96,7 +97,8 @@ fn render( let mut context = BTreeMap::new(); let base_path = rc - .evaluate_absolute(ctx, "path", false)? + .evaluate(ctx, "@root/path")? + .as_json() .as_str() .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .replace("\"", ""); diff --git a/src/renderer/html_handlebars/helpers/theme.rs b/src/renderer/html_handlebars/helpers/theme.rs index a42c2f91..297ba10d 100644 --- a/src/renderer/html_handlebars/helpers/theme.rs +++ b/src/renderer/html_handlebars/helpers/theme.rs @@ -13,13 +13,14 @@ pub fn theme_option( RenderError::new("Param 0 with String type is required for theme_option helper.") })?; - let theme_name = rc - .evaluate_absolute(ctx, "default_theme", true)? + let default_theme = rc.evaluate(ctx, "@root/default_theme")?; + let default_theme_name = default_theme + .as_json() .as_str() .ok_or_else(|| RenderError::new("Type error for `default_theme`, string expected"))?; out.write(param)?; - if param.to_lowercase() == theme_name.to_lowercase() { + if param.to_lowercase() == default_theme_name.to_lowercase() { out.write(" (default)")?; } diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index 3fdec599..33e7ef84 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -24,12 +24,13 @@ impl HelperDef for RenderToc { // 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 - let chapters = rc.evaluate_absolute(ctx, "chapters", true).and_then(|c| { - serde_json::value::from_value::>>(c.clone()) + let chapters = rc.evaluate(ctx, "@root/chapters").and_then(|c| { + serde_json::value::from_value::>>(c.as_json().clone()) .map_err(|_| RenderError::new("Could not decode the JSON data")) })?; let current = rc - .evaluate_absolute(ctx, "path", true)? + .evaluate(ctx, "@root/path")? + .as_json() .as_str() .ok_or_else(|| RenderError::new("Type error for `path`, string expected"))? .replace("\"", "");