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.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +347 -0
  4. data/Steepfile +10 -2
  5. data/ext/html_to_markdown_rb/Cargo.toml +3 -2
  6. data/ext/html_to_markdown_rb/extconf.rb +5 -5
  7. data/ext/html_to_markdown_rb/native/Cargo.lock +962 -0
  8. data/ext/html_to_markdown_rb/native/Cargo.toml +6 -11
  9. data/ext/html_to_markdown_rb/native/extconf.rb +14 -0
  10. data/ext/html_to_markdown_rb/src/lib.rs +1715 -646
  11. data/lib/html_to_markdown/native.rb +913 -37
  12. data/lib/html_to_markdown/version.rb +3 -3
  13. data/lib/html_to_markdown.rb +9 -4
  14. data/lib/html_to_markdown_rb.so +0 -0
  15. data/sig/types.rbs +59 -292
  16. metadata +32 -179
  17. data/ext/html_to_markdown_rb/Makefile +0 -592
  18. data/lib/bin/html-to-markdown +0 -0
  19. data/vendor/Cargo.toml +0 -33
  20. data/vendor/html-to-markdown-rs/Cargo.toml +0 -54
  21. data/vendor/html-to-markdown-rs/README.md +0 -278
  22. data/vendor/html-to-markdown-rs/examples/basic.rs +0 -24
  23. data/vendor/html-to-markdown-rs/examples/table.rs +0 -25
  24. data/vendor/html-to-markdown-rs/examples/test_deser.rs +0 -12
  25. data/vendor/html-to-markdown-rs/examples/test_escape.rs +0 -58
  26. data/vendor/html-to-markdown-rs/examples/test_inline_formatting.rs +0 -113
  27. data/vendor/html-to-markdown-rs/examples/test_lists.rs +0 -39
  28. data/vendor/html-to-markdown-rs/examples/test_semantic_tags.rs +0 -89
  29. data/vendor/html-to-markdown-rs/examples/test_tables.rs +0 -100
  30. data/vendor/html-to-markdown-rs/examples/test_task_lists.rs +0 -61
  31. data/vendor/html-to-markdown-rs/examples/test_whitespace.rs +0 -34
  32. data/vendor/html-to-markdown-rs/src/convert_api.rs +0 -349
  33. data/vendor/html-to-markdown-rs/src/converter/block/blockquote.rs +0 -178
  34. data/vendor/html-to-markdown-rs/src/converter/block/container.rs +0 -114
  35. data/vendor/html-to-markdown-rs/src/converter/block/div.rs +0 -149
  36. data/vendor/html-to-markdown-rs/src/converter/block/heading.rs +0 -428
  37. data/vendor/html-to-markdown-rs/src/converter/block/horizontal_rule.rs +0 -103
  38. data/vendor/html-to-markdown-rs/src/converter/block/line_break.rs +0 -89
  39. data/vendor/html-to-markdown-rs/src/converter/block/mod.rs +0 -10
  40. data/vendor/html-to-markdown-rs/src/converter/block/paragraph.rs +0 -140
  41. data/vendor/html-to-markdown-rs/src/converter/block/preformatted.rs +0 -298
  42. data/vendor/html-to-markdown-rs/src/converter/block/table/builder.rs +0 -453
  43. data/vendor/html-to-markdown-rs/src/converter/block/table/caption.rs +0 -44
  44. data/vendor/html-to-markdown-rs/src/converter/block/table/cell.rs +0 -276
  45. data/vendor/html-to-markdown-rs/src/converter/block/table/cells.rs +0 -336
  46. data/vendor/html-to-markdown-rs/src/converter/block/table/layout.rs +0 -58
  47. data/vendor/html-to-markdown-rs/src/converter/block/table/mod.rs +0 -266
  48. data/vendor/html-to-markdown-rs/src/converter/block/table/scanner.rs +0 -146
  49. data/vendor/html-to-markdown-rs/src/converter/block/table/utils.rs +0 -34
  50. data/vendor/html-to-markdown-rs/src/converter/block/unknown.rs +0 -138
  51. data/vendor/html-to-markdown-rs/src/converter/context.rs +0 -208
  52. data/vendor/html-to-markdown-rs/src/converter/dom_context.rs +0 -337
  53. data/vendor/html-to-markdown-rs/src/converter/form/elements.rs +0 -770
  54. data/vendor/html-to-markdown-rs/src/converter/form/mod.rs +0 -82
  55. data/vendor/html-to-markdown-rs/src/converter/format/djot.rs +0 -64
  56. data/vendor/html-to-markdown-rs/src/converter/format/markdown.rs +0 -59
  57. data/vendor/html-to-markdown-rs/src/converter/format/mod.rs +0 -43
  58. data/vendor/html-to-markdown-rs/src/converter/handlers/blockquote.rs +0 -173
  59. data/vendor/html-to-markdown-rs/src/converter/handlers/code_block.rs +0 -434
  60. data/vendor/html-to-markdown-rs/src/converter/handlers/graphic.rs +0 -234
  61. data/vendor/html-to-markdown-rs/src/converter/handlers/image.rs +0 -282
  62. data/vendor/html-to-markdown-rs/src/converter/handlers/link.rs +0 -316
  63. data/vendor/html-to-markdown-rs/src/converter/handlers/mod.rs +0 -26
  64. data/vendor/html-to-markdown-rs/src/converter/inline/code.rs +0 -306
  65. data/vendor/html-to-markdown-rs/src/converter/inline/emphasis.rs +0 -345
  66. data/vendor/html-to-markdown-rs/src/converter/inline/link.rs +0 -428
  67. data/vendor/html-to-markdown-rs/src/converter/inline/mod.rs +0 -237
  68. data/vendor/html-to-markdown-rs/src/converter/inline/ruby.rs +0 -337
  69. data/vendor/html-to-markdown-rs/src/converter/inline/semantic/marks.rs +0 -566
  70. data/vendor/html-to-markdown-rs/src/converter/inline/semantic/mod.rs +0 -86
  71. data/vendor/html-to-markdown-rs/src/converter/inline/semantic/typography.rs +0 -558
  72. data/vendor/html-to-markdown-rs/src/converter/list/definition.rs +0 -232
  73. data/vendor/html-to-markdown-rs/src/converter/list/item.rs +0 -332
  74. data/vendor/html-to-markdown-rs/src/converter/list/mod.rs +0 -70
  75. data/vendor/html-to-markdown-rs/src/converter/list/ordered.rs +0 -201
  76. data/vendor/html-to-markdown-rs/src/converter/list/unordered.rs +0 -195
  77. data/vendor/html-to-markdown-rs/src/converter/list/utils.rs +0 -314
  78. data/vendor/html-to-markdown-rs/src/converter/main.rs +0 -710
  79. data/vendor/html-to-markdown-rs/src/converter/main_helpers.rs +0 -452
  80. data/vendor/html-to-markdown-rs/src/converter/media/embedded.rs +0 -393
  81. data/vendor/html-to-markdown-rs/src/converter/media/graphic.rs +0 -4
  82. data/vendor/html-to-markdown-rs/src/converter/media/image.rs +0 -183
  83. data/vendor/html-to-markdown-rs/src/converter/media/mod.rs +0 -87
  84. data/vendor/html-to-markdown-rs/src/converter/media/svg.rs +0 -280
  85. data/vendor/html-to-markdown-rs/src/converter/metadata.rs +0 -220
  86. data/vendor/html-to-markdown-rs/src/converter/mod.rs +0 -156
  87. data/vendor/html-to-markdown-rs/src/converter/plain_text.rs +0 -516
  88. data/vendor/html-to-markdown-rs/src/converter/preprocessing_helpers.rs +0 -201
  89. data/vendor/html-to-markdown-rs/src/converter/reference_collector.rs +0 -69
  90. data/vendor/html-to-markdown-rs/src/converter/semantic/attributes.rs +0 -269
  91. data/vendor/html-to-markdown-rs/src/converter/semantic/definition_list.rs +0 -266
  92. data/vendor/html-to-markdown-rs/src/converter/semantic/figure.rs +0 -391
  93. data/vendor/html-to-markdown-rs/src/converter/semantic/mod.rs +0 -112
  94. data/vendor/html-to-markdown-rs/src/converter/semantic/sectioning.rs +0 -85
  95. data/vendor/html-to-markdown-rs/src/converter/semantic/summary.rs +0 -324
  96. data/vendor/html-to-markdown-rs/src/converter/text/mod.rs +0 -8
  97. data/vendor/html-to-markdown-rs/src/converter/text/processing.rs +0 -56
  98. data/vendor/html-to-markdown-rs/src/converter/text_node.rs +0 -269
  99. data/vendor/html-to-markdown-rs/src/converter/utility/attributes.rs +0 -151
  100. data/vendor/html-to-markdown-rs/src/converter/utility/caching.rs +0 -74
  101. data/vendor/html-to-markdown-rs/src/converter/utility/content.rs +0 -271
  102. data/vendor/html-to-markdown-rs/src/converter/utility/mod.rs +0 -17
  103. data/vendor/html-to-markdown-rs/src/converter/utility/preprocessing.rs +0 -1002
  104. data/vendor/html-to-markdown-rs/src/converter/utility/serialization.rs +0 -126
  105. data/vendor/html-to-markdown-rs/src/converter/utility/siblings.rs +0 -97
  106. data/vendor/html-to-markdown-rs/src/converter/visitor_hooks.rs +0 -189
  107. data/vendor/html-to-markdown-rs/src/error.rs +0 -43
  108. data/vendor/html-to-markdown-rs/src/exports.rs +0 -24
  109. data/vendor/html-to-markdown-rs/src/inline_images.rs +0 -336
  110. data/vendor/html-to-markdown-rs/src/lib.rs +0 -139
  111. data/vendor/html-to-markdown-rs/src/metadata/collector.rs +0 -457
  112. data/vendor/html-to-markdown-rs/src/metadata/config.rs +0 -394
  113. data/vendor/html-to-markdown-rs/src/metadata/extraction.rs +0 -398
  114. data/vendor/html-to-markdown-rs/src/metadata/mod.rs +0 -288
  115. data/vendor/html-to-markdown-rs/src/metadata/types.rs +0 -477
  116. data/vendor/html-to-markdown-rs/src/options/conversion.rs +0 -559
  117. data/vendor/html-to-markdown-rs/src/options/inline_image.rs +0 -111
  118. data/vendor/html-to-markdown-rs/src/options/mod.rs +0 -20
  119. data/vendor/html-to-markdown-rs/src/options/preprocessing.rs +0 -201
  120. data/vendor/html-to-markdown-rs/src/options/validation.rs +0 -416
  121. data/vendor/html-to-markdown-rs/src/prelude.rs +0 -1
  122. data/vendor/html-to-markdown-rs/src/rcdom.rs +0 -487
  123. data/vendor/html-to-markdown-rs/src/text.rs +0 -358
  124. data/vendor/html-to-markdown-rs/src/types/document.rs +0 -191
  125. data/vendor/html-to-markdown-rs/src/types/mod.rs +0 -17
  126. data/vendor/html-to-markdown-rs/src/types/result.rs +0 -54
  127. data/vendor/html-to-markdown-rs/src/types/structure_builder.rs +0 -791
  128. data/vendor/html-to-markdown-rs/src/types/structure_collector.rs +0 -483
  129. data/vendor/html-to-markdown-rs/src/types/tables.rs +0 -52
  130. data/vendor/html-to-markdown-rs/src/types/warnings.rs +0 -33
  131. data/vendor/html-to-markdown-rs/src/validation.rs +0 -158
  132. data/vendor/html-to-markdown-rs/src/visitor/default_impl.rs +0 -63
  133. data/vendor/html-to-markdown-rs/src/visitor/mod.rs +0 -41
  134. data/vendor/html-to-markdown-rs/src/visitor/traits.rs +0 -370
  135. data/vendor/html-to-markdown-rs/src/visitor/types.rs +0 -319
  136. data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/callbacks/mod.rs +0 -1
  137. data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/content.rs +0 -126
  138. data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/mod.rs +0 -27
  139. data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/state.rs +0 -110
  140. data/vendor/html-to-markdown-rs/src/visitor_helpers/helpers/traversal.rs +0 -250
  141. data/vendor/html-to-markdown-rs/src/visitor_helpers.rs +0 -597
  142. data/vendor/html-to-markdown-rs/src/wrapper/sync.rs +0 -413
  143. data/vendor/html-to-markdown-rs/src/wrapper/utils.rs +0 -290
  144. data/vendor/html-to-markdown-rs/src/wrapper.rs +0 -9
  145. data/vendor/html-to-markdown-rs/tests/br_in_inline_test.rs +0 -87
  146. data/vendor/html-to-markdown-rs/tests/commonmark_compliance_test.rs +0 -297
  147. data/vendor/html-to-markdown-rs/tests/djot_output_test.rs +0 -153
  148. data/vendor/html-to-markdown-rs/tests/exclude_selectors_test.rs +0 -132
  149. data/vendor/html-to-markdown-rs/tests/integration_test.rs +0 -631
  150. data/vendor/html-to-markdown-rs/tests/issue_121_regressions.rs +0 -49
  151. data/vendor/html-to-markdown-rs/tests/issue_127_regressions.rs +0 -58
  152. data/vendor/html-to-markdown-rs/tests/issue_128_regressions.rs +0 -17
  153. data/vendor/html-to-markdown-rs/tests/issue_131_regressions.rs +0 -41
  154. data/vendor/html-to-markdown-rs/tests/issue_134_regressions.rs +0 -40
  155. data/vendor/html-to-markdown-rs/tests/issue_139_regressions.rs +0 -26
  156. data/vendor/html-to-markdown-rs/tests/issue_140_regressions.rs +0 -185
  157. data/vendor/html-to-markdown-rs/tests/issue_143_regressions.rs +0 -100
  158. data/vendor/html-to-markdown-rs/tests/issue_145_regressions.rs +0 -133
  159. data/vendor/html-to-markdown-rs/tests/issue_146_regressions.rs +0 -144
  160. data/vendor/html-to-markdown-rs/tests/issue_176_regressions.rs +0 -62
  161. data/vendor/html-to-markdown-rs/tests/issue_190_regressions.rs +0 -128
  162. data/vendor/html-to-markdown-rs/tests/issue_199_regressions.rs +0 -20
  163. data/vendor/html-to-markdown-rs/tests/issue_200_regressions.rs +0 -62
  164. data/vendor/html-to-markdown-rs/tests/issue_212_regressions.rs +0 -68
  165. data/vendor/html-to-markdown-rs/tests/issue_216_217_regressions.rs +0 -87
  166. data/vendor/html-to-markdown-rs/tests/issue_336_regressions.rs +0 -74
  167. data/vendor/html-to-markdown-rs/tests/issue_339_regressions.rs +0 -92
  168. data/vendor/html-to-markdown-rs/tests/issue_347_regressions.rs +0 -154
  169. data/vendor/html-to-markdown-rs/tests/issue_348_visitor_plain.rs +0 -93
  170. data/vendor/html-to-markdown-rs/tests/json_ld_script_extraction.rs +0 -44
  171. data/vendor/html-to-markdown-rs/tests/lists_test.rs +0 -199
  172. data/vendor/html-to-markdown-rs/tests/plain_output_test.rs +0 -273
  173. data/vendor/html-to-markdown-rs/tests/preprocessing_tests.rs +0 -61
  174. data/vendor/html-to-markdown-rs/tests/reference_links_test.rs +0 -169
  175. data/vendor/html-to-markdown-rs/tests/sectioning_elements_test.rs +0 -137
  176. data/vendor/html-to-markdown-rs/tests/skip_images_test.rs +0 -522
  177. data/vendor/html-to-markdown-rs/tests/tables_test.rs +0 -743
  178. data/vendor/html-to-markdown-rs/tests/test_custom_elements.rs +0 -41
  179. data/vendor/html-to-markdown-rs/tests/test_issue_187.rs +0 -204
  180. data/vendor/html-to-markdown-rs/tests/test_issue_218.rs +0 -68
  181. data/vendor/html-to-markdown-rs/tests/test_issue_277.rs +0 -77
  182. data/vendor/html-to-markdown-rs/tests/test_max_depth.rs +0 -82
  183. data/vendor/html-to-markdown-rs/tests/test_nested_simple.rs +0 -45
  184. data/vendor/html-to-markdown-rs/tests/test_script_style_stripping.rs +0 -396
  185. data/vendor/html-to-markdown-rs/tests/test_spa_bisect.rs +0 -34
  186. data/vendor/html-to-markdown-rs/tests/visitor_code_integration_test.rs +0 -121
  187. data/vendor/html-to-markdown-rs/tests/visitor_integration_test.rs +0 -1190
  188. data/vendor/html-to-markdown-rs/tests/xml_tables_test.rs +0 -372
