2019-05-26 02:50:41 +08:00
|
|
|
use crate::book::{Book, BookItem};
|
|
|
|
use crate::config::{Config, HtmlConfig, Playpen};
|
|
|
|
use crate::errors::*;
|
|
|
|
use crate::renderer::html_handlebars::helpers;
|
|
|
|
use crate::renderer::{RenderContext, Renderer};
|
|
|
|
use crate::theme::{self, playpen_editor, Theme};
|
|
|
|
use crate::utils;
|
2015-08-04 21:15:36 +08:00
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
2017-03-24 01:54:26 +08:00
|
|
|
use std::collections::HashMap;
|
2018-08-03 01:43:40 +08:00
|
|
|
use std::fs;
|
2018-03-07 21:02:06 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2016-08-01 20:06:08 +08:00
|
|
|
use handlebars::Handlebars;
|
2018-03-07 21:02:06 +08:00
|
|
|
use regex::{Captures, Regex};
|
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
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
fn render_item(
|
|
|
|
&self,
|
2018-03-07 21:02:06 +08:00
|
|
|
item: &BookItem,
|
2019-05-07 04:50:34 +08:00
|
|
|
mut ctx: RenderItemContext<'_>,
|
2018-01-07 22:10:48 +08:00
|
|
|
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
|
2018-12-04 07:10:09 +08:00
|
|
|
if let BookItem::Chapter(ref ch) = *item {
|
|
|
|
let content = ch.content.clone();
|
|
|
|
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
|
2019-01-16 02:19:10 +08:00
|
|
|
|
|
|
|
let string_path = ch.path.parent().unwrap().display().to_string();
|
|
|
|
|
2019-05-05 22:57:43 +08:00
|
|
|
let fixed_content = utils::render_markdown_with_base(
|
|
|
|
&ch.content,
|
|
|
|
ctx.html_config.curly_quotes,
|
|
|
|
&string_path,
|
|
|
|
);
|
2019-01-16 02:19:10 +08:00
|
|
|
print_content.push_str(&fixed_content);
|
2018-12-04 07:10:09 +08:00
|
|
|
|
|
|
|
// Update the context with data for this file
|
|
|
|
let path = ch
|
|
|
|
.path
|
|
|
|
.to_str()
|
|
|
|
.chain_err(|| "Could not convert path to str")?;
|
|
|
|
let filepath = Path::new(&ch.path).with_extension("html");
|
|
|
|
|
|
|
|
// "print.html" is used for the print page.
|
|
|
|
if ch.path == Path::new("print.md") {
|
|
|
|
bail!(ErrorKind::ReservedFilenameError(ch.path.clone()));
|
|
|
|
};
|
|
|
|
|
|
|
|
// Non-lexical lifetimes needed :'(
|
|
|
|
let 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)),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Render the handlebars template with the data
|
|
|
|
debug!("Render template");
|
|
|
|
let rendered = ctx.handlebars.render("index", &ctx.data)?;
|
|
|
|
|
|
|
|
let rendered = self.post_process(rendered, &ctx.html_config.playpen);
|
|
|
|
|
|
|
|
// Write to file
|
|
|
|
debug!("Creating {}", filepath.display());
|
|
|
|
utils::fs::write_file(&ctx.destination, &filepath, rendered.as_bytes())?;
|
|
|
|
|
|
|
|
if ctx.is_index {
|
2019-05-08 06:27:48 +08:00
|
|
|
ctx.data.insert("path".to_owned(), json!("index.md"));
|
2018-12-04 07:10:09 +08:00
|
|
|
ctx.data.insert("path_to_root".to_owned(), json!(""));
|
|
|
|
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
|
|
|
|
let rendered_index = self.post_process(rendered_index, &ctx.html_config.playpen);
|
|
|
|
debug!("Creating index.html from {}", path);
|
|
|
|
utils::fs::write_file(&ctx.destination, "index.html", rendered_index.as_bytes())?;
|
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
|
|
|
|
2018-12-04 07:10:09 +08:00
|
|
|
#[cfg_attr(feature = "cargo-clippy", allow(clippy::let_and_return))]
|
2018-07-11 21:33:44 +08:00
|
|
|
fn post_process(&self, rendered: String, playpen_config: &Playpen) -> String {
|
|
|
|
let rendered = build_header_links(&rendered);
|
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
|
|
|
|
}
|
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
fn copy_static_files(
|
|
|
|
&self,
|
|
|
|
destination: &Path,
|
|
|
|
theme: &Theme,
|
|
|
|
html_config: &HtmlConfig,
|
|
|
|
) -> Result<()> {
|
2019-05-26 02:50:41 +08:00
|
|
|
use crate::utils::fs::write_file;
|
2018-03-07 21:02:06 +08:00
|
|
|
|
2018-07-24 01:45:01 +08:00
|
|
|
write_file(
|
|
|
|
destination,
|
|
|
|
".nojekyll",
|
|
|
|
b"This file makes sure that Github Pages doesn't process mdBook's output.",
|
|
|
|
)?;
|
2018-05-16 01:23:08 +08:00
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(destination, "book.js", &theme.js)?;
|
2018-07-26 04:51:09 +08:00
|
|
|
write_file(destination, "css/general.css", &theme.general_css)?;
|
|
|
|
write_file(destination, "css/chrome.css", &theme.chrome_css)?;
|
|
|
|
write_file(destination, "css/print.css", &theme.print_css)?;
|
|
|
|
write_file(destination, "css/variables.css", &theme.variables_css)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(destination, "favicon.png", &theme.favicon)?;
|
|
|
|
write_file(destination, "highlight.css", &theme.highlight_css)?;
|
|
|
|
write_file(destination, "tomorrow-night.css", &theme.tomorrow_night_css)?;
|
|
|
|
write_file(destination, "ayu-highlight.css", &theme.ayu_highlight_css)?;
|
|
|
|
write_file(destination, "highlight.js", &theme.highlight_js)?;
|
|
|
|
write_file(destination, "clipboard.min.js", &theme.clipboard_js)?;
|
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/css/font-awesome.css",
|
2018-01-07 22:10:48 +08:00
|
|
|
theme::FONT_AWESOME,
|
|
|
|
)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/fonts/fontawesome-webfont.eot",
|
2018-01-07 22:10:48 +08:00
|
|
|
theme::FONT_AWESOME_EOT,
|
|
|
|
)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/fonts/fontawesome-webfont.svg",
|
2018-01-07 22:10:48 +08:00
|
|
|
theme::FONT_AWESOME_SVG,
|
|
|
|
)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/fonts/fontawesome-webfont.ttf",
|
2018-01-07 22:10:48 +08:00
|
|
|
theme::FONT_AWESOME_TTF,
|
|
|
|
)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/fonts/fontawesome-webfont.woff",
|
2018-01-07 22:10:48 +08:00
|
|
|
theme::FONT_AWESOME_WOFF,
|
|
|
|
)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/fonts/fontawesome-webfont.woff2",
|
2018-01-07 22:10:48 +08:00
|
|
|
theme::FONT_AWESOME_WOFF2,
|
|
|
|
)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(
|
2018-01-07 22:10:48 +08:00
|
|
|
destination,
|
2018-05-16 01:18:02 +08:00
|
|
|
"FontAwesome/fonts/FontAwesome.ttf",
|
2018-01-07 22:10:48 +08:00
|
|
|
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
|
2018-03-07 21:02:06 +08:00
|
|
|
if playpen_config.editable && playpen_config.copy_js {
|
2017-06-29 12:35:20 +08:00
|
|
|
// Load the editor
|
2018-03-07 21:02:06 +08:00
|
|
|
write_file(destination, "editor.js", playpen_editor::JS)?;
|
|
|
|
write_file(destination, "ace.js", playpen_editor::ACE_JS)?;
|
|
|
|
write_file(destination, "mode-rust.js", playpen_editor::MODE_RUST_JS)?;
|
|
|
|
write_file(destination, "theme-dawn.js", playpen_editor::THEME_DAWN_JS)?;
|
2018-05-17 01:08:23 +08:00
|
|
|
write_file(
|
|
|
|
destination,
|
2018-01-07 22:10:48 +08:00
|
|
|
"theme-tomorrow_night.js",
|
2018-03-07 21:02:06 +08:00
|
|
|
playpen_editor::THEME_TOMORROW_NIGHT_JS,
|
2018-01-07 22:10:48 +08:00
|
|
|
)?;
|
2017-06-29 12:35:20 +08:00
|
|
|
}
|
|
|
|
|
2017-06-15 18:03:10 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Update the context with data for this file
|
2018-05-17 01:08: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));
|
2018-05-17 01:08: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
|
|
|
}
|
|
|
|
|
2018-01-08 00:31:46 +08:00
|
|
|
fn register_hbs_helpers(&self, handlebars: &mut Handlebars, html_config: &HtmlConfig) {
|
2018-05-17 01:08:23 +08:00
|
|
|
handlebars.register_helper(
|
|
|
|
"toc",
|
|
|
|
Box::new(helpers::toc::RenderToc {
|
|
|
|
no_section_label: html_config.no_section_label,
|
|
|
|
}),
|
|
|
|
);
|
2017-06-15 18:03:10 +08:00
|
|
|
handlebars.register_helper("previous", Box::new(helpers::navigation::previous));
|
|
|
|
handlebars.register_helper("next", Box::new(helpers::navigation::next));
|
2018-10-13 21:44:10 +08:00
|
|
|
handlebars.register_helper("theme_option", Box::new(helpers::theme::theme_option));
|
2017-06-15 18:03:10 +08:00
|
|
|
}
|
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.
|
2018-05-17 01:08:23 +08:00
|
|
|
fn copy_additional_css_and_js(
|
|
|
|
&self,
|
|
|
|
html: &HtmlConfig,
|
|
|
|
root: &Path,
|
|
|
|
destination: &Path,
|
|
|
|
) -> Result<()> {
|
2018-01-07 22:10:48 +08:00
|
|
|
let custom_files = html.additional_css.iter().chain(html.additional_js.iter());
|
2017-09-30 21:36:03 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
debug!("Copying additional CSS and JS");
|
2017-06-17 21:15:54 +08:00
|
|
|
|
|
|
|
for custom_file in custom_files {
|
2018-01-30 12:29:09 +08:00
|
|
|
let input_location = root.join(custom_file);
|
2018-01-07 22:10:48 +08:00
|
|
|
let output_location = destination.join(custom_file);
|
2018-01-26 14:38:53 +08:00
|
|
|
if let Some(parent) = output_location.parent() {
|
|
|
|
fs::create_dir_all(parent)
|
|
|
|
.chain_err(|| format!("Unable to create {}", parent.display()))?;
|
|
|
|
}
|
2018-01-07 22:10:48 +08:00
|
|
|
debug!(
|
|
|
|
"Copying {} -> {}",
|
2018-01-30 12:29:09 +08:00
|
|
|
input_location.display(),
|
2018-01-07 22:10:48 +08:00
|
|
|
output_location.display()
|
|
|
|
);
|
|
|
|
|
2018-01-30 12:29:09 +08:00
|
|
|
fs::copy(&input_location, &output_location).chain_err(|| {
|
2018-01-07 22:10:48 +08:00
|
|
|
format!(
|
|
|
|
"Unable to copy {} to {}",
|
2018-01-30 12:29:09 +08:00
|
|
|
input_location.display(),
|
2018-01-07 22:10:48 +08:00
|
|
|
output_location.display()
|
|
|
|
)
|
|
|
|
})?;
|
2017-06-15 18:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-08-04 22:52:10 +08:00
|
|
|
}
|
|
|
|
|
2018-05-17 01:06:55 +08:00
|
|
|
// TODO(mattico): Remove some time after the 0.1.8 release
|
|
|
|
fn maybe_wrong_theme_dir(dir: &Path) -> Result<bool> {
|
|
|
|
fn entry_is_maybe_book_file(entry: fs::DirEntry) -> Result<bool> {
|
2018-05-17 01:08:23 +08:00
|
|
|
Ok(entry.file_type()?.is_file()
|
|
|
|
&& entry.path().extension().map_or(false, |ext| ext == "md"))
|
2018-05-17 01:06:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if dir.is_dir() {
|
|
|
|
for entry in fs::read_dir(dir)? {
|
|
|
|
if entry_is_maybe_book_file(entry?).unwrap_or(false) {
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
impl Renderer for HtmlHandlebars {
|
2018-01-07 22:10:48 +08:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"html"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render(&self, ctx: &RenderContext) -> Result<()> {
|
|
|
|
let html_config = ctx.config.html_config().unwrap_or_default();
|
|
|
|
let src_dir = ctx.root.join(&ctx.config.book.src);
|
|
|
|
let destination = &ctx.destination;
|
|
|
|
let book = &ctx.book;
|
2017-09-30 21:36:03 +08:00
|
|
|
|
2018-01-23 01:28:37 +08:00
|
|
|
trace!("render");
|
2015-08-04 21:15:36 +08:00
|
|
|
let mut handlebars = Handlebars::new();
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
let theme_dir = match html_config.theme {
|
2017-11-18 23:21:59 +08:00
|
|
|
Some(ref theme) => theme.to_path_buf(),
|
2018-05-15 03:52:29 +08:00
|
|
|
None => ctx.root.join("theme"),
|
2017-09-30 21:36:03 +08:00
|
|
|
};
|
|
|
|
|
2018-05-17 01:08:23 +08:00
|
|
|
if html_config.theme.is_none()
|
|
|
|
&& maybe_wrong_theme_dir(&src_dir.join("theme")).unwrap_or(false)
|
|
|
|
{
|
|
|
|
warn!(
|
|
|
|
"Previous versions of mdBook erroneously accepted `./src/theme` as an automatic \
|
|
|
|
theme directory"
|
|
|
|
);
|
2018-05-17 01:06:55 +08:00
|
|
|
warn!("Please move your theme files to `./theme` for them to continue being used");
|
|
|
|
}
|
|
|
|
|
2017-09-30 21:36:03 +08:00
|
|
|
let theme = theme::Theme::new(theme_dir);
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Register the index handlebars template");
|
|
|
|
handlebars.register_template_string("index", String::from_utf8(theme.index.clone())?)?;
|
2017-09-28 07:06:30 +08:00
|
|
|
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Register the header handlebars template");
|
|
|
|
handlebars.register_partial("header", String::from_utf8(theme.header.clone())?)?;
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Register handlebars helpers");
|
2018-01-08 00:31:46 +08:00
|
|
|
self.register_hbs_helpers(&mut handlebars, &html_config);
|
2015-08-04 21:15:36 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let mut data = make_data(&ctx.root, &book, &ctx.config, &html_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 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
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let mut is_index = true;
|
|
|
|
for item in book.iter() {
|
2017-06-20 08:40:01 +08:00
|
|
|
let ctx = RenderItemContext {
|
|
|
|
handlebars: &handlebars,
|
|
|
|
destination: destination.to_path_buf(),
|
|
|
|
data: data.clone(),
|
2018-12-04 07:10:09 +08:00
|
|
|
is_index,
|
2017-09-30 21:36:03 +08:00
|
|
|
html_config: html_config.clone(),
|
2017-06-20 08:40:01 +08:00
|
|
|
};
|
2018-05-17 01:08:23 +08:00
|
|
|
self.render_item(item, ctx, &mut print_content)?;
|
2018-03-07 21:02:06 +08:00
|
|
|
is_index = false;
|
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);
|
2018-01-07 22:10:48 +08:00
|
|
|
if let Some(ref title) = ctx.config.book.title {
|
2017-09-30 21:36:03 +08:00
|
|
|
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
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Render template");
|
2017-05-19 19:04:37 +08:00
|
|
|
let rendered = handlebars.render("index", &data)?;
|
2017-02-28 08:41:06 +08:00
|
|
|
|
2018-07-11 21:33:44 +08:00
|
|
|
let rendered = self.post_process(rendered, &html_config.playpen);
|
2017-09-02 07:54:57 +08:00
|
|
|
|
2018-08-03 01:43:40 +08:00
|
|
|
utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?;
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Creating print.html ✓");
|
2015-09-05 23:26:17 +08:00
|
|
|
|
2018-01-23 01:28:37 +08:00
|
|
|
debug!("Copy static files");
|
2018-01-07 22:10:48 +08:00
|
|
|
self.copy_static_files(&destination, &theme, &html_config)
|
2017-12-14 17:03:17 +08:00
|
|
|
.chain_err(|| "Unable to copy across static files")?;
|
2018-01-30 12:29:09 +08:00
|
|
|
self.copy_additional_css_and_js(&html_config, &ctx.root, &destination)
|
2017-12-14 17:03:17 +08:00
|
|
|
.chain_err(|| "Unable to copy across additional CSS and JS")?;
|
2017-05-20 19:56:01 +08:00
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
// Render search index
|
2018-07-26 01:20:48 +08:00
|
|
|
#[cfg(feature = "search")]
|
|
|
|
{
|
2018-07-25 05:15:20 +08:00
|
|
|
let search = html_config.search.unwrap_or_default();
|
|
|
|
if search.enable {
|
|
|
|
super::search::create_files(&search, &destination, &book)?;
|
|
|
|
}
|
2018-06-14 02:11:25 +08:00
|
|
|
}
|
2018-03-07 21:02:06 +08:00
|
|
|
|
2015-09-18 01:45:06 +08:00
|
|
|
// Copy all remaining files
|
2018-01-07 22:10:48 +08:00
|
|
|
utils::fs::copy_files_except_ext(&src_dir, &destination, true, &["md"])?;
|
2017-05-19 05:52:38 +08:00
|
|
|
|
2015-08-04 21:15:36 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 01:08:23 +08:00
|
|
|
fn make_data(
|
|
|
|
root: &Path,
|
|
|
|
book: &Book,
|
|
|
|
config: &Config,
|
|
|
|
html_config: &HtmlConfig,
|
|
|
|
) -> Result<serde_json::Map<String, serde_json::Value>> {
|
2018-01-23 01:28:37 +08:00
|
|
|
trace!("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"));
|
2018-05-17 01:08:23 +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"));
|
2018-01-07 22:10:48 +08:00
|
|
|
if let Some(ref livereload) = html_config.livereload_url {
|
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
|
|
|
|
2018-10-13 02:57:59 +08:00
|
|
|
let default_theme = match html_config.default_theme {
|
|
|
|
Some(ref theme) => theme,
|
|
|
|
None => "light",
|
|
|
|
};
|
|
|
|
data.insert("default_theme".to_owned(), json!(default_theme));
|
|
|
|
|
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 {
|
2018-01-07 22:10:48 +08:00
|
|
|
match style.strip_prefix(root) {
|
2018-05-17 01:08:23 +08:00
|
|
|
Ok(p) => css.push(p.to_str().expect("Could not convert to str")),
|
|
|
|
Err(_) => css.push(style.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 {
|
2018-01-07 22:10:48 +08:00
|
|
|
match script.strip_prefix(root) {
|
2017-06-07 02:34:57 +08:00
|
|
|
Ok(p) => js.push(p.to_str().expect("Could not convert to str")),
|
2018-09-20 02:36:32 +08:00
|
|
|
Err(_) => js.push(script.to_str().expect("Could not convert to str")),
|
2017-06-07 02:34:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
data.insert("additional_js".to_owned(), json!(js));
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
if html.playpen.editable && html.playpen.copy_js {
|
|
|
|
data.insert("playpen_js".to_owned(), json!(true));
|
2017-06-29 12:35:20 +08:00
|
|
|
}
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let search = html_config.search.clone();
|
|
|
|
if cfg!(feature = "search") {
|
2018-06-14 02:11:25 +08:00
|
|
|
let search = search.unwrap_or_default();
|
|
|
|
data.insert("search_enabled".to_owned(), json!(search.enable));
|
2018-07-24 01:45:01 +08:00
|
|
|
data.insert(
|
|
|
|
"search_js".to_owned(),
|
|
|
|
json!(search.enable && search.copy_js),
|
|
|
|
);
|
2018-03-07 21:02:06 +08:00
|
|
|
} else if search.is_some() {
|
|
|
|
warn!("mdBook compiled without search support, ignoring `output.html.search` table");
|
2018-05-17 01:08:23 +08:00
|
|
|
warn!(
|
|
|
|
"please reinstall with `cargo install mdbook --force --features search`to use the \
|
|
|
|
search feature"
|
|
|
|
)
|
2018-03-07 21:02:06 +08:00
|
|
|
}
|
2018-05-17 01:08:23 +08:00
|
|
|
|
2018-10-13 19:17:33 +08:00
|
|
|
if let Some(ref git_repository_url) = html_config.git_repository_url {
|
2018-10-16 02:48:54 +08:00
|
|
|
data.insert("git_repository_url".to_owned(), json!(git_repository_url));
|
2018-10-13 19:17:33 +08:00
|
|
|
}
|
2018-10-16 02:48:54 +08:00
|
|
|
let git_repository_icon = match html_config.git_repository_icon {
|
|
|
|
Some(ref git_repository_icon) => git_repository_icon,
|
|
|
|
None => "fa-github",
|
|
|
|
};
|
|
|
|
data.insert("git_repository_icon".to_owned(), json!(git_repository_icon));
|
2018-10-13 19:17:33 +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 {
|
2017-11-18 20:01:50 +08:00
|
|
|
BookItem::Chapter(ref ch) => {
|
|
|
|
if let Some(ref section) = ch.number {
|
|
|
|
chapter.insert("section".to_owned(), json!(section.to_string()));
|
|
|
|
}
|
|
|
|
|
2017-02-16 11:34:37 +08:00
|
|
|
chapter.insert("name".to_owned(), json!(ch.name));
|
2018-08-03 09:22:49 +08:00
|
|
|
let path = ch
|
|
|
|
.path
|
2018-01-23 01:28:37 +08:00
|
|
|
.to_str()
|
|
|
|
.chain_err(|| "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
|
|
|
}
|
2017-11-18 20:01:50 +08:00
|
|
|
BookItem::Separator => {
|
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.
|
2018-07-11 21:33:44 +08:00
|
|
|
fn build_header_links(html: &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
|
|
|
|
2018-05-17 01:08:23 +08:00
|
|
|
regex
|
2019-05-07 04:50:34 +08:00
|
|
|
.replace_all(html, |caps: &Captures<'_>| {
|
2018-05-17 01:08:23 +08:00
|
|
|
let level = caps[1]
|
|
|
|
.parse()
|
|
|
|
.expect("Regex should ensure we only ever get numbers here");
|
2017-05-19 19:04:37 +08:00
|
|
|
|
2018-07-11 21:33:44 +08:00
|
|
|
wrap_header_with_link(level, &caps[2], &mut id_counter)
|
2019-05-05 22:57:43 +08:00
|
|
|
})
|
|
|
|
.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).
|
2018-05-17 01:08:23 +08:00
|
|
|
fn wrap_header_with_link(
|
|
|
|
level: usize,
|
|
|
|
content: &str,
|
2018-07-24 01:45:01 +08:00
|
|
|
id_counter: &mut HashMap<String, usize>,
|
2018-05-17 01:08:23 +08:00
|
|
|
) -> String {
|
2018-03-07 21:02:06 +08:00
|
|
|
let raw_id = utils::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!(
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"<a class="header" href="#{id}" id="{id}"><h{level}>{text}</h{level}></a>"##,
|
2017-06-20 11:06:30 +08:00
|
|
|
level = level,
|
|
|
|
id = id,
|
2018-07-11 21:33:44 +08:00
|
|
|
text = content
|
2017-06-20 11:06:30 +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();
|
2018-05-17 01:08:23 +08:00
|
|
|
regex
|
2019-05-07 04:50:34 +08:00
|
|
|
.replace_all(html, |caps: &Captures<'_>| {
|
2018-05-17 01:08:23 +08:00
|
|
|
let before = &caps[1];
|
|
|
|
let classes = &caps[2].replace(",", " ");
|
|
|
|
let after = &caps[3];
|
|
|
|
|
|
|
|
format!(
|
2019-05-10 02:18:28 +08:00
|
|
|
r#"<code{before}class="{classes}"{after}>"#,
|
2017-10-03 19:40:23 +08:00
|
|
|
before = before,
|
|
|
|
classes = classes,
|
2018-05-17 01:08:23 +08:00
|
|
|
after = after
|
|
|
|
)
|
2019-05-05 22:57:43 +08:00
|
|
|
})
|
|
|
|
.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();
|
2018-05-17 01:08:23 +08:00
|
|
|
regex
|
2019-05-07 04:50:34 +08:00
|
|
|
.replace_all(html, |caps: &Captures<'_>| {
|
2018-05-17 01:08:23 +08:00
|
|
|
let text = &caps[1];
|
|
|
|
let classes = &caps[2];
|
|
|
|
let code = &caps[3];
|
|
|
|
|
2018-07-27 05:49:40 +08:00
|
|
|
if (classes.contains("language-rust")
|
|
|
|
&& !classes.contains("ignore")
|
|
|
|
&& !classes.contains("noplaypen"))
|
2018-05-17 01:08:23 +08:00
|
|
|
|| classes.contains("mdbook-runnable")
|
2017-10-03 19:40:23 +08:00
|
|
|
{
|
2018-05-17 01:08:23 +08:00
|
|
|
// wrap the contents in an external pre block
|
|
|
|
if playpen_config.editable && classes.contains("editable")
|
2018-07-24 01:45:01 +08:00
|
|
|
|| text.contains("fn main")
|
|
|
|
|| text.contains("quick_main!")
|
2018-05-17 01:08:23 +08:00
|
|
|
{
|
|
|
|
format!("<pre class=\"playpen\">{}</pre>", text)
|
|
|
|
} else {
|
|
|
|
// 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-05-19 19:04:37 +08:00
|
|
|
} else {
|
2018-05-17 01:08:23 +08:00
|
|
|
// not language-rust, so no-op
|
|
|
|
text.to_owned()
|
2017-03-07 02:27:25 +08:00
|
|
|
}
|
2019-05-05 22:57:43 +08:00
|
|
|
})
|
|
|
|
.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();
|
2019-05-07 02:20:58 +08:00
|
|
|
let header = trimline.chars().all(char::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,
|
|
|
|
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
|
|
|
|
|
|
|
#[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>",
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"blah blah <a class="header" href="#foo" id="foo"><h1>Foo</h1></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h1>Foo</h1>",
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"<a class="header" href="#foo" id="foo"><h1>Foo</h1></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h3>Foo^bar</h3>",
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"<a class="header" href="#foobar" id="foobar"><h3>Foo^bar</h3></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h4></h4>",
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"<a class="header" href="#" id=""><h4></h4></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h4><em>Hï</em></h4>",
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"<a class="header" href="#hï" id="hï"><h4><em>Hï</em></h4></a>"##,
|
2017-09-02 07:54:57 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"<h1>Foo</h1><h3>Foo</h3>",
|
2018-07-11 21:33:44 +08:00
|
|
|
r##"<a class="header" href="#foo" id="foo"><h1>Foo</h1></a><a class="header" href="#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 {
|
2018-07-11 21:33:44 +08:00
|
|
|
let got = build_header_links(&src);
|
2017-06-20 10:54:32 +08:00
|
|
|
assert_eq!(got, should_be);
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 11:06:30 +08:00
|
|
|
}
|