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,100 +0,0 @@
|
|
|
1
|
-
//! Example: Converting HTML tables to Markdown
|
|
2
|
-
|
|
3
|
-
fn convert(
|
|
4
|
-
html: &str,
|
|
5
|
-
opts: Option<html_to_markdown_rs::ConversionOptions>,
|
|
6
|
-
) -> html_to_markdown_rs::error::Result<String> {
|
|
7
|
-
html_to_markdown_rs::convert(html, opts).map(|r| r.content.unwrap_or_default())
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
fn main() {
|
|
11
|
-
let html = r"<table>
|
|
12
|
-
<tr>
|
|
13
|
-
<th>Name</th>
|
|
14
|
-
<th>Age</th>
|
|
15
|
-
</tr>
|
|
16
|
-
<tr>
|
|
17
|
-
<td>Alice</td>
|
|
18
|
-
<td>30</td>
|
|
19
|
-
</tr>
|
|
20
|
-
<tr>
|
|
21
|
-
<td>Bob</td>
|
|
22
|
-
<td>25</td>
|
|
23
|
-
</tr>
|
|
24
|
-
</table>";
|
|
25
|
-
|
|
26
|
-
match convert(html, None) {
|
|
27
|
-
Ok(markdown) => {
|
|
28
|
-
println!("Test - Simple table with header:");
|
|
29
|
-
println!("HTML: {html}");
|
|
30
|
-
println!("\nMarkdown:\n{markdown}");
|
|
31
|
-
println!("Expected:");
|
|
32
|
-
println!("| Name | Age |");
|
|
33
|
-
println!("| --- | --- |");
|
|
34
|
-
println!("| Alice | 30 |");
|
|
35
|
-
println!("| Bob | 25 |");
|
|
36
|
-
println!();
|
|
37
|
-
}
|
|
38
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let html2 = r#"<table>
|
|
42
|
-
<tr>
|
|
43
|
-
<th colspan="2">Full Name</th>
|
|
44
|
-
<th>Age</th>
|
|
45
|
-
</tr>
|
|
46
|
-
<tr>
|
|
47
|
-
<td>Alice</td>
|
|
48
|
-
<td>Smith</td>
|
|
49
|
-
<td>30</td>
|
|
50
|
-
</tr>
|
|
51
|
-
</table>"#;
|
|
52
|
-
|
|
53
|
-
match convert(html2, None) {
|
|
54
|
-
Ok(markdown) => {
|
|
55
|
-
println!("Test - Table with colspan:");
|
|
56
|
-
println!("HTML: {html2}");
|
|
57
|
-
println!("\nMarkdown:\n{markdown}");
|
|
58
|
-
println!("Expected:");
|
|
59
|
-
println!("| Full Name | | Age |");
|
|
60
|
-
println!("| --- | --- | --- |");
|
|
61
|
-
println!("| Alice | Smith | 30 |");
|
|
62
|
-
println!();
|
|
63
|
-
}
|
|
64
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let html3 = r"<table>
|
|
68
|
-
<thead>
|
|
69
|
-
<tr>
|
|
70
|
-
<th>Product</th>
|
|
71
|
-
<th>Price</th>
|
|
72
|
-
</tr>
|
|
73
|
-
</thead>
|
|
74
|
-
<tbody>
|
|
75
|
-
<tr>
|
|
76
|
-
<td>Widget</td>
|
|
77
|
-
<td>$10</td>
|
|
78
|
-
</tr>
|
|
79
|
-
<tr>
|
|
80
|
-
<td>Gadget</td>
|
|
81
|
-
<td>$15</td>
|
|
82
|
-
</tr>
|
|
83
|
-
</tbody>
|
|
84
|
-
</table>";
|
|
85
|
-
|
|
86
|
-
match convert(html3, None) {
|
|
87
|
-
Ok(markdown) => {
|
|
88
|
-
println!("Test - Table with thead/tbody:");
|
|
89
|
-
println!("HTML: {html3}");
|
|
90
|
-
println!("\nMarkdown:\n{markdown}");
|
|
91
|
-
println!("Expected:");
|
|
92
|
-
println!("| Product | Price |");
|
|
93
|
-
println!("| --- | --- |");
|
|
94
|
-
println!("| Widget | $10 |");
|
|
95
|
-
println!("| Gadget | $15 |");
|
|
96
|
-
println!();
|
|
97
|
-
}
|
|
98
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
99
|
-
}
|
|
100
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
//! Example: Testing task list conversion (checkboxes)
|
|
2
|
-
|
|
3
|
-
fn convert(
|
|
4
|
-
html: &str,
|
|
5
|
-
opts: Option<html_to_markdown_rs::ConversionOptions>,
|
|
6
|
-
) -> html_to_markdown_rs::error::Result<String> {
|
|
7
|
-
html_to_markdown_rs::convert(html, opts).map(|r| r.content.unwrap_or_default())
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
fn main() {
|
|
11
|
-
let html = r#"<ul>
|
|
12
|
-
<li><input type="checkbox"> Unchecked task</li>
|
|
13
|
-
<li><input type="checkbox" checked> Checked task</li>
|
|
14
|
-
<li>Regular list item</li>
|
|
15
|
-
</ul>"#;
|
|
16
|
-
|
|
17
|
-
match convert(html, None) {
|
|
18
|
-
Ok(markdown) => {
|
|
19
|
-
println!("Test - Task list:");
|
|
20
|
-
println!("HTML: {html}");
|
|
21
|
-
println!("\nMarkdown:\n{markdown}");
|
|
22
|
-
println!("Expected:");
|
|
23
|
-
println!("- [ ] Unchecked task");
|
|
24
|
-
println!("- [x] Checked task");
|
|
25
|
-
println!("- Regular list item");
|
|
26
|
-
println!();
|
|
27
|
-
}
|
|
28
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
let html2 = r#"<ul>
|
|
32
|
-
<li><input type="checkbox"> Parent task
|
|
33
|
-
<ul>
|
|
34
|
-
<li><input type="checkbox" checked> Child task</li>
|
|
35
|
-
</ul>
|
|
36
|
-
</li>
|
|
37
|
-
</ul>"#;
|
|
38
|
-
|
|
39
|
-
match convert(html2, None) {
|
|
40
|
-
Ok(markdown) => {
|
|
41
|
-
println!("Test - Nested task list:");
|
|
42
|
-
println!("HTML: {html2}");
|
|
43
|
-
println!("\nMarkdown:\n{markdown}");
|
|
44
|
-
println!();
|
|
45
|
-
}
|
|
46
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
let html3 = r"<ruby>東京<rt>とうきょう</rt></ruby>";
|
|
50
|
-
|
|
51
|
-
match convert(html3, None) {
|
|
52
|
-
Ok(markdown) => {
|
|
53
|
-
println!("Test - Ruby annotation:");
|
|
54
|
-
println!("HTML: {html3}");
|
|
55
|
-
println!("Markdown: {markdown}");
|
|
56
|
-
println!("Expected: 東京 (とうきょう)");
|
|
57
|
-
println!();
|
|
58
|
-
}
|
|
59
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
60
|
-
}
|
|
61
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
//! Example: Testing whitespace handling and normalization
|
|
2
|
-
|
|
3
|
-
fn convert(
|
|
4
|
-
html: &str,
|
|
5
|
-
opts: Option<html_to_markdown_rs::ConversionOptions>,
|
|
6
|
-
) -> html_to_markdown_rs::error::Result<String> {
|
|
7
|
-
html_to_markdown_rs::convert(html, opts).map(|r| r.content.unwrap_or_default())
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
fn main() {
|
|
11
|
-
let html = "<p>text with multiple spaces</p>";
|
|
12
|
-
match convert(html, None) {
|
|
13
|
-
Ok(markdown) => {
|
|
14
|
-
println!("Test - Multiple spaces:");
|
|
15
|
-
println!("HTML: {html}");
|
|
16
|
-
println!("Markdown: {markdown}");
|
|
17
|
-
println!("Expected: text with multiple spaces");
|
|
18
|
-
println!();
|
|
19
|
-
}
|
|
20
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let html2 = "<p>text\nwith\nnewlines</p>";
|
|
24
|
-
match convert(html2, None) {
|
|
25
|
-
Ok(markdown) => {
|
|
26
|
-
println!("Test - Newlines:");
|
|
27
|
-
println!("HTML: {html2}");
|
|
28
|
-
println!("Markdown: {markdown}");
|
|
29
|
-
println!("Expected: text with newlines");
|
|
30
|
-
println!();
|
|
31
|
-
}
|
|
32
|
-
Err(e) => eprintln!("Error: {e}"),
|
|
33
|
-
}
|
|
34
|
-
}
|
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
//! Main HTML to Markdown conversion API.
|
|
2
|
-
//!
|
|
3
|
-
//! This module provides the primary `convert()` function for converting HTML to Markdown.
|
|
4
|
-
|
|
5
|
-
use std::borrow::Cow;
|
|
6
|
-
|
|
7
|
-
#[cfg(any(feature = "metadata", feature = "inline-images"))]
|
|
8
|
-
use crate::ConversionError;
|
|
9
|
-
use crate::error::Result;
|
|
10
|
-
use crate::options::{ConversionOptions, WhitespaceMode};
|
|
11
|
-
use crate::text;
|
|
12
|
-
use crate::types::ConversionResult;
|
|
13
|
-
use crate::validation::{Utf16Encoding, detect_utf16_encoding, validate_input};
|
|
14
|
-
|
|
15
|
-
#[cfg(feature = "metadata")]
|
|
16
|
-
use crate::{HtmlMetadata, MetadataConfig};
|
|
17
|
-
|
|
18
|
-
/// Convert HTML to Markdown, returning a [`ConversionResult`] with content, metadata, images,
|
|
19
|
-
/// and warnings.
|
|
20
|
-
///
|
|
21
|
-
/// # Arguments
|
|
22
|
-
///
|
|
23
|
-
/// * `html` — the HTML string to convert.
|
|
24
|
-
/// * `options` — optional conversion options. Defaults to [`ConversionOptions::default`].
|
|
25
|
-
///
|
|
26
|
-
/// # Example
|
|
27
|
-
///
|
|
28
|
-
/// ```
|
|
29
|
-
/// use html_to_markdown_rs::convert;
|
|
30
|
-
///
|
|
31
|
-
/// let html = "<h1>Hello World</h1>";
|
|
32
|
-
/// let result = convert(html, None).unwrap();
|
|
33
|
-
/// assert!(result.content.as_deref().unwrap_or("").contains("Hello World"));
|
|
34
|
-
/// ```
|
|
35
|
-
///
|
|
36
|
-
/// # Errors
|
|
37
|
-
///
|
|
38
|
-
/// Returns an error if HTML parsing fails or if the input contains invalid UTF-8.
|
|
39
|
-
pub fn convert(html: &str, options: Option<ConversionOptions>) -> Result<ConversionResult> {
|
|
40
|
-
#[cfg(any(feature = "metadata", feature = "inline-images"))]
|
|
41
|
-
use std::cell::RefCell;
|
|
42
|
-
#[cfg(any(feature = "metadata", feature = "inline-images"))]
|
|
43
|
-
use std::rc::Rc;
|
|
44
|
-
|
|
45
|
-
let options = options.unwrap_or_default();
|
|
46
|
-
|
|
47
|
-
#[cfg(feature = "visitor")]
|
|
48
|
-
let visitor = options.visitor.clone();
|
|
49
|
-
|
|
50
|
-
let normalized_html = normalize_input(html)?;
|
|
51
|
-
|
|
52
|
-
// Fast path: plain text with no HTML tags — skip full parsing pipeline.
|
|
53
|
-
if !options.wrap {
|
|
54
|
-
if let Some(markdown) = fast_text_only(normalized_html.as_ref(), &options) {
|
|
55
|
-
return Ok(ConversionResult {
|
|
56
|
-
content: Some(markdown),
|
|
57
|
-
..ConversionResult::default()
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Determine whether metadata / inline-image extraction is requested.
|
|
63
|
-
#[cfg(feature = "metadata")]
|
|
64
|
-
let wants_metadata = options.extract_metadata;
|
|
65
|
-
#[cfg(not(feature = "metadata"))]
|
|
66
|
-
let wants_metadata = false;
|
|
67
|
-
|
|
68
|
-
#[cfg(feature = "inline-images")]
|
|
69
|
-
let wants_images = options.extract_images;
|
|
70
|
-
#[cfg(not(feature = "inline-images"))]
|
|
71
|
-
let wants_images = false;
|
|
72
|
-
|
|
73
|
-
// Build optional collectors based on requested features.
|
|
74
|
-
#[cfg(feature = "metadata")]
|
|
75
|
-
let metadata_collector = if wants_metadata {
|
|
76
|
-
Some(Rc::new(RefCell::new(crate::metadata::MetadataCollector::new(
|
|
77
|
-
MetadataConfig::default(),
|
|
78
|
-
))))
|
|
79
|
-
} else {
|
|
80
|
-
None
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
#[cfg(feature = "inline-images")]
|
|
84
|
-
let image_collector = if wants_images {
|
|
85
|
-
use crate::inline_images::{DEFAULT_INLINE_IMAGE_LIMIT, InlineImageConfig as IIC};
|
|
86
|
-
Some(Rc::new(RefCell::new(crate::inline_images::InlineImageCollector::new(
|
|
87
|
-
IIC::new(DEFAULT_INLINE_IMAGE_LIMIT),
|
|
88
|
-
)?)))
|
|
89
|
-
} else {
|
|
90
|
-
None
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// Build optional structure collector when requested.
|
|
94
|
-
let structure_collector: Option<std::rc::Rc<std::cell::RefCell<crate::types::StructureCollector>>> =
|
|
95
|
-
if options.include_document_structure {
|
|
96
|
-
Some(std::rc::Rc::new(std::cell::RefCell::new(
|
|
97
|
-
crate::types::StructureCollector::new(),
|
|
98
|
-
)))
|
|
99
|
-
} else {
|
|
100
|
-
None
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
#[cfg(not(feature = "visitor"))]
|
|
104
|
-
let visitor: Option<()> = None;
|
|
105
|
-
|
|
106
|
-
// Run the conversion pipeline.
|
|
107
|
-
// Pass structure_collector by value — convert_html_impl will consume it via Rc::try_unwrap
|
|
108
|
-
// to return the finished DocumentStructure. We must not hold a second Rc reference.
|
|
109
|
-
let (markdown, document, tables) = {
|
|
110
|
-
#[cfg(all(feature = "metadata", feature = "inline-images"))]
|
|
111
|
-
{
|
|
112
|
-
crate::converter::convert_html_impl(
|
|
113
|
-
normalized_html.as_ref(),
|
|
114
|
-
&options,
|
|
115
|
-
image_collector.as_ref().map(Rc::clone),
|
|
116
|
-
metadata_collector.as_ref().map(Rc::clone),
|
|
117
|
-
visitor,
|
|
118
|
-
structure_collector,
|
|
119
|
-
)?
|
|
120
|
-
}
|
|
121
|
-
#[cfg(all(feature = "metadata", not(feature = "inline-images")))]
|
|
122
|
-
{
|
|
123
|
-
crate::converter::convert_html_impl(
|
|
124
|
-
normalized_html.as_ref(),
|
|
125
|
-
&options,
|
|
126
|
-
None,
|
|
127
|
-
metadata_collector.as_ref().map(Rc::clone),
|
|
128
|
-
visitor,
|
|
129
|
-
structure_collector,
|
|
130
|
-
)?
|
|
131
|
-
}
|
|
132
|
-
#[cfg(all(not(feature = "metadata"), feature = "inline-images"))]
|
|
133
|
-
{
|
|
134
|
-
crate::converter::convert_html_impl(
|
|
135
|
-
normalized_html.as_ref(),
|
|
136
|
-
&options,
|
|
137
|
-
image_collector.as_ref().map(Rc::clone),
|
|
138
|
-
None,
|
|
139
|
-
visitor,
|
|
140
|
-
structure_collector,
|
|
141
|
-
)?
|
|
142
|
-
}
|
|
143
|
-
#[cfg(all(not(feature = "metadata"), not(feature = "inline-images")))]
|
|
144
|
-
{
|
|
145
|
-
crate::converter::convert_html_impl(
|
|
146
|
-
normalized_html.as_ref(),
|
|
147
|
-
&options,
|
|
148
|
-
None,
|
|
149
|
-
None,
|
|
150
|
-
visitor,
|
|
151
|
-
structure_collector,
|
|
152
|
-
)?
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
let markdown = if options.wrap {
|
|
157
|
-
crate::wrapper::wrap_markdown(&markdown, &options)
|
|
158
|
-
} else {
|
|
159
|
-
markdown
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
// Collect metadata if extracted.
|
|
163
|
-
#[cfg(feature = "metadata")]
|
|
164
|
-
let metadata = if let Some(collector) = metadata_collector {
|
|
165
|
-
Rc::try_unwrap(collector)
|
|
166
|
-
.map_err(|_| ConversionError::Other("failed to recover metadata state".to_string()))?
|
|
167
|
-
.into_inner()
|
|
168
|
-
.finish()
|
|
169
|
-
} else {
|
|
170
|
-
HtmlMetadata::default()
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
// Collect inline images if extracted.
|
|
174
|
-
#[cfg(feature = "inline-images")]
|
|
175
|
-
let (images, image_warnings) = if let Some(collector) = image_collector {
|
|
176
|
-
let c = Rc::try_unwrap(collector)
|
|
177
|
-
.map_err(|_| ConversionError::Other("failed to recover inline image state".to_string()))?
|
|
178
|
-
.into_inner();
|
|
179
|
-
c.finish()
|
|
180
|
-
} else {
|
|
181
|
-
(Vec::new(), Vec::new())
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
// Map InlineImageWarnings → ProcessingWarnings.
|
|
185
|
-
#[cfg(feature = "inline-images")]
|
|
186
|
-
let warnings: Vec<crate::types::ProcessingWarning> = image_warnings
|
|
187
|
-
.into_iter()
|
|
188
|
-
.map(|w| crate::types::ProcessingWarning {
|
|
189
|
-
kind: crate::types::WarningKind::ImageExtractionFailed,
|
|
190
|
-
message: w.message,
|
|
191
|
-
})
|
|
192
|
-
.collect();
|
|
193
|
-
#[cfg(not(feature = "inline-images"))]
|
|
194
|
-
let warnings: Vec<crate::types::ProcessingWarning> = Vec::new();
|
|
195
|
-
|
|
196
|
-
let _ = wants_metadata;
|
|
197
|
-
let _ = wants_images;
|
|
198
|
-
|
|
199
|
-
Ok(ConversionResult {
|
|
200
|
-
content: Some(markdown),
|
|
201
|
-
document,
|
|
202
|
-
#[cfg(feature = "metadata")]
|
|
203
|
-
metadata,
|
|
204
|
-
tables,
|
|
205
|
-
#[cfg(feature = "inline-images")]
|
|
206
|
-
images,
|
|
207
|
-
warnings,
|
|
208
|
-
})
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/// Validate and normalize HTML input for conversion.
|
|
212
|
-
fn normalize_input(html: &str) -> Result<Cow<'_, str>> {
|
|
213
|
-
let decoded = decode_utf16_if_needed(html);
|
|
214
|
-
match decoded {
|
|
215
|
-
Cow::Borrowed(borrowed) => {
|
|
216
|
-
validate_input(borrowed)?;
|
|
217
|
-
let sanitized = strip_nul_bytes(borrowed);
|
|
218
|
-
match sanitized {
|
|
219
|
-
Cow::Borrowed(b) => Ok(normalize_line_endings(b)),
|
|
220
|
-
Cow::Owned(o) => Ok(Cow::Owned(normalize_line_endings(&o).into_owned())),
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
Cow::Owned(mut owned) => {
|
|
224
|
-
validate_input(&owned)?;
|
|
225
|
-
if owned.contains('\0') {
|
|
226
|
-
owned = owned.replace('\0', "");
|
|
227
|
-
}
|
|
228
|
-
if owned.contains('\r') {
|
|
229
|
-
owned = owned.replace("\r\n", "\n").replace('\r', "\n");
|
|
230
|
-
}
|
|
231
|
-
Ok(Cow::Owned(owned))
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/// Attempt to decode UTF-16 HTML that was provided as a lossy UTF-8 string.
|
|
237
|
-
///
|
|
238
|
-
/// Some callers read raw bytes and convert with `from_utf8_lossy`, which preserves
|
|
239
|
-
/// the NUL-byte pattern of UTF-16 input. When we detect that pattern, we can
|
|
240
|
-
/// recover the original HTML instead of rejecting it as binary data.
|
|
241
|
-
fn decode_utf16_if_needed(html: &str) -> Cow<'_, str> {
|
|
242
|
-
let bytes = html.as_bytes();
|
|
243
|
-
if !bytes.contains(&0) {
|
|
244
|
-
return Cow::Borrowed(html);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
let Some(encoding) = detect_utf16_encoding(bytes) else {
|
|
248
|
-
return Cow::Borrowed(html);
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
let decoded = decode_utf16_bytes(bytes, encoding);
|
|
252
|
-
if decoded.is_empty() {
|
|
253
|
-
Cow::Borrowed(html)
|
|
254
|
-
} else {
|
|
255
|
-
Cow::Owned(decoded)
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
fn decode_utf16_bytes(bytes: &[u8], encoding: Utf16Encoding) -> String {
|
|
260
|
-
let (is_little_endian, skip_bom) = match encoding {
|
|
261
|
-
Utf16Encoding::BomLe => (true, true),
|
|
262
|
-
Utf16Encoding::BomBe => (false, true),
|
|
263
|
-
Utf16Encoding::NoBomLe => (true, false),
|
|
264
|
-
Utf16Encoding::NoBomBe => (false, false),
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
let mut units = Vec::with_capacity(bytes.len() / 2);
|
|
268
|
-
for chunk in bytes.chunks_exact(2) {
|
|
269
|
-
let unit = if is_little_endian {
|
|
270
|
-
u16::from_le_bytes([chunk[0], chunk[1]])
|
|
271
|
-
} else {
|
|
272
|
-
u16::from_be_bytes([chunk[0], chunk[1]])
|
|
273
|
-
};
|
|
274
|
-
units.push(unit);
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
let mut decoded = String::from_utf16_lossy(&units);
|
|
278
|
-
if skip_bom {
|
|
279
|
-
decoded = decoded.trim_start_matches('\u{FEFF}').to_string();
|
|
280
|
-
}
|
|
281
|
-
decoded
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/// Strip NUL bytes that can appear in malformed HTML inputs.
|
|
285
|
-
fn strip_nul_bytes(html: &str) -> Cow<'_, str> {
|
|
286
|
-
if html.contains('\0') {
|
|
287
|
-
Cow::Owned(html.replace('\0', ""))
|
|
288
|
-
} else {
|
|
289
|
-
Cow::Borrowed(html)
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
/// Normalize line endings in HTML input.
|
|
294
|
-
///
|
|
295
|
-
/// Converts CRLF and CR line endings to LF for consistent processing.
|
|
296
|
-
fn normalize_line_endings(html: &str) -> Cow<'_, str> {
|
|
297
|
-
if html.contains('\r') {
|
|
298
|
-
Cow::Owned(html.replace("\r\n", "\n").replace('\r', "\n"))
|
|
299
|
-
} else {
|
|
300
|
-
Cow::Borrowed(html)
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/// Fast path for plain text (no HTML) conversion.
|
|
305
|
-
///
|
|
306
|
-
/// Skips HTML parsing if no angle brackets are present.
|
|
307
|
-
fn fast_text_only(html: &str, options: &ConversionOptions) -> Option<String> {
|
|
308
|
-
if html.contains('<') {
|
|
309
|
-
return None;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
let mut decoded = text::decode_html_entities_cow(html);
|
|
313
|
-
if options.strip_newlines && (decoded.contains('\n') || decoded.contains('\r')) {
|
|
314
|
-
decoded = Cow::Owned(decoded.replace(&['\r', '\n'][..], " "));
|
|
315
|
-
}
|
|
316
|
-
let trimmed = decoded.trim_end_matches('\n');
|
|
317
|
-
if trimmed.is_empty() {
|
|
318
|
-
return Some(String::new());
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
let normalized = if options.whitespace_mode == WhitespaceMode::Normalized {
|
|
322
|
-
text::normalize_whitespace_cow(trimmed)
|
|
323
|
-
} else {
|
|
324
|
-
Cow::Borrowed(trimmed)
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
let escaped = if options.output_format == crate::options::OutputFormat::Plain {
|
|
328
|
-
normalized.into_owned()
|
|
329
|
-
} else if options.escape_misc || options.escape_asterisks || options.escape_underscores || options.escape_ascii {
|
|
330
|
-
text::escape(
|
|
331
|
-
normalized.as_ref(),
|
|
332
|
-
options.escape_misc,
|
|
333
|
-
options.escape_asterisks,
|
|
334
|
-
options.escape_underscores,
|
|
335
|
-
options.escape_ascii,
|
|
336
|
-
)
|
|
337
|
-
.into_owned()
|
|
338
|
-
} else {
|
|
339
|
-
normalized.into_owned()
|
|
340
|
-
};
|
|
341
|
-
|
|
342
|
-
let mut output = String::with_capacity(escaped.len() + 1);
|
|
343
|
-
output.push_str(&escaped);
|
|
344
|
-
while output.ends_with(' ') || output.ends_with('\t') {
|
|
345
|
-
output.pop();
|
|
346
|
-
}
|
|
347
|
-
output.push('\n');
|
|
348
|
-
Some(output)
|
|
349
|
-
}
|