@@ -1,269 +0,0 @@
1
- //! Text node processing for HTML to Markdown conversion.
2
- //!
3
- //! Handles raw text nodes with:
4
- //! - HTML entity decoding
5
- //! - Whitespace normalization and stripping
6
- //! - Text escaping with configurable escape modes
7
- //! - Visitor callbacks (when feature enabled)
8
- //! - List item indentation
9
-
10
- use std::borrow::Cow;
11
-
12
- use crate::converter::dom_context::DomContext;
13
- use crate::converter::main_helpers::{has_more_than_one_char, is_inline_element};
14
- use crate::converter::utility::siblings::{
15
- get_next_sibling_tag, next_sibling_is_inline_tag, previous_sibling_is_inline_tag,
16
- };
17
- use crate::options::ConversionOptions;
18
- use crate::text;
19
-
20
- // Type aliases for Context to avoid circular imports
21
- type Context = crate::converter::Context;
22
-
23
- /// Process a raw text node during HTML to Markdown conversion.
24
- ///
25
- /// Handles:
26
- /// - HTML entity decoding
27
- /// - Whitespace normalization and stripping
28
- /// - Text escaping with configurable escape modes
29
- /// - Visitor callbacks (when feature enabled)
30
- /// - List item indentation
31
- #[allow(clippy::too_many_lines)]
32
- #[cfg_attr(not(feature = "visitor"), allow(unused_variables))]
33
- pub fn process_text_node(
34
- raw: &str,
35
- node_handle: &tl::NodeHandle,
36
- parser: &tl::Parser,
37
- output: &mut String,
38
- options: &ConversionOptions,
39
- ctx: &Context,
40
- depth: usize,
41
- dom_ctx: &DomContext,
42
- ) {
43
- let mut text = text::decode_html_entities_cow(raw);
44
-
45
- if text.is_empty() {
46
- return;
47
- }
48
-
49
- let text_ref = text.as_ref();
50
- let had_newlines = text_ref.contains('\n');
51
- let has_double_newline = text_ref.contains("\n\n") || text_ref.contains("\r\n\r\n");
52
-
53
- if options.strip_newlines && (text.contains('\r') || text.contains('\n')) {
54
- text = Cow::Owned(text.replace(['\r', '\n'], " "));
55
- }
56
-
57
- if text.trim().is_empty() {
58
- if ctx.in_code {
59
- output.push_str(text.as_ref());
60
- return;
61
- }
62
-
63
- if options.whitespace_mode == crate::options::WhitespaceMode::Strict {
64
- if ctx.convert_as_inline || ctx.in_table_cell || ctx.in_list_item {
65
- output.push_str(text.as_ref());
66
- return;
67
- }
68
- if has_double_newline {
69
- if !output.ends_with("\n\n") {
70
- output.push('\n');
71
- }
72
- return;
73
- }
74
- output.push_str(text.as_ref());
75
- return;
76
- }
77
-
78
- if had_newlines {
79
- if output.is_empty() {
80
- return;
81
- }
82
- if !output.ends_with("\n\n") {
83
- if let Some(next_tag) = get_next_sibling_tag(node_handle, parser, dom_ctx) {
84
- if is_inline_element(next_tag) {
85
- // Newlines between inline elements collapse to a single space
86
- // in HTML rendering (per CSS white-space: normal). Preserve
87
- // this word boundary so adjacent inline content doesn't merge.
88
- if !output.ends_with(' ') && !output.ends_with('\n') {
89
- output.push(' ');
90
- }
91
- return;
92
- }
93
- }
94
- }
95
- return;
96
- }
97
-
98
- if previous_sibling_is_inline_tag(node_handle, parser, dom_ctx)
99
- && next_sibling_is_inline_tag(node_handle, parser, dom_ctx)
100
- {
101
- if has_more_than_one_char(text.as_ref()) {
102
- if !output.ends_with(' ') {
103
- output.push(' ');
104
- }
105
- } else {
106
- output.push_str(text.as_ref());
107
- }
108
- } else {
109
- output.push_str(text.as_ref());
110
- }
111
- return;
112
- }
113
-
114
- let processed_text = if ctx.in_code || ctx.in_ruby {
115
- text.into_owned()
116
- } else if ctx.in_table_cell {
117
- // Always escape * and _ in table cells to prevent unintended emphasis.
118
- let escaped = if options.whitespace_mode == crate::options::WhitespaceMode::Normalized {
119
- let normalized_text = text::normalize_whitespace_cow(text.as_ref());
120
- let escaped_result = text::escape(
121
- normalized_text.as_ref(),
122
- options.escape_misc,
123
- true,
124
- true,
125
- options.escape_ascii,
126
- );
127
- escaped_result.into_owned()
128
- } else {
129
- text::escape(text.as_ref(), options.escape_misc, true, true, options.escape_ascii).into_owned()
130
- };
131
- if options.escape_misc {
132
- escaped
133
- } else {
134
- escaped.replace('|', r"\|")
135
- }
136
- } else if options.whitespace_mode == crate::options::WhitespaceMode::Strict {
137
- text::escape(
138
- text.as_ref(),
139
- options.escape_misc,
140
- options.escape_asterisks,
141
- options.escape_underscores,
142
- options.escape_ascii,
143
- )
144
- .into_owned()
145
- } else {
146
- let has_double_newline = text.contains("\n\n") || text.contains("\r\n\r\n");
147
- let has_trailing_single_newline =
148
- text.ends_with('\n') && !text.ends_with("\n\n") && !text.ends_with("\r\n\r\n");
149
-
150
- let normalized_text = text::normalize_whitespace_cow(text.as_ref());
151
-
152
- let (prefix, suffix, core) = text::chomp(normalized_text.as_ref());
153
-
154
- let skip_prefix = output.ends_with("\n\n")
155
- || output.ends_with("* ")
156
- || output.ends_with("- ")
157
- || output.ends_with(". ")
158
- || output.ends_with("] ")
159
- || (output.ends_with('\n') && prefix == " ")
160
- || (output.ends_with(' ')
161
- && prefix == " "
162
- && !previous_sibling_is_inline_tag(node_handle, parser, dom_ctx));
163
-
164
- let mut final_text = String::with_capacity(prefix.len() + core.len() + suffix.len() + 2);
165
- if !skip_prefix && !prefix.is_empty() {
166
- final_text.push_str(prefix);
167
- }
168
-
169
- let escaped_core = text::escape(
170
- core,
171
- options.escape_misc,
172
- options.escape_asterisks,
173
- options.escape_underscores,
174
- options.escape_ascii,
175
- );
176
- final_text.push_str(&escaped_core);
177
-
178
- if !suffix.is_empty() {
179
- final_text.push_str(suffix);
180
- } else if has_trailing_single_newline {
181
- // Check if the "\n\n" at the end of the output buffer came from within
182
- // the current block's content, not from a previous block's closing.
183
- // Without this distinction, the second paragraph after a "\n\n" boundary
184
- // would incorrectly suppress the trailing space before inline elements.
185
- let safe_start = ctx.block_content_start.min(output.len());
186
- let safe_start = crate::converter::utility::content::floor_char_boundary(output, safe_start);
187
- let current_block_output = &output[safe_start..];
188
- let at_paragraph_break = current_block_output.ends_with("\n\n");
189
- if !at_paragraph_break {
190
- if has_double_newline {
191
- final_text.push('\n');
192
- } else if let Some(next_tag) = get_next_sibling_tag(node_handle, parser, dom_ctx) {
193
- if matches!(next_tag, "span") {
194
- } else if ctx.inline_depth > 0 || ctx.convert_as_inline || ctx.in_paragraph {
195
- final_text.push(' ');
196
- } else {
197
- final_text.push('\n');
198
- }
199
- } else if ctx.inline_depth > 0 || ctx.convert_as_inline || ctx.in_paragraph {
200
- final_text.push(' ');
201
- } else {
202
- final_text.push('\n');
203
- }
204
- }
205
- }
206
-
207
- final_text
208
- };
209
-
210
- #[cfg(feature = "visitor")]
211
- let final_text = if let Some(ref visitor_handle) = ctx.visitor {
212
- use crate::visitor::{NodeContext, NodeType, VisitResult};
213
- use std::collections::BTreeMap;
214
-
215
- let node_id = node_handle.get_inner();
216
- let parent_tag = dom_ctx.parent_tag_name(node_id, parser);
217
- let index_in_parent = dom_ctx.get_sibling_index(node_id).unwrap_or(0);
218
-
219
- let node_ctx = NodeContext {
220
- node_type: NodeType::Text,
221
- tag_name: String::new(),
222
- attributes: BTreeMap::new(),
223
- depth,
224
- index_in_parent,
225
- parent_tag,
226
- is_inline: true,
227
- };
228
-
229
- let mut visitor = visitor_handle.borrow_mut();
230
- match visitor.visit_text(&node_ctx, &processed_text) {
231
- VisitResult::Continue => processed_text,
232
- VisitResult::Custom(custom) => {
233
- if ctx.inline_depth > 0 || ctx.in_heading {
234
- processed_text
235
- } else {
236
- custom
237
- }
238
- }
239
- VisitResult::Skip => return,
240
- VisitResult::Error(err) => {
241
- if ctx.visitor_error.borrow().is_none() {
242
- *ctx.visitor_error.borrow_mut() = Some(err);
243
- }
244
- return;
245
- }
246
- VisitResult::PreserveHtml => processed_text,
247
- }
248
- } else {
249
- processed_text
250
- };
251
-
252
- #[cfg(not(feature = "visitor"))]
253
- let final_text = processed_text;
254
-
255
- if ctx.in_list_item && final_text.contains("\n\n") {
256
- let indent = " ".repeat(4 * ctx.list_depth);
257
- let mut first = true;
258
- for part in final_text.split("\n\n") {
259
- if !first {
260
- output.push_str("\n\n");
261
- output.push_str(&indent);
262
- }
263
- first = false;
264
- output.push_str(part.trim());
265
- }
266
- } else {
267
- output.push_str(&final_text);
268
- }
269
- }
@@ -1,151 +0,0 @@
1
- //! Attribute handling and extraction utilities.
2
- //!
3
- //! Functions for working with element attributes, semantic detection, and hOCR document detection.
4
-
5
- use crate::converter::DomContext;
6
- use crate::converter::utility::content::normalized_tag_name;
7
-
8
- /// Check if a tag has main content semantics based on role or class.
9
- pub fn tag_has_main_semantics(tag: &tl::HTMLTag) -> bool {
10
- if let Some(Some(role)) = tag.attributes().get("role") {
11
- let lowered = role.as_utf8_str().to_ascii_lowercase();
12
- if matches!(lowered.as_str(), "main" | "article" | "document" | "region") {
13
- return true;
14
- }
15
- }
16
-
17
- if let Some(Some(class_bytes)) = tag.attributes().get("class") {
18
- let class_value = class_bytes.as_utf8_str().to_ascii_lowercase();
19
- const MAIN_CLASS_HINTS: &[&str] = &[
20
- "mw-body",
21
- "mw-parser-output",
22
- "content-body",
23
- "content-container",
24
- "article-body",
25
- "article-content",
26
- "main-content",
27
- "page-content",
28
- "entry-content",
29
- "post-content",
30
- "document-body",
31
- ];
32
- if MAIN_CLASS_HINTS.iter().any(|hint| class_value.contains(hint)) {
33
- return true;
34
- }
35
- }
36
-
37
- false
38
- }
39
-
40
- /// Check if an element has navigation-related hints in its attributes.
41
- pub fn element_has_navigation_hint(tag: &tl::HTMLTag) -> bool {
42
- if attribute_matches_any(tag, "role", &["navigation", "menubar", "tablist", "toolbar"]) {
43
- return true;
44
- }
45
-
46
- if attribute_contains_any(
47
- tag,
48
- "aria-label",
49
- &["navigation", "menu", "contents", "table of contents", "toc"],
50
- ) {
51
- return true;
52
- }
53
-
54
- const NAV_KEYWORDS: &[&str] = &[
55
- "nav",
56
- "navigation",
57
- "navbar",
58
- "breadcrumbs",
59
- "breadcrumb",
60
- "toc",
61
- "sidebar",
62
- "sidenav",
63
- "menu",
64
- "menubar",
65
- "mainmenu",
66
- "subnav",
67
- "tabs",
68
- "tablist",
69
- "toolbar",
70
- "pager",
71
- "pagination",
72
- "skipnav",
73
- "skip-link",
74
- "skiplinks",
75
- "site-nav",
76
- "site-menu",
77
- "site-header",
78
- "site-footer",
79
- "topbar",
80
- "bottombar",
81
- "masthead",
82
- "vector-nav",
83
- "vector-header",
84
- "vector-footer",
85
- ];
86
-
87
- attribute_matches_any(tag, "class", NAV_KEYWORDS) || attribute_matches_any(tag, "id", NAV_KEYWORDS)
88
- }
89
-
90
- /// Check if an attribute value matches any of the given keywords (space or custom-separator aware).
91
- pub fn attribute_matches_any(tag: &tl::HTMLTag, attr: &str, keywords: &[&str]) -> bool {
92
- let Some(attr_value) = tag.attributes().get(attr) else {
93
- return false;
94
- };
95
- let Some(value) = attr_value else {
96
- return false;
97
- };
98
- let raw = value.as_utf8_str();
99
- raw.split_whitespace()
100
- .map(|token| {
101
- token
102
- .chars()
103
- .map(|c| match c {
104
- '_' | ':' | '.' | '/' => '-',
105
- _ => c,
106
- })
107
- .collect::<String>()
108
- .to_ascii_lowercase()
109
- })
110
- .filter(|token| !token.is_empty())
111
- .any(|token| keywords.iter().any(|kw| token == *kw))
112
- }
113
-
114
- /// Check if an attribute contains any of the given keywords (substring match).
115
- #[allow(clippy::trivially_copy_pass_by_ref)]
116
- pub fn attribute_contains_any(tag: &tl::HTMLTag, attr: &str, keywords: &[&str]) -> bool {
117
- let Some(attr_value) = tag.attributes().get(attr) else {
118
- return false;
119
- };
120
- let Some(value) = attr_value else {
121
- return false;
122
- };
123
- let lower = value.as_utf8_str().to_ascii_lowercase();
124
- keywords.iter().any(|kw| lower.contains(*kw))
125
- }
126
-
127
- /// Check if a node has a semantic content ancestor (main, article, section).
128
- #[allow(clippy::trivially_copy_pass_by_ref)]
129
- pub fn has_semantic_content_ancestor(node_handle: &tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
130
- let mut current_id = node_handle.get_inner();
131
- while let Some(parent_id) = dom_ctx.parent_of(current_id) {
132
- if let Some(parent_info) = dom_ctx.tag_info(parent_id, parser) {
133
- if matches!(parent_info.name.as_str(), "main" | "article" | "section") {
134
- return true;
135
- }
136
- }
137
- if let Some(parent_handle) = dom_ctx.node_handle(parent_id) {
138
- if let Some(tl::Node::Tag(parent_tag)) = parent_handle.get(parser) {
139
- let parent_name = normalized_tag_name(parent_tag.name().as_utf8_str());
140
- if matches!(parent_name.as_ref(), "main" | "article" | "section") {
141
- return true;
142
- }
143
- if tag_has_main_semantics(parent_tag) {
144
- return true;
145
- }
146
- }
147
- }
148
- current_id = parent_id;
149
- }
150
- false
151
- }
@@ -1,74 +0,0 @@
1
- //! Performance caching utilities.
2
- //!
3
- //! Caching mechanisms for expensive operations during conversion, including
4
- //! DOM context building and cache capacity management.
5
-
6
- use crate::converter::DomContext;
7
- use std::num::NonZeroUsize;
8
-
9
- /// Build a DOM context with hierarchical node information.
10
- ///
11
- /// Pre-computes parent-child relationships, sibling indices, and caches
12
- /// tag information for efficient DOM navigation during conversion.
13
- pub fn build_dom_context(dom: &tl::VDom, parser: &tl::Parser, input_len: usize) -> DomContext {
14
- let cache_capacity = text_cache_capacity_for_input(input_len);
15
- let mut ctx = DomContext {
16
- parent_map: Vec::new(),
17
- children_map: Vec::new(),
18
- sibling_index_map: Vec::new(),
19
- root_children: dom.children().to_vec(),
20
- node_map: Vec::new(),
21
- tag_info_map: Vec::new(),
22
- prev_inline_like_map: Vec::new(),
23
- next_inline_like_map: Vec::new(),
24
- next_tag_map: Vec::new(),
25
- next_whitespace_map: Vec::new(),
26
- text_cache: std::cell::RefCell::new(lru::LruCache::new(cache_capacity)),
27
- };
28
-
29
- for (index, child_handle) in dom.children().iter().enumerate() {
30
- let id = child_handle.get_inner();
31
- ctx.ensure_capacity(id);
32
- ctx.sibling_index_map[id as usize] = Some(index);
33
- record_node_hierarchy(*child_handle, None, parser, &mut ctx);
34
- }
35
-
36
- ctx
37
- }
38
-
39
- /// Calculate appropriate cache capacity based on input size.
40
- ///
41
- /// Returns a cache capacity between 32 and TEXT_CACHE_CAPACITY,
42
- /// scaled proportionally to input size (1KB = 1 slot).
43
- pub fn text_cache_capacity_for_input(input_len: usize) -> NonZeroUsize {
44
- const TEXT_CACHE_CAPACITY: usize = 256;
45
- // `clamp(32, TEXT_CACHE_CAPACITY)` guarantees `target >= 32 > 0`, so `new` always returns Some.
46
- let target = (input_len / 1024).clamp(32, TEXT_CACHE_CAPACITY);
47
- NonZeroUsize::new(target).unwrap_or(NonZeroUsize::MIN)
48
- }
49
-
50
- /// Recursively record node hierarchy into DOM context.
51
- ///
52
- /// Builds the complete parent-child relationship map for efficient tree traversal.
53
- pub fn record_node_hierarchy(
54
- node_handle: tl::NodeHandle,
55
- parent: Option<u32>,
56
- parser: &tl::Parser,
57
- ctx: &mut DomContext,
58
- ) {
59
- let id = node_handle.get_inner();
60
- ctx.ensure_capacity(id);
61
- ctx.parent_map[id as usize] = parent;
62
- ctx.node_map[id as usize] = Some(node_handle);
63
-
64
- if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
65
- let children: Vec<_> = tag.children().top().iter().copied().collect();
66
- for (index, child) in children.iter().enumerate() {
67
- let child_id = child.get_inner();
68
- ctx.ensure_capacity(child_id);
69
- ctx.sibling_index_map[child_id as usize] = Some(index);
70
- record_node_hierarchy(*child, Some(id), parser, ctx);
71
- }
72
- ctx.children_map[id as usize] = Some(children);
73
- }
74
- }