Remove the logic in handlebars and expose the 3 different titles in the handlebars variables

This commit is contained in:
Mathieu David 2017-09-07 23:19:22 +02:00
parent 0a4a2b66da
commit 881a1b39ff
2 changed files with 13 additions and 4 deletions

View File

@ -51,9 +51,17 @@ impl HtmlHandlebars {
io::Error::new(io::ErrorKind::Other, "Could not convert path to str") 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("path".to_owned(), json!(path));
ctx.data.insert("content".to_owned(), json!(content)); ctx.data.insert("content".to_owned(), json!(content));
ctx.data.insert("chapter_title".to_owned(), json!(ch.name)); ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
ctx.data.insert("title".to_owned(), json!(title));
ctx.data.insert( ctx.data.insert(
"path_to_root".to_owned(), "path_to_root".to_owned(),
json!(utils::fs::path_to_root(&ch.path)), json!(utils::fs::path_to_root(&ch.path)),
@ -209,7 +217,7 @@ impl HtmlHandlebars {
fn configure_print_version(&self, data: &mut serde_json::Map<String, serde_json::Value>, print_content: &str) { fn configure_print_version(&self, data: &mut serde_json::Map<String, serde_json::Value>, print_content: &str) {
// Make sure that the Print chapter does not display the title from // Make sure that the Print chapter does not display the title from
// the last rendered chapter by removing it from its context // 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("path".to_owned(), json!("print.md"));
data.insert("content".to_owned(), json!(print_content)); 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")))); 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 // Print version
self.configure_print_version(&mut data, &print_content); self.configure_print_version(&mut data, &print_content);
data.insert("title".to_owned(), json!(book.get_title()));
// Render the handlebars template with the data // Render the handlebars template with the data
debug!("[*]: Render template"); debug!("[*]: Render template");
@ -311,7 +320,7 @@ fn make_data(book: &MDBook) -> Result<serde_json::Map<String, serde_json::Value>
let mut data = serde_json::Map::new(); let mut data = serde_json::Map::new();
data.insert("language".to_owned(), json!("en")); 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("description".to_owned(), json!(book.get_description()));
data.insert("favicon".to_owned(), json!("favicon.png")); data.insert("favicon".to_owned(), json!("favicon.png"));
if let Some(livereload) = book.get_livereload() { if let Some(livereload) = book.get_livereload() {

View File

@ -2,7 +2,7 @@
<html lang="{{ language }}"> <html lang="{{ language }}">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>{{ chapter_title }} {{#if chapter_title }}-{{/if}} {{ title }}</title> <title>{{ title }}</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="{{ description }}"> <meta name="description" content="{{ description }}">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
@ -85,7 +85,7 @@
<i id="theme-toggle" class="fa fa-paint-brush"></i> <i id="theme-toggle" class="fa fa-paint-brush"></i>
</div> </div>
<h1 class="menu-title">{{ title }}</h1> <h1 class="menu-title">{{ book_title }}</h1>
<div class="right-buttons"> <div class="right-buttons">
<i id="print-button" class="fa fa-print" title="Print this book"></i> <i id="print-button" class="fa fa-print" title="Print this book"></i>