diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index c7878f9379..ee214dd238 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -210,6 +210,7 @@ The following configuration options are available: - **site-url:** The url where the book will be hosted. This is required to ensure navigation links and script/css imports in the 404 file work correctly, even when accessing urls in subdirectories. Defaults to `/`. +- **print:** A subtable for configuring the printed version (PDF in most case) of HTML. Available configuration options for the `[output.html.fold]` table: @@ -251,6 +252,10 @@ Available configuration options for the `[output.html.search]` table: - **copy-js:** Copy JavaScript files for the search implementation to the output directory. Defaults to `true`. +Available configuration options for the `[output.html.print]` table: + +- **page-break:** Insert page breaks between chapters. Defaults to `true`. + This shows all available HTML output options in the **book.toml**: ```toml @@ -296,6 +301,9 @@ expand = true heading-split-level = 3 copy-js = true +[output.html.print] +page-break = false + [output.html.redirect] "/appendices/bibliography.html" = "https://rustc-dev-guide.rust-lang.org/appendix/bibliography.html" "/other-installation-methods.html" = "../infra/other-installation-methods.html" diff --git a/src/config.rs b/src/config.rs index 1389f13921..455fbc0fbd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -519,6 +519,8 @@ pub struct HtmlConfig { /// The mapping from old pages to new pages/URLs to use when generating /// redirects. pub redirect: HashMap, + /// Print settings. + pub print: Print, } impl Default for HtmlConfig { @@ -543,6 +545,7 @@ impl Default for HtmlConfig { site_url: None, livereload_url: None, redirect: HashMap::new(), + print: Print::default(), } } } @@ -596,6 +599,20 @@ impl Default for Playground { } } +/// Configuration for printing of HTML +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "kebab-case")] +pub struct Print { + /// Insert page breaks between chapters. Default: `true`. + pub page_break: bool, +} + +impl Default for Print { + fn default() -> Print { + Print { page_break: true } + } +} + /// Configuration of the search functionality of the HTML renderer. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "kebab-case")] @@ -706,6 +723,9 @@ mod tests { "index.html" = "overview.html" "nexted/page.md" = "https://rust-lang.org/" + [output.html.print] + page-break = true + [preprocessor.first] [preprocessor.second] @@ -735,6 +755,7 @@ mod tests { copy_js: true, line_numbers: false, }; + let print_should_be = Print { page_break: true }; let html_should_be = HtmlConfig { curly_quotes: true, google_analytics: Some(String::from("123456")), @@ -753,6 +774,7 @@ mod tests { ] .into_iter() .collect(), + print: print_should_be, ..Default::default() }; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 7880817b1e..efe07392ef 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -45,6 +45,12 @@ impl HtmlHandlebars { ctx.html_config.curly_quotes, Some(&path), ); + if !ctx.is_index && ctx.html_config.print.page_break { + // Add page break between chapters + // See https://developer.mozilla.org/en-US/docs/Web/CSS/break-before and https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-before + // Add both two CSS properties because of the compatibility issue + print_content.push_str(r#"
"#); + } print_content.push_str(&fixed_content); // Update the context with data for this file