2015-08-06 04:35:26 +08:00
|
|
|
use renderer::html_handlebars::helpers;
|
2017-07-04 05:18:27 +08:00
|
|
|
use preprocess;
|
2015-08-04 21:15:36 +08:00
|
|
|
use renderer::Renderer;
|
2015-08-31 21:24:42 +08:00
|
|
|
use book::MDBook;
|
2017-06-20 07:53:46 +08:00
|
|
|
use book::bookitem::{BookItem, Chapter};
|
2017-09-30 21:36:03 +08:00
|
|
|
use config::{Config, Playpen, HtmlConfig};
|
|
|
|
use {utils, theme};
|
|
|
|
use theme::{Theme, playpen_editor};
|
2017-06-25 00:10:06 +08:00
|
|
|
use errors::*;
|
2017-10-03 19:40:23 +08:00
|
|
|
use regex::{Captures, Regex};
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2017-02-16 14:39:13 +08:00
|
|
|
use std::ascii::AsciiExt;
|
2015-09-05 23:26:17 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-01-03 20:02:04 +08:00
|
|
|
use std::fs::{self, File};
|
2017-01-01 14:27:38 +08:00
|
|
|
use std::io::{self, Read};
|
2015-08-04 21:15:36 +08:00
|
|
|
use std::collections::BTreeMap;
|
2017-03-24 01:54:26 +08:00
|
|
|
use std::collections::HashMap;
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2016-08-01 20:06:08 +08:00
|
|
|
use handlebars::Handlebars;
|
2016-11-03 08:58:42 +08:00
|
|
|
|
|
|
|
use serde_json;
|
2015-12-30 07:46:55 +08:00
|
|
|
|
2017-02-16 11:01:26 +08:00
|
|
|
#[derive(Default)]
|
2015-08-04 21:15:36 +08:00
|
|
|
pub struct HtmlHandlebars;
|
|
|
|
|
2015-08-04 22:52:10 +08:00
|
|
|
impl HtmlHandlebars {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
HtmlHandlebars
|
|
|
|
}
|
2017-06-15 17:43:44 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
fn render_item(&self,
|
|
|
|
item: &BookItem,
|
|
|
|
mut ctx: RenderItemContext,
|
|
|
|
print_content: &mut String)
|
|
|
|
-> Result<()> {
|
2017-06-17 21:15:54 +08:00
|
|
|
// FIXME: This should be made DRY-er and rely less on mutable state
|
2017-06-15 17:43:44 +08:00
|
|
|
match *item {
|
2017-10-03 19:40:23 +08:00
|
|
|
BookItem::Chapter(_, ref ch) | BookItem::Affix(ref ch)
|
|
|
|
if !ch.path.as_os_str().is_empty() =>
|
|
|
|
{
|
2017-08-07 07:36:29 +08:00
|
|
|
let path = ctx.book.get_source().join(&ch.path);
|
|
|
|
let content = utils::fs::file_to_string(&path)?;
|
2017-10-03 19:40:23 +08:00
|
|
|
let base = path.parent()
|
|
|
|
.ok_or_else(|| String::from("Invalid bookitem path!"))?;
|
2017-08-07 07:36:29 +08:00
|
|
|
|
|
|
|
// Parse and expand links
|
|
|
|
let content = preprocess::links::replace_all(&content, base)?;
|
2017-09-30 21:36:03 +08:00
|
|
|
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
|
2017-08-07 07:36:29 +08:00
|
|
|
print_content.push_str(&content);
|
|
|
|
|
|
|
|
// Update the context with data for this file
|
|
|
|
let path = ch.path.to_str().ok_or_else(|| {
|
2017-10-03 19:40:23 +08:00
|
|
|
io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path \
|
|
|
|
to str")
|
|
|
|
})?;
|
2017-08-07 07:36:29 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
// Non-lexical lifetimes needed :'(
|
2017-09-08 05:24:43 +08:00
|
|
|
let title: String;
|
2017-09-08 05:19:22 +08:00
|
|
|
{
|
2017-10-03 19:40:23 +08:00
|
|
|
let book_title = ctx.data
|
|
|
|
.get("book_title")
|
|
|
|
.and_then(serde_json::Value::as_str)
|
|
|
|
.unwrap_or("");
|
2017-09-08 05:19:22 +08:00
|
|
|
title = ch.name.clone() + " - " + book_title;
|
|
|
|
}
|
2017-10-03 19:40:23 +08:00
|
|
|
|
2017-08-07 07:36:29 +08:00
|
|
|
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));
|
2017-09-08 05:19:22 +08:00
|
|
|
ctx.data.insert("title".to_owned(), json!(title));
|
2017-10-03 19:40:23 +08:00
|
|
|
ctx.data.insert("path_to_root".to_owned(),
|
|
|
|
json!(utils::fs::path_to_root(&ch.path)));
|
2017-08-07 07:36:29 +08:00
|
|
|
|
|
|
|
// Render the handlebars template with the data
|
|
|
|
debug!("[*]: Render template");
|
|
|
|
let rendered = ctx.handlebars.render("index", &ctx.data)?;
|
|
|
|
|
2017-09-02 07:54:57 +08:00
|
|
|
let filepath = Path::new(&ch.path).with_extension("html");
|
2017-10-03 19:40:23 +08:00
|
|
|
let rendered = self.post_process(
|
|
|
|
rendered,
|
2017-10-08 00:11:05 +08:00
|
|
|
&normalize_path(filepath.to_str().ok_or_else(|| Error::from(
|
2017-10-03 19:40:23 +08:00
|
|
|
format!("Bad file name: {}", filepath.display()),
|
|
|
|
))?),
|
2017-09-30 22:11:47 +08:00
|
|
|
&ctx.book.config.html_config().unwrap_or_default().playpen,
|
2017-09-06 16:05:27 +08:00
|
|
|
);
|
2017-08-07 07:36:29 +08:00
|
|
|
|
|
|
|
// Write to file
|
2017-09-02 07:54:57 +08:00
|
|
|
info!("[*] Creating {:?} ✓", filepath.display());
|
|
|
|
ctx.book.write_file(filepath, &rendered.into_bytes())?;
|
2017-08-07 07:36:29 +08:00
|
|
|
|
|
|
|
if ctx.is_index {
|
|
|
|
self.render_index(ctx.book, ch, &ctx.destination)?;
|
2017-06-15 17:43:44 +08:00
|
|
|
}
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
|
|
|
_ => {}
|
2017-06-15 17:43:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-06-15 18:03:10 +08:00
|
|
|
|
2017-06-20 11:06:30 +08:00
|
|
|
/// Create an index.html from the first element in SUMMARY.md
|
2017-06-25 00:10:06 +08:00
|
|
|
fn render_index(&self, book: &MDBook, ch: &Chapter, destination: &Path) -> Result<()> {
|
2017-06-20 08:40:01 +08:00
|
|
|
debug!("[*]: index.html");
|
|
|
|
|
|
|
|
let mut content = String::new();
|
|
|
|
|
|
|
|
File::open(destination.join(&ch.path.with_extension("html")))?
|
|
|
|
.read_to_string(&mut content)?;
|
|
|
|
|
|
|
|
// This could cause a problem when someone displays
|
|
|
|
// code containing <base href=...>
|
|
|
|
// on the front page, however this case should be very very rare...
|
2017-10-03 19:40:23 +08:00
|
|
|
content = content.lines()
|
|
|
|
.filter(|line| !line.contains("<base href="))
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join("\n");
|
2017-06-20 08:40:01 +08:00
|
|
|
|
|
|
|
book.write_file("index.html", content.as_bytes())?;
|
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
info!("[*] Creating index.html from {:?} ✓",
|
|
|
|
book.get_destination().join(&ch.path.with_extension("html")));
|
2017-06-20 07:53:46 +08:00
|
|
|
|
2017-06-20 08:40:01 +08:00
|
|
|
Ok(())
|
2017-06-20 07:53:46 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 04:14:48 +08:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(let_and_return))]
|
2017-10-03 19:40:23 +08:00
|
|
|
fn post_process(&self,
|
|
|
|
rendered: String,
|
|
|
|
filepath: &str,
|
2017-09-30 22:11:47 +08:00
|
|
|
playpen_config: &Playpen)
|
2017-10-03 19:40:23 +08:00
|
|
|
-> String {
|
2017-10-08 00:11:05 +08:00
|
|
|
let rendered = build_header_links(&rendered, filepath);
|
|
|
|
let rendered = fix_anchor_links(&rendered, filepath);
|
2017-06-27 15:08:58 +08:00
|
|
|
let rendered = fix_code_blocks(&rendered);
|
2017-06-29 12:35:20 +08:00
|
|
|
let rendered = add_playpen_pre(&rendered, playpen_config);
|
2017-06-15 18:03:10 +08:00
|
|
|
|
|
|
|
rendered
|
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
fn copy_static_files(&self, book: &MDBook, theme: &Theme, html_config: &HtmlConfig) -> Result<()> {
|
2017-06-15 18:03:10 +08:00
|
|
|
book.write_file("book.js", &theme.js)?;
|
|
|
|
book.write_file("book.css", &theme.css)?;
|
|
|
|
book.write_file("favicon.png", &theme.favicon)?;
|
|
|
|
book.write_file("jquery.js", &theme.jquery)?;
|
|
|
|
book.write_file("highlight.css", &theme.highlight_css)?;
|
2017-10-03 19:40:23 +08:00
|
|
|
book.write_file("tomorrow-night.css", &theme.tomorrow_night_css)?;
|
|
|
|
book.write_file("ayu-highlight.css", &theme.ayu_highlight_css)?;
|
2017-06-15 18:03:10 +08:00
|
|
|
book.write_file("highlight.js", &theme.highlight_js)?;
|
|
|
|
book.write_file("clipboard.min.js", &theme.clipboard_js)?;
|
|
|
|
book.write_file("store.js", &theme.store_js)?;
|
2017-10-03 19:40:23 +08:00
|
|
|
book.write_file("_FontAwesome/css/font-awesome.css", theme::FONT_AWESOME)?;
|
|
|
|
book.write_file("_FontAwesome/fonts/fontawesome-webfont.eot",
|
|
|
|
theme::FONT_AWESOME_EOT)?;
|
|
|
|
book.write_file("_FontAwesome/fonts/fontawesome-webfont.svg",
|
|
|
|
theme::FONT_AWESOME_SVG)?;
|
|
|
|
book.write_file("_FontAwesome/fonts/fontawesome-webfont.ttf",
|
|
|
|
theme::FONT_AWESOME_TTF)?;
|
|
|
|
book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff",
|
|
|
|
theme::FONT_AWESOME_WOFF)?;
|
|
|
|
book.write_file("_FontAwesome/fonts/fontawesome-webfont.woff2",
|
|
|
|
theme::FONT_AWESOME_WOFF2)?;
|
|
|
|
book.write_file("_FontAwesome/fonts/FontAwesome.ttf",
|
|
|
|
theme::FONT_AWESOME_TTF)?;
|
2017-06-15 18:03:10 +08:00
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
let playpen_config = &html_config.playpen;
|
2017-06-29 12:35:20 +08:00
|
|
|
|
|
|
|
// Ace is a very large dependency, so only load it when requested
|
2017-09-30 21:36:03 +08:00
|
|
|
if playpen_config.editable {
|
2017-06-29 12:35:20 +08:00
|
|
|
// Load the editor
|
2017-09-30 21:36:03 +08:00
|
|
|
let editor = playpen_editor::PlaypenEditor::new(&playpen_config.editor);
|
2017-06-29 12:35:20 +08:00
|
|
|
book.write_file("editor.js", &editor.js)?;
|
|
|
|
book.write_file("ace.js", &editor.ace_js)?;
|
|
|
|
book.write_file("mode-rust.js", &editor.mode_rust_js)?;
|
|
|
|
book.write_file("theme-dawn.js", &editor.theme_dawn_js)?;
|
|
|
|
book.write_file("theme-tomorrow_night.js", &editor.theme_tomorrow_night_js)?;
|
|
|
|
}
|
|
|
|
|
2017-06-15 18:03:10 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2017-09-02 07:54:57 +08:00
|
|
|
/// Helper function to write a file to the build directory, normalizing
|
2017-06-24 15:50:51 +08:00
|
|
|
/// the path to be relative to the book root.
|
2017-06-25 00:10:06 +08:00
|
|
|
fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<()> {
|
2017-06-17 21:15:54 +08:00
|
|
|
let mut data = Vec::new();
|
|
|
|
let mut f = File::open(custom_file)?;
|
|
|
|
f.read_to_end(&mut data)?;
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
let name = match custom_file.strip_prefix(&book.root) {
|
2017-06-17 21:15:54 +08:00
|
|
|
Ok(p) => p.to_str().expect("Could not convert to str"),
|
|
|
|
Err(_) => {
|
2017-10-03 19:40:23 +08:00
|
|
|
custom_file.file_name()
|
|
|
|
.expect("File has a file name")
|
|
|
|
.to_str()
|
|
|
|
.expect("Could not convert to str")
|
|
|
|
}
|
2017-06-17 21:15:54 +08:00
|
|
|
};
|
2017-06-15 18:03:10 +08:00
|
|
|
|
2017-06-17 21:15:54 +08:00
|
|
|
book.write_file(name, &data)?;
|
2017-06-15 18:03:10 +08:00
|
|
|
|
2017-06-17 21:15:54 +08:00
|
|
|
Ok(())
|
2017-06-15 18:03:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the context with data for this file
|
2017-10-03 19:40:23 +08:00
|
|
|
fn configure_print_version(&self,
|
|
|
|
data: &mut serde_json::Map<String, serde_json::Value>,
|
|
|
|
print_content: &str) {
|
2017-09-01 14:20:27 +08:00
|
|
|
// Make sure that the Print chapter does not display the title from
|
|
|
|
// the last rendered chapter by removing it from its context
|
2017-09-08 05:19:22 +08:00
|
|
|
data.remove("title");
|
2017-09-14 04:17:23 +08:00
|
|
|
data.insert("is_print".to_owned(), json!(true));
|
2017-06-15 18:03:10 +08:00
|
|
|
data.insert("path".to_owned(), json!("print.md"));
|
|
|
|
data.insert("content".to_owned(), json!(print_content));
|
2017-10-03 19:40:23 +08:00
|
|
|
data.insert("path_to_root".to_owned(),
|
|
|
|
json!(utils::fs::path_to_root(Path::new("print.md"))));
|
2017-06-15 18:03:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn register_hbs_helpers(&self, handlebars: &mut Handlebars) {
|
|
|
|
handlebars.register_helper("toc", Box::new(helpers::toc::RenderToc));
|
|
|
|
handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
|
|
|
|
handlebars.register_helper("next", Box::new(helpers::navigation::next));
|
|
|
|
}
|
2017-06-15 18:17:16 +08:00
|
|
|
|
2017-06-17 21:15:54 +08:00
|
|
|
/// Copy across any additional CSS and JavaScript files which the book
|
|
|
|
/// has been configured to use.
|
2017-06-25 00:10:06 +08:00
|
|
|
fn copy_additional_css_and_js(&self, book: &MDBook) -> Result<()> {
|
2017-09-30 21:36:03 +08:00
|
|
|
let html = book.config.html_config().unwrap_or_default();
|
|
|
|
|
|
|
|
let custom_files = html.additional_css
|
2017-10-03 19:40:23 +08:00
|
|
|
.iter()
|
2017-09-30 21:36:03 +08:00
|
|
|
.chain(html.additional_js.iter());
|
2017-06-17 21:15:54 +08:00
|
|
|
|
|
|
|
for custom_file in custom_files {
|
|
|
|
self.write_custom_file(custom_file, book)?;
|
2017-06-15 18:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-08-04 22:52:10 +08:00
|
|
|
}
|
|
|
|
|
2017-06-15 18:03:10 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
impl Renderer for HtmlHandlebars {
|
2017-06-25 00:10:06 +08:00
|
|
|
fn render(&self, book: &MDBook) -> Result<()> {
|
2017-09-30 21:36:03 +08:00
|
|
|
let html_config = book.config.html_config().unwrap_or_default();
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
debug!("[fn]: render");
|
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
let theme_dir = match html_config.theme {
|
|
|
|
Some(ref theme) => theme,
|
|
|
|
None => Path::new("theme"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let theme = theme::Theme::new(theme_dir);
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2017-09-28 07:06:30 +08:00
|
|
|
debug!("[*]: Register the index handlebars template");
|
|
|
|
handlebars.register_template_string(
|
|
|
|
"index",
|
|
|
|
String::from_utf8(theme.index.clone())?,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
debug!("[*]: Register the header handlebars template");
|
|
|
|
handlebars.register_partial(
|
|
|
|
"header",
|
|
|
|
String::from_utf8(theme.header.clone())?,
|
|
|
|
)?;
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
debug!("[*]: Register handlebars helpers");
|
2017-06-15 18:03:10 +08:00
|
|
|
self.register_hbs_helpers(&mut handlebars);
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
let mut data = make_data(book, &book.config)?;
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// Print version
|
2017-06-15 18:39:41 +08:00
|
|
|
let mut print_content = String::new();
|
2015-09-05 23:26:17 +08:00
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
// TODO: The Renderer trait should really pass in where it wants us to build to...
|
2017-09-30 22:11:47 +08:00
|
|
|
let destination = book.get_destination();
|
2017-06-15 18:03:10 +08:00
|
|
|
|
2017-06-15 18:39:41 +08:00
|
|
|
debug!("[*]: Check if destination directory exists");
|
2017-09-30 22:11:47 +08:00
|
|
|
fs::create_dir_all(&destination)
|
|
|
|
.chain_err(|| "Unexpected error when constructing destination path")?;
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2017-06-20 07:53:46 +08:00
|
|
|
for (i, item) in book.iter().enumerate() {
|
2017-06-20 08:40:01 +08:00
|
|
|
let ctx = RenderItemContext {
|
|
|
|
book: book,
|
|
|
|
handlebars: &handlebars,
|
|
|
|
destination: destination.to_path_buf(),
|
|
|
|
data: data.clone(),
|
|
|
|
is_index: i == 0,
|
2017-09-30 21:36:03 +08:00
|
|
|
html_config: html_config.clone(),
|
2017-06-20 08:40:01 +08:00
|
|
|
};
|
|
|
|
self.render_item(item, ctx, &mut print_content)?;
|
2015-08-04 21:15:36 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 23:26:17 +08:00
|
|
|
// Print version
|
2017-06-15 18:03:10 +08:00
|
|
|
self.configure_print_version(&mut data, &print_content);
|
2017-09-30 21:36:03 +08:00
|
|
|
if let Some(ref title) = book.config.book.title {
|
|
|
|
data.insert("title".to_owned(), json!(title));
|
|
|
|
}
|
2015-09-05 23:26:17 +08:00
|
|
|
|
2017-01-01 10:33:17 +08:00
|
|
|
// Render the handlebars template with the data
|
2015-09-05 23:26:17 +08:00
|
|
|
debug!("[*]: Render template");
|
2017-02-20 22:28:49 +08:00
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
let rendered = handlebars.render("index", &data)?;
|
2017-02-28 08:41:06 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
let rendered = self.post_process(rendered,
|
|
|
|
"print.html",
|
2017-10-16 20:47:22 +08:00
|
|
|
&html_config.playpen);
|
2017-09-02 07:54:57 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
book.write_file(Path::new("print").with_extension("html"),
|
|
|
|
&rendered.into_bytes())?;
|
2016-08-14 20:32:34 +08:00
|
|
|
info!("[*] Creating print.html ✓");
|
2015-09-05 23:26:17 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
// Copy static files (js, css, images, ...)
|
|
|
|
debug!("[*] Copy static files");
|
2017-09-30 21:36:03 +08:00
|
|
|
self.copy_static_files(book, &theme, &html_config)?;
|
2017-06-17 21:15:54 +08:00
|
|
|
self.copy_additional_css_and_js(book)?;
|
2017-05-20 19:56:01 +08:00
|
|
|
|
2015-09-18 01:45:06 +08:00
|
|
|
// Copy all remaining files
|
2017-09-30 21:36:03 +08:00
|
|
|
let src = book.get_source();
|
|
|
|
utils::fs::copy_files_except_ext(&src, &destination, true, &["md"])?;
|
2017-05-19 05:52:38 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
fn make_data(book: &MDBook, config: &Config) -> Result<serde_json::Map<String, serde_json::Value>> {
|
2015-08-04 21:15:36 +08:00
|
|
|
debug!("[fn]: make_data");
|
2017-09-30 21:36:03 +08:00
|
|
|
let html = config.html_config().unwrap_or_default();
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2016-11-03 08:58:42 +08:00
|
|
|
let mut data = serde_json::Map::new();
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("language".to_owned(), json!("en"));
|
2017-09-30 21:36:03 +08:00
|
|
|
data.insert("book_title".to_owned(), json!(config.book.title.clone().unwrap_or_default()));
|
|
|
|
data.insert("description".to_owned(), json!(config.book.description.clone().unwrap_or_default()));
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("favicon".to_owned(), json!("favicon.png"));
|
2017-09-30 21:36:03 +08:00
|
|
|
if let Some(ref livereload) = book.livereload {
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("livereload".to_owned(), json!(livereload));
|
2016-04-02 10:46:05 +08:00
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2017-05-16 13:05:21 +08:00
|
|
|
// Add google analytics tag
|
2017-10-16 20:47:22 +08:00
|
|
|
if let Some(ref ga) = config.html_config().and_then(|html| html.google_analytics) {
|
2017-05-16 13:24:05 +08:00
|
|
|
data.insert("google_analytics".to_owned(), json!(ga));
|
|
|
|
}
|
2017-05-16 13:05:21 +08:00
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
if html.mathjax_support {
|
2017-06-25 06:32:26 +08:00
|
|
|
data.insert("mathjax_support".to_owned(), json!(true));
|
|
|
|
}
|
|
|
|
|
2017-05-20 19:56:01 +08:00
|
|
|
// Add check to see if there is an additional style
|
2017-09-30 21:36:03 +08:00
|
|
|
if !html.additional_css.is_empty() {
|
2017-05-20 19:56:01 +08:00
|
|
|
let mut css = Vec::new();
|
2017-09-30 21:36:03 +08:00
|
|
|
for style in &html.additional_css {
|
|
|
|
match style.strip_prefix(&book.root) {
|
2017-05-20 19:56:01 +08:00
|
|
|
Ok(p) => css.push(p.to_str().expect("Could not convert to str")),
|
2017-06-15 17:43:44 +08:00
|
|
|
Err(_) => {
|
2017-10-03 19:40:23 +08:00
|
|
|
css.push(style.file_name()
|
|
|
|
.expect("File has a file name")
|
|
|
|
.to_str()
|
|
|
|
.expect("Could not convert to str"))
|
|
|
|
}
|
2017-05-20 19:56:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data.insert("additional_css".to_owned(), json!(css));
|
|
|
|
}
|
|
|
|
|
2017-06-07 02:34:57 +08:00
|
|
|
// Add check to see if there is an additional script
|
2017-09-30 21:36:03 +08:00
|
|
|
if !html.additional_js.is_empty() {
|
2017-06-07 02:34:57 +08:00
|
|
|
let mut js = Vec::new();
|
2017-09-30 21:36:03 +08:00
|
|
|
for script in &html.additional_js {
|
|
|
|
match script.strip_prefix(&book.root) {
|
2017-06-07 02:34:57 +08:00
|
|
|
Ok(p) => js.push(p.to_str().expect("Could not convert to str")),
|
2017-06-15 17:43:44 +08:00
|
|
|
Err(_) => {
|
2017-10-03 19:40:23 +08:00
|
|
|
js.push(script.file_name()
|
|
|
|
.expect("File has a file name")
|
|
|
|
.to_str()
|
|
|
|
.expect("Could not convert to str"))
|
|
|
|
}
|
2017-06-07 02:34:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data.insert("additional_js".to_owned(), json!(js));
|
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
if html.playpen.editable {
|
2017-06-29 12:35:20 +08:00
|
|
|
data.insert("playpens_editable".to_owned(), json!(true));
|
|
|
|
data.insert("editor_js".to_owned(), json!("editor.js"));
|
|
|
|
data.insert("ace_js".to_owned(), json!("ace.js"));
|
|
|
|
data.insert("mode_rust_js".to_owned(), json!("mode-rust.js"));
|
|
|
|
data.insert("theme_dawn_js".to_owned(), json!("theme-dawn.js"));
|
2017-10-03 19:40:23 +08:00
|
|
|
data.insert("theme_tomorrow_night_js".to_owned(),
|
|
|
|
json!("theme-tomorrow_night.js"));
|
2017-06-29 12:35:20 +08:00
|
|
|
}
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
let mut chapters = vec![];
|
|
|
|
|
2015-09-12 02:52:55 +08:00
|
|
|
for item in book.iter() {
|
|
|
|
// Create the data to inject in the template
|
2015-08-04 21:15:36 +08:00
|
|
|
let mut chapter = BTreeMap::new();
|
2015-09-12 02:52:55 +08:00
|
|
|
|
2015-09-17 10:46:23 +08:00
|
|
|
match *item {
|
|
|
|
BookItem::Affix(ref ch) => {
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("name".to_owned(), json!(ch.name));
|
2017-06-20 08:40:01 +08:00
|
|
|
let path = ch.path.to_str().ok_or_else(|| {
|
2017-10-03 19:40:23 +08:00
|
|
|
io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path \
|
|
|
|
to str")
|
|
|
|
})?;
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("path".to_owned(), json!(path));
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2015-09-17 10:46:23 +08:00
|
|
|
BookItem::Chapter(ref s, ref ch) => {
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("section".to_owned(), json!(s));
|
|
|
|
chapter.insert("name".to_owned(), json!(ch.name));
|
2017-06-20 08:40:01 +08:00
|
|
|
let path = ch.path.to_str().ok_or_else(|| {
|
2017-10-03 19:40:23 +08:00
|
|
|
io::Error::new(io::ErrorKind::Other,
|
|
|
|
"Could not convert path \
|
|
|
|
to str")
|
|
|
|
})?;
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("path".to_owned(), json!(path));
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2015-09-17 10:46:23 +08:00
|
|
|
BookItem::Spacer => {
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("spacer".to_owned(), json!("_spacer_"));
|
2017-10-03 19:40:23 +08:00
|
|
|
}
|
2015-08-12 04:55:51 +08:00
|
|
|
}
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
chapters.push(chapter);
|
|
|
|
}
|
|
|
|
|
2017-02-16 11:34:37 +08:00
|
|
|
data.insert("chapters".to_owned(), json!(chapters));
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
debug!("[*]: JSON constructed");
|
|
|
|
Ok(data)
|
|
|
|
}
|
2017-02-16 14:39:13 +08:00
|
|
|
|
2017-06-20 11:15:12 +08:00
|
|
|
/// Goes through the rendered HTML, making sure all header tags are wrapped in
|
|
|
|
/// an anchor so people can link to sections directly.
|
2017-09-02 07:54:57 +08:00
|
|
|
fn build_header_links(html: &str, filepath: &str) -> String {
|
2017-02-17 08:29:50 +08:00
|
|
|
let regex = Regex::new(r"<h(\d)>(.*?)</h\d>").unwrap();
|
2017-03-24 01:54:26 +08:00
|
|
|
let mut id_counter = HashMap::new();
|
2017-02-17 08:29:50 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
regex.replace_all(html, |caps: &Captures| {
|
|
|
|
let level = caps[1].parse()
|
|
|
|
.expect("Regex should ensure we only ever get numbers here");
|
2017-05-19 19:04:37 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
wrap_header_with_link(level, &caps[2], &mut id_counter, filepath)
|
|
|
|
})
|
|
|
|
.into_owned()
|
2017-06-20 11:06:30 +08:00
|
|
|
}
|
2017-05-19 19:04:37 +08:00
|
|
|
|
2017-06-20 11:23:53 +08:00
|
|
|
/// Wraps a single header tag with a link, making sure each tag gets its own
|
2017-06-20 11:15:12 +08:00
|
|
|
/// unique ID by appending an auto-incremented number (if necessary).
|
2017-10-03 19:40:23 +08:00
|
|
|
fn wrap_header_with_link(level: usize,
|
|
|
|
content: &str,
|
|
|
|
id_counter: &mut HashMap<String, usize>,
|
|
|
|
filepath: &str)
|
|
|
|
-> String {
|
2017-06-20 11:15:12 +08:00
|
|
|
let raw_id = id_from_content(content);
|
2017-06-20 11:06:30 +08:00
|
|
|
|
2017-06-20 11:15:12 +08:00
|
|
|
let id_count = id_counter.entry(raw_id.clone()).or_insert(0);
|
2017-06-20 11:06:30 +08:00
|
|
|
|
2017-06-20 11:15:12 +08:00
|
|
|
let id = match *id_count {
|
|
|
|
0 => raw_id,
|
|
|
|
other => format!("{}-{}", raw_id, other),
|
2017-06-20 11:06:30 +08:00
|
|
|
};
|
|
|
|
|
2017-06-20 11:15:12 +08:00
|
|
|
*id_count += 1;
|
|
|
|
|
2017-06-20 11:06:30 +08:00
|
|
|
format!(
|
2017-09-02 07:54:57 +08:00
|
|
|
r##"<a class="header" href="{filepath}#{id}" id="{id}"><h{level}>{text}</h{level}></a>"##,
|
2017-06-20 11:06:30 +08:00
|
|
|
level = level,
|
|
|
|
id = id,
|
|
|
|
text = content,
|
2017-09-02 07:54:57 +08:00
|
|
|
filepath = filepath
|
2017-06-20 11:06:30 +08:00
|
|
|
)
|
|
|
|
}
|
2017-05-19 19:04:37 +08:00
|
|
|
|
2017-10-03 19:40:23 +08:00
|
|
|
/// Generate an id for use with anchors which is derived from a "normalised"
|
2017-06-20 11:23:53 +08:00
|
|
|
/// string.
|
2017-06-20 11:06:30 +08:00
|
|
|
fn id_from_content(content: &str) -> String {
|
|
|
|
let mut content = content.to_string();
|
|
|
|
|
|
|
|
// Skip any tags or html-encoded stuff
|
2017-10-03 19:40:23 +08:00
|
|
|
const REPL_SUB: &[&str] = &["<em>",
|
|
|
|
"</em>",
|
|
|
|
"<code>",
|
|
|
|
"</code>",
|
|
|
|
"<strong>",
|
|
|
|
"</strong>",
|
|
|
|
"<",
|
|
|
|
">",
|
|
|
|
"&",
|
|
|
|
"'",
|
|
|
|
"""];
|
2017-09-02 07:54:57 +08:00
|
|
|
for sub in REPL_SUB {
|
2017-06-20 11:06:30 +08:00
|
|
|
content = content.replace(sub, "");
|
|
|
|
}
|
|
|
|
|
2017-09-09 01:59:04 +08:00
|
|
|
// Remove spaces and hastags indicating a header
|
2017-10-08 00:11:05 +08:00
|
|
|
let trimmed = content.trim().trim_left_matches('#').trim();
|
2017-09-09 01:59:04 +08:00
|
|
|
|
|
|
|
normalize_id(trimmed)
|
2017-02-16 14:39:13 +08:00
|
|
|
}
|
2017-02-28 08:41:06 +08:00
|
|
|
|
2017-02-28 08:17:42 +08:00
|
|
|
// anchors to the same page (href="#anchor") do not work because of
|
|
|
|
// <base href="../"> pointing to the root folder. This function *fixes*
|
|
|
|
// that in a very inelegant way
|
2017-09-02 07:54:57 +08:00
|
|
|
fn fix_anchor_links(html: &str, filepath: &str) -> String {
|
2017-02-28 08:17:42 +08:00
|
|
|
let regex = Regex::new(r##"<a([^>]+)href="#([^"]+)"([^>]*)>"##).unwrap();
|
2017-10-03 19:40:23 +08:00
|
|
|
regex.replace_all(html, |caps: &Captures| {
|
|
|
|
let before = &caps[1];
|
|
|
|
let anchor = &caps[2];
|
|
|
|
let after = &caps[3];
|
|
|
|
|
|
|
|
format!("<a{before}href=\"{filepath}#{anchor}\"{after}>",
|
2017-06-20 08:40:01 +08:00
|
|
|
before = before,
|
2017-09-02 07:54:57 +08:00
|
|
|
filepath = filepath,
|
2017-06-20 08:40:01 +08:00
|
|
|
anchor = anchor,
|
2017-10-03 19:40:23 +08:00
|
|
|
after = after)
|
|
|
|
})
|
|
|
|
.into_owned()
|
2017-02-28 08:17:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-19 19:04:37 +08:00
|
|
|
// The rust book uses annotations for rustdoc to test code snippets,
|
|
|
|
// like the following:
|
2017-02-28 08:41:06 +08:00
|
|
|
// ```rust,should_panic
|
|
|
|
// fn main() {
|
|
|
|
// // Code here
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// This function replaces all commas by spaces in the code block classes
|
2017-06-27 15:08:58 +08:00
|
|
|
fn fix_code_blocks(html: &str) -> String {
|
2017-02-28 08:41:06 +08:00
|
|
|
let regex = Regex::new(r##"<code([^>]+)class="([^"]+)"([^>]*)>"##).unwrap();
|
2017-10-03 19:40:23 +08:00
|
|
|
regex.replace_all(html, |caps: &Captures| {
|
|
|
|
let before = &caps[1];
|
|
|
|
let classes = &caps[2].replace(",", " ");
|
|
|
|
let after = &caps[3];
|
|
|
|
|
|
|
|
format!(r#"<code{before}class="{classes}"{after}>"#,
|
|
|
|
before = before,
|
|
|
|
classes = classes,
|
|
|
|
after = after)
|
|
|
|
})
|
|
|
|
.into_owned()
|
2017-02-28 08:41:06 +08:00
|
|
|
}
|
2017-03-07 01:23:15 +08:00
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
fn add_playpen_pre(html: &str, playpen_config: &Playpen) -> String {
|
2017-03-07 02:27:25 +08:00
|
|
|
let regex = Regex::new(r##"((?s)<code[^>]?class="([^"]+)".*?>(.*?)</code>)"##).unwrap();
|
2017-10-03 19:40:23 +08:00
|
|
|
regex.replace_all(html, |caps: &Captures| {
|
|
|
|
let text = &caps[1];
|
|
|
|
let classes = &caps[2];
|
|
|
|
let code = &caps[3];
|
|
|
|
|
|
|
|
if (classes.contains("language-rust") && !classes.contains("ignore")) ||
|
|
|
|
classes.contains("mdbook-runnable")
|
|
|
|
{
|
|
|
|
// wrap the contents in an external pre block
|
2017-09-30 21:36:03 +08:00
|
|
|
if playpen_config.editable && classes.contains("editable") ||
|
2017-10-03 19:40:23 +08:00
|
|
|
text.contains("fn main") || text.contains("quick_main!")
|
|
|
|
{
|
|
|
|
format!("<pre class=\"playpen\">{}</pre>", text)
|
2017-05-19 19:04:37 +08:00
|
|
|
} else {
|
2017-10-03 19:40:23 +08:00
|
|
|
// we need to inject our own main
|
|
|
|
let (attrs, code) = partition_source(code);
|
|
|
|
|
|
|
|
format!("<pre class=\"playpen\"><code class=\"{}\">\n# \
|
|
|
|
#![allow(unused_variables)]\n\
|
|
|
|
{}#fn main() {{\n\
|
|
|
|
{}\
|
|
|
|
#}}</code></pre>",
|
|
|
|
classes,
|
|
|
|
attrs,
|
|
|
|
code)
|
2017-03-07 02:27:25 +08:00
|
|
|
}
|
2017-10-03 19:40:23 +08:00
|
|
|
} else {
|
|
|
|
// not language-rust, so no-op
|
|
|
|
text.to_owned()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into_owned()
|
2017-03-07 01:23:15 +08:00
|
|
|
}
|
2017-04-01 05:06:03 +08:00
|
|
|
|
|
|
|
fn partition_source(s: &str) -> (String, String) {
|
|
|
|
let mut after_header = false;
|
|
|
|
let mut before = String::new();
|
|
|
|
let mut after = String::new();
|
|
|
|
|
|
|
|
for line in s.lines() {
|
|
|
|
let trimline = line.trim();
|
2017-05-19 19:04:37 +08:00
|
|
|
let header = trimline.chars().all(|c| c.is_whitespace()) || trimline.starts_with("#![");
|
2017-04-01 05:06:03 +08:00
|
|
|
if !header || after_header {
|
|
|
|
after_header = true;
|
|
|
|
after.push_str(line);
|
|
|
|
after.push_str("\n");
|
|
|
|
} else {
|
|
|
|
before.push_str(line);
|
|
|
|
before.push_str("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(before, after)
|
2017-05-19 19:04:37 +08:00
|
|
|
}
|
2017-06-20 08:40:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
struct RenderItemContext<'a> {
|
|
|
|
handlebars: &'a Handlebars,
|
|
|
|
book: &'a MDBook,
|
|
|
|
destination: PathBuf,
|
|
|
|
data: serde_json::Map<String, serde_json::Value>,
|
|
|
|
is_index: bool,
|
2017-09-30 21:36:03 +08:00
|
|
|
html_config: HtmlConfig,
|
2017-06-20 08:40:01 +08:00
|
|
|
}
|
2017-06-20 10:54:32 +08:00
|
|
|
|
2017-09-03 04:19:24 +08:00
|
|
|
pub fn normalize_path(path: &str) -> String {
|
|
|
|
use std::path::is_separator;
|
|
|
|
path.chars()
|
|
|
|
.map(|ch| if is_separator(ch) { '/' } else { ch })
|
|
|
|
.collect::<String>()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn normalize_id(content: &str) -> String {
|
|
|
|
content.chars()
|
2017-10-03 19:40:23 +08:00
|
|
|
.filter_map(|ch| if ch.is_alphanumeric() || ch == '_' || ch == '-' {
|
|
|
|
Some(ch.to_ascii_lowercase())
|
|
|
|
} else if ch.is_whitespace() {
|
|
|
|
Some('-')
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.collect::<String>()
|
2017-09-03 04:19:24 +08:00
|
|
|
}
|
2017-06-20 10:54:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn original_build_header_links() {
|
|
|
|
let inputs = vec![
|
2017-09-02 07:54:57 +08:00
|
|
|
(
|
|
|
|
"blah blah <h1>Foo</h1>",
|
|
|
|
r##"blah blah <a class="header" href="./some_chapter/some_section.html#foo" id="foo"><h1>Foo</h1></a>"##,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h1>Foo</h1>",
|
|
|
|
r##"<a class="header" href="./some_chapter/some_section.html#foo" id="foo"><h1>Foo</h1></a>"##,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h3>Foo^bar</h3>",
|
|
|
|
r##"<a class="header" href="./some_chapter/some_section.html#foobar" id="foobar"><h3>Foo^bar</h3></a>"##,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h4></h4>",
|
2017-10-03 19:40:23 +08:00
|
|
|
r##"<a class="header" href="./some_chapter/some_section.html#" id=""><h4></h4></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h4><em>Hï</em></h4>",
|
2017-10-03 19:40:23 +08:00
|
|
|
r##"<a class="header" href="./some_chapter/some_section.html#hï" id="hï"><h4><em>Hï</em></h4></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h1>Foo</h1><h3>Foo</h3>",
|
2017-10-03 19:40:23 +08:00
|
|
|
r##"<a class="header" href="./some_chapter/some_section.html#foo" id="foo"><h1>Foo</h1></a><a class="header" href="./some_chapter/some_section.html#foo-1" id="foo-1"><h3>Foo</h3></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
2017-06-20 10:54:32 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
for (src, should_be) in inputs {
|
2017-09-02 07:54:57 +08:00
|
|
|
let filepath = "./some_chapter/some_section.html";
|
|
|
|
let got = build_header_links(&src, filepath);
|
|
|
|
assert_eq!(got, should_be);
|
|
|
|
|
|
|
|
// This is redundant for most cases
|
|
|
|
let got = fix_anchor_links(&got, filepath);
|
2017-06-20 10:54:32 +08:00
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 01:59:04 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn anchor_generation() {
|
2017-10-03 19:40:23 +08:00
|
|
|
assert_eq!(id_from_content("## `--passes`: add more rustdoc passes"),
|
|
|
|
"--passes-add-more-rustdoc-passes");
|
|
|
|
assert_eq!(id_from_content("## Method-call expressions"),
|
|
|
|
"method-call-expressions");
|
2017-09-09 01:59:04 +08:00
|
|
|
}
|
2017-06-20 11:06:30 +08:00
|
|
|
}
|