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,266 +0,0 @@
|
|
|
1
|
-
//! Table element handler for HTML to Markdown conversion.
|
|
2
|
-
//!
|
|
3
|
-
//! This module provides specialized handling for table elements including:
|
|
4
|
-
//! - Table structure detection and scanning (TableScan)
|
|
5
|
-
//! - Row and cell conversion to Markdown table format
|
|
6
|
-
//! - Cell content processing with colspan/rowspan support
|
|
7
|
-
//! - Layout table detection (tables used for visual layout)
|
|
8
|
-
//! - Integration with the visitor pattern for custom table handling
|
|
9
|
-
//!
|
|
10
|
-
//! Tables are converted to Markdown pipe-delimited format with header separators.
|
|
11
|
-
//! Layout tables (tables without proper semantic headers) may be converted to lists
|
|
12
|
-
//! instead of tables for better readability.
|
|
13
|
-
|
|
14
|
-
pub mod builder;
|
|
15
|
-
pub mod caption;
|
|
16
|
-
pub mod cell;
|
|
17
|
-
pub mod cells;
|
|
18
|
-
pub mod layout;
|
|
19
|
-
pub mod scanner;
|
|
20
|
-
pub(super) mod utils;
|
|
21
|
-
|
|
22
|
-
// Re-export types from parent module for submodule access
|
|
23
|
-
|
|
24
|
-
// Re-export for use in converter.rs
|
|
25
|
-
pub use caption::handle_caption;
|
|
26
|
-
|
|
27
|
-
/// Dispatches table element handling to the main convert_table function.
|
|
28
|
-
///
|
|
29
|
-
/// # Usage in converter.rs
|
|
30
|
-
/// ```text
|
|
31
|
-
/// if "table" == tag_name {
|
|
32
|
-
/// crate::converter::block::table::handle_table(
|
|
33
|
-
/// node_handle,
|
|
34
|
-
/// parser,
|
|
35
|
-
/// output,
|
|
36
|
-
/// options,
|
|
37
|
-
/// ctx,
|
|
38
|
-
/// dom_ctx,
|
|
39
|
-
/// depth,
|
|
40
|
-
/// );
|
|
41
|
-
/// return;
|
|
42
|
-
/// }
|
|
43
|
-
/// ```
|
|
44
|
-
pub fn dispatch_table_handler(
|
|
45
|
-
tag_name: &str,
|
|
46
|
-
node_handle: &tl::NodeHandle,
|
|
47
|
-
parser: &tl::Parser,
|
|
48
|
-
output: &mut String,
|
|
49
|
-
options: &crate::options::ConversionOptions,
|
|
50
|
-
ctx: &super::super::Context,
|
|
51
|
-
depth: usize,
|
|
52
|
-
dom_ctx: &super::super::DomContext,
|
|
53
|
-
) -> bool {
|
|
54
|
-
match tag_name {
|
|
55
|
-
"table" => {
|
|
56
|
-
builder::handle_table(node_handle, parser, output, options, ctx, dom_ctx, depth);
|
|
57
|
-
true
|
|
58
|
-
}
|
|
59
|
-
_ => false,
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/// Handles table output with context-aware formatting.
|
|
64
|
-
///
|
|
65
|
-
/// This function wraps `handle_table` with list indentation and whitespace management logic.
|
|
66
|
-
/// It handles two distinct contexts:
|
|
67
|
-
///
|
|
68
|
-
/// 1. **List Context** (`ctx.in_list_item = true`):
|
|
69
|
-
/// - Indents table content using `indent_table_for_list` for proper nesting
|
|
70
|
-
/// - Preserves special caption formatting (lines starting with `*`)
|
|
71
|
-
/// - Adds newlines for proper list item separation
|
|
72
|
-
///
|
|
73
|
-
/// 2. **Normal Context**:
|
|
74
|
-
/// - Ensures proper spacing with double newlines around tables
|
|
75
|
-
/// - Handles various output state scenarios (empty, ends with newline, etc.)
|
|
76
|
-
///
|
|
77
|
-
/// # Arguments
|
|
78
|
-
/// * `node_handle` - The table node handle
|
|
79
|
-
/// * `parser` - The HTML parser instance
|
|
80
|
-
/// * `output` - The output string being built
|
|
81
|
-
/// * `options` - Conversion options (includes list indent type)
|
|
82
|
-
/// * `ctx` - Conversion context (includes list state)
|
|
83
|
-
/// * `dom_ctx` - DOM context for tree structure info
|
|
84
|
-
/// * `depth` - Current nesting depth
|
|
85
|
-
pub fn handle_table_with_context(
|
|
86
|
-
node_handle: &tl::NodeHandle,
|
|
87
|
-
parser: &tl::Parser,
|
|
88
|
-
output: &mut String,
|
|
89
|
-
options: &crate::options::ConversionOptions,
|
|
90
|
-
ctx: &super::super::Context,
|
|
91
|
-
dom_ctx: &super::super::DomContext,
|
|
92
|
-
depth: usize,
|
|
93
|
-
) {
|
|
94
|
-
let mut table_output = String::new();
|
|
95
|
-
builder::handle_table(node_handle, parser, &mut table_output, options, ctx, dom_ctx, depth);
|
|
96
|
-
|
|
97
|
-
// Feed the table into the structure collector when document structure extraction is enabled.
|
|
98
|
-
// Use push_table_data so that ConversionResult.tables is populated with both the grid and
|
|
99
|
-
// the markdown rendering that was just generated above.
|
|
100
|
-
if let Some(ref sc) = ctx.structure_collector {
|
|
101
|
-
if let Some(grid) = collect_table_grid(node_handle, parser, options, ctx, dom_ctx) {
|
|
102
|
-
sc.borrow_mut().push_table_data(grid, table_output.trim().to_string());
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if ctx.in_list_item {
|
|
107
|
-
let has_caption = table_output.starts_with('*');
|
|
108
|
-
|
|
109
|
-
if !has_caption {
|
|
110
|
-
use crate::converter::main_helpers::trim_trailing_whitespace;
|
|
111
|
-
trim_trailing_whitespace(output);
|
|
112
|
-
if !output.is_empty() && !output.ends_with('\n') {
|
|
113
|
-
output.push('\n');
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
let indented = layout::indent_table_for_list(&table_output, ctx.list_depth, options);
|
|
118
|
-
output.push_str(&indented);
|
|
119
|
-
} else {
|
|
120
|
-
if !output.is_empty() && !output.ends_with("\n\n") {
|
|
121
|
-
if output.ends_with('\n') {
|
|
122
|
-
output.push('\n');
|
|
123
|
-
} else {
|
|
124
|
-
output.push_str("\n\n");
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
output.push_str(&table_output);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if !output.ends_with('\n') {
|
|
131
|
-
output.push('\n');
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/// Collect a [`crate::types::TableGrid`] from the DOM for the structure collector.
|
|
136
|
-
///
|
|
137
|
-
/// Walks the table's rows and cells, extracting text content and span attributes
|
|
138
|
-
/// to build a structured grid representation.
|
|
139
|
-
fn collect_table_grid(
|
|
140
|
-
node_handle: &tl::NodeHandle,
|
|
141
|
-
parser: &tl::Parser,
|
|
142
|
-
options: &crate::options::ConversionOptions,
|
|
143
|
-
ctx: &super::super::Context,
|
|
144
|
-
dom_ctx: &super::super::DomContext,
|
|
145
|
-
) -> Option<crate::types::TableGrid> {
|
|
146
|
-
use utils::{is_tag_name, normalized_tag_name};
|
|
147
|
-
|
|
148
|
-
let tl::Node::Tag(tag) = node_handle.get(parser)? else {
|
|
149
|
-
return None;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
let mut grid_cells = Vec::new();
|
|
153
|
-
let mut row_index: u32 = 0;
|
|
154
|
-
let mut max_cols: u32 = 0;
|
|
155
|
-
let mut cell_handles = Vec::new();
|
|
156
|
-
|
|
157
|
-
let children = tag.children();
|
|
158
|
-
for child_handle in children.top().iter() {
|
|
159
|
-
if let Some(tl::Node::Tag(child_tag)) = child_handle.get(parser) {
|
|
160
|
-
let tag_name = normalized_tag_name(child_tag.name().as_utf8_str());
|
|
161
|
-
match tag_name.as_ref() {
|
|
162
|
-
"thead" | "tbody" | "tfoot" => {
|
|
163
|
-
let is_header_section = tag_name.as_ref() == "thead";
|
|
164
|
-
for row_handle in child_tag.children().top().iter() {
|
|
165
|
-
if is_tag_name(row_handle, parser, dom_ctx, "tr") {
|
|
166
|
-
collect_grid_row(
|
|
167
|
-
row_handle,
|
|
168
|
-
parser,
|
|
169
|
-
options,
|
|
170
|
-
ctx,
|
|
171
|
-
dom_ctx,
|
|
172
|
-
&mut cell_handles,
|
|
173
|
-
&mut grid_cells,
|
|
174
|
-
&mut row_index,
|
|
175
|
-
&mut max_cols,
|
|
176
|
-
is_header_section,
|
|
177
|
-
);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
"tr" | "row" => {
|
|
182
|
-
let is_first = row_index == 0;
|
|
183
|
-
collect_grid_row(
|
|
184
|
-
child_handle,
|
|
185
|
-
parser,
|
|
186
|
-
options,
|
|
187
|
-
ctx,
|
|
188
|
-
dom_ctx,
|
|
189
|
-
&mut cell_handles,
|
|
190
|
-
&mut grid_cells,
|
|
191
|
-
&mut row_index,
|
|
192
|
-
&mut max_cols,
|
|
193
|
-
is_first,
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
_ => {}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if row_index == 0 {
|
|
202
|
-
return None;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
Some(crate::types::TableGrid {
|
|
206
|
-
rows: row_index,
|
|
207
|
-
cols: max_cols,
|
|
208
|
-
cells: grid_cells,
|
|
209
|
-
})
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/// Process a single table row for grid collection.
|
|
213
|
-
#[allow(clippy::too_many_arguments)]
|
|
214
|
-
fn collect_grid_row(
|
|
215
|
-
row_handle: &tl::NodeHandle,
|
|
216
|
-
parser: &tl::Parser,
|
|
217
|
-
options: &crate::options::ConversionOptions,
|
|
218
|
-
ctx: &super::super::Context,
|
|
219
|
-
dom_ctx: &super::super::DomContext,
|
|
220
|
-
cell_handles: &mut Vec<tl::NodeHandle>,
|
|
221
|
-
grid_cells: &mut Vec<crate::types::GridCell>,
|
|
222
|
-
row_index: &mut u32,
|
|
223
|
-
max_cols: &mut u32,
|
|
224
|
-
is_header_section: bool,
|
|
225
|
-
) {
|
|
226
|
-
use cell::{collect_table_cells, get_colspan_rowspan};
|
|
227
|
-
|
|
228
|
-
collect_table_cells(row_handle, parser, dom_ctx, cell_handles);
|
|
229
|
-
|
|
230
|
-
let mut col_index: u32 = 0;
|
|
231
|
-
for cell_handle in cell_handles.iter() {
|
|
232
|
-
let is_header = is_header_section
|
|
233
|
-
|| dom_ctx
|
|
234
|
-
.tag_name_for(*cell_handle, parser)
|
|
235
|
-
.is_some_and(|name| name.as_ref() == "th");
|
|
236
|
-
|
|
237
|
-
let mut text = String::new();
|
|
238
|
-
let cell_ctx = super::super::Context {
|
|
239
|
-
in_table_cell: true,
|
|
240
|
-
..ctx.clone()
|
|
241
|
-
};
|
|
242
|
-
if let Some(tl::Node::Tag(cell_tag)) = cell_handle.get(parser) {
|
|
243
|
-
for child_handle in cell_tag.children().top().iter() {
|
|
244
|
-
super::super::walk_node(child_handle, parser, &mut text, options, &cell_ctx, 0, dom_ctx);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
let content = crate::text::normalize_whitespace_cow(&text).trim().to_string();
|
|
248
|
-
|
|
249
|
-
let (colspan, rowspan) = get_colspan_rowspan(cell_handle, parser);
|
|
250
|
-
|
|
251
|
-
grid_cells.push(crate::types::GridCell {
|
|
252
|
-
content,
|
|
253
|
-
row: *row_index,
|
|
254
|
-
col: col_index,
|
|
255
|
-
row_span: rowspan as u32,
|
|
256
|
-
col_span: colspan as u32,
|
|
257
|
-
is_header,
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
col_index += colspan as u32;
|
|
261
|
-
}
|
|
262
|
-
if col_index > *max_cols {
|
|
263
|
-
*max_cols = col_index;
|
|
264
|
-
}
|
|
265
|
-
*row_index += 1;
|
|
266
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
//! Table scanning and analysis utilities.
|
|
2
|
-
//!
|
|
3
|
-
//! Provides the TableScan struct and scanning functions for analyzing table structure
|
|
4
|
-
//! to determine if it should be rendered as a Markdown table or converted to list format.
|
|
5
|
-
|
|
6
|
-
use crate::converter::utility::content::normalized_tag_name;
|
|
7
|
-
use std::borrow::Cow;
|
|
8
|
-
|
|
9
|
-
/// Scan results for a table element.
|
|
10
|
-
///
|
|
11
|
-
/// Contains metadata about table structure to determine optimal rendering:
|
|
12
|
-
/// - Row counts for consistency checking
|
|
13
|
-
/// - Presence of headers, captions, and nested tables
|
|
14
|
-
/// - Presence of colspan/rowspan (spanning cells)
|
|
15
|
-
/// - Link and text content counts
|
|
16
|
-
#[derive(Default)]
|
|
17
|
-
pub struct TableScan {
|
|
18
|
-
/// Number of cells in each row
|
|
19
|
-
pub row_counts: Vec<usize>,
|
|
20
|
-
/// Whether any cells have colspan or rowspan attributes
|
|
21
|
-
pub has_span: bool,
|
|
22
|
-
/// Whether the table has header cells (th elements or role="head")
|
|
23
|
-
pub has_header: bool,
|
|
24
|
-
/// Whether the table has a caption element
|
|
25
|
-
pub has_caption: bool,
|
|
26
|
-
/// Number of nested tables found inside this table
|
|
27
|
-
pub nested_table_count: usize,
|
|
28
|
-
/// Count of anchor elements in the table
|
|
29
|
-
pub link_count: usize,
|
|
30
|
-
/// Whether the table contains text content (not empty)
|
|
31
|
-
pub has_text: bool,
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/// Scan a table element for structural metadata.
|
|
35
|
-
///
|
|
36
|
-
/// Analyzes the table to determine characteristics that influence rendering:
|
|
37
|
-
/// - Whether to render as a Markdown table or layout table
|
|
38
|
-
/// - If spanning cells are present
|
|
39
|
-
/// - If the table has semantic meaning (headers, captions)
|
|
40
|
-
///
|
|
41
|
-
/// # Arguments
|
|
42
|
-
/// * `node_handle` - Handle to the table element
|
|
43
|
-
/// * `parser` - HTML parser instance
|
|
44
|
-
/// * `dom_ctx` - DOM context for tag name resolution
|
|
45
|
-
#[allow(clippy::trivially_copy_pass_by_ref)]
|
|
46
|
-
pub fn scan_table(
|
|
47
|
-
node_handle: &tl::NodeHandle,
|
|
48
|
-
parser: &tl::Parser,
|
|
49
|
-
dom_ctx: &super::super::super::DomContext,
|
|
50
|
-
) -> TableScan {
|
|
51
|
-
let mut scan = TableScan::default();
|
|
52
|
-
scan_table_node(node_handle, parser, dom_ctx, true, &mut scan);
|
|
53
|
-
scan
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/// Recursively scan table structure.
|
|
57
|
-
///
|
|
58
|
-
/// Internal recursive function that walks the table tree and collects metadata.
|
|
59
|
-
///
|
|
60
|
-
/// # Arguments
|
|
61
|
-
/// * `node_handle` - Current node to scan
|
|
62
|
-
/// * `parser` - HTML parser instance
|
|
63
|
-
/// * `dom_ctx` - DOM context for tag name resolution
|
|
64
|
-
/// * `is_root` - Whether this is the root table element
|
|
65
|
-
/// * `scan` - Mutable scan results to accumulate
|
|
66
|
-
#[allow(clippy::trivially_copy_pass_by_ref)]
|
|
67
|
-
fn scan_table_node(
|
|
68
|
-
node_handle: &tl::NodeHandle,
|
|
69
|
-
parser: &tl::Parser,
|
|
70
|
-
dom_ctx: &super::super::super::DomContext,
|
|
71
|
-
is_root: bool,
|
|
72
|
-
scan: &mut TableScan,
|
|
73
|
-
) {
|
|
74
|
-
if let Some(node) = node_handle.get(parser) {
|
|
75
|
-
match node {
|
|
76
|
-
tl::Node::Raw(bytes) => {
|
|
77
|
-
if !scan.has_text {
|
|
78
|
-
let raw = bytes.as_utf8_str();
|
|
79
|
-
let decoded = crate::text::decode_html_entities_cow(raw.as_ref());
|
|
80
|
-
if !decoded.trim().is_empty() {
|
|
81
|
-
scan.has_text = true;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
tl::Node::Tag(tag) => {
|
|
86
|
-
let tag_name: Cow<'_, str> = dom_ctx.tag_info(node_handle.get_inner(), parser).map_or_else(
|
|
87
|
-
|| normalized_tag_name(tag.name().as_utf8_str()).into_owned().into(),
|
|
88
|
-
|info| Cow::Borrowed(info.name.as_str()),
|
|
89
|
-
);
|
|
90
|
-
|
|
91
|
-
match tag_name.as_ref() {
|
|
92
|
-
"a" => scan.link_count += 1,
|
|
93
|
-
"caption" => scan.has_caption = true,
|
|
94
|
-
"th" => scan.has_header = true,
|
|
95
|
-
"img" | "graphic" => {
|
|
96
|
-
// Images with src or alt attributes count as content
|
|
97
|
-
if tag.attributes().get("src").is_some() || tag.attributes().get("alt").is_some() {
|
|
98
|
-
scan.has_text = true;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
"cell" => {
|
|
102
|
-
// Check if cell has role="head" attribute
|
|
103
|
-
if let Some(role) = tag.attributes().get("role") {
|
|
104
|
-
if let Some(role_val) = role {
|
|
105
|
-
let role_str = role_val.as_utf8_str();
|
|
106
|
-
if role_str == "head" {
|
|
107
|
-
scan.has_header = true;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
"table" if !is_root => scan.nested_table_count += 1,
|
|
113
|
-
"tr" | "row" => {
|
|
114
|
-
let mut cell_count = 0;
|
|
115
|
-
for child in tag.children().top().iter() {
|
|
116
|
-
if let Some(tl::Node::Tag(cell_tag)) = child.get(parser) {
|
|
117
|
-
let cell_name: Cow<'_, str> = dom_ctx
|
|
118
|
-
.tag_info(child.get_inner(), parser)
|
|
119
|
-
.map(|info| Cow::Borrowed(info.name.as_str()))
|
|
120
|
-
.unwrap_or_else(|| {
|
|
121
|
-
normalized_tag_name(cell_tag.name().as_utf8_str()).into_owned().into()
|
|
122
|
-
});
|
|
123
|
-
if matches!(cell_name.as_ref(), "td" | "th" | "cell") {
|
|
124
|
-
cell_count += super::cell::get_colspan(child, parser);
|
|
125
|
-
let attrs = cell_tag.attributes();
|
|
126
|
-
if attrs.get("colspan").is_some() || attrs.get("rowspan").is_some() {
|
|
127
|
-
scan.has_span = true;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
scan_table_node(child, parser, dom_ctx, false, scan);
|
|
132
|
-
}
|
|
133
|
-
scan.row_counts.push(cell_count);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
_ => {}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
for child in tag.children().top().iter() {
|
|
140
|
-
scan_table_node(child, parser, dom_ctx, false, scan);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
_ => {}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
//! Utility functions for table processing.
|
|
2
|
-
//!
|
|
3
|
-
//! Provides helper functions for tag name normalization and comparison.
|
|
4
|
-
|
|
5
|
-
pub(super) use crate::converter::main_helpers::tag_name_eq;
|
|
6
|
-
pub(super) use crate::converter::utility::content::normalized_tag_name;
|
|
7
|
-
|
|
8
|
-
/// Check if a node has a specific tag name.
|
|
9
|
-
///
|
|
10
|
-
/// Handles both direct tag matching and DOM context-based tag resolution.
|
|
11
|
-
///
|
|
12
|
-
/// # Arguments
|
|
13
|
-
/// * `node_handle` - Handle to the node
|
|
14
|
-
/// * `parser` - HTML parser instance
|
|
15
|
-
/// * `dom_ctx` - DOM context for tag name resolution
|
|
16
|
-
/// * `name` - Expected tag name
|
|
17
|
-
///
|
|
18
|
-
/// # Returns
|
|
19
|
-
/// True if node has the specified tag name
|
|
20
|
-
#[allow(clippy::trivially_copy_pass_by_ref)]
|
|
21
|
-
pub(super) fn is_tag_name(
|
|
22
|
-
node_handle: &tl::NodeHandle,
|
|
23
|
-
parser: &tl::Parser,
|
|
24
|
-
dom_ctx: &super::super::super::DomContext,
|
|
25
|
-
name: &str,
|
|
26
|
-
) -> bool {
|
|
27
|
-
if let Some(info) = dom_ctx.tag_info(node_handle.get_inner(), parser) {
|
|
28
|
-
return info.name == name;
|
|
29
|
-
}
|
|
30
|
-
matches!(
|
|
31
|
-
node_handle.get(parser),
|
|
32
|
-
Some(tl::Node::Tag(tag)) if tag_name_eq(tag.name().as_utf8_str(), name)
|
|
33
|
-
)
|
|
34
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
//! Handler for unknown/unspecified HTML elements.
|
|
2
|
-
//!
|
|
3
|
-
//! Processes HTML elements that don't have specific handlers by passing through
|
|
4
|
-
//! their children while managing whitespace and content preservation:
|
|
5
|
-
//! - Processes all child nodes recursively
|
|
6
|
-
//! - Validates UTF-8 character boundaries when checking content
|
|
7
|
-
//! - Preserves code blocks (indented or fenced) while removing empty content
|
|
8
|
-
//! - Manages trailing whitespace intelligently
|
|
9
|
-
|
|
10
|
-
use crate::options::ConversionOptions;
|
|
11
|
-
use tl::{NodeHandle, Parser};
|
|
12
|
-
|
|
13
|
-
// Type aliases for Context and DomContext to avoid circular imports
|
|
14
|
-
type Context = crate::converter::Context;
|
|
15
|
-
type DomContext = crate::converter::DomContext;
|
|
16
|
-
|
|
17
|
-
/// Handles unknown/unspecified HTML elements.
|
|
18
|
-
///
|
|
19
|
-
/// For elements without specific handlers, this function:
|
|
20
|
-
/// 1. Records the output position before processing children
|
|
21
|
-
/// 2. Processes all children recursively
|
|
22
|
-
/// 3. Checks if any content was added
|
|
23
|
-
/// 4. Validates UTF-8 character boundaries for safe string slicing
|
|
24
|
-
/// 5. Detects if added content is a code block (indented or fenced)
|
|
25
|
-
/// 6. Removes empty content while preserving code blocks and spacing
|
|
26
|
-
///
|
|
27
|
-
/// # UTF-8 Safety
|
|
28
|
-
/// The function carefully handles UTF-8 boundaries to prevent panic when
|
|
29
|
-
/// slicing the output string. It finds the nearest valid boundary if the
|
|
30
|
-
/// position falls in the middle of a multi-byte character.
|
|
31
|
-
///
|
|
32
|
-
/// # Code Block Detection
|
|
33
|
-
/// Code blocks (identified by markdown formatting) are always preserved,
|
|
34
|
-
/// even if they appear "empty" according to trim().
|
|
35
|
-
#[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
|
|
36
|
-
pub fn handle(
|
|
37
|
-
node_handle: &NodeHandle,
|
|
38
|
-
parser: &Parser,
|
|
39
|
-
output: &mut String,
|
|
40
|
-
options: &ConversionOptions,
|
|
41
|
-
ctx: &Context,
|
|
42
|
-
depth: usize,
|
|
43
|
-
dom_ctx: &DomContext,
|
|
44
|
-
) {
|
|
45
|
-
use crate::converter::walk_node;
|
|
46
|
-
|
|
47
|
-
let Some(node) = node_handle.get(parser) else { return };
|
|
48
|
-
|
|
49
|
-
let tag = match node {
|
|
50
|
-
tl::Node::Tag(tag) => tag,
|
|
51
|
-
_ => return,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
#[cfg(feature = "visitor")]
|
|
55
|
-
if let Some(ref visitor_handle) = ctx.visitor {
|
|
56
|
-
use crate::converter::utility::content::collect_tag_attributes;
|
|
57
|
-
use crate::converter::utility::serialization::serialize_tag_to_html;
|
|
58
|
-
use crate::visitor::{NodeContext, NodeType, VisitResult};
|
|
59
|
-
|
|
60
|
-
let tag_name = tag.name().as_utf8_str().to_string();
|
|
61
|
-
let raw_html = serialize_tag_to_html(node_handle, parser);
|
|
62
|
-
let attributes = collect_tag_attributes(tag);
|
|
63
|
-
let node_id = node_handle.get_inner();
|
|
64
|
-
let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
|
|
65
|
-
let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
|
|
66
|
-
let node_ctx = NodeContext {
|
|
67
|
-
node_type: NodeType::Custom,
|
|
68
|
-
tag_name: tag_name.clone(),
|
|
69
|
-
attributes,
|
|
70
|
-
depth,
|
|
71
|
-
index_in_parent,
|
|
72
|
-
parent_tag,
|
|
73
|
-
is_inline: false,
|
|
74
|
-
};
|
|
75
|
-
let visit_result = {
|
|
76
|
-
let mut visitor = visitor_handle.borrow_mut();
|
|
77
|
-
visitor.visit_custom_element(&node_ctx, &tag_name, &raw_html)
|
|
78
|
-
};
|
|
79
|
-
match visit_result {
|
|
80
|
-
VisitResult::Continue => {}
|
|
81
|
-
VisitResult::Skip => return,
|
|
82
|
-
VisitResult::Custom(custom) => {
|
|
83
|
-
output.push_str(&custom);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
VisitResult::PreserveHtml => {
|
|
87
|
-
output.push_str(&raw_html);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
VisitResult::Error(err) => {
|
|
91
|
-
if ctx.visitor_error.borrow().is_none() {
|
|
92
|
-
*ctx.visitor_error.borrow_mut() = Some(err);
|
|
93
|
-
}
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
let len_before = output.len();
|
|
100
|
-
let had_trailing_space = output.ends_with(' ');
|
|
101
|
-
|
|
102
|
-
let children = tag.children();
|
|
103
|
-
{
|
|
104
|
-
for child_handle in children.top().iter() {
|
|
105
|
-
walk_node(child_handle, parser, output, options, ctx, depth + 1, dom_ctx);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let len_after = output.len();
|
|
110
|
-
if len_after > len_before {
|
|
111
|
-
let start_idx = if output.is_char_boundary(len_before) {
|
|
112
|
-
len_before
|
|
113
|
-
} else {
|
|
114
|
-
// Find the nearest valid UTF-8 character boundary
|
|
115
|
-
let capped = len_before.min(output.len());
|
|
116
|
-
output
|
|
117
|
-
.char_indices()
|
|
118
|
-
.map(|(idx, _)| idx)
|
|
119
|
-
.take_while(|idx| *idx <= capped)
|
|
120
|
-
.last()
|
|
121
|
-
.unwrap_or(capped)
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
let added_content = output[start_idx..].to_string();
|
|
125
|
-
|
|
126
|
-
// Detect code blocks by markdown formatting
|
|
127
|
-
let is_code_block =
|
|
128
|
-
added_content.starts_with(" ") || added_content.starts_with("```") || added_content.starts_with("~~~");
|
|
129
|
-
|
|
130
|
-
// Remove empty content while preserving code blocks
|
|
131
|
-
if added_content.trim().is_empty() && !is_code_block {
|
|
132
|
-
output.truncate(start_idx);
|
|
133
|
-
if !had_trailing_space && added_content.contains(' ') {
|
|
134
|
-
output.push(' ');
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|