From e0b247e9d6ec16c66d9eb630c992b4a9a49108d9 Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Mon, 30 Mar 2020 10:38:37 +0200 Subject: [PATCH] Add config option to disable print html, css, and icon --- book-example/src/format/config.md | 11 +++++++++++ src/book/init.rs | 13 +++++++------ src/config.rs | 17 +++++++++++++++++ src/renderer/html_handlebars/hbs_renderer.rs | 19 ++++++++++++------- src/theme/index.hbs | 4 ++++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index ec8d0c28..d9c77e66 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -187,6 +187,9 @@ The following configuration options are available: - **additional-js:** If you need to add some behaviour to your book without removing the current behaviour, you can specify a set of JavaScript files that will be loaded alongside the default one. +- **print:** A subtable for configuration print settings. mdBook by default adds + support for printing out the book as a single page. This is accessed using the + print icon on the top right of the book. - **no-section-label:** mdBook by defaults adds section label in table of contents column. For example, "1.", "2.1". Set this option to true to disable those labels. Defaults to `false`. @@ -217,6 +220,11 @@ The following configuration options are available: [custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site +Available configuration options for the `[output.html.print]` table: + +- **enable:** Enable print support. When `false`, all print support will not be + rendered. Defaults to `true`. + Available configuration options for the `[output.html.fold]` table: - **enable:** Enable section-folding. When off, all folds are open. @@ -282,6 +290,9 @@ site-url = "/example-book/" cname = "myproject.rs" input-404 = "not-found.md" +[output.html.print] +enable = true + [output.html.fold] enable = false level = 0 diff --git a/src/book/init.rs b/src/book/init.rs index 7ae00b65..ea1911db 100644 --- a/src/book/init.rs +++ b/src/book/init.rs @@ -109,10 +109,9 @@ impl BookBuilder { fn copy_across_theme(&self) -> Result<()> { debug!("Copying theme"); - let themedir = self - .config - .html_config() - .and_then(|html| html.theme) + let html_config = self.config.html_config().unwrap_or_default(); + let themedir = html_config + .theme .unwrap_or_else(|| self.config.book.src.join("theme")); let themedir = self.root.join(themedir); @@ -136,8 +135,10 @@ impl BookBuilder { let mut chrome_css = File::create(cssdir.join("chrome.css"))?; chrome_css.write_all(theme::CHROME_CSS)?; - let mut print_css = File::create(cssdir.join("print.css"))?; - print_css.write_all(theme::PRINT_CSS)?; + if html_config.print.enable { + let mut print_css = File::create(cssdir.join("print.css"))?; + print_css.write_all(theme::PRINT_CSS)?; + } let mut variables_css = File::create(cssdir.join("variables.css"))?; variables_css.write_all(theme::VARIABLES_CSS)?; diff --git a/src/config.rs b/src/config.rs index ef6bc687..91d40300 100644 --- a/src/config.rs +++ b/src/config.rs @@ -495,6 +495,8 @@ pub struct HtmlConfig { /// Playground settings. #[serde(alias = "playpen")] pub playground: Playground, + /// Print settings. + pub print: Print, /// Don't render section labels. pub no_section_label: bool, /// Search settings. If `None`, the default will be used. @@ -542,6 +544,7 @@ impl Default for HtmlConfig { additional_js: Vec::new(), fold: Fold::default(), playground: Playground::default(), + print: Print::default(), no_section_label: false, search: None, git_repository_url: None, @@ -566,6 +569,20 @@ impl HtmlConfig { } } +/// Configuration for how to render the print icon, print.html, and print.css. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct Print { + /// Whether print support is enabled. + pub enable: bool, +} + +impl Default for Print { + fn default() -> Self { + Self { enable: true } + } +} + /// Configuration for how to fold chapters of sidebar. #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case")] diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 4992730e..51cb9c79 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -194,7 +194,9 @@ impl HtmlHandlebars { write_file(destination, "book.js", &theme.js)?; 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)?; + if html_config.print.enable { + write_file(destination, "css/print.css", &theme.print_css)?; + } write_file(destination, "css/variables.css", &theme.variables_css)?; if let Some(contents) = &theme.favicon_png { write_file(destination, "favicon.png", &contents)?; @@ -516,14 +518,16 @@ impl Renderer for HtmlHandlebars { } // Render the handlebars template with the data - debug!("Render template"); - let rendered = handlebars.render("index", &data)?; + if html_config.print.enable { + debug!("Render template"); + let rendered = handlebars.render("index", &data)?; - let rendered = - self.post_process(rendered, &html_config.playground, ctx.config.rust.edition); + let rendered = + self.post_process(rendered, &html_config.playground, ctx.config.rust.edition); - utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?; - debug!("Creating print.html ✓"); + utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?; + debug!("Creating print.html ✓"); + } debug!("Copy static files"); self.copy_static_files(&destination, &theme, &html_config) @@ -644,6 +648,7 @@ fn make_data( data.insert("playground_copyable".to_owned(), json!(true)); } + data.insert("print_enable".to_owned(), json!(!html_config.print.enable)); data.insert("fold_enable".to_owned(), json!((html_config.fold.enable))); data.insert("fold_level".to_owned(), json!((html_config.fold.level))); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 7ad4cbf7..e9e6cff8 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -29,7 +29,9 @@ + {{#if print_enable}} + {{/if}} @@ -136,9 +138,11 @@

{{ book_title }}

+ {{#if print_enable}} + {{/if}} {{#if git_repository_url}}