Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions book-example/src/format/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ pub struct HtmlConfig {
/// The mapping from old pages to new pages/URLs to use when generating
/// redirects.
pub redirect: HashMap<String, String>,
/// Print settings.
pub print: Print,
}

impl Default for HtmlConfig {
Expand All @@ -543,6 +545,7 @@ impl Default for HtmlConfig {
site_url: None,
livereload_url: None,
redirect: HashMap::new(),
print: Print::default(),
}
}
}
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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")),
Expand All @@ -753,6 +774,7 @@ mod tests {
]
.into_iter()
.collect(),
print: print_should_be,
..Default::default()
};

Expand Down
6 changes: 6 additions & 0 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"<div id="chapter_begin" style="break-before: page; page-break-before: always;"></div>"#);
}
print_content.push_str(&fixed_content);

// Update the context with data for this file
Expand Down