Skip to content

Commit

Permalink
Merge pull request rust-lang#761 from sunng87/feature/handlebars-1.0
Browse files Browse the repository at this point in the history
Update to handlebars 1.0
  • Loading branch information
mattico authored Aug 7, 2018
2 parents 44e27e0 + 34ed98b commit 8e1dc3e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exclude = ["book-example/*"]
[dependencies]
clap = "2.24"
chrono = "0.4"
handlebars = "0.32"
handlebars = "1.0"
serde = "1.0"
serde_derive = "1.0"
error-chain = "0.12"
Expand Down
29 changes: 16 additions & 13 deletions src/renderer/html_handlebars/helpers/navigation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeMap;
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 utils;
Expand Down Expand Up @@ -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");

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())
.map_err(|_| RenderError::new("Could not decode the JSON data"))
})?;

let base_path = rc
.evaluate_absolute("path", true)?
.evaluate_absolute(ctx, "path", true)?
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");
Expand Down Expand Up @@ -84,14 +84,16 @@ fn find_chapter(rc: &mut RenderContext, target: Target) -> Result<Option<StringM
fn render(
_h: &Helper,
r: &Handlebars,
ctx: &Context,
rc: &mut RenderContext,
out: &mut Output,
chapter: &StringMap,
) -> Result<(), RenderError> {
trace!("Creating BTreeMap to inject in context");

let mut context = BTreeMap::new();
let base_path = rc
.evaluate_absolute("path", false)?
.evaluate_absolute(ctx, "path", false)?
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");
Expand Down Expand Up @@ -122,28 +124,29 @@ fn render(
_h.template()
.ok_or_else(|| RenderError::new("Error with the handlebars template"))
.and_then(|t| {
let mut local_rc = rc.with_context(Context::wraps(&context)?);
t.render(r, &mut local_rc)
let mut local_rc = rc.new_for_block();
let local_ctx = Context::wraps(&context)?;
t.render(r, &local_ctx, &mut local_rc, out)
})?;

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)");

if let Some(previous) = find_chapter(rc, Target::Previous)? {
render(_h, r, rc, &previous)?;
if let Some(previous) = find_chapter(ctx, rc, Target::Previous)? {
render(_h, r, ctx, rc, out, &previous)?;
}

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)");

if let Some(next) = find_chapter(rc, Target::Next)? {
render(_h, r, rc, &next)?;
if let Some(next) = find_chapter(ctx, rc, Target::Next)? {
render(_h, r, ctx, rc, out, &next)?;
}

Ok(())
Expand Down
61 changes: 30 additions & 31 deletions src/renderer/html_handlebars/helpers/toc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;

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 serde_json;

Expand All @@ -14,28 +14,28 @@ pub struct 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
// 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("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())
.map_err(|_| RenderError::new("Could not decode the JSON data"))
})?;
let current = rc
.evaluate_absolute("path", true)?
.evaluate_absolute(ctx, "path", true)?
.as_str()
.ok_or_else(|| RenderError::new("Type error for `path`, string expected"))?
.replace("\"", "");

rc.writer.write_all(b"<ol class=\"chapter\">")?;
out.write("<ol class=\"chapter\">")?;

let mut current_level = 1;

for item in chapters {
// Spacer
if item.get("spacer").is_some() {
rc.writer.write_all(b"<li class=\"spacer\"></li>")?;
out.write("<li class=\"spacer\"></li>")?;
continue;
}

Expand All @@ -47,30 +47,30 @@ impl HelperDef for RenderToc {

if level > current_level {
while level > current_level {
rc.writer.write_all(b"<li>")?;
rc.writer.write_all(b"<ol class=\"section\">")?;
out.write("<li>")?;
out.write("<ol class=\"section\">")?;
current_level += 1;
}
rc.writer.write_all(b"<li>")?;
out.write("<li>")?;
} else if level < current_level {
while level < current_level {
rc.writer.write_all(b"</ol>")?;
rc.writer.write_all(b"</li>")?;
out.write("</ol>")?;
out.write("</li>")?;
current_level -= 1;
}
rc.writer.write_all(b"<li>")?;
out.write("<li>")?;
} else {
rc.writer.write_all(b"<li")?;
out.write("<li")?;
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
let path_exists = if let Some(path) = item.get("path") {
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(_)"))
.with_extension("html")
Expand All @@ -80,16 +80,15 @@ impl HelperDef for RenderToc {
.replace("\\", "/");

// Add link
rc.writer
.write_all(&utils::fs::path_to_root(&current).as_bytes())?;
rc.writer.write_all(tmp.as_bytes())?;
rc.writer.write_all(b"\"")?;
out.write(&utils::fs::path_to_root(&current))?;
out.write(&tmp)?;
out.write("\"")?;

if path == &current {
rc.writer.write_all(b" class=\"active\"")?;
out.write(" class=\"active\"")?;
}

rc.writer.write_all(b">")?;
out.write(">")?;
true
} else {
false
Expand All @@ -101,9 +100,9 @@ impl HelperDef for RenderToc {
if !self.no_section_label {
// Section does not necessarily exist
if let Some(section) = item.get("section") {
rc.writer.write_all(b"<strong aria-hidden=\"true\">")?;
rc.writer.write_all(section.as_bytes())?;
rc.writer.write_all(b"</strong> ")?;
out.write("<strong aria-hidden=\"true\">")?;
out.write(&section)?;
out.write("</strong> ")?;
}
}

Expand All @@ -124,22 +123,22 @@ impl HelperDef for RenderToc {
html::push_html(&mut markdown_parsed_name, parser);

// write to the handlebars template
rc.writer.write_all(markdown_parsed_name.as_bytes())?;
out.write(&markdown_parsed_name)?;
}

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 {
rc.writer.write_all(b"</ol>")?;
rc.writer.write_all(b"</li>")?;
out.write("</ol>")?;
out.write("</li>")?;
current_level -= 1;
}

rc.writer.write_all(b"</ol>")?;
out.write("</ol>")?;
Ok(())
}
}

0 comments on commit 8e1dc3e

Please sign in to comment.