Skip to content

Commit d1f5ecc

Browse files
authored
Merge pull request #1169 from rossmacarthur/ft/no-print
Add config option to disable print html, css, and icon
2 parents db8a282 + e0b247e commit d1f5ecc

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

book-example/src/format/config.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ The following configuration options are available:
187187
- **additional-js:** If you need to add some behaviour to your book without
188188
removing the current behaviour, you can specify a set of JavaScript files that
189189
will be loaded alongside the default one.
190+
- **print:** A subtable for configuration print settings. mdBook by default adds
191+
support for printing out the book as a single page. This is accessed using the
192+
print icon on the top right of the book.
190193
- **no-section-label:** mdBook by defaults adds section label in table of
191194
contents column. For example, "1.", "2.1". Set this option to true to disable
192195
those labels. Defaults to `false`.
@@ -217,6 +220,11 @@ The following configuration options are available:
217220

218221
[custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
219222

223+
Available configuration options for the `[output.html.print]` table:
224+
225+
- **enable:** Enable print support. When `false`, all print support will not be
226+
rendered. Defaults to `true`.
227+
220228
Available configuration options for the `[output.html.fold]` table:
221229

222230
- **enable:** Enable section-folding. When off, all folds are open.
@@ -282,6 +290,9 @@ site-url = "/example-book/"
282290
cname = "myproject.rs"
283291
input-404 = "not-found.md"
284292

293+
[output.html.print]
294+
enable = true
295+
285296
[output.html.fold]
286297
enable = false
287298
level = 0

src/book/init.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ impl BookBuilder {
109109
fn copy_across_theme(&self) -> Result<()> {
110110
debug!("Copying theme");
111111

112-
let themedir = self
113-
.config
114-
.html_config()
115-
.and_then(|html| html.theme)
112+
let html_config = self.config.html_config().unwrap_or_default();
113+
let themedir = html_config
114+
.theme
116115
.unwrap_or_else(|| self.config.book.src.join("theme"));
117116
let themedir = self.root.join(themedir);
118117

@@ -136,8 +135,10 @@ impl BookBuilder {
136135
let mut chrome_css = File::create(cssdir.join("chrome.css"))?;
137136
chrome_css.write_all(theme::CHROME_CSS)?;
138137

139-
let mut print_css = File::create(cssdir.join("print.css"))?;
140-
print_css.write_all(theme::PRINT_CSS)?;
138+
if html_config.print.enable {
139+
let mut print_css = File::create(cssdir.join("print.css"))?;
140+
print_css.write_all(theme::PRINT_CSS)?;
141+
}
141142

142143
let mut variables_css = File::create(cssdir.join("variables.css"))?;
143144
variables_css.write_all(theme::VARIABLES_CSS)?;

src/config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ pub struct HtmlConfig {
495495
/// Playground settings.
496496
#[serde(alias = "playpen")]
497497
pub playground: Playground,
498+
/// Print settings.
499+
pub print: Print,
498500
/// Don't render section labels.
499501
pub no_section_label: bool,
500502
/// Search settings. If `None`, the default will be used.
@@ -542,6 +544,7 @@ impl Default for HtmlConfig {
542544
additional_js: Vec::new(),
543545
fold: Fold::default(),
544546
playground: Playground::default(),
547+
print: Print::default(),
545548
no_section_label: false,
546549
search: None,
547550
git_repository_url: None,
@@ -566,6 +569,20 @@ impl HtmlConfig {
566569
}
567570
}
568571

572+
/// Configuration for how to render the print icon, print.html, and print.css.
573+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
574+
#[serde(rename_all = "kebab-case")]
575+
pub struct Print {
576+
/// Whether print support is enabled.
577+
pub enable: bool,
578+
}
579+
580+
impl Default for Print {
581+
fn default() -> Self {
582+
Self { enable: true }
583+
}
584+
}
585+
569586
/// Configuration for how to fold chapters of sidebar.
570587
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
571588
#[serde(default, rename_all = "kebab-case")]

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ impl HtmlHandlebars {
194194
write_file(destination, "book.js", &theme.js)?;
195195
write_file(destination, "css/general.css", &theme.general_css)?;
196196
write_file(destination, "css/chrome.css", &theme.chrome_css)?;
197-
write_file(destination, "css/print.css", &theme.print_css)?;
197+
if html_config.print.enable {
198+
write_file(destination, "css/print.css", &theme.print_css)?;
199+
}
198200
write_file(destination, "css/variables.css", &theme.variables_css)?;
199201
if let Some(contents) = &theme.favicon_png {
200202
write_file(destination, "favicon.png", &contents)?;
@@ -516,14 +518,16 @@ impl Renderer for HtmlHandlebars {
516518
}
517519

518520
// Render the handlebars template with the data
519-
debug!("Render template");
520-
let rendered = handlebars.render("index", &data)?;
521+
if html_config.print.enable {
522+
debug!("Render template");
523+
let rendered = handlebars.render("index", &data)?;
521524

522-
let rendered =
523-
self.post_process(rendered, &html_config.playground, ctx.config.rust.edition);
525+
let rendered =
526+
self.post_process(rendered, &html_config.playground, ctx.config.rust.edition);
524527

525-
utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?;
526-
debug!("Creating print.html ✓");
528+
utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?;
529+
debug!("Creating print.html ✓");
530+
}
527531

528532
debug!("Copy static files");
529533
self.copy_static_files(&destination, &theme, &html_config)
@@ -644,6 +648,7 @@ fn make_data(
644648
data.insert("playground_copyable".to_owned(), json!(true));
645649
}
646650

651+
data.insert("print_enable".to_owned(), json!(!html_config.print.enable));
647652
data.insert("fold_enable".to_owned(), json!((html_config.fold.enable)));
648653
data.insert("fold_level".to_owned(), json!((html_config.fold.level)));
649654

src/theme/index.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
<link rel="stylesheet" href="{{ path_to_root }}css/variables.css">
3030
<link rel="stylesheet" href="{{ path_to_root }}css/general.css">
3131
<link rel="stylesheet" href="{{ path_to_root }}css/chrome.css">
32+
{{#if print_enable}}
3233
<link rel="stylesheet" href="{{ path_to_root }}css/print.css" media="print">
34+
{{/if}}
3335

3436
<!-- Fonts -->
3537
<link rel="stylesheet" href="{{ path_to_root }}FontAwesome/css/font-awesome.css">
@@ -136,9 +138,11 @@
136138
<h1 class="menu-title">{{ book_title }}</h1>
137139

138140
<div class="right-buttons">
141+
{{#if print_enable}}
139142
<a href="{{ path_to_root }}print.html" title="Print this book" aria-label="Print this book">
140143
<i id="print-button" class="fa fa-print"></i>
141144
</a>
145+
{{/if}}
142146
{{#if git_repository_url}}
143147
<a href="{{git_repository_url}}" title="Git repository" aria-label="Git repository">
144148
<i id="git-repository-button" class="fa {{git_repository_icon}}"></i>

0 commit comments

Comments
 (0)