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,201 +0,0 @@
1
- //! Ordered list handling (ol, li elements).
2
- //!
3
- //! Processes ordered lists with support for:
4
- //! - Custom start counters
5
- //! - Nested list handling
6
- //! - Loose/tight list detection
7
- //! - Proper indentation and numbering
8
-
9
- use super::utils::{
10
- add_list_leading_separator, add_nested_list_trailing_separator, calculate_list_nesting_depth, is_loose_list,
11
- process_list_children,
12
- };
13
- #[cfg(feature = "visitor")]
14
- use crate::converter::utility::content::collect_tag_attributes;
15
- use crate::options::ConversionOptions;
16
- #[allow(unused_imports)]
17
- use std::collections::BTreeMap;
18
- use tl;
19
-
20
- // Type aliases for Context and DomContext to avoid circular imports
21
- type Context = crate::converter::Context;
22
- type DomContext = crate::converter::DomContext;
23
-
24
- /// Handle ordered list element (<ol>).
25
- ///
26
- /// Extracts the `start` attribute to set initial counter value,
27
- /// detects loose/tight list format, and processes list items.
28
- #[allow(clippy::too_many_arguments)]
29
- pub fn handle_ol(
30
- node_handle: &tl::NodeHandle,
31
- parser: &tl::Parser,
32
- output: &mut String,
33
- options: &ConversionOptions,
34
- ctx: &Context,
35
- depth: usize,
36
- dom_ctx: &DomContext,
37
- ) {
38
- add_list_leading_separator(output, ctx);
39
-
40
- let nested_depth = calculate_list_nesting_depth(ctx);
41
- let is_loose = is_loose_list(*node_handle, parser, dom_ctx);
42
-
43
- let tag = match node_handle.get(parser) {
44
- Some(tl::Node::Tag(t)) => t,
45
- _ => return,
46
- };
47
-
48
- let start = tag
49
- .attributes()
50
- .get("start")
51
- .flatten()
52
- .and_then(|v| v.as_utf8_str().parse::<usize>().ok())
53
- .unwrap_or(1);
54
-
55
- #[cfg(feature = "visitor")]
56
- let list_output_start = output.len();
57
-
58
- #[cfg(feature = "visitor")]
59
- let mut list_start_custom: Option<String> = None;
60
-
61
- #[cfg(feature = "visitor")]
62
- if let Some(ref visitor_handle) = ctx.visitor {
63
- use crate::visitor::{NodeContext, NodeType, VisitResult};
64
-
65
- let attributes: BTreeMap<String, String> = collect_tag_attributes(tag);
66
-
67
- let parent_tag = dom_ctx
68
- .parent_of(node_handle.get_inner())
69
- .and_then(|pid| dom_ctx.tag_name_for(dom_ctx.node_handle(pid).copied()?, parser))
70
- .map(|s| s.to_string());
71
-
72
- let index = dom_ctx.sibling_index(node_handle.get_inner()).unwrap_or(0);
73
-
74
- let node_ctx = NodeContext {
75
- node_type: NodeType::List,
76
- tag_name: "ol".to_string(),
77
- attributes,
78
- depth,
79
- index_in_parent: index,
80
- parent_tag,
81
- is_inline: false,
82
- };
83
-
84
- let visit_result = {
85
- let mut visitor = visitor_handle.borrow_mut();
86
- visitor.visit_list_start(&node_ctx, true)
87
- };
88
- match visit_result {
89
- VisitResult::Continue => {}
90
- VisitResult::Custom(custom) => {
91
- list_start_custom = Some(custom);
92
- }
93
- VisitResult::Skip => {
94
- return;
95
- }
96
- VisitResult::PreserveHtml => {
97
- use crate::converter::serialize_node_to_html;
98
- serialize_node_to_html(node_handle, parser, output);
99
- return;
100
- }
101
- VisitResult::Error(err) => {
102
- if ctx.visitor_error.borrow().is_none() {
103
- *ctx.visitor_error.borrow_mut() = Some(err);
104
- }
105
- return;
106
- }
107
- }
108
- }
109
-
110
- if !ctx.in_table_cell {
111
- if let Some(ref sc) = ctx.structure_collector {
112
- sc.borrow_mut().push_list_start(true);
113
- }
114
- }
115
-
116
- process_list_children(
117
- *node_handle,
118
- parser,
119
- output,
120
- options,
121
- ctx,
122
- depth,
123
- true,
124
- is_loose,
125
- nested_depth,
126
- start,
127
- dom_ctx,
128
- );
129
-
130
- if !ctx.in_table_cell {
131
- if let Some(ref sc) = ctx.structure_collector {
132
- sc.borrow_mut().push_list_end();
133
- }
134
- }
135
-
136
- add_nested_list_trailing_separator(output, ctx);
137
-
138
- #[cfg(feature = "visitor")]
139
- if let Some(ref visitor_handle) = ctx.visitor {
140
- use crate::visitor::{NodeContext, NodeType, VisitResult};
141
-
142
- let attributes: BTreeMap<String, String> = collect_tag_attributes(tag);
143
-
144
- let parent_tag = dom_ctx
145
- .parent_of(node_handle.get_inner())
146
- .and_then(|pid| dom_ctx.tag_name_for(dom_ctx.node_handle(pid).copied()?, parser))
147
- .map(|s| s.to_string());
148
-
149
- let index = dom_ctx.sibling_index(node_handle.get_inner()).unwrap_or(0);
150
-
151
- let node_ctx = NodeContext {
152
- node_type: NodeType::List,
153
- tag_name: "ol".to_string(),
154
- attributes,
155
- depth,
156
- index_in_parent: index,
157
- parent_tag,
158
- is_inline: false,
159
- };
160
-
161
- let list_content = &output[list_output_start..];
162
-
163
- let visit_result = {
164
- let mut visitor = visitor_handle.borrow_mut();
165
- visitor.visit_list_end(&node_ctx, true, list_content)
166
- };
167
- match visit_result {
168
- VisitResult::Continue => {
169
- if let Some(custom_start) = list_start_custom {
170
- output.insert_str(list_output_start, &custom_start);
171
- }
172
- }
173
- VisitResult::Custom(custom) => {
174
- let children_output = output[list_output_start..].to_string();
175
- output.truncate(list_output_start);
176
- if let Some(custom_start) = list_start_custom {
177
- output.push_str(&custom_start);
178
- }
179
- output.push_str(&children_output);
180
- output.push_str(&custom);
181
- }
182
- VisitResult::Skip => {
183
- output.truncate(list_output_start);
184
- }
185
- VisitResult::PreserveHtml => {
186
- output.truncate(list_output_start);
187
- use crate::converter::serialize_node_to_html;
188
- serialize_node_to_html(node_handle, parser, output);
189
- }
190
- VisitResult::Error(err) => {
191
- if ctx.visitor_error.borrow().is_none() {
192
- *ctx.visitor_error.borrow_mut() = Some(err);
193
- }
194
- output.truncate(list_output_start);
195
- }
196
- }
197
- }
198
- }
199
-
200
- /// Public alias for `handle_ol` to match the expected module interface.
201
- pub use handle_ol as handle;
@@ -1,195 +0,0 @@
1
- //! Unordered list handling (ul, li elements).
2
- //!
3
- //! Processes unordered lists with support for:
4
- //! - Bullet cycling based on nesting depth
5
- //! - Nested list handling
6
- //! - Loose/tight list detection
7
- //! - Proper indentation
8
-
9
- use super::utils::{
10
- add_list_leading_separator, add_nested_list_trailing_separator, calculate_list_nesting_depth, is_loose_list,
11
- process_list_children,
12
- };
13
- #[cfg(feature = "visitor")]
14
- use crate::converter::utility::content::collect_tag_attributes;
15
- use crate::options::ConversionOptions;
16
- #[cfg(feature = "visitor")]
17
- use std::collections::BTreeMap;
18
- use tl;
19
-
20
- // Type aliases for Context and DomContext to avoid circular imports
21
- type Context = crate::converter::Context;
22
- type DomContext = crate::converter::DomContext;
23
-
24
- /// Handle unordered list element (<ul>).
25
- ///
26
- /// Detects loose/tight list format, handles nested bullets,
27
- /// and processes list items with proper indentation.
28
- #[allow(clippy::too_many_arguments)]
29
- pub fn handle_ul(
30
- node_handle: &tl::NodeHandle,
31
- parser: &tl::Parser,
32
- output: &mut String,
33
- options: &ConversionOptions,
34
- ctx: &Context,
35
- depth: usize,
36
- dom_ctx: &DomContext,
37
- ) {
38
- add_list_leading_separator(output, ctx);
39
-
40
- let nested_depth = calculate_list_nesting_depth(ctx);
41
- let is_loose = is_loose_list(*node_handle, parser, dom_ctx);
42
-
43
- #[allow(unused_variables)]
44
- let tag = match node_handle.get(parser) {
45
- Some(tl::Node::Tag(t)) => t,
46
- _ => return,
47
- };
48
-
49
- #[cfg(feature = "visitor")]
50
- let list_output_start = output.len();
51
-
52
- #[cfg(feature = "visitor")]
53
- let mut list_start_custom: Option<String> = None;
54
-
55
- #[cfg(feature = "visitor")]
56
- if let Some(ref visitor_handle) = ctx.visitor {
57
- use crate::visitor::{NodeContext, NodeType, VisitResult};
58
-
59
- let attributes: BTreeMap<String, String> = collect_tag_attributes(tag);
60
-
61
- let parent_tag = dom_ctx
62
- .parent_of(node_handle.get_inner())
63
- .and_then(|pid| dom_ctx.tag_name_for(dom_ctx.node_handle(pid).copied()?, parser))
64
- .map(|s| s.to_string());
65
-
66
- let index = dom_ctx.sibling_index(node_handle.get_inner()).unwrap_or(0);
67
-
68
- let node_ctx = NodeContext {
69
- node_type: NodeType::List,
70
- tag_name: "ul".to_string(),
71
- attributes,
72
- depth,
73
- index_in_parent: index,
74
- parent_tag,
75
- is_inline: false,
76
- };
77
-
78
- let visit_result = {
79
- let mut visitor = visitor_handle.borrow_mut();
80
- visitor.visit_list_start(&node_ctx, false)
81
- };
82
- match visit_result {
83
- VisitResult::Continue => {}
84
- VisitResult::Custom(custom) => {
85
- list_start_custom = Some(custom);
86
- }
87
- VisitResult::Skip => {
88
- return;
89
- }
90
- VisitResult::PreserveHtml => {
91
- use crate::converter::serialize_node_to_html;
92
- serialize_node_to_html(node_handle, parser, output);
93
- return;
94
- }
95
- VisitResult::Error(err) => {
96
- if ctx.visitor_error.borrow().is_none() {
97
- *ctx.visitor_error.borrow_mut() = Some(err);
98
- }
99
- return;
100
- }
101
- }
102
- }
103
-
104
- if !ctx.in_table_cell {
105
- if let Some(ref sc) = ctx.structure_collector {
106
- sc.borrow_mut().push_list_start(false);
107
- }
108
- }
109
-
110
- process_list_children(
111
- *node_handle,
112
- parser,
113
- output,
114
- options,
115
- ctx,
116
- depth,
117
- false,
118
- is_loose,
119
- nested_depth,
120
- 1,
121
- dom_ctx,
122
- );
123
-
124
- if !ctx.in_table_cell {
125
- if let Some(ref sc) = ctx.structure_collector {
126
- sc.borrow_mut().push_list_end();
127
- }
128
- }
129
-
130
- add_nested_list_trailing_separator(output, ctx);
131
-
132
- #[cfg(feature = "visitor")]
133
- if let Some(ref visitor_handle) = ctx.visitor {
134
- use crate::visitor::{NodeContext, NodeType, VisitResult};
135
-
136
- let attributes: BTreeMap<String, String> = collect_tag_attributes(tag);
137
-
138
- let parent_tag = dom_ctx
139
- .parent_of(node_handle.get_inner())
140
- .and_then(|pid| dom_ctx.tag_name_for(dom_ctx.node_handle(pid).copied()?, parser))
141
- .map(|s| s.to_string());
142
-
143
- let index = dom_ctx.sibling_index(node_handle.get_inner()).unwrap_or(0);
144
-
145
- let node_ctx = NodeContext {
146
- node_type: NodeType::List,
147
- tag_name: "ul".to_string(),
148
- attributes,
149
- depth,
150
- index_in_parent: index,
151
- parent_tag,
152
- is_inline: false,
153
- };
154
-
155
- let list_content = &output[list_output_start..];
156
-
157
- let visit_result = {
158
- let mut visitor = visitor_handle.borrow_mut();
159
- visitor.visit_list_end(&node_ctx, false, list_content)
160
- };
161
- match visit_result {
162
- VisitResult::Continue => {
163
- if let Some(custom_start) = list_start_custom {
164
- output.insert_str(list_output_start, &custom_start);
165
- }
166
- }
167
- VisitResult::Custom(custom) => {
168
- let children_output = output[list_output_start..].to_string();
169
- output.truncate(list_output_start);
170
- if let Some(custom_start) = list_start_custom {
171
- output.push_str(&custom_start);
172
- }
173
- output.push_str(&children_output);
174
- output.push_str(&custom);
175
- }
176
- VisitResult::Skip => {
177
- output.truncate(list_output_start);
178
- }
179
- VisitResult::PreserveHtml => {
180
- output.truncate(list_output_start);
181
- use crate::converter::serialize_node_to_html;
182
- serialize_node_to_html(node_handle, parser, output);
183
- }
184
- VisitResult::Error(err) => {
185
- if ctx.visitor_error.borrow().is_none() {
186
- *ctx.visitor_error.borrow_mut() = Some(err);
187
- }
188
- output.truncate(list_output_start);
189
- }
190
- }
191
- }
192
- }
193
-
194
- /// Public alias for `handle_ul` to match the expected module interface.
195
- pub use handle_ul as handle;
@@ -1,314 +0,0 @@
1
- //! Utility functions for list processing.
2
- //!
3
- //! Contains helper functions for loose list detection, indentation calculation,
4
- //! list spacing, and list child processing.
5
-
6
- use crate::converter::main_helpers::{tag_name_eq, trim_trailing_whitespace};
7
- use crate::options::{ConversionOptions, ListIndentType};
8
- use tl;
9
-
10
- // Type aliases for Context and DomContext to avoid circular imports
11
- // These are imported from converter.rs and should be made accessible
12
- type Context = crate::converter::Context;
13
- type DomContext = crate::converter::DomContext;
14
-
15
- /// Calculate indentation level for list item continuations.
16
- ///
17
- /// Returns the number of 4-space indent groups needed for list continuations.
18
- ///
19
- /// List continuations (block elements inside list items) need special indentation:
20
- /// - Base indentation: (depth - 1) groups (for the nesting level)
21
- /// - Content indentation: depth groups (for the list item content)
22
- /// - Combined formula: (2 * depth - 1) groups of 4 spaces each
23
- ///
24
- /// # Examples
25
- ///
26
- /// ```text
27
- /// * Item 1 (depth=0, no continuation)
28
- /// * Item 2 (depth=0)
29
- /// Continuation (depth=0: 0 groups = 0 spaces)
30
- ///
31
- /// * Level 1 (depth=0)
32
- /// + Level 2 (depth=1)
33
- /// Cont (depth=1: (2*1-1) = 1 group = 4 spaces, total 12 with bullet indent)
34
- /// ```
35
- pub const fn calculate_list_continuation_indent(depth: usize) -> usize {
36
- if depth > 0 { 2 * depth - 1 } else { 0 }
37
- }
38
-
39
- /// Check if a list (ul or ol) is "loose".
40
- ///
41
- /// A loose list is one where any list item contains block-level elements
42
- /// like paragraphs (<p>). In loose lists, all items should have blank line
43
- /// separation (ending with \n\n) regardless of their own content.
44
- ///
45
- /// # Examples
46
- ///
47
- /// ```html
48
- /// <!-- Loose list (has <p> in an item) -->
49
- /// <ul>
50
- /// <li><p>Item 1</p></li>
51
- /// <li>Item 2</li> <!-- Also gets \n\n ending -->
52
- /// </ul>
53
- ///
54
- /// <!-- Tight list (no block elements) -->
55
- /// <ul>
56
- /// <li>Item 1</li>
57
- /// <li>Item 2</li>
58
- /// </ul>
59
- /// ```
60
- pub fn is_loose_list(node_handle: tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
61
- if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
62
- let children = tag.children();
63
- {
64
- for child_handle in children.top().iter() {
65
- let is_li = dom_ctx.tag_info(child_handle.get_inner(), parser).map_or_else(
66
- || {
67
- matches!(
68
- child_handle.get(parser),
69
- Some(tl::Node::Tag(child_tag))
70
- if tag_name_eq(child_tag.name().as_utf8_str(), "li")
71
- )
72
- },
73
- |info| info.name == "li",
74
- );
75
- if !is_li {
76
- continue;
77
- }
78
-
79
- if let Some(tl::Node::Tag(child_tag)) = child_handle.get(parser) {
80
- let li_children = child_tag.children();
81
- for li_child_handle in li_children.top().iter() {
82
- let is_p = dom_ctx.tag_info(li_child_handle.get_inner(), parser).map_or_else(
83
- || {
84
- matches!(
85
- li_child_handle.get(parser),
86
- Some(tl::Node::Tag(li_child_tag))
87
- if tag_name_eq(li_child_tag.name().as_utf8_str(), "p")
88
- )
89
- },
90
- |info| info.name == "p",
91
- );
92
- if is_p {
93
- return true;
94
- }
95
- }
96
- }
97
- }
98
- }
99
- }
100
- false
101
- }
102
-
103
- /// Add list continuation indentation to output.
104
- ///
105
- /// Used when block elements (like <p> or <div>) appear inside list items.
106
- /// Adds appropriate line separation and indentation to continue the list item.
107
- ///
108
- /// # Arguments
109
- ///
110
- /// * `output` - The output string to append to
111
- /// * `list_depth` - Current list nesting depth
112
- /// * `blank_line` - If true, adds blank line separation (\n\n); if false, single newline (\n)
113
- ///
114
- /// # Examples
115
- ///
116
- /// ```text
117
- /// Paragraph continuation (blank_line = true):
118
- /// * First para
119
- ///
120
- /// Second para (blank line + indentation)
121
- ///
122
- /// Div continuation (blank_line = false):
123
- /// * First div
124
- /// Second div (single newline + indentation)
125
- /// ```
126
- pub fn add_list_continuation_indent(
127
- output: &mut String,
128
- list_depth: usize,
129
- blank_line: bool,
130
- options: &ConversionOptions,
131
- ) {
132
- trim_trailing_whitespace(output);
133
-
134
- if blank_line {
135
- if !output.ends_with("\n\n") {
136
- if output.ends_with('\n') {
137
- output.push('\n');
138
- } else {
139
- output.push_str("\n\n");
140
- }
141
- }
142
- } else if !output.ends_with('\n') {
143
- output.push('\n');
144
- }
145
-
146
- let indent_level = calculate_list_continuation_indent(list_depth);
147
- let indent_char = match options.list_indent_type {
148
- ListIndentType::Tabs => "\t",
149
- ListIndentType::Spaces => {
150
- for _ in 0..options.list_indent_width {
151
- output.push(' ');
152
- }
153
- return;
154
- }
155
- };
156
- for _ in 0..indent_level {
157
- output.push_str(indent_char);
158
- }
159
- }
160
-
161
- /// Calculate the indentation string for list continuations based on depth and options.
162
- pub fn continuation_indent_string(list_depth: usize, options: &ConversionOptions) -> Option<String> {
163
- let indent_level = calculate_list_continuation_indent(list_depth);
164
- if indent_level == 0 {
165
- return None;
166
- }
167
-
168
- let mut indent = String::new();
169
- match options.list_indent_type {
170
- ListIndentType::Tabs => {
171
- for _ in 0..indent_level {
172
- indent.push('\t');
173
- }
174
- }
175
- ListIndentType::Spaces => {
176
- for _ in 0..(options.list_indent_width * indent_level) {
177
- indent.push(' ');
178
- }
179
- }
180
- }
181
- Some(indent)
182
- }
183
-
184
- /// Add appropriate leading separator before a list.
185
- ///
186
- /// Lists need different separators depending on context:
187
- /// - In table cells: <br> tag if there's already content
188
- /// - Outside lists: blank line (\n\n) if needed
189
- /// - Inside list items: blank line before nested list
190
- pub fn add_list_leading_separator(output: &mut String, ctx: &Context) {
191
- if ctx.in_table_cell {
192
- let is_table_continuation =
193
- !output.is_empty() && !output.ends_with('|') && !output.ends_with(' ') && !output.ends_with("<br>");
194
- if is_table_continuation {
195
- output.push_str("<br>");
196
- }
197
- return;
198
- }
199
-
200
- if !output.is_empty() && !ctx.in_list {
201
- let needs_newline =
202
- !output.ends_with("\n\n") && !output.ends_with("* ") && !output.ends_with("- ") && !output.ends_with(". ");
203
- if needs_newline {
204
- output.push_str("\n\n");
205
- }
206
- return;
207
- }
208
-
209
- if ctx.in_list_item && !output.is_empty() {
210
- let needs_newline =
211
- !output.ends_with('\n') && !output.ends_with("* ") && !output.ends_with("- ") && !output.ends_with(". ");
212
- if needs_newline {
213
- trim_trailing_whitespace(output);
214
- output.push('\n');
215
- }
216
- }
217
- }
218
-
219
- /// Add appropriate trailing separator after a nested list.
220
- ///
221
- /// Nested lists inside list items need trailing newlines to separate
222
- /// from following content. In loose lists, use blank line (\n\n). In tight lists, single newline (\n).
223
- pub fn add_nested_list_trailing_separator(output: &mut String, ctx: &Context) {
224
- if !ctx.in_list_item {
225
- return;
226
- }
227
-
228
- if ctx.loose_list {
229
- if !output.ends_with("\n\n") {
230
- if !output.ends_with('\n') {
231
- output.push('\n');
232
- }
233
- output.push('\n');
234
- }
235
- } else if !output.ends_with('\n') {
236
- output.push('\n');
237
- }
238
- }
239
-
240
- /// Calculate the nesting depth for a list.
241
- ///
242
- /// If we're in a list but NOT in a list item, this is incorrectly nested HTML
243
- /// and we need to increment the depth. If in a list item, the depth was already
244
- /// incremented by the <li> element.
245
- pub const fn calculate_list_nesting_depth(ctx: &Context) -> usize {
246
- if ctx.in_list && !ctx.in_list_item {
247
- ctx.list_depth + 1
248
- } else {
249
- ctx.list_depth
250
- }
251
- }
252
-
253
- /// Check if a node is a list item element.
254
- pub fn is_list_item(node_handle: tl::NodeHandle, parser: &tl::Parser, dom_ctx: &DomContext) -> bool {
255
- if let Some(info) = dom_ctx.tag_info(node_handle.get_inner(), parser) {
256
- return info.name == "li";
257
- }
258
- matches!(
259
- node_handle.get(parser),
260
- Some(tl::Node::Tag(tag)) if tag_name_eq(tag.name().as_utf8_str(), "li")
261
- )
262
- }
263
-
264
- /// Process a list's children, tracking which items had block elements.
265
- ///
266
- /// This is used to determine proper spacing between list items.
267
- /// Returns true if the last processed item had block children.
268
- #[allow(clippy::too_many_arguments)]
269
- pub fn process_list_children(
270
- node_handle: tl::NodeHandle,
271
- parser: &tl::Parser,
272
- output: &mut String,
273
- options: &ConversionOptions,
274
- ctx: &Context,
275
- depth: usize,
276
- is_ordered: bool,
277
- is_loose: bool,
278
- nested_depth: usize,
279
- start_counter: usize,
280
- dom_ctx: &DomContext,
281
- ) {
282
- let mut counter = start_counter;
283
-
284
- if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
285
- let children = tag.children();
286
- {
287
- for child_handle in children.top().iter() {
288
- if let Some(tl::Node::Raw(bytes)) = child_handle.get(parser) {
289
- if bytes.as_utf8_str().trim().is_empty() {
290
- continue;
291
- }
292
- }
293
-
294
- let list_ctx = Context {
295
- in_ordered_list: is_ordered,
296
- list_counter: if is_ordered { counter } else { 0 },
297
- in_list: true,
298
- list_depth: nested_depth,
299
- ul_depth: if is_ordered { ctx.ul_depth } else { ctx.ul_depth + 1 },
300
- loose_list: is_loose,
301
- prev_item_had_blocks: false,
302
- ..ctx.clone()
303
- };
304
-
305
- use crate::converter::walk_node;
306
- walk_node(child_handle, parser, output, options, &list_ctx, depth, dom_ctx);
307
-
308
- if is_ordered && is_list_item(*child_handle, parser, dom_ctx) {
309
- counter += 1;
310
- }
311
- }
312
- }
313
- }
314
- }