Added a page containing the whole book for printing purposes, closes #41 + cleaning root path

This commit is contained in:
Mathieu David 2015-09-05 17:26:17 +02:00
parent 4789073e6d
commit d03ae79765
5 changed files with 48 additions and 23 deletions

View File

@ -7,7 +7,7 @@ use renderer::Renderer;
use book::MDBook; use book::MDBook;
use {utils, theme}; use {utils, theme};
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::fs::{self, File}; use std::fs::{self, File};
use std::error::Error; use std::error::Error;
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
@ -45,6 +45,9 @@ impl Renderer for HtmlHandlebars {
let mut data = try!(make_data(book)); let mut data = try!(make_data(book));
// Print version
let mut print_content: String = String::new();
// Check if dest directory exists // Check if dest directory exists
debug!("[*]: Check if destination directory exists"); debug!("[*]: Check if destination directory exists");
match utils::create_path(book.get_dest()) { match utils::create_path(book.get_dest()) {
@ -69,6 +72,7 @@ impl Renderer for HtmlHandlebars {
// Render markdown using the pulldown-cmark crate // Render markdown using the pulldown-cmark crate
content = render_html(&content); content = render_html(&content);
print_content.push_str(&content);
// Remove content from previous file and render content for this one // Remove content from previous file and render content for this one
data.remove("path"); 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, ...) // Copy static files (js, css, images, ...)
debug!("[*] Copy static files"); debug!("[*] Copy static files");

View File

@ -1,7 +1,7 @@
extern crate handlebars; extern crate handlebars;
extern crate rustc_serialize; extern crate rustc_serialize;
use std::path::{PathBuf, Path}; use std::path::Path;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use self::rustc_serialize::json::{self, ToJson}; 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() .to_string()
.replace("\"", ""); .replace("\"", "");
let path_to_root = PathBuf::from(
c.navigate(rc.get_path(), "path_to_root")
.to_string()
.replace("\"", "")
);
debug!("[*]: Decode chapters from JSON"); debug!("[*]: Decode chapters from JSON");
// Decode json format // Decode json format
@ -68,7 +63,7 @@ pub fn previous(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext
match previous.get("path") { match previous.get("path") {
Some(p) => { 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); debug!("[*]: Inserting link: {:?}", path);
match path.to_str() { match path.to_str() {
@ -125,12 +120,6 @@ pub fn next(c: &Context, _h: &Helper, r: &Handlebars, rc: &mut RenderContext) ->
.to_string() .to_string()
.replace("\"", ""); .replace("\"", "");
let path_to_root = PathBuf::from(
c.navigate(rc.get_path(), "path_to_root")
.to_string()
.replace("\"", "")
);
debug!("[*]: Decode chapters from JSON"); debug!("[*]: Decode chapters from JSON");
// Decode json format // Decode json format
let decoded: Vec<BTreeMap<String, String>> = match json::decode(&chapters.to_string()) { let decoded: Vec<BTreeMap<String, String>> = 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); debug!("[*]: Inserting link: {:?}", link);
match link.to_str() { match link.to_str() {

View File

@ -19,7 +19,6 @@ impl HelperDef for RenderToc {
// param is the key of value you want to display // param is the key of value you want to display
let chapters = c.navigate(rc.get_path(), "chapters"); let chapters = c.navigate(rc.get_path(), "chapters");
let current = c.navigate(rc.get_path(), "path").to_string().replace("\"", ""); 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("<ul class=\"chapter\">".as_bytes())); try!(rc.writer.write("<ul class=\"chapter\">".as_bytes()));
// Decode json format // Decode json format
@ -51,9 +50,6 @@ impl HelperDef for RenderToc {
if path.len() > 0 { if path.len() > 0 {
try!(rc.writer.write("<a href=\"".as_bytes())); try!(rc.writer.write("<a href=\"".as_bytes()));
// Prefix with path to root
try!(rc.writer.write(path_to_root.as_bytes()));
// Add link // Add link
try!(rc.writer.write( try!(rc.writer.write(
Path::new( Path::new(

View File

@ -1,5 +1,8 @@
$( document ).ready(function() { $( document ).ready(function() {
// url
var url = window.location.pathname;
// Syntax highlighting Configuration // Syntax highlighting Configuration
hljs.configure({ hljs.configure({
tabReplace: ' ', // 4 spaces tabReplace: ' ', // 4 spaces
@ -25,4 +28,13 @@ $( document ).ready(function() {
} }
}); });
// Print button
$("#print-button").click(function(){
var printWindow = window.open("print.html");
});
if( url.substring(url.lastIndexOf('/')+1) == "print.html" ) {
window.print();
}
}); });

View File

@ -7,10 +7,14 @@
<meta name="description" content="{% block description %}{% endblock %}"> <meta name="description" content="{% block description %}{% endblock %}">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{ path_to_root }}book.css"> <base href="{{ path_to_root }}">
<link rel="stylesheet" href="book.css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link rel="stylesheet" href="{{ path_to_root }}highlight.css"> <link rel="stylesheet" href="highlight.css">
</head> </head>
<body> <body>
@ -67,7 +71,7 @@
</div> </div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="{{ path_to_root }}highlight.js"></script> <script src="highlight.js"></script>
<script src="{{ path_to_root }}book.js"></script> <script src="book.js"></script>
</body> </body>
</html> </html>