From d03ae79765f70ddaea0563b7c3ddaad76d53996f Mon Sep 17 00:00:00 2001 From: Mathieu David Date: Sat, 5 Sep 2015 17:26:17 +0200 Subject: [PATCH] Added a page containing the whole book for printing purposes, closes #41 + cleaning root path --- src/renderer/html_handlebars/hbs_renderer.rs | 26 ++++++++++++++++++- .../html_handlebars/helpers/navigation.rs | 17 +++--------- src/renderer/html_handlebars/helpers/toc.rs | 4 --- src/theme/book.js | 12 +++++++++ src/theme/index.hbs | 12 ++++++--- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index ffce05e1..80c03bf7 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -7,7 +7,7 @@ use renderer::Renderer; use book::MDBook; use {utils, theme}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::fs::{self, File}; use std::error::Error; use std::io::{self, Read, Write}; @@ -45,6 +45,9 @@ impl Renderer for HtmlHandlebars { let mut data = try!(make_data(book)); + // Print version + let mut print_content: String = String::new(); + // Check if dest directory exists debug!("[*]: Check if destination directory exists"); match utils::create_path(book.get_dest()) { @@ -69,6 +72,7 @@ impl Renderer for HtmlHandlebars { // Render markdown using the pulldown-cmark crate content = render_html(&content); + print_content.push_str(&content); // Remove content from previous file and render content for this one data.remove("path"); @@ -114,6 +118,26 @@ impl Renderer for HtmlHandlebars { } } + // Print version + + // Remove content from previous file and render content for this one + data.remove("path"); + data.insert("path".to_string(), "print.md".to_json()); + + // Remove content from previous file and render content for this one + data.remove("content"); + data.insert("content".to_string(), print_content.to_json()); + + // Remove path to root from previous file and render content for this one + data.remove("path_to_root"); + data.insert("path_to_root".to_string(), utils::path_to_root(Path::new("print.md")).to_json()); + + // Rendere the handlebars template with the data + debug!("[*]: Render template"); + let rendered = try!(handlebars.render("index", &data)); + let mut file = try!(utils::create_file(&book.get_dest().join("print").with_extension("html"))); + try!(file.write_all(&rendered.into_bytes())); + // Copy static files (js, css, images, ...) debug!("[*] Copy static files"); diff --git a/src/renderer/html_handlebars/helpers/navigation.rs b/src/renderer/html_handlebars/helpers/navigation.rs index 56c6c5a9..2c2c36b3 100644 --- a/src/renderer/html_handlebars/helpers/navigation.rs +++ b/src/renderer/html_handlebars/helpers/navigation.rs @@ -1,7 +1,7 @@ extern crate handlebars; extern crate rustc_serialize; -use std::path::{PathBuf, Path}; +use std::path::Path; use std::collections::BTreeMap; use self::rustc_serialize::json::{self, ToJson}; @@ -22,11 +22,6 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext .to_string() .replace("\"", ""); - let path_to_root = PathBuf::from( - c.navigate(rc.get_path(), "path_to_root") - .to_string() - .replace("\"", "") - ); debug!("[*]: Decode chapters from JSON"); // Decode json format @@ -68,7 +63,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext match previous.get("path") { Some(p) => { - let path = path_to_root.join(Path::new(p).with_extension("html")); + let path = Path::new(p).with_extension("html"); debug!("[*]: Inserting link: {:?}", path); match path.to_str() { @@ -125,12 +120,6 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> .to_string() .replace("\"", ""); - let path_to_root = PathBuf::from( - c.navigate(rc.get_path(), "path_to_root") - .to_string() - .replace("\"", "") - ); - debug!("[*]: Decode chapters from JSON"); // Decode json format let decoded: Vec> = match json::decode(&chapters.to_string()) { @@ -170,7 +159,7 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) -> } - let link = path_to_root.join(Path::new(path).with_extension("html")); + let link = Path::new(path).with_extension("html"); debug!("[*]: Inserting link: {:?}", link); match link.to_str() { diff --git a/src/renderer/html_handlebars/helpers/toc.rs b/src/renderer/html_handlebars/helpers/toc.rs index e2d9ba24..a2c3d46d 100644 --- a/src/renderer/html_handlebars/helpers/toc.rs +++ b/src/renderer/html_handlebars/helpers/toc.rs @@ -19,7 +19,6 @@ impl HelperDef for RenderToc { // param is the key of value you want to display let chapters = c.navigate(rc.get_path(), "chapters"); let current = c.navigate(rc.get_path(), "path").to_string().replace("\"", ""); - let path_to_root = c.navigate(rc.get_path(), "path_to_root").to_string().replace("\"", ""); try!(rc.writer.write("