Skip to content

Commit

Permalink
Unfork headless_chrome and fix clippy warning
Browse files Browse the repository at this point in the history
Signed-off-by: Hollow Man <[email protected]>
  • Loading branch information
HollowMan6 committed Nov 30, 2023
1 parent 96062de commit 9ea2011
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ include = [
env_logger = "0"
lazy_static = "1"
mdbook = "0"
mdbook-pdf-headless_chrome = "0"
headless_chrome = "1"
regex = "1"
serde = "1"
serde_derive = "1"

[features]
# Enables auto fetching chromium support.
fetch = ["mdbook-pdf-headless_chrome/fetch"]
fetch = ["headless_chrome/fetch"]
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,3 @@ Will appreciate if you can report it to the [issue tracker](https://github.com/H
```bash
RUST_BACKTRACE=full RUST_LOG=trace mdbook build
```
## Credits
- This project relies on [headless_chrome](https://github.com/atroche/rust-headless-chrome). Because the default timeout is not friendly to PDF generation, I use my [forked version](https://github.com/HollowMan6/rust-headless-chrome) to published [mdbook-pdf-headless_chrome](https://crates.io/crates/mdbook-pdf-headless_chrome) for expanding the relevant timeout to 600 seconds, thus enabling the project to be published on [Crates.io](https://crates.io/) as well.
3 changes: 0 additions & 3 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,3 @@ like-wkhtmltopdf = true
```bash
RUST_BACKTRACE=full RUST_LOG=trace mdbook build
```

## 致谢
- 本项目依赖于 [headless_chrome](https://github.com/atroche/rust-headless-chrome)。因其默认超时时间对PDF生成不友好,所以使用了我的[分叉版本](https://github.com/HollowMan6/rust-headless-chrome)发布了[mdbook-pdf-headless_chrome](https://crates.io/crates/mdbook-pdf-headless_chrome),将相关超时时间扩大为600秒,从而使得项目能够发布在[Crates.io](https://crates.io/)
40 changes: 17 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* mdbook-pdf
* A PDF generator for mdBook using headless Chrome.
*
*
* Author: Hollow Man <[email protected]>
* License: GPL-3.0
*
*
* Copyright (C) 2022-2023 Hollow Man (@HollowMan6)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -77,10 +77,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
if base.ends_with(".md") {
base.truncate(base.len() - 3);
}
&base
.replace("/", "-")
.replace("\\", "-")
.to_ascii_lowercase()
&base.replace(['/', '\\'], "-").to_ascii_lowercase()
};
toc_fix.push_str(&(format!(r##"<a href="#{print_page_id}">{print_page_id}</a>"##)));
}
Expand Down Expand Up @@ -114,27 +111,24 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
});
</script>
".to_owned();
"
.to_owned();

contents = contents.replacen(
"</script>",
&(script + &toc_fix),
1,
);
contents = contents.replacen("</script>", &(script + &toc_fix), 1);
if !cfg.static_site_url.is_empty() {
contents = A_LINK
.replace_all(&contents, |caps: &regex::Captures<'_>| {
// Ensure that there is no '\' in the link to ensure the following judgement work
let link = caps[2].replace("\\", "/");
let link = caps[2].replace('\\', "/");
// Don't modify links with schemes like `https`, and no need to modify pages inside the book.
if !link.starts_with("#")
if !link.starts_with('#')
&& !SCHEME_LINK.is_match(&link)
&& (link.starts_with("../") || link.contains("/../"))
{
let mut fixed_link = String::new();

fixed_link.push_str(&cfg.static_site_url);
if !fixed_link.ends_with("/") {
if !fixed_link.ends_with('/') {
fixed_link.push('/');
}
fixed_link.push_str(&link);
Expand Down Expand Up @@ -209,13 +203,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
page.wait_for_element("#content-has-all-loaded-for-mdbook-pdf-generation")?;

// Accept the Google Analytics cookie.
match page.find_element("a.cookieBarConsentButton") {
Ok(_) => {
page.evaluate("document.querySelector('a.cookieBarConsentButton').click()", false)?;
println!("The book you built uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.");
println!("Learn more at: https://policies.google.com/technologies/cookies");
}
Err(_) => (),
if page.find_element("a.cookieBarConsentButton").is_ok() {
page.evaluate(
"document.querySelector('a.cookieBarConsentButton').click()",
false,
)?;
println!("The book you built uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.");
println!("Learn more at: https://policies.google.com/technologies/cookies");
};

// Find the theme and click it to change the theme.
Expand Down Expand Up @@ -257,7 +251,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.to_str()
.unwrap()
.to_owned();
fs::write(generated_pdf_path.clone(), &generated_pdf)?;
fs::write(generated_pdf_path.clone(), generated_pdf)?;
println!("PDF successfully generated at: {}", generated_pdf_path);
break;
}
Expand Down
3 changes: 2 additions & 1 deletion test_doc/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ title = "An Example"
# browser-binary-path = ""
## Assign the static hosting site url so that relative links outside the book can be fixed.
# static_site_url = "https://rust-lang.github.io/mdBook"
## Set the theme to be applied for the generated PDF.
# theme = "ayu"
## Check Chrome Devtools Protocol Docs for the explanation of the following params:
## https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
landscape = false
display-header-footer = true
print-background = true
theme = "ayu"
scale = 0.7
paper-width = 8
paper-height = 10
Expand Down

0 comments on commit 9ea2011

Please sign in to comment.