From 2765af3c89e8874d910ce85adbb1a46fd43cc289 Mon Sep 17 00:00:00 2001 From: Dmitry Luschan Date: Tue, 9 Jan 2024 13:20:28 +0600 Subject: [PATCH 1/3] move tag into

add recursive walking to nested html tags for find assets --- src/resources.rs | 29 ++++++++++++++++++++++++----- tests/dummy/src/chapter_1.md | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/resources.rs b/src/resources.rs index 34b1a44b3..0dcbd36bf 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -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; @@ -252,6 +252,27 @@ impl Asset { } } +// Look up resources in nested HTML element +fn find_assets_in_nested_html_tags(element: &Element) -> Result, 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, Error> { let mut found_asset = Vec::new(); @@ -269,10 +290,8 @@ fn find_assets_in_markdown(chapter_src_content: &str) -> Result, 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()); } _ => {} } diff --git a/tests/dummy/src/chapter_1.md b/tests/dummy/src/chapter_1.md index 30f432717..7de6cb2e2 100644 --- a/tests/dummy/src/chapter_1.md +++ b/tests/dummy/src/chapter_1.md @@ -9,7 +9,7 @@ Here is the Rust logo: Listing example: {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs:here}} -Rust Logo in html +

Rust Logo in html

![Image](assets/rust-logo.png) From 29454fd85e5d15604faba489bb2bdca6e57cdb91 Mon Sep 17 00:00:00 2001 From: Dmitry Luschan Date: Tue, 9 Jan 2024 13:21:57 +0600 Subject: [PATCH 2/3] remove unnecessary additional-resources --- tests/dummy/book.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/dummy/book.toml b/tests/dummy/book.toml index 9b96e751b..82d468d62 100644 --- a/tests/dummy/book.toml +++ b/tests/dummy/book.toml @@ -7,4 +7,3 @@ lang = "en" [output.epub] curly-quotes = true -additional-resources = ["assets/rust-logo.png"] \ No newline at end of file From 8e9debb4acc81e968df291ab86baae533b0f0e49 Mon Sep 17 00:00:00 2001 From: Dmitry Luschan Date: Tue, 9 Jan 2024 13:22:24 +0600 Subject: [PATCH 3/3] remove outside image --- tests/dummy/src/chapter_1.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/dummy/src/chapter_1.md b/tests/dummy/src/chapter_1.md index 7de6cb2e2..22dd3c830 100644 --- a/tests/dummy/src/chapter_1.md +++ b/tests/dummy/src/chapter_1.md @@ -4,8 +4,6 @@ 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}}