Skip to content

Commit

Permalink
v0.1.4: add support for different theme than html
Browse files Browse the repository at this point in the history
Signed-off-by: Hollow Man <[email protected]>
  • Loading branch information
HollowMan6 committed Dec 29, 2022
1 parent 433b5cf commit 1716a5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
run: |
cargo build --release --target ${{ matrix.target }}
- name: Upload Binary File to release
if: startsWith(github.ref, 'refs/tags/v')
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -80,6 +81,7 @@ jobs:
run: |
cargo build --release --target ${{ matrix.target }}
- name: Upload Binary File to release
if: startsWith(github.ref, 'refs/tags/v')
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -120,6 +122,7 @@ jobs:
-w /build --platform arm64 rust bash -c \
"rustup target add ${{ matrix.target }} && cargo build --release --target ${{ matrix.target }}"
- name: Upload Binary File to release
if: startsWith(github.ref, 'refs/tags/v')
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "GPL-3.0"
name = "mdbook-pdf"
readme = "README.md"
repository = "https://github.com/HollowMan6/mdbook-pdf"
version = "0.1.3"
version = "0.1.4"
include = [
"**/*.rs",
"Cargo.toml",
Expand All @@ -23,6 +23,3 @@ mdbook-pdf-headless_chrome = "0"
regex = "1"
serde = "1"
serde_derive = "1"

[output.pdf-outline]
like-wkhtmltopdf = false
22 changes: 22 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.idle_browser_timeout(Duration::from_secs(600))
.path(browser_binary)
.args(vec![
OsStr::new("--disable-pdf-tagging"),
OsStr::new("--unlimited-storage"),
OsStr::new("--disable-web-security"),
OsStr::new("--webkit-print-color-adjust"),
Expand Down Expand Up @@ -177,6 +178,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let page = tab.navigate_to(&url)?.wait_until_navigated()?;
page.wait_for_element("#content-has-all-loaded-for-mdbook-pdf-generation")?;

// Find the theme and click it to change the theme.
if !cloned_cfg.theme.is_empty() {
match tab.find_element(&format!("button.theme#{}", cloned_cfg.theme.to_lowercase())) {
Ok(_) => {
tab.evaluate(
&format!(
"document.querySelector('button.theme#{}').click()",
cloned_cfg.theme.to_lowercase()
),
false,
)?;
}
Err(_) => println!(
"Unable to find theme {}, return to default one.",
cloned_cfg.theme.to_lowercase()
),
};
}

// Generate the PDF.
let generated_pdf = match page.print_to_pdf(Some(pdf_opts)) {
Ok(output) => output,
Expand Down Expand Up @@ -222,6 +242,7 @@ pub struct PrintOptions {
pub landscape: bool,
pub display_header_footer: bool,
pub print_background: bool,
pub theme: String,
pub scale: f64,
pub paper_width: f64,
pub paper_height: f64,
Expand Down Expand Up @@ -250,6 +271,7 @@ impl Default for PrintOptions {
landscape: false,
display_header_footer: false,
print_background: false,
theme: "".to_string(),
scale: 1_f64,
paper_width: 8.5_f64,
paper_height: 11_f64,
Expand Down
1 change: 1 addition & 0 deletions test_doc/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ title = "An Example"
landscape = false
display-header-footer = true
print-background = true
theme = "ayu"
scale = 1
paper-width = 8
paper-height = 10
Expand Down

0 comments on commit 1716a5c

Please sign in to comment.