html-to-markdown 3.4.0 → 3.6.0.pre.rc.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +347 -0
- data/Steepfile +10 -2
- data/ext/html_to_markdown_rb/Cargo.toml +3 -2
- data/ext/html_to_markdown_rb/extconf.rb +5 -5
- data/ext/html_to_markdown_rb/native/Cargo.lock +962 -0
- data/ext/html_to_markdown_rb/native/Cargo.toml +6 -11
- data/ext/html_to_markdown_rb/native/extconf.rb +14 -0
- data/ext/html_to_markdown_rb/src/lib.rs +1715 -646
- data/lib/html_to_markdown/native.rb +913 -37
- data/lib/html_to_markdown/version.rb +3 -3
- data/lib/html_to_markdown.rb +9 -4
- data/lib/html_to_markdown_rb.so +0 -0
- data/sig/types.rbs +59 -292
- metadata +32 -179
- data/ext/html_to_markdown_rb/Makefile +0 -592
- data/lib/bin/html-to-markdown +0 -0
- data/vendor/Cargo.toml +0 -33
- data/vendor/html-to-markdown-rs/Cargo.toml +0 -54
- data/vendor/html-to-markdown-rs/README.md +0 -278
- data/vendor/html-to-markdown-rs/examples/basic.rs +0 -24
- data/vendor/html-to-markdown-rs/examples/table.rs +0 -25
- data/vendor/html-to-markdown-rs/examples/test_deser.rs +0 -12
- data/vendor/html-to-markdown-rs/examples/test_escape.rs +0 -58
- data/vendor/html-to-markdown-rs/examples/test_inline_formatting.rs +0 -113
- data/vendor/html-to-markdown-rs/examples/test_lists.rs +0 -39
- data/vendor/html-to-markdown-rs/examples/test_semantic_tags.rs +0 -89
- data/vendor/html-to-markdown-rs/examples/test_tables.rs +0 -100
- data/vendor/html-to-markdown-rs/examples/test_task_lists.rs +0 -61
- data/vendor/html-to-markdown-rs/examples/test_whitespace.rs +0 -34
- data/vendor/html-to-markdown-rs/src/convert_api.rs +0 -349
- data/vendor/html-to-markdown-rs/src/converter/block/blockquote.rs +0 -178
- data/vendor/html-to-markdown-rs/src/converter/block/container.rs +0 -114
- data/vendor/html-to-markdown-rs/src/converter/block/div.rs +0 -149
- data/vendor/html-to-markdown-rs/src/converter/block/heading.rs +0 -428
- data/vendor/html-to-markdown-rs/src/converter/block/horizontal_rule.rs +0 -103
- data/vendor/html-to-markdown-rs/src/converter/block/line_break.rs +0 -89
- data/vendor/html-to-markdown-rs/src/converter/block/mod.rs +0 -10
- data/vendor/html-to-markdown-rs/src/converter/block/paragraph.rs +0 -140
- data/vendor/html-to-markdown-rs/src/converter/block/preformatted.rs +0 -298
- data/vendor/html-to-markdown-rs/src/converter/block/table/builder.rs +0 -453
- data/vendor/html-to-markdown-rs/src/converter/block/table/caption.rs +0 -44
- data/vendor/html-to-markdown-rs/src/converter/block/table/cell.rs +0 -276
- data/vendor/html-to-markdown-rs/src/converter/block/table/cells.rs +0 -336
- data/vendor/html-to-markdown-rs/src/converter/block/table/layout.rs +0 -58
- data/vendor/html-to-markdown-rs/src/converter/block/table/mod.rs +0 -266
- data/vendor/html-to-markdown-rs/src/converter/block/table/scanner.rs +0 -146
- data/vendor/html-to-markdown-rs/src/converter/block/table/utils.rs +0 -34
- data/vendor/html-to-markdown-rs/src/converter/block/unknown.rs +0 -138
- data/vendor/html-to-markdown-rs/src/converter/context.rs +0 -208
- data/vendor/html-to-markdown-rs/src/converter/dom_context.rs +0 -337
- data/vendor/html-to-markdown-rs/src/converter/form/elements.rs +0 -770
- data/vendor/html-to-markdown-rs/src/converter/form/mod.rs +0 -82
- data/vendor/html-to-markdown-rs/src/converter/format/djot.rs +0 -64
- data/vendor/html-to-markdown-rs/src/converter/format/markdown.rs +0 -59
- data/vendor/html-to-markdown-rs/src/converter/format/mod.rs +0 -43
- data/vendor/html-to-markdown-rs/src/converter/handlers/blockquote.rs +0 -173
- data/vendor/html-to-markdown-rs/src/converter/handlers/code_block.rs +0 -434
- data/vendor/html-to-markdown-rs/src/converter/handlers/graphic.rs +0 -234
- data/vendor/html-to-markdown-rs/src/converter/handlers/image.rs +0 -282
- data/vendor/html-to-markdown-rs/src/converter/handlers/link.rs +0 -316
- data/vendor/html-to-markdown-rs/src/converter/handlers/mod.rs +0 -26
- data/vendor/html-to-markdown-rs/src/converter/inline/code.rs +0 -306
- data/vendor/html-to-markdown-rs/src/converter/inline/emphasis.rs +0 -345
- data/vendor/html-to-markdown-rs/src/converter/inline/link.rs +0 -428
- data/vendor/html-to-markdown-rs/src/converter/inline/mod.rs +0 -237
- data/vendor/html-to-markdown-rs/src/converter/inline/ruby.rs +0 -337
- data/vendor/html-to-markdown-rs/src/converter/inline/semantic/marks.rs +0 -566
- data/vendor/html-to-markdown-rs/src/converter/inline/semantic/mod.rs +0 -86
- data/vendor/html-to-markdown-rs/src/converter/inline/semantic/typography.rs +0 -558
- data/vendor/html-to-markdown-rs/src/converter/list/definition.rs +0 -232
- data/vendor/html-to-markdown-rs/src/converter/list/item.rs +0 -332
- data/vendor/html-to-markdown-rs/src/converter/list/mod.rs +0 -70
- data/vendor/html-to-markdown-rs/src/converter/list/ordered.rs +0 -201
- data/vendor/html-to-markdown-rs/src/converter/list/unordered.rs +0 -195
- data/vendor/html-to-markdown-rs/src/converter/list/utils.rs +0 -314
- data/vendor/html-to-markdown-rs/src/converter/main.rs +0 -710
- data/vendor/html-to-markdown-rs/src/converter/main_helpers.rs +0 -452
- data/vendor/html-to-markdown-rs/src/converter/media/embedded.rs +0 -393
- data/vendor/html-to-markdown-rs/src/converter/media/graphic.rs +0 -4
- data/vendor/html-to-markdown-rs/src/converter/media/image.rs +0 -183
- data/vendor/html-to-markdown-rs/src/converter/media/mod.rs +0 -87
- data/vendor/html-to-markdown-rs/src/converter/media/svg.rs +0 -280
- data/vendor/html-to-markdown-rs/src/converter/metadata.rs +0 -220
- data/vendor/html-to-markdown-rs/src/converter/mod.rs +0 -156
- data/vendor/html-to-markdown-rs/src/converter/plain_text.rs +0 -516
- data/vendor/html-to-markdown-rs/src/converter/preprocessing_helpers.rs +0 -201
- data/vendor/html-to-markdown-rs/src/converter/reference_collector.rs +0 -69
- data/vendor/html-to-markdown-rs/src/converter/semantic/attributes.rs +0 -269
- data/vendor/html-to-markdown-rs/src/converter/semantic/definition_list.rs +0 -266
- data/vendor/html-to-markdown-rs/src/converter/semantic/figure.rs +0 -391
- data/vendor/html-to-markdown-rs/src/converter/semantic/mod.rs +0 -112
- data/vendor/html-to-markdown-rs/src/converter/semantic/sectioning.rs +0 -85
- data/vendor/html-to-markdown-rs/src/converter/semantic/summary.rs +0 -324
- data/vendor/html-to-markdown-rs/src/converter/text/mod.rs +0 -8
- data/vendor/html-to-markdown-rs/src/converter/text/processing.rs +0 -56
- data/vendor/html-to-markdown-rs/src/converter/text_node.rs +0 -269
- data/vendor/html-to-markdown-rs/src/converter/utility/attributes.rs +0 -151
- data/vendor/html-to-markdown-rs/src/converter/utility/caching.rs +0 -74
- data/vendor/html-to-markdown-rs/src/converter/utility/content.rs +0 -271
- data/vendor/html-to-markdown-rs/src/converter/utility/mod.rs +0 -17
- data/vendor/html-to-markdown-rs/src/converter/utility/preprocessing.rs +0 -1002
- data/vendor/html-to-markdown-rs/src/converter/utility/serialization.rs +0 -126
- data/vendor/html-to-markdown-rs/src/converter/utility/siblings.rs +0 -97
- data/vendor/html-to-markdown-rs/src/converter/visitor_hooks.rs +0 -189
- data/vendor/html-to-markdown-rs/src/error.rs +0 -43
- data/vendor/html-to-markdown-rs/src/exports.rs +0 -24
- data/vendor/html-to-markdown-rs/src/inline_images.rs +0 -336
- data/vendor/html-to-markdown-rs/src/lib.rs +0 -139
- data/vendor/html-to-markdown-rs/src/metadata/collector.rs +0 -457
- data/vendor/html-to-markdown-rs/src/metadata/config.rs +0 -394
- data/vendor/html-to-markdown-rs/src/metadata/extraction.rs +0 -398
- data/vendor/html-to-markdown-rs/src/metadata/mod.rs +0 -288
- data/vendor/html-to-markdown-rs/src/metadata/types.rs +0 -477
- data/vendor/html-to-markdown-rs/src/options/conversion.rs +0 -559
- data/vendor/html-to-markdown-rs/src/options/inline_image.rs +0 -111
- data/vendor/html-to-markdown-rs/src/options/mod.rs +0 -20
- data/vendor/html-to-markdown-rs/src/options/preprocessing.rs +0 -201
- data/vendor/html-to-markdown-rs/src/options/validation.rs +0 -416
- data/vendor/html-to-markdown-rs/src/prelude.rs +0 -1
- data/vendor/html-to-markdown-rs/src/rcdom.rs +0 -487
- data/vendor/html-to-markdown-rs/src/text.rs +0 -358
- data/vendor/html-to-markdown-rs/src/types/document.rs +0 -191
- data/vendor/html-to-markdown-rs/src/types/mod.rs +0 -17
- data/vendor/html-to-markdown-rs/src/types/result.rs +0 -54
- data/vendor/html-to-markdown-rs/src/types/structure_builder.rs +0 -791
- data/vendor/html-to-markdown-rs/src/types/structure_collector.rs +0 -483
- data/vendor/html-to-markdown-rs/src/types/tables.rs +0 -52
- data/vendor/html-to-markdown-rs/src/types/warnings.rs +0 -33
- data/vendor/html-to-markdown-rs/src/validation.rs +0 -158
- data/vendor/html-to-markdown-rs/src/visitor/default_impl.rs +0 -63
- data/vendor/html-to-markdown-rs/src/visitor/mod.rs +0 -41
- data/vendor/html-to-markdown-rs/src/visitor/traits.rs +0 -370
- data/vendor/html-to-markdown-rs/src/visitor/types.rs +0 -319
- data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/callbacks/mod.rs +0 -1
- data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/content.rs +0 -126
- data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/mod.rs +0 -27
- data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/state.rs +0 -110
- data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/traversal.rs +0 -250
- data/vendor/html-to-markdown-rs/src/visitor_helpers.rs +0 -597
- data/vendor/html-to-markdown-rs/src/wrapper/sync.rs +0 -413
- data/vendor/html-to-markdown-rs/src/wrapper/utils.rs +0 -290
- data/vendor/html-to-markdown-rs/src/wrapper.rs +0 -9
- data/vendor/html-to-markdown-rs/tests/br_in_inline_test.rs +0 -87
- data/vendor/html-to-markdown-rs/tests/commonmark_compliance_test.rs +0 -297
- data/vendor/html-to-markdown-rs/tests/djot_output_test.rs +0 -153
- data/vendor/html-to-markdown-rs/tests/exclude_selectors_test.rs +0 -132
- data/vendor/html-to-markdown-rs/tests/integration_test.rs +0 -631
- data/vendor/html-to-markdown-rs/tests/issue_121_regressions.rs +0 -49
- data/vendor/html-to-markdown-rs/tests/issue_127_regressions.rs +0 -58
- data/vendor/html-to-markdown-rs/tests/issue_128_regressions.rs +0 -17
- data/vendor/html-to-markdown-rs/tests/issue_131_regressions.rs +0 -41
- data/vendor/html-to-markdown-rs/tests/issue_134_regressions.rs +0 -40
- data/vendor/html-to-markdown-rs/tests/issue_139_regressions.rs +0 -26
- data/vendor/html-to-markdown-rs/tests/issue_140_regressions.rs +0 -185
- data/vendor/html-to-markdown-rs/tests/issue_143_regressions.rs +0 -100
- data/vendor/html-to-markdown-rs/tests/issue_145_regressions.rs +0 -133
- data/vendor/html-to-markdown-rs/tests/issue_146_regressions.rs +0 -144
- data/vendor/html-to-markdown-rs/tests/issue_176_regressions.rs +0 -62
- data/vendor/html-to-markdown-rs/tests/issue_190_regressions.rs +0 -128
- data/vendor/html-to-markdown-rs/tests/issue_199_regressions.rs +0 -20
- data/vendor/html-to-markdown-rs/tests/issue_200_regressions.rs +0 -62
- data/vendor/html-to-markdown-rs/tests/issue_212_regressions.rs +0 -68
- data/vendor/html-to-markdown-rs/tests/issue_216_217_regressions.rs +0 -87
- data/vendor/html-to-markdown-rs/tests/issue_336_regressions.rs +0 -74
- data/vendor/html-to-markdown-rs/tests/issue_339_regressions.rs +0 -92
- data/vendor/html-to-markdown-rs/tests/issue_347_regressions.rs +0 -154
- data/vendor/html-to-markdown-rs/tests/issue_348_visitor_plain.rs +0 -93
- data/vendor/html-to-markdown-rs/tests/json_ld_script_extraction.rs +0 -44
- data/vendor/html-to-markdown-rs/tests/lists_test.rs +0 -199
- data/vendor/html-to-markdown-rs/tests/plain_output_test.rs +0 -273
- data/vendor/html-to-markdown-rs/tests/preprocessing_tests.rs +0 -61
- data/vendor/html-to-markdown-rs/tests/reference_links_test.rs +0 -169
- data/vendor/html-to-markdown-rs/tests/sectioning_elements_test.rs +0 -137
- data/vendor/html-to-markdown-rs/tests/skip_images_test.rs +0 -522
- data/vendor/html-to-markdown-rs/tests/tables_test.rs +0 -743
- data/vendor/html-to-markdown-rs/tests/test_custom_elements.rs +0 -41
- data/vendor/html-to-markdown-rs/tests/test_issue_187.rs +0 -204
- data/vendor/html-to-markdown-rs/tests/test_issue_218.rs +0 -68
- data/vendor/html-to-markdown-rs/tests/test_issue_277.rs +0 -77
- data/vendor/html-to-markdown-rs/tests/test_max_depth.rs +0 -82
- data/vendor/html-to-markdown-rs/tests/test_nested_simple.rs +0 -45
- data/vendor/html-to-markdown-rs/tests/test_script_style_stripping.rs +0 -396
- data/vendor/html-to-markdown-rs/tests/test_spa_bisect.rs +0 -34
- data/vendor/html-to-markdown-rs/tests/visitor_code_integration_test.rs +0 -121
- data/vendor/html-to-markdown-rs/tests/visitor_integration_test.rs +0 -1190
- data/vendor/html-to-markdown-rs/tests/xml_tables_test.rs +0 -372
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
//! Image element handler for HTML to Markdown conversion.
|
|
2
|
-
//!
|
|
3
|
-
//! Handles `<img>` elements including:
|
|
4
|
-
//! - Basic image markdown output ``
|
|
5
|
-
//! - Metadata collection for image extraction
|
|
6
|
-
//! - Inline data URI image handling
|
|
7
|
-
//! - Visitor callback integration
|
|
8
|
-
|
|
9
|
-
use std::borrow::Cow;
|
|
10
|
-
#[cfg(any(feature = "metadata", feature = "inline-images", feature = "visitor"))]
|
|
11
|
-
use std::collections::BTreeMap;
|
|
12
|
-
|
|
13
|
-
use crate::converter::Context;
|
|
14
|
-
use crate::converter::dom_context::DomContext;
|
|
15
|
-
#[cfg(feature = "visitor")]
|
|
16
|
-
use crate::converter::utility::content::collect_tag_attributes;
|
|
17
|
-
use crate::converter::utility::preprocessing::sanitize_markdown_url;
|
|
18
|
-
use crate::options::ConversionOptions;
|
|
19
|
-
|
|
20
|
-
#[cfg(feature = "inline-images")]
|
|
21
|
-
use crate::converter::media::handle_inline_data_image;
|
|
22
|
-
|
|
23
|
-
#[cfg(feature = "visitor")]
|
|
24
|
-
use crate::converter::utility::serialization::serialize_node;
|
|
25
|
-
|
|
26
|
-
#[cfg(feature = "metadata")]
|
|
27
|
-
type ImageMetadataPayload = (BTreeMap<String, String>, Option<u32>, Option<u32>);
|
|
28
|
-
|
|
29
|
-
/// Handle an `<img>` element and convert to Markdown.
|
|
30
|
-
///
|
|
31
|
-
/// This handler processes image elements including:
|
|
32
|
-
/// - Extracting src, alt, and title attributes
|
|
33
|
-
/// - Collecting metadata when the metadata feature is enabled
|
|
34
|
-
/// - Handling inline data URIs when the inline-images feature is enabled
|
|
35
|
-
/// - Invoking visitor callbacks when the visitor feature is enabled
|
|
36
|
-
/// - Generating appropriate markdown output
|
|
37
|
-
#[allow(clippy::too_many_arguments)]
|
|
38
|
-
#[allow(clippy::too_many_lines)]
|
|
39
|
-
#[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
|
|
40
|
-
pub fn handle_img(
|
|
41
|
-
node_handle: &tl::NodeHandle,
|
|
42
|
-
tag: &tl::HTMLTag,
|
|
43
|
-
parser: &tl::Parser,
|
|
44
|
-
output: &mut String,
|
|
45
|
-
options: &ConversionOptions,
|
|
46
|
-
ctx: &Context,
|
|
47
|
-
depth: usize,
|
|
48
|
-
dom_ctx: &DomContext,
|
|
49
|
-
) {
|
|
50
|
-
let src = tag.attributes().get("src").flatten().map_or(Cow::Borrowed(""), |v| {
|
|
51
|
-
let s = v.as_utf8_str();
|
|
52
|
-
Cow::Owned(sanitize_markdown_url(&s).into_owned())
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
let alt = tag
|
|
56
|
-
.attributes()
|
|
57
|
-
.get("alt")
|
|
58
|
-
.flatten()
|
|
59
|
-
.map_or(Cow::Borrowed(""), |v| v.as_utf8_str());
|
|
60
|
-
|
|
61
|
-
let title = tag.attributes().get("title").flatten().map(|v| v.as_utf8_str());
|
|
62
|
-
|
|
63
|
-
// Collect metadata payload if metadata feature is enabled
|
|
64
|
-
#[cfg(feature = "metadata")]
|
|
65
|
-
#[allow(clippy::useless_let_if_seq)]
|
|
66
|
-
let mut metadata_payload: Option<ImageMetadataPayload> = None;
|
|
67
|
-
#[cfg(feature = "metadata")]
|
|
68
|
-
if ctx.metadata_wants_images {
|
|
69
|
-
let mut attributes_map = BTreeMap::new();
|
|
70
|
-
let mut width: Option<u32> = None;
|
|
71
|
-
let mut height: Option<u32> = None;
|
|
72
|
-
for (key, value_opt) in tag.attributes().iter() {
|
|
73
|
-
let key_str = key.to_string();
|
|
74
|
-
if key_str == "src" {
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
let value = value_opt.map(|v| v.to_string()).unwrap_or_default();
|
|
78
|
-
if key_str == "width" {
|
|
79
|
-
if let Ok(parsed) = value.parse::<u32>() {
|
|
80
|
-
width = Some(parsed);
|
|
81
|
-
}
|
|
82
|
-
} else if key_str == "height" {
|
|
83
|
-
if let Ok(parsed) = value.parse::<u32>() {
|
|
84
|
-
height = Some(parsed);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
attributes_map.insert(key_str, value);
|
|
88
|
-
}
|
|
89
|
-
metadata_payload = Some((attributes_map, width, height));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// Handle inline data URI images
|
|
93
|
-
#[cfg(feature = "inline-images")]
|
|
94
|
-
if let Some(ref collector_ref) = ctx.inline_collector {
|
|
95
|
-
if src.trim_start().starts_with("data:") {
|
|
96
|
-
let mut attributes_map = BTreeMap::new();
|
|
97
|
-
for (key, value_opt) in tag.attributes().iter() {
|
|
98
|
-
let key_str = key.to_string();
|
|
99
|
-
let keep = key_str == "width"
|
|
100
|
-
|| key_str == "height"
|
|
101
|
-
|| key_str == "filename"
|
|
102
|
-
|| key_str == "aria-label"
|
|
103
|
-
|| key_str.starts_with("data-");
|
|
104
|
-
if keep {
|
|
105
|
-
let value = value_opt.map(|value| value.to_string()).unwrap_or_default();
|
|
106
|
-
attributes_map.insert(key_str, value);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
handle_inline_data_image(
|
|
110
|
-
collector_ref,
|
|
111
|
-
src.as_ref(),
|
|
112
|
-
alt.as_ref(),
|
|
113
|
-
title.as_deref(),
|
|
114
|
-
attributes_map,
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
let keep_as_markdown = ctx.in_heading && ctx.heading_allow_inline_images;
|
|
120
|
-
|
|
121
|
-
let should_use_alt_text =
|
|
122
|
-
!keep_as_markdown && (ctx.convert_as_inline || (ctx.in_heading && !ctx.heading_allow_inline_images));
|
|
123
|
-
|
|
124
|
-
// Generate image output with visitor integration
|
|
125
|
-
#[cfg(feature = "visitor")]
|
|
126
|
-
let image_output = if let Some(ref visitor_handle) = ctx.visitor {
|
|
127
|
-
use crate::visitor::{NodeContext, NodeType, VisitResult};
|
|
128
|
-
|
|
129
|
-
let attributes: BTreeMap<String, String> = collect_tag_attributes(tag);
|
|
130
|
-
|
|
131
|
-
let node_id = node_handle.get_inner();
|
|
132
|
-
let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
|
|
133
|
-
let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
|
|
134
|
-
|
|
135
|
-
let node_ctx = NodeContext {
|
|
136
|
-
node_type: NodeType::Image,
|
|
137
|
-
tag_name: "img".to_string(),
|
|
138
|
-
attributes,
|
|
139
|
-
depth,
|
|
140
|
-
index_in_parent,
|
|
141
|
-
parent_tag,
|
|
142
|
-
is_inline: true,
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
let visit_result = {
|
|
146
|
-
let mut visitor = visitor_handle.borrow_mut();
|
|
147
|
-
visitor.visit_image(&node_ctx, &src, &alt, title.as_deref())
|
|
148
|
-
};
|
|
149
|
-
match visit_result {
|
|
150
|
-
VisitResult::Continue => Some(format_image_markdown(
|
|
151
|
-
&src,
|
|
152
|
-
&alt,
|
|
153
|
-
title.as_deref(),
|
|
154
|
-
should_use_alt_text,
|
|
155
|
-
options.link_style,
|
|
156
|
-
ctx.reference_collector.as_ref(),
|
|
157
|
-
)),
|
|
158
|
-
VisitResult::Custom(custom) => Some(custom),
|
|
159
|
-
VisitResult::Skip => None,
|
|
160
|
-
VisitResult::Error(err) => {
|
|
161
|
-
if ctx.visitor_error.borrow().is_none() {
|
|
162
|
-
*ctx.visitor_error.borrow_mut() = Some(err);
|
|
163
|
-
}
|
|
164
|
-
None
|
|
165
|
-
}
|
|
166
|
-
VisitResult::PreserveHtml => Some(serialize_node(node_handle, parser)),
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
Some(format_image_markdown(
|
|
170
|
-
&src,
|
|
171
|
-
&alt,
|
|
172
|
-
title.as_deref(),
|
|
173
|
-
should_use_alt_text,
|
|
174
|
-
options.link_style,
|
|
175
|
-
ctx.reference_collector.as_ref(),
|
|
176
|
-
))
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
#[cfg(not(feature = "visitor"))]
|
|
180
|
-
let image_output = Some(format_image_markdown(
|
|
181
|
-
&src,
|
|
182
|
-
&alt,
|
|
183
|
-
title.as_deref(),
|
|
184
|
-
should_use_alt_text,
|
|
185
|
-
options.link_style,
|
|
186
|
-
ctx.reference_collector.as_ref(),
|
|
187
|
-
));
|
|
188
|
-
|
|
189
|
-
// Only output image if skip_images is not enabled
|
|
190
|
-
if !options.skip_images {
|
|
191
|
-
if let Some(img_text) = image_output {
|
|
192
|
-
output.push_str(&img_text);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Add image to metadata collector
|
|
197
|
-
#[cfg(feature = "metadata")]
|
|
198
|
-
if ctx.metadata_wants_images {
|
|
199
|
-
if let Some(ref collector) = ctx.metadata_collector {
|
|
200
|
-
if let Some((attributes_map, width, height)) = metadata_payload {
|
|
201
|
-
if !src.is_empty() {
|
|
202
|
-
let dimensions = match (width, height) {
|
|
203
|
-
(Some(w), Some(h)) => Some((w, h)),
|
|
204
|
-
_ => None,
|
|
205
|
-
};
|
|
206
|
-
collector.borrow_mut().add_image(
|
|
207
|
-
src.to_string(),
|
|
208
|
-
if alt.is_empty() { None } else { Some(alt.to_string()) },
|
|
209
|
-
title.as_deref().map(std::string::ToString::to_string),
|
|
210
|
-
dimensions,
|
|
211
|
-
attributes_map,
|
|
212
|
-
);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if let Some(ref sc) = ctx.structure_collector {
|
|
219
|
-
let src_opt = if src.is_empty() { None } else { Some(src.as_ref()) };
|
|
220
|
-
let alt_opt = if alt.is_empty() { None } else { Some(alt.as_ref()) };
|
|
221
|
-
sc.borrow_mut().push_image(src_opt, alt_opt);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/// Format an image as Markdown syntax.
|
|
226
|
-
///
|
|
227
|
-
/// If `use_alt_only` is true, returns just the alt text.
|
|
228
|
-
/// Otherwise returns the full `` syntax.
|
|
229
|
-
fn format_image_markdown(
|
|
230
|
-
src: &str,
|
|
231
|
-
alt: &str,
|
|
232
|
-
title: Option<&str>,
|
|
233
|
-
use_alt_only: bool,
|
|
234
|
-
link_style: crate::options::validation::LinkStyle,
|
|
235
|
-
reference_collector: Option<&crate::converter::reference_collector::ReferenceCollectorHandle>,
|
|
236
|
-
) -> String {
|
|
237
|
-
if use_alt_only {
|
|
238
|
-
return alt.to_string();
|
|
239
|
-
}
|
|
240
|
-
if link_style == crate::options::validation::LinkStyle::Reference {
|
|
241
|
-
if let Some(collector) = reference_collector {
|
|
242
|
-
let ref_num = collector.borrow_mut().get_or_insert(src, title);
|
|
243
|
-
let mut buf = String::with_capacity(alt.len() + 10);
|
|
244
|
-
buf.push_str("![");
|
|
245
|
-
buf.push_str(alt);
|
|
246
|
-
buf.push_str("][");
|
|
247
|
-
buf.push_str(&ref_num.to_string());
|
|
248
|
-
buf.push(']');
|
|
249
|
-
return buf;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
let mut buf = String::with_capacity(src.len() + alt.len() + 10);
|
|
253
|
-
buf.push_str(";
|
|
256
|
-
|
|
257
|
-
if src.is_empty() {
|
|
258
|
-
buf.push_str("<>");
|
|
259
|
-
} else if src.contains(' ') || src.contains('\n') {
|
|
260
|
-
buf.push('<');
|
|
261
|
-
buf.push_str(src);
|
|
262
|
-
buf.push('>');
|
|
263
|
-
} else {
|
|
264
|
-
let open_count = src.chars().filter(|&c| c == '(').count();
|
|
265
|
-
let close_count = src.chars().filter(|&c| c == ')').count();
|
|
266
|
-
|
|
267
|
-
if open_count == close_count {
|
|
268
|
-
buf.push_str(src);
|
|
269
|
-
} else {
|
|
270
|
-
let escaped_src = src.replace('(', "\\(").replace(')', "\\)");
|
|
271
|
-
buf.push_str(&escaped_src);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
if let Some(title_text) = title {
|
|
276
|
-
buf.push_str(" \"");
|
|
277
|
-
buf.push_str(title_text);
|
|
278
|
-
buf.push('"');
|
|
279
|
-
}
|
|
280
|
-
buf.push(')');
|
|
281
|
-
buf
|
|
282
|
-
}
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
//! Link element handler for HTML to Markdown conversion.
|
|
2
|
-
//!
|
|
3
|
-
//! Handles `<a>` elements including:
|
|
4
|
-
//! - Basic link markdown output `[text](href "title")`
|
|
5
|
-
//! - Autolinks when text matches href
|
|
6
|
-
//! - Links containing heading elements
|
|
7
|
-
//! - Complex link content with mixed block/inline elements
|
|
8
|
-
//! - Visitor callback integration
|
|
9
|
-
//! - Link metadata collection
|
|
10
|
-
|
|
11
|
-
#[cfg(any(feature = "metadata", feature = "visitor"))]
|
|
12
|
-
use std::collections::BTreeMap;
|
|
13
|
-
|
|
14
|
-
use crate::converter::Context;
|
|
15
|
-
use crate::converter::block::heading::{find_single_heading_child, heading_allows_inline_images, push_heading};
|
|
16
|
-
use crate::converter::dom_context::DomContext;
|
|
17
|
-
use crate::converter::inline::link::append_markdown_link;
|
|
18
|
-
use crate::converter::main::walk_node;
|
|
19
|
-
#[cfg(feature = "visitor")]
|
|
20
|
-
use crate::converter::utility::content::collect_tag_attributes;
|
|
21
|
-
use crate::converter::utility::content::{
|
|
22
|
-
collect_link_label_text, escape_link_label, get_text_content, normalize_link_label, normalized_tag_name,
|
|
23
|
-
};
|
|
24
|
-
use crate::options::ConversionOptions;
|
|
25
|
-
use crate::text;
|
|
26
|
-
|
|
27
|
-
#[cfg(feature = "visitor")]
|
|
28
|
-
use crate::converter::utility::serialization::serialize_node;
|
|
29
|
-
|
|
30
|
-
/// Handle an `<a>` (link) element and convert to Markdown.
|
|
31
|
-
///
|
|
32
|
-
/// This handler processes link elements including:
|
|
33
|
-
/// - Extracting href and title attributes
|
|
34
|
-
/// - Detecting autolinks (where text equals href)
|
|
35
|
-
/// - Handling links that contain heading elements
|
|
36
|
-
/// - Processing complex link content (mixed block/inline)
|
|
37
|
-
/// - Invoking visitor callbacks when the visitor feature is enabled
|
|
38
|
-
/// - Collecting link metadata when the metadata feature is enabled
|
|
39
|
-
/// - Generating appropriate markdown link output
|
|
40
|
-
#[allow(clippy::too_many_arguments)]
|
|
41
|
-
#[allow(clippy::too_many_lines)]
|
|
42
|
-
#[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
|
|
43
|
-
pub fn handle_link(
|
|
44
|
-
node_handle: &tl::NodeHandle,
|
|
45
|
-
tag: &tl::HTMLTag,
|
|
46
|
-
parser: &tl::Parser,
|
|
47
|
-
output: &mut String,
|
|
48
|
-
options: &ConversionOptions,
|
|
49
|
-
ctx: &Context,
|
|
50
|
-
depth: usize,
|
|
51
|
-
dom_ctx: &DomContext,
|
|
52
|
-
) {
|
|
53
|
-
let href_attr = tag
|
|
54
|
-
.attributes()
|
|
55
|
-
.get("href")
|
|
56
|
-
.flatten()
|
|
57
|
-
.map(|v| text::decode_html_entities(&v.as_utf8_str()));
|
|
58
|
-
let title = tag
|
|
59
|
-
.attributes()
|
|
60
|
-
.get("title")
|
|
61
|
-
.flatten()
|
|
62
|
-
.map(|v| v.as_utf8_str().to_string());
|
|
63
|
-
|
|
64
|
-
if let Some(href) = href_attr {
|
|
65
|
-
let raw_text = text::normalize_whitespace(&get_text_content(node_handle, parser, dom_ctx))
|
|
66
|
-
.trim()
|
|
67
|
-
.to_string();
|
|
68
|
-
|
|
69
|
-
let is_autolink = options.autolinks
|
|
70
|
-
&& !options.default_title
|
|
71
|
-
&& !href.is_empty()
|
|
72
|
-
&& (raw_text == href || (href.starts_with("mailto:") && raw_text == href[7..]));
|
|
73
|
-
|
|
74
|
-
if is_autolink {
|
|
75
|
-
output.push('<');
|
|
76
|
-
if href.starts_with("mailto:") && raw_text == href[7..] {
|
|
77
|
-
output.push_str(&raw_text);
|
|
78
|
-
} else {
|
|
79
|
-
output.push_str(&href);
|
|
80
|
-
}
|
|
81
|
-
output.push('>');
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if let Some((heading_level, heading_handle)) = find_single_heading_child(*node_handle, parser) {
|
|
86
|
-
if let Some(heading_node) = heading_handle.get(parser) {
|
|
87
|
-
if let tl::Node::Tag(heading_tag) = heading_node {
|
|
88
|
-
let heading_name = normalized_tag_name(heading_tag.name().as_utf8_str()).into_owned();
|
|
89
|
-
let mut heading_text = String::new();
|
|
90
|
-
let heading_ctx = Context {
|
|
91
|
-
in_heading: true,
|
|
92
|
-
convert_as_inline: true,
|
|
93
|
-
heading_allow_inline_images: heading_allows_inline_images(
|
|
94
|
-
&heading_name,
|
|
95
|
-
&ctx.keep_inline_images_in,
|
|
96
|
-
),
|
|
97
|
-
..ctx.clone()
|
|
98
|
-
};
|
|
99
|
-
walk_node(
|
|
100
|
-
&heading_handle,
|
|
101
|
-
parser,
|
|
102
|
-
&mut heading_text,
|
|
103
|
-
options,
|
|
104
|
-
&heading_ctx,
|
|
105
|
-
depth + 1,
|
|
106
|
-
dom_ctx,
|
|
107
|
-
);
|
|
108
|
-
let trimmed_heading = heading_text.trim();
|
|
109
|
-
if !trimmed_heading.is_empty() {
|
|
110
|
-
let escaped_label = escape_link_label(trimmed_heading);
|
|
111
|
-
let mut link_buffer = String::new();
|
|
112
|
-
append_markdown_link(
|
|
113
|
-
&mut link_buffer,
|
|
114
|
-
&escaped_label,
|
|
115
|
-
href.as_str(),
|
|
116
|
-
title.as_deref(),
|
|
117
|
-
raw_text.as_str(),
|
|
118
|
-
options,
|
|
119
|
-
ctx.reference_collector.as_ref(),
|
|
120
|
-
);
|
|
121
|
-
push_heading(output, ctx, options, heading_level, link_buffer.as_str());
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
let children: Vec<_> = tag.children().top().iter().copied().collect();
|
|
129
|
-
let (inline_label, _block_nodes, saw_block) = collect_link_label_text(&children, parser, dom_ctx);
|
|
130
|
-
let mut label = if saw_block {
|
|
131
|
-
let mut content = String::new();
|
|
132
|
-
let link_ctx = Context {
|
|
133
|
-
inline_depth: ctx.inline_depth + 1,
|
|
134
|
-
convert_as_inline: true,
|
|
135
|
-
..ctx.clone()
|
|
136
|
-
};
|
|
137
|
-
for child_handle in &children {
|
|
138
|
-
let mut child_buf = String::new();
|
|
139
|
-
walk_node(
|
|
140
|
-
child_handle,
|
|
141
|
-
parser,
|
|
142
|
-
&mut child_buf,
|
|
143
|
-
options,
|
|
144
|
-
&link_ctx,
|
|
145
|
-
depth + 1,
|
|
146
|
-
dom_ctx,
|
|
147
|
-
);
|
|
148
|
-
if !child_buf.trim().is_empty()
|
|
149
|
-
&& !content.is_empty()
|
|
150
|
-
&& !content.chars().last().is_none_or(char::is_whitespace)
|
|
151
|
-
&& !child_buf.chars().next().is_none_or(char::is_whitespace)
|
|
152
|
-
{
|
|
153
|
-
content.push(' ');
|
|
154
|
-
}
|
|
155
|
-
content.push_str(&child_buf);
|
|
156
|
-
}
|
|
157
|
-
if content.trim().is_empty() {
|
|
158
|
-
normalize_link_label(&inline_label)
|
|
159
|
-
} else {
|
|
160
|
-
normalize_link_label(&content)
|
|
161
|
-
}
|
|
162
|
-
} else {
|
|
163
|
-
let mut content = String::new();
|
|
164
|
-
let link_ctx = Context {
|
|
165
|
-
inline_depth: ctx.inline_depth + 1,
|
|
166
|
-
..ctx.clone()
|
|
167
|
-
};
|
|
168
|
-
for child_handle in &children {
|
|
169
|
-
walk_node(
|
|
170
|
-
child_handle,
|
|
171
|
-
parser,
|
|
172
|
-
&mut content,
|
|
173
|
-
options,
|
|
174
|
-
&link_ctx,
|
|
175
|
-
depth + 1,
|
|
176
|
-
dom_ctx,
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
normalize_link_label(&content)
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
if label.is_empty() && saw_block {
|
|
183
|
-
let fallback = text::normalize_whitespace(&get_text_content(node_handle, parser, dom_ctx));
|
|
184
|
-
label = normalize_link_label(&fallback);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if label.is_empty() && !raw_text.is_empty() {
|
|
188
|
-
label = normalize_link_label(&raw_text);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
if label.is_empty() && !href.is_empty() && !children.is_empty() {
|
|
192
|
-
label = href.clone();
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Normalize Wikipedia-style back-reference links: <a href="#cite_ref-N">^</a>
|
|
196
|
-
// These produce `[^](#cite_ref-N)` which is confusing (looks like a footnote).
|
|
197
|
-
// Convert to `[↑](#cite_ref-N)` to avoid ambiguity with markdown footnote syntax.
|
|
198
|
-
if label == "^" && href.starts_with('#') {
|
|
199
|
-
label = "↑".to_string();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
let escaped_label = escape_link_label(&label);
|
|
203
|
-
|
|
204
|
-
#[cfg(feature = "visitor")]
|
|
205
|
-
let link_output = if let Some(ref visitor_handle) = ctx.visitor {
|
|
206
|
-
use crate::visitor::{NodeContext, NodeType, VisitResult};
|
|
207
|
-
|
|
208
|
-
let attributes: BTreeMap<String, String> = collect_tag_attributes(tag);
|
|
209
|
-
|
|
210
|
-
let node_id = node_handle.get_inner();
|
|
211
|
-
let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
|
|
212
|
-
let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
|
|
213
|
-
|
|
214
|
-
let node_ctx = NodeContext {
|
|
215
|
-
node_type: NodeType::Link,
|
|
216
|
-
tag_name: "a".to_string(),
|
|
217
|
-
attributes,
|
|
218
|
-
depth,
|
|
219
|
-
index_in_parent,
|
|
220
|
-
parent_tag,
|
|
221
|
-
is_inline: true,
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
let visit_result = {
|
|
225
|
-
let mut visitor = visitor_handle.borrow_mut();
|
|
226
|
-
visitor.visit_link(&node_ctx, &href, &label, title.as_deref())
|
|
227
|
-
};
|
|
228
|
-
match visit_result {
|
|
229
|
-
VisitResult::Continue => {
|
|
230
|
-
let mut buf = String::new();
|
|
231
|
-
append_markdown_link(
|
|
232
|
-
&mut buf,
|
|
233
|
-
&escaped_label,
|
|
234
|
-
href.as_str(),
|
|
235
|
-
title.as_deref(),
|
|
236
|
-
label.as_str(),
|
|
237
|
-
options,
|
|
238
|
-
ctx.reference_collector.as_ref(),
|
|
239
|
-
);
|
|
240
|
-
Some(buf)
|
|
241
|
-
}
|
|
242
|
-
VisitResult::Custom(custom) => Some(custom),
|
|
243
|
-
VisitResult::Skip => None,
|
|
244
|
-
VisitResult::Error(err) => {
|
|
245
|
-
if ctx.visitor_error.borrow().is_none() {
|
|
246
|
-
*ctx.visitor_error.borrow_mut() = Some(err);
|
|
247
|
-
}
|
|
248
|
-
None
|
|
249
|
-
}
|
|
250
|
-
VisitResult::PreserveHtml => Some(serialize_node(node_handle, parser)),
|
|
251
|
-
}
|
|
252
|
-
} else {
|
|
253
|
-
let mut buf = String::new();
|
|
254
|
-
append_markdown_link(
|
|
255
|
-
&mut buf,
|
|
256
|
-
&escaped_label,
|
|
257
|
-
href.as_str(),
|
|
258
|
-
title.as_deref(),
|
|
259
|
-
label.as_str(),
|
|
260
|
-
options,
|
|
261
|
-
ctx.reference_collector.as_ref(),
|
|
262
|
-
);
|
|
263
|
-
Some(buf)
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
#[cfg(not(feature = "visitor"))]
|
|
267
|
-
let link_output = {
|
|
268
|
-
let mut buf = String::new();
|
|
269
|
-
append_markdown_link(
|
|
270
|
-
&mut buf,
|
|
271
|
-
&escaped_label,
|
|
272
|
-
href.as_str(),
|
|
273
|
-
title.as_deref(),
|
|
274
|
-
label.as_str(),
|
|
275
|
-
options,
|
|
276
|
-
ctx.reference_collector.as_ref(),
|
|
277
|
-
);
|
|
278
|
-
Some(buf)
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
if let Some(link_text) = link_output {
|
|
282
|
-
output.push_str(&link_text);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
#[cfg(feature = "metadata")]
|
|
286
|
-
if ctx.metadata_wants_links {
|
|
287
|
-
if let Some(ref collector) = ctx.metadata_collector {
|
|
288
|
-
let rel_attr = tag
|
|
289
|
-
.attributes()
|
|
290
|
-
.get("rel")
|
|
291
|
-
.flatten()
|
|
292
|
-
.map(|v| v.as_utf8_str().to_string());
|
|
293
|
-
let mut attributes_map = BTreeMap::new();
|
|
294
|
-
for (key, value_opt) in tag.attributes().iter() {
|
|
295
|
-
let key_str = key.to_string();
|
|
296
|
-
if key_str == "href" {
|
|
297
|
-
continue;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
let value = value_opt.map(|v| v.to_string()).unwrap_or_default();
|
|
301
|
-
attributes_map.insert(key_str, value);
|
|
302
|
-
}
|
|
303
|
-
collector
|
|
304
|
-
.borrow_mut()
|
|
305
|
-
.add_link(href.clone(), label, title.clone(), rel_attr, attributes_map);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
} else {
|
|
309
|
-
let children = tag.children();
|
|
310
|
-
{
|
|
311
|
-
for child_handle in children.top().iter() {
|
|
312
|
-
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
//! Element handlers extracted from the main conversion pipeline.
|
|
2
|
-
//!
|
|
3
|
-
//! This module contains handler functions for specific HTML elements,
|
|
4
|
-
//! allowing the main walk_node function to delegate to specialized handlers.
|
|
5
|
-
//!
|
|
6
|
-
//! Each handler takes the standard set of parameters:
|
|
7
|
-
//! - `node_handle`: Reference to the DOM node
|
|
8
|
-
//! - `tag`: The HTML tag being processed
|
|
9
|
-
//! - `parser`: The DOM parser
|
|
10
|
-
//! - `output`: The output string buffer
|
|
11
|
-
//! - `options`: Conversion options
|
|
12
|
-
//! - `ctx`: Conversion context
|
|
13
|
-
//! - `depth`: Current tree depth
|
|
14
|
-
//! - `dom_ctx`: DOM context cache
|
|
15
|
-
|
|
16
|
-
pub mod blockquote;
|
|
17
|
-
pub mod code_block;
|
|
18
|
-
pub mod graphic;
|
|
19
|
-
pub mod image;
|
|
20
|
-
pub mod link;
|
|
21
|
-
|
|
22
|
-
pub use blockquote::handle_blockquote;
|
|
23
|
-
pub use code_block::{handle_code, handle_pre};
|
|
24
|
-
pub use graphic::handle_graphic;
|
|
25
|
-
pub use image::handle_img;
|
|
26
|
-
pub use link::handle_link;
|