From 0a4a2b66da843e3c36c17e6c77d85c34cb2617f0 Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Fri, 1 Sep 2017 08:20:27 +0200 Subject: [PATCH 1/2] Fix the print title that was using the title from the last rendered chapter. Fixes #414 --- src/renderer/html_handlebars/hbs_renderer.rs | 3 +++ src/theme/index.hbs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 9ace5dcc..0333999a 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -207,6 +207,9 @@ impl HtmlHandlebars { /// Update the context with data for this file fn configure_print_version(&self, data: &mut serde_json::Map, print_content: &str) { + // Make sure that the Print chapter does not display the title from + // the last rendered chapter by removing it from its context + data.remove("chapter_title"); data.insert("path".to_owned(), json!("print.md")); data.insert("content".to_owned(), json!(print_content)); data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(Path::new("print.md")))); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 9340ba7a..2d30bbf0 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -2,7 +2,7 @@ - {{ chapter_title }} - {{ title }} + {{ chapter_title }} {{#if chapter_title }}-{{/if}} {{ title }} From 881a1b39ff32ad25e88036ac3744add14e075a4b Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Thu, 7 Sep 2017 23:19:22 +0200 Subject: [PATCH 2/2] Remove the logic in handlebars and expose the 3 different titles in the handlebars variables --- src/renderer/html_handlebars/hbs_renderer.rs | 13 +++++++++++-- src/theme/index.hbs | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 0333999a..f76cb8eb 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -51,9 +51,17 @@ impl HtmlHandlebars { io::Error::new(io::ErrorKind::Other, "Could not convert path to str") })?; + // Non-lexical lifetimes needed :'( + let mut title: String; + { + let book_title = ctx.data.get("book_title").and_then(serde_json::Value::as_str).unwrap_or(""); + title = ch.name.clone() + " - " + book_title; + } + ctx.data.insert("path".to_owned(), json!(path)); ctx.data.insert("content".to_owned(), json!(content)); ctx.data.insert("chapter_title".to_owned(), json!(ch.name)); + ctx.data.insert("title".to_owned(), json!(title)); ctx.data.insert( "path_to_root".to_owned(), json!(utils::fs::path_to_root(&ch.path)), @@ -209,7 +217,7 @@ impl HtmlHandlebars { fn configure_print_version(&self, data: &mut serde_json::Map, print_content: &str) { // Make sure that the Print chapter does not display the title from // the last rendered chapter by removing it from its context - data.remove("chapter_title"); + data.remove("title"); data.insert("path".to_owned(), json!("print.md")); data.insert("content".to_owned(), json!(print_content)); data.insert("path_to_root".to_owned(), json!(utils::fs::path_to_root(Path::new("print.md")))); @@ -279,6 +287,7 @@ impl Renderer for HtmlHandlebars { // Print version self.configure_print_version(&mut data, &print_content); + data.insert("title".to_owned(), json!(book.get_title())); // Render the handlebars template with the data debug!("[*]: Render template"); @@ -311,7 +320,7 @@ fn make_data(book: &MDBook) -> Result let mut data = serde_json::Map::new(); data.insert("language".to_owned(), json!("en")); - data.insert("title".to_owned(), json!(book.get_title())); + data.insert("book_title".to_owned(), json!(book.get_title())); data.insert("description".to_owned(), json!(book.get_description())); data.insert("favicon".to_owned(), json!("favicon.png")); if let Some(livereload) = book.get_livereload() { diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 2d30bbf0..9f735e6c 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -2,7 +2,7 @@ - {{ chapter_title }} {{#if chapter_title }}-{{/if}} {{ title }} + {{ title }} @@ -85,7 +85,7 @@ -

{{ title }}

+

{{ book_title }}