html-to-markdown 3.4.0 → 3.6.0.pre.rc.23
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,452 +0,0 @@
|
|
|
1
|
-
//! Helper functions for HTML to Markdown conversion.
|
|
2
|
-
//!
|
|
3
|
-
//! This module contains utility functions used by the main conversion pipeline,
|
|
4
|
-
//! including preprocessing helpers, HTML repair, and metadata formatting.
|
|
5
|
-
|
|
6
|
-
use std::collections::BTreeMap;
|
|
7
|
-
|
|
8
|
-
use crate::options::ConversionOptions;
|
|
9
|
-
|
|
10
|
-
/// Compare two tag names case-insensitively.
|
|
11
|
-
pub fn tag_name_eq(a: impl AsRef<str>, b: &str) -> bool {
|
|
12
|
-
a.as_ref().eq_ignore_ascii_case(b)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/// Remove trailing spaces and tabs from a string.
|
|
16
|
-
pub fn trim_trailing_whitespace(output: &mut String) {
|
|
17
|
-
while output.ends_with(' ') || output.ends_with('\t') {
|
|
18
|
-
output.pop();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/// Remove trailing spaces/tabs from every line while preserving newlines.
|
|
23
|
-
pub fn trim_line_end_whitespace(output: &mut String) {
|
|
24
|
-
if output.is_empty() {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let mut cleaned = String::with_capacity(output.len());
|
|
29
|
-
for (idx, line) in output.split('\n').enumerate() {
|
|
30
|
-
if idx > 0 {
|
|
31
|
-
cleaned.push('\n');
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let has_soft_break = line.ends_with(" ");
|
|
35
|
-
let trimmed = line.trim_end_matches([' ', '\t']);
|
|
36
|
-
|
|
37
|
-
cleaned.push_str(trimmed);
|
|
38
|
-
if has_soft_break {
|
|
39
|
-
cleaned.push_str(" ");
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
cleaned.push('\n');
|
|
44
|
-
*output = cleaned;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// has_inline_block_misnest and should_drop_for_preprocessing moved back to main.rs
|
|
48
|
-
// due to DomContext circular dependency
|
|
49
|
-
|
|
50
|
-
/// Check if HTML contains custom element tags.
|
|
51
|
-
pub fn has_custom_element_tags(html: &str) -> bool {
|
|
52
|
-
// Custom elements must have a hyphen in their TAG NAME, not in attributes
|
|
53
|
-
// Look for patterns like <foo-bar> or </foo-bar>
|
|
54
|
-
let bytes = html.as_bytes();
|
|
55
|
-
let len = bytes.len();
|
|
56
|
-
let mut i = 0;
|
|
57
|
-
|
|
58
|
-
while i < len {
|
|
59
|
-
if bytes[i] == b'<' {
|
|
60
|
-
i += 1;
|
|
61
|
-
if i >= len {
|
|
62
|
-
break;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Skip closing tag marker
|
|
66
|
-
if bytes[i] == b'/' {
|
|
67
|
-
i += 1;
|
|
68
|
-
if i >= len {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Skip whitespace
|
|
74
|
-
while i < len && bytes[i].is_ascii_whitespace() {
|
|
75
|
-
i += 1;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// Now we're at the start of a tag name - check if it contains a hyphen
|
|
79
|
-
let tag_start = i;
|
|
80
|
-
while i < len {
|
|
81
|
-
let ch = bytes[i];
|
|
82
|
-
if ch == b'>' || ch == b'/' || ch.is_ascii_whitespace() {
|
|
83
|
-
// End of tag name
|
|
84
|
-
let tag_name = &bytes[tag_start..i];
|
|
85
|
-
if tag_name.contains(&b'-') {
|
|
86
|
-
return true;
|
|
87
|
-
}
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
i += 1;
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
i += 1;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
false
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/// HTML5 void elements that are self-closing by spec and must NOT be expanded.
|
|
101
|
-
///
|
|
102
|
-
/// These elements are always void in HTML5: they have no end tag, and `<br />` is
|
|
103
|
-
/// equivalent to `<br>`. We must leave them as-is when pre-processing XML-style
|
|
104
|
-
/// self-closing syntax so that `repair_with_html5ever` can parse them correctly.
|
|
105
|
-
const HTML5_VOID_ELEMENTS: &[&str] = &[
|
|
106
|
-
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr",
|
|
107
|
-
];
|
|
108
|
-
|
|
109
|
-
/// Expand XML-style self-closing tags to explicit open+close pairs.
|
|
110
|
-
///
|
|
111
|
-
/// HTML5 does not honour the `/>` self-close syntax for non-void elements. When
|
|
112
|
-
/// `repair_with_html5ever` re-parses content that contains custom / namespaced tags
|
|
113
|
-
/// written as `<ac:parameter name="foo" />`, the HTML5 parser treats the `/>` as `>`
|
|
114
|
-
/// and leaves the element open. Subsequent siblings then nest inside it, breaking
|
|
115
|
-
/// visitor pre-order/post-order start/end pairing.
|
|
116
|
-
///
|
|
117
|
-
/// This function scans the input byte-by-byte and rewrites any `<tag ... />` where
|
|
118
|
-
/// `tag` is not a known HTML5 void element into `<tag ...></tag>`. Known void
|
|
119
|
-
/// elements are left unchanged because they must not receive an explicit close tag.
|
|
120
|
-
///
|
|
121
|
-
/// # Correctness guarantees
|
|
122
|
-
/// - Non-ASCII bytes are never interpreted as structural characters; all multi-byte
|
|
123
|
-
/// UTF-8 sequences pass through unmodified via `&input[byte_offset..]` slicing.
|
|
124
|
-
/// - Attribute values containing `/>` are skipped correctly (the scanner tracks
|
|
125
|
-
/// whether it is inside a quoted attribute).
|
|
126
|
-
/// - `</closing>` tags are never modified.
|
|
127
|
-
/// - The function is pure and returns a new `String`; if no substitution is needed
|
|
128
|
-
/// the allocation is still performed (cheap given repair is already rare).
|
|
129
|
-
pub fn expand_xml_self_closing_tags(input: &str) -> String {
|
|
130
|
-
let bytes = input.as_bytes();
|
|
131
|
-
let len = bytes.len();
|
|
132
|
-
let mut output = String::with_capacity(len);
|
|
133
|
-
// `copy_start` tracks the beginning of a contiguous span of unmodified input
|
|
134
|
-
// that should be copied verbatim to `output`.
|
|
135
|
-
let mut copy_start = 0usize;
|
|
136
|
-
let mut i = 0;
|
|
137
|
-
|
|
138
|
-
while i < len {
|
|
139
|
-
if bytes[i] != b'<' {
|
|
140
|
-
i += 1;
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// We are at `<`. Flush the unmodified span up to (but not including) this `<`.
|
|
145
|
-
let tag_open = i;
|
|
146
|
-
i += 1;
|
|
147
|
-
|
|
148
|
-
// Skip closing tags entirely — they must not be modified.
|
|
149
|
-
if i < len && bytes[i] == b'/' {
|
|
150
|
-
// Scan to the matching `>`.
|
|
151
|
-
while i < len && bytes[i] != b'>' {
|
|
152
|
-
i += 1;
|
|
153
|
-
}
|
|
154
|
-
if i < len {
|
|
155
|
-
i += 1; // consume `>`
|
|
156
|
-
}
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Skip leading whitespace after `<` (unusual but tolerated).
|
|
161
|
-
while i < len && bytes[i].is_ascii_whitespace() {
|
|
162
|
-
i += 1;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Collect the tag name (byte-aligned; tag names are always ASCII).
|
|
166
|
-
let name_start = i;
|
|
167
|
-
while i < len {
|
|
168
|
-
let ch = bytes[i];
|
|
169
|
-
if ch == b'>' || ch == b'/' || ch.is_ascii_whitespace() {
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
i += 1;
|
|
173
|
-
}
|
|
174
|
-
let tag_name_bytes = &bytes[name_start..i];
|
|
175
|
-
|
|
176
|
-
// Empty tag name — emit verbatim and continue.
|
|
177
|
-
if tag_name_bytes.is_empty() {
|
|
178
|
-
continue;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// Check whether this is a known HTML5 void element (case-insensitive).
|
|
182
|
-
let tag_name_lower = tag_name_bytes.iter().map(u8::to_ascii_lowercase).collect::<Vec<_>>();
|
|
183
|
-
let is_void = HTML5_VOID_ELEMENTS
|
|
184
|
-
.iter()
|
|
185
|
-
.any(|v| v.as_bytes() == tag_name_lower.as_slice());
|
|
186
|
-
|
|
187
|
-
// Scan the rest of the tag to find `/>` or `>`, skipping quoted attrs.
|
|
188
|
-
let attrs_start = i;
|
|
189
|
-
let mut in_single_quote = false;
|
|
190
|
-
let mut in_double_quote = false;
|
|
191
|
-
let mut self_closing = false;
|
|
192
|
-
|
|
193
|
-
while i < len {
|
|
194
|
-
match bytes[i] {
|
|
195
|
-
b'"' if !in_single_quote => {
|
|
196
|
-
in_double_quote = !in_double_quote;
|
|
197
|
-
i += 1;
|
|
198
|
-
}
|
|
199
|
-
b'\'' if !in_double_quote => {
|
|
200
|
-
in_single_quote = !in_single_quote;
|
|
201
|
-
i += 1;
|
|
202
|
-
}
|
|
203
|
-
b'/' if !in_single_quote && !in_double_quote => {
|
|
204
|
-
if i + 1 < len && bytes[i + 1] == b'>' {
|
|
205
|
-
self_closing = true;
|
|
206
|
-
break;
|
|
207
|
-
}
|
|
208
|
-
i += 1;
|
|
209
|
-
}
|
|
210
|
-
b'>' if !in_single_quote && !in_double_quote => {
|
|
211
|
-
break;
|
|
212
|
-
}
|
|
213
|
-
_ => {
|
|
214
|
-
i += 1;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if self_closing && !is_void {
|
|
220
|
-
// Flush unchanged input up to (not including) this tag.
|
|
221
|
-
output.push_str(&input[copy_start..tag_open]);
|
|
222
|
-
|
|
223
|
-
let tag_name_str = std::str::from_utf8(tag_name_bytes).unwrap_or("");
|
|
224
|
-
// attrs_part covers everything between the end of the tag name and `/>`,
|
|
225
|
-
// i.e. `&input[attrs_start..i]` (the `/` at `i` is the start of `/>`)
|
|
226
|
-
let attrs_part = &input[attrs_start..i];
|
|
227
|
-
|
|
228
|
-
// Non-void: expand `<tag attrs/>` → `<tag attrs></tag>`.
|
|
229
|
-
output.push('<');
|
|
230
|
-
output.push_str(tag_name_str);
|
|
231
|
-
output.push_str(attrs_part);
|
|
232
|
-
output.push('>');
|
|
233
|
-
output.push('<');
|
|
234
|
-
output.push('/');
|
|
235
|
-
output.push_str(tag_name_str);
|
|
236
|
-
output.push('>');
|
|
237
|
-
|
|
238
|
-
i += 2; // consume `/>`
|
|
239
|
-
copy_start = i;
|
|
240
|
-
} else {
|
|
241
|
-
// Not a self-closing non-void tag: advance past `/>` or `>`.
|
|
242
|
-
if i < len && bytes[i] == b'/' {
|
|
243
|
-
i += 2; // skip `/>`
|
|
244
|
-
} else if i < len && bytes[i] == b'>' {
|
|
245
|
-
i += 1;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Flush the remaining unchanged tail.
|
|
251
|
-
output.push_str(&input[copy_start..]);
|
|
252
|
-
output
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/// Try to repair HTML using html5ever parser.
|
|
256
|
-
///
|
|
257
|
-
/// Returns Some(repaired_html) if repair was successful, None otherwise.
|
|
258
|
-
///
|
|
259
|
-
/// Before feeding the input to the HTML5 parser, XML-style self-closing tags on
|
|
260
|
-
/// non-void elements (e.g. `<ac:parameter name="foo" />`) are expanded to explicit
|
|
261
|
-
/// open+close pairs. This preserves the intended document structure because HTML5
|
|
262
|
-
/// semantics do not honour `/>` on unknown elements — without the expansion, the
|
|
263
|
-
/// element would be left open and subsequent siblings would nest inside it, breaking
|
|
264
|
-
/// visitor start/end event pairing (issue #331).
|
|
265
|
-
pub fn repair_with_html5ever(input: &str) -> Option<String> {
|
|
266
|
-
use crate::rcdom::{RcDom, SerializableHandle};
|
|
267
|
-
use html5ever::serialize::{SerializeOpts, serialize};
|
|
268
|
-
use html5ever::tendril::TendrilSink;
|
|
269
|
-
|
|
270
|
-
// Expand XML-style self-closing on non-void elements before the HTML5 parse so
|
|
271
|
-
// that `<ac:parameter ... />` is not silently left open by the HTML5 parser.
|
|
272
|
-
let expanded = expand_xml_self_closing_tags(input);
|
|
273
|
-
|
|
274
|
-
let dom = html5ever::parse_document(RcDom::default(), Default::default())
|
|
275
|
-
.from_utf8()
|
|
276
|
-
.read_from(&mut expanded.as_bytes())
|
|
277
|
-
.ok()?;
|
|
278
|
-
|
|
279
|
-
let mut buf = Vec::with_capacity(input.len());
|
|
280
|
-
let handle = SerializableHandle::from(dom.document);
|
|
281
|
-
serialize(&mut buf, &handle, SerializeOpts::default()).ok()?;
|
|
282
|
-
String::from_utf8(buf).ok()
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/// Format metadata as YAML frontmatter.
|
|
286
|
-
pub fn format_metadata_frontmatter(metadata: &BTreeMap<String, String>) -> String {
|
|
287
|
-
let mut result = String::from("---\n");
|
|
288
|
-
for (key, value) in metadata {
|
|
289
|
-
use std::fmt::Write as _;
|
|
290
|
-
let _ = writeln!(&mut result, "{}: {}", key, value);
|
|
291
|
-
}
|
|
292
|
-
result.push_str("---\n");
|
|
293
|
-
result
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// should_drop_for_preprocessing moved back to main.rs due to DomContext dependency
|
|
297
|
-
|
|
298
|
-
/// Extract metadata from the head element.
|
|
299
|
-
pub fn extract_head_metadata(
|
|
300
|
-
node_handle: &tl::NodeHandle,
|
|
301
|
-
parser: &tl::Parser,
|
|
302
|
-
options: &ConversionOptions,
|
|
303
|
-
) -> BTreeMap<String, String> {
|
|
304
|
-
let mut metadata = BTreeMap::new();
|
|
305
|
-
|
|
306
|
-
if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
|
|
307
|
-
// Check if this is a head tag
|
|
308
|
-
if tag.name().as_utf8_str().eq_ignore_ascii_case("head") {
|
|
309
|
-
let children = tag.children();
|
|
310
|
-
for child_handle in children.top().iter() {
|
|
311
|
-
if let Some(tl::Node::Tag(child_tag)) = child_handle.get(parser) {
|
|
312
|
-
// Look for meta tags
|
|
313
|
-
if child_tag.name().as_utf8_str().eq_ignore_ascii_case("meta")
|
|
314
|
-
&& !options.strip_tags.iter().any(|t| t == "meta")
|
|
315
|
-
&& !options.preserve_tags.iter().any(|t| t == "meta")
|
|
316
|
-
{
|
|
317
|
-
if let (Some(name), Some(content)) = (
|
|
318
|
-
child_tag.attributes().get("name").flatten(),
|
|
319
|
-
child_tag.attributes().get("content").flatten(),
|
|
320
|
-
) {
|
|
321
|
-
let name_str = name.as_utf8_str();
|
|
322
|
-
let content_str = content.as_utf8_str();
|
|
323
|
-
metadata.insert(format!("meta-{}", name_str), content_str.to_string());
|
|
324
|
-
}
|
|
325
|
-
// Also check for property attribute (Open Graph, etc.)
|
|
326
|
-
if let (Some(property), Some(content)) = (
|
|
327
|
-
child_tag.attributes().get("property").flatten(),
|
|
328
|
-
child_tag.attributes().get("content").flatten(),
|
|
329
|
-
) {
|
|
330
|
-
let property_str = property.as_utf8_str();
|
|
331
|
-
let content_str = content.as_utf8_str();
|
|
332
|
-
metadata.insert(format!("meta-{}", property_str), content_str.to_string());
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
// Look for title tag
|
|
336
|
-
if child_tag.name().as_utf8_str().eq_ignore_ascii_case("title")
|
|
337
|
-
&& !options.strip_tags.iter().any(|t| t == "title")
|
|
338
|
-
&& !options.preserve_tags.iter().any(|t| t == "title")
|
|
339
|
-
{
|
|
340
|
-
// Extract text content from title tag
|
|
341
|
-
let mut title_content = String::new();
|
|
342
|
-
let title_children = child_tag.children();
|
|
343
|
-
for title_child in title_children.top().iter() {
|
|
344
|
-
if let Some(tl::Node::Raw(raw)) = title_child.get(parser) {
|
|
345
|
-
title_content.push_str(raw.as_utf8_str().as_ref());
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
title_content = title_content.trim().to_string();
|
|
349
|
-
if !title_content.is_empty() {
|
|
350
|
-
metadata.insert("title".to_string(), title_content);
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
// Look for link tags with rel attribute (e.g., canonical)
|
|
354
|
-
if child_tag.name().as_utf8_str().eq_ignore_ascii_case("link") {
|
|
355
|
-
if let Some(rel_attr) = child_tag.attributes().get("rel").flatten() {
|
|
356
|
-
let rel_str = rel_attr.as_utf8_str();
|
|
357
|
-
// Check for canonical link
|
|
358
|
-
if rel_str.contains("canonical") {
|
|
359
|
-
if let Some(href_attr) = child_tag.attributes().get("href").flatten() {
|
|
360
|
-
let href_str = href_attr.as_utf8_str();
|
|
361
|
-
metadata.insert("canonical".to_string(), href_str.to_string());
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
// Look for base tag with href attribute
|
|
367
|
-
if child_tag.name().as_utf8_str().eq_ignore_ascii_case("base") {
|
|
368
|
-
if let Some(href_attr) = child_tag.attributes().get("href").flatten() {
|
|
369
|
-
let href_str = href_attr.as_utf8_str();
|
|
370
|
-
// Store as "base" which will be mapped to base_href in extract_document_metadata
|
|
371
|
-
metadata.insert("base".to_string(), href_str.to_string());
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
} else {
|
|
377
|
-
// If this is not a head tag, recursively search children for head tag
|
|
378
|
-
let children = tag.children();
|
|
379
|
-
for child_handle in children.top().iter() {
|
|
380
|
-
let child_metadata = extract_head_metadata(child_handle, parser, options);
|
|
381
|
-
if !child_metadata.is_empty() {
|
|
382
|
-
metadata.extend(child_metadata);
|
|
383
|
-
break; // Only process first head tag found
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
metadata
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/// Check if text has more than one character.
|
|
393
|
-
pub fn has_more_than_one_char(text: &str) -> bool {
|
|
394
|
-
let mut chars = text.chars();
|
|
395
|
-
chars.next().is_some() && chars.next().is_some()
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/// Check if an element is inline (not block-level).
|
|
399
|
-
pub fn is_inline_element(tag_name: &str) -> bool {
|
|
400
|
-
matches!(
|
|
401
|
-
tag_name,
|
|
402
|
-
"a" | "abbr"
|
|
403
|
-
| "b"
|
|
404
|
-
| "bdi"
|
|
405
|
-
| "bdo"
|
|
406
|
-
| "br"
|
|
407
|
-
| "cite"
|
|
408
|
-
| "code"
|
|
409
|
-
| "data"
|
|
410
|
-
| "dfn"
|
|
411
|
-
| "em"
|
|
412
|
-
| "i"
|
|
413
|
-
| "kbd"
|
|
414
|
-
| "mark"
|
|
415
|
-
| "q"
|
|
416
|
-
| "rp"
|
|
417
|
-
| "rt"
|
|
418
|
-
| "ruby"
|
|
419
|
-
| "s"
|
|
420
|
-
| "samp"
|
|
421
|
-
| "small"
|
|
422
|
-
| "span"
|
|
423
|
-
| "strong"
|
|
424
|
-
| "sub"
|
|
425
|
-
| "sup"
|
|
426
|
-
| "time"
|
|
427
|
-
| "u"
|
|
428
|
-
| "var"
|
|
429
|
-
| "wbr"
|
|
430
|
-
| "del"
|
|
431
|
-
| "ins"
|
|
432
|
-
| "img"
|
|
433
|
-
| "map"
|
|
434
|
-
| "area"
|
|
435
|
-
| "audio"
|
|
436
|
-
| "video"
|
|
437
|
-
| "picture"
|
|
438
|
-
| "source"
|
|
439
|
-
| "track"
|
|
440
|
-
| "embed"
|
|
441
|
-
| "object"
|
|
442
|
-
| "param"
|
|
443
|
-
| "input"
|
|
444
|
-
| "label"
|
|
445
|
-
| "button"
|
|
446
|
-
| "select"
|
|
447
|
-
| "textarea"
|
|
448
|
-
| "output"
|
|
449
|
-
| "progress"
|
|
450
|
-
| "meter"
|
|
451
|
-
)
|
|
452
|
-
}
|