Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix output_epub_is_valid test #89

Merged
merged 3 commits into from
Jan 19, 2024
Merged
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
29 changes: 24 additions & 5 deletions src/resources.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use const_format::concatcp;
use html_parser::{Dom, Node};
use html_parser::{Dom, Node, Element};
use mdbook::book::BookItem;
use mdbook::renderer::RenderContext;
use mime_guess::Mime;
Expand Down Expand Up @@ -252,6 +252,27 @@ impl Asset {
}
}

// Look up resources in nested HTML element
fn find_assets_in_nested_html_tags(element: &Element) -> Result<Vec<String>, Error> {
let mut found_asset = Vec::new();

if element.name == "img" {
if let Some(dest) = &element.attributes["src"] {
found_asset.push(dest.clone());
}
}
for item in &element.children {
match item {
Node::Element(ref nested_element) => {
found_asset.extend(find_assets_in_nested_html_tags(nested_element)?.into_iter());
}
_ => {}
}
}

Ok(found_asset)
}

// Look up resources in chapter md content
fn find_assets_in_markdown(chapter_src_content: &str) -> Result<Vec<String>, Error> {
let mut found_asset = Vec::new();
Expand All @@ -269,10 +290,8 @@ fn find_assets_in_markdown(chapter_src_content: &str) -> Result<Vec<String>, Err
if let Ok(dom) = Dom::parse(&content) {
for item in dom.children {
match item {
Node::Element(ref element) if element.name == "img" => {
if let Some(dest) = &element.attributes["src"] {
found_asset.push(dest.clone());
}
Node::Element(ref element) => {
found_asset.extend(find_assets_in_nested_html_tags(element)?.into_iter());
}
_ => {}
}
Expand Down
1 change: 0 additions & 1 deletion tests/dummy/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ lang = "en"

[output.epub]
curly-quotes = true
additional-resources = ["assets/rust-logo.png"]
4 changes: 1 addition & 3 deletions tests/dummy/src/chapter_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ Here is the Rust logo:

![Rust Logo](rust-logo.png)

![Image](../third_party/wikimedia/Epub_logo_color.svg)

Listing example:
{{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs:here}}

<img alt="Rust Logo in html" src="rust-logo.svg" class="center" style="width: 20%;" />
<p><img alt="Rust Logo in html" src="rust-logo.svg" class="center" style="width: 20%;" /></p>

![Image](assets/rust-logo.png)

Expand Down
Loading