Skip to content

Commit

Permalink
Merge pull request #89 from dluschan/master
Browse files Browse the repository at this point in the history
fix output_epub_is_valid test
  • Loading branch information
blandger authored Jan 19, 2024
2 parents db145fc + 8e9debb commit a77f1f4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
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

0 comments on commit a77f1f4

Please sign in to comment.