pyhtml2text 0.4.0__tar.gz → 0.4.1__tar.gz

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 (42) hide show
  1. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/.gitignore +1 -1
  2. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/CHANGELOG.md +18 -0
  3. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/Cargo.lock +25 -24
  4. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/Cargo.toml +5 -4
  5. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/PKG-INFO +16 -3
  6. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/README.md +5 -2
  7. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/pyproject.toml +11 -1
  8. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/src/table.rs +7 -18
  9. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/src/text.rs +77 -32
  10. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/tests/test_basic.py +24 -0
  11. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/README.md +14 -12
  12. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/Cargo.toml +5 -10
  13. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/encoding.rs +51 -1
  14. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/serialize/mod.rs +43 -9
  15. pyhtml2text-0.4.1/vendor/html5ever/src/tokenizer/mod.rs +3292 -0
  16. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tree_builder/mod.rs +13 -21
  17. pyhtml2text-0.4.0/vendor/html5ever/src/tokenizer/mod.rs +0 -2324
  18. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/LICENSE +0 -0
  19. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/python/pyhtml2text/__init__.py +0 -0
  20. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/python/pyhtml2text/__init__.pyi +0 -0
  21. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/python/pyhtml2text/py.typed +0 -0
  22. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/src/dom.rs +0 -0
  23. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/src/lib.rs +0 -0
  24. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/src/markdown.rs +0 -0
  25. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/src/url.rs +0 -0
  26. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/tests/markdown_check.py +0 -0
  27. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/tests/test_markdown.py +0 -0
  28. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/LICENSE-APACHE +0 -0
  29. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/LICENSE-MIT +0 -0
  30. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/README.md +0 -0
  31. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/driver.rs +0 -0
  32. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/lib.rs +0 -0
  33. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/macros.rs +0 -0
  34. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tokenizer/char_ref/mod.rs +0 -0
  35. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tokenizer/interface.rs +0 -0
  36. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tokenizer/states.rs +0 -0
  37. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tree_builder/data.rs +0 -0
  38. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tree_builder/open_elems.rs +0 -0
  39. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tree_builder/rules.rs +0 -0
  40. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tree_builder/tag_sets.rs +0 -0
  41. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/tree_builder/types.rs +0 -0
  42. {pyhtml2text-0.4.0 → pyhtml2text-0.4.1}/vendor/html5ever/src/util/str.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  target/
2
2
  dist/
3
- .venv/
3
+ .venv*/
4
4
  __pycache__/
5
5
  *.so
6
6
  .DS_Store
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.1 (2026-10-30)
4
+
5
+ - Deeply nested lists, definitions, paragraphs and emphasis are rendered in
6
+ linear time again. In 0.4.0 each level cost time for every level around
7
+ it: 400,000 nested lists took 49 s, and now take 0.5 s. Output is
8
+ unchanged.
9
+ - Wheels for free-threaded Python 3.15 (3.15t), alongside 3.14t. The abi3
10
+ wheels already cover Python 3.15. CI now tests on 3.10, 3.14t, 3.15 and
11
+ 3.15t.
12
+ - Character widths from Unicode 18 (unicode-width 0.2.3). The halfwidth
13
+ katakana sound marks ゙ and ゚ count one column, as terminals show them,
14
+ rather than none, and characters new in Unicode 18 (emoji, kana,
15
+ ideographic characters and combining marks) get their own widths. Other
16
+ text wraps as before.
17
+ - html5ever 0.40.1, still with
18
+ [servo/html5ever#800](https://github.com/servo/html5ever/pull/800).
19
+ Output is unchanged. Building from source needs Rust 1.85 or later.
20
+
3
21
  ## 0.4.0 (2026-10-22)
4
22
 
5
23
  - `html2text()` is rendered by this package's own code. The library it used
@@ -10,9 +10,9 @@ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
10
10
 
11
11
  [[package]]
12
12
  name = "cc"
13
- version = "1.9.0"
13
+ version = "1.10.0"
14
14
  source = "registry+https://github.com/rust-lang/crates.io-index"
15
- checksum = "877bf67ac8e52449eb2f9e98a344ea163beb70dd1f9eebe3cc5357dcfeaee4df"
15
+ checksum = "86e055fa6e3e40ad6187ba25b6855eeb635d5271f8e0f0e1e9a5826b8d75bea0"
16
16
  dependencies = [
17
17
  "find-msvc-tools",
18
18
  "shlex",
@@ -44,10 +44,11 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
44
44
 
45
45
  [[package]]
46
46
  name = "html5ever"
47
- version = "0.39.0"
47
+ version = "0.40.1"
48
48
  dependencies = [
49
49
  "log",
50
50
  "markup5ever",
51
+ "memchr",
51
52
  ]
52
53
 
53
54
  [[package]]
@@ -82,9 +83,9 @@ checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
82
83
 
83
84
  [[package]]
84
85
  name = "markup5ever"
85
- version = "0.39.0"
86
+ version = "0.40.0"
86
87
  source = "registry+https://github.com/rust-lang/crates.io-index"
87
- checksum = "7122d987ec5f704ee56f6e5b41a7d93722e9aae27ae07cafa4036c4d3f9757de"
88
+ checksum = "0ab3dc68ac4a0f5719e560136778c1ee716e296030d75dbd4484e37e39e3a842"
88
89
  dependencies = [
89
90
  "log",
90
91
  "tendril",
@@ -143,9 +144,9 @@ dependencies = [
143
144
 
144
145
  [[package]]
145
146
  name = "phf"
146
- version = "0.13.1"
147
+ version = "0.14.0"
147
148
  source = "registry+https://github.com/rust-lang/crates.io-index"
148
- checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf"
149
+ checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6"
149
150
  dependencies = [
150
151
  "phf_shared",
151
152
  "serde",
@@ -153,9 +154,9 @@ dependencies = [
153
154
 
154
155
  [[package]]
155
156
  name = "phf_codegen"
156
- version = "0.13.1"
157
+ version = "0.14.0"
157
158
  source = "registry+https://github.com/rust-lang/crates.io-index"
158
- checksum = "49aa7f9d80421bca176ca8dbfebe668cc7a2684708594ec9f3c0db0805d5d6e1"
159
+ checksum = "41b585a510fb76fdebead6897982ef2a03a21d8e6cbcca904999742a4afc6ffe"
159
160
  dependencies = [
160
161
  "phf_generator",
161
162
  "phf_shared",
@@ -163,9 +164,9 @@ dependencies = [
163
164
 
164
165
  [[package]]
165
166
  name = "phf_generator"
166
- version = "0.13.1"
167
+ version = "0.14.0"
167
168
  source = "registry+https://github.com/rust-lang/crates.io-index"
168
- checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737"
169
+ checksum = "aeb62e0959d5a1bebc965f4d15d9e2b7cea002b6b0f5ba8cde6cc26738467100"
169
170
  dependencies = [
170
171
  "fastrand",
171
172
  "phf_shared",
@@ -173,9 +174,9 @@ dependencies = [
173
174
 
174
175
  [[package]]
175
176
  name = "phf_shared"
176
- version = "0.13.1"
177
+ version = "0.14.0"
177
178
  source = "registry+https://github.com/rust-lang/crates.io-index"
178
- checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266"
179
+ checksum = "c6fd9027e2d9319be6349febd1db4e8d02aa544921200c9b777720ac34a3aa89"
179
180
  dependencies = [
180
181
  "siphasher",
181
182
  ]
@@ -214,7 +215,7 @@ dependencies = [
214
215
 
215
216
  [[package]]
216
217
  name = "pyhtml2text"
217
- version = "0.4.0"
218
+ version = "0.4.1"
218
219
  dependencies = [
219
220
  "html5ever",
220
221
  "mimalloc",
@@ -347,15 +348,15 @@ checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256"
347
348
 
348
349
  [[package]]
349
350
  name = "smallvec"
350
- version = "1.16.3"
351
+ version = "1.16.4"
351
352
  source = "registry+https://github.com/rust-lang/crates.io-index"
352
- checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b"
353
+ checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8"
353
354
 
354
355
  [[package]]
355
356
  name = "string_cache"
356
- version = "0.9.0"
357
+ version = "0.11.0"
357
358
  source = "registry+https://github.com/rust-lang/crates.io-index"
358
- checksum = "a18596f8c785a729f2819c0f6a7eae6ebeebdfffbfe4214ae6b087f690e31901"
359
+ checksum = "ffa8a5dbe8b3f0bbe29d4c3225daafaeead63afdc1b65fc4c01a1384166038e6"
359
360
  dependencies = [
360
361
  "new_debug_unreachable",
361
362
  "parking_lot",
@@ -365,9 +366,9 @@ dependencies = [
365
366
 
366
367
  [[package]]
367
368
  name = "string_cache_codegen"
368
- version = "0.6.1"
369
+ version = "0.11.2"
369
370
  source = "registry+https://github.com/rust-lang/crates.io-index"
370
- checksum = "585635e46db231059f76c5849798146164652513eb9e8ab2685939dd90f29b69"
371
+ checksum = "928dcdf75e47626b3617a976ec205d9f057584c371c1f23b782129268d0e6edc"
371
372
  dependencies = [
372
373
  "phf_generator",
373
374
  "phf_shared",
@@ -415,15 +416,15 @@ checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
415
416
 
416
417
  [[package]]
417
418
  name = "unicode-width"
418
- version = "0.2.2"
419
+ version = "0.2.3"
419
420
  source = "registry+https://github.com/rust-lang/crates.io-index"
420
- checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254"
421
+ checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557"
421
422
 
422
423
  [[package]]
423
424
  name = "web_atoms"
424
- version = "0.2.6"
425
+ version = "0.3.0"
425
426
  source = "registry+https://github.com/rust-lang/crates.io-index"
426
- checksum = "ba8b815c1b593dc0baf78dd0f4fc8fdb2de53198fb1163738093e9a311c33fb3"
427
+ checksum = "7572660c8890448ba236b7376f27e389c6a7e1c70195622faced601f855c0ada"
427
428
  dependencies = [
428
429
  "phf",
429
430
  "phf_codegen",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "pyhtml2text"
3
- version = "0.4.0"
3
+ version = "0.4.1"
4
4
  edition = "2021"
5
5
  license = "MIT"
6
6
  publish = false
@@ -13,9 +13,10 @@ crate-type = ["cdylib"]
13
13
  [dependencies]
14
14
  pyo3 = { version = "0.30", features = ["abi3-py310"] }
15
15
  # The HTML parser; patched below.
16
- html5ever = "0.39"
17
- # Display width of characters, for wrapping.
18
- unicode-width = "=0.2.2"
16
+ html5ever = "0.40"
17
+ # Display width of characters, for wrapping. Pinned, since newer Unicode data
18
+ # can change the output.
19
+ unicode-width = "=0.2.3"
19
20
  # Reads the Markdown back to check that emphasis parses as intended.
20
21
  pulldown-cmark = { version = "0.13", default-features = false }
21
22
 
@@ -1,9 +1,19 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyhtml2text
3
- Version: 0.4.0
3
+ Version: 0.4.1
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: 3
6
+ Classifier: Programming Language :: Python :: 3 :: Only
7
+ Classifier: Programming Language :: Python :: 3.10
8
+ Classifier: Programming Language :: Python :: 3.11
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Classifier: Programming Language :: Python :: 3.14
12
+ Classifier: Programming Language :: Python :: 3.15
13
+ Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
6
14
  Classifier: Topic :: Text Processing :: Markup :: HTML
15
+ Classifier: Topic :: Text Processing :: Markup :: Markdown
16
+ Classifier: Typing :: Typed
7
17
  License-File: LICENSE
8
18
  Summary: Fast HTML to plain text and Markdown, with relative link resolution
9
19
  Keywords: html,text,markdown,html2text
@@ -33,6 +43,8 @@ html = """<h2>Before you start</h2>
33
43
  print(html2markdown(html, base_url="https://example.com/docs/intro.html"))
34
44
  ```
35
45
 
46
+ Output:
47
+
36
48
  ```markdown
37
49
  ## Before you start
38
50
 
@@ -115,12 +127,13 @@ To plain text:
115
127
  | **pyhtml2text** | **7.0 ms** | **1.3 ms** |
116
128
 
117
129
  - The GIL is released while converting, so threads scale across cores. On
118
- free-threaded Python (3.14t) the module runs without re-enabling the GIL.
130
+ free-threaded Python (3.14t and 3.15t) the module runs without re-enabling
131
+ the GIL.
119
132
  - Wheels for Linux (x86_64, aarch64), macOS and Windows x64 are built with
120
133
  profile-guided optimisation, which makes them about 25% faster. Other
121
134
  platforms get the same code without it.
122
135
  - Deeply nested HTML is parsed in linear time, so hostile input can't stall a
123
- worker: 40,000 nested lists take 0.14 s, against 6 s with stock html5ever,
136
+ worker: 40,000 nested lists take 0.02 s, against 18 s with stock html5ever,
124
137
  and 100,000 levels of nesting followed by unmatched end tags take under
125
138
  0.1 s.
126
139
 
@@ -18,6 +18,8 @@ html = """<h2>Before you start</h2>
18
18
  print(html2markdown(html, base_url="https://example.com/docs/intro.html"))
19
19
  ```
20
20
 
21
+ Output:
22
+
21
23
  ```markdown
22
24
  ## Before you start
23
25
 
@@ -100,12 +102,13 @@ To plain text:
100
102
  | **pyhtml2text** | **7.0 ms** | **1.3 ms** |
101
103
 
102
104
  - The GIL is released while converting, so threads scale across cores. On
103
- free-threaded Python (3.14t) the module runs without re-enabling the GIL.
105
+ free-threaded Python (3.14t and 3.15t) the module runs without re-enabling
106
+ the GIL.
104
107
  - Wheels for Linux (x86_64, aarch64), macOS and Windows x64 are built with
105
108
  profile-guided optimisation, which makes them about 25% faster. Other
106
109
  platforms get the same code without it.
107
110
  - Deeply nested HTML is parsed in linear time, so hostile input can't stall a
108
- worker: 40,000 nested lists take 0.14 s, against 6 s with stock html5ever,
111
+ worker: 40,000 nested lists take 0.02 s, against 18 s with stock html5ever,
109
112
  and 100,000 levels of nesting followed by unmatched end tags take under
110
113
  0.1 s.
111
114
 
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "pyhtml2text"
7
- version = "0.4.0"
7
+ version = "0.4.1"
8
8
  description = "Fast HTML to plain text and Markdown, with relative link resolution"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -15,7 +15,17 @@ keywords = ["html", "text", "markdown", "html2text"]
15
15
  classifiers = [
16
16
  "Programming Language :: Rust",
17
17
  "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3 :: Only",
19
+ "Programming Language :: Python :: 3.10",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Programming Language :: Python :: 3.14",
24
+ "Programming Language :: Python :: 3.15",
25
+ "Programming Language :: Python :: Free Threading :: 3 - Stable",
18
26
  "Topic :: Text Processing :: Markup :: HTML",
27
+ "Topic :: Text Processing :: Markup :: Markdown",
28
+ "Typing :: Typed",
19
29
  ]
20
30
 
21
31
  [project.urls]
@@ -756,11 +756,7 @@ fn layout(r: &mut Renderer, cells: &mut [Cell], nrows_all: usize, width: usize)
756
756
  fn vertical(cells: &[Cell], nrows: usize, width: usize, borders: bool) -> Job {
757
757
  let mut pieces = Vec::new();
758
758
  let mut to_render = Vec::new();
759
- let rule = || Line {
760
- text: "─".repeat(width),
761
- width,
762
- kind: LineKind::Rule,
763
- };
759
+ let rule = || Line::of_kind("─".repeat(width), width, LineKind::Rule);
764
760
  let sep = || Line::new("/".repeat(width), width);
765
761
  if borders {
766
762
  pieces.push(Some(rule()));
@@ -1020,15 +1016,12 @@ impl Grid {
1020
1016
  }
1021
1017
 
1022
1018
  fn row_line(&self, row: usize, line: usize, sep: &str) -> Line {
1023
- let mut out = Line {
1024
- text: String::new(),
1025
- width: 0,
1026
- kind: if sep == " " {
1027
- LineKind::Text
1028
- } else {
1029
- LineKind::Row
1030
- },
1019
+ let kind = if sep == " " {
1020
+ LineKind::Text
1021
+ } else {
1022
+ LineKind::Row
1031
1023
  };
1024
+ let mut out = Line::of_kind(String::new(), 0, kind);
1032
1025
  for (i, &(idx, _, _)) in self.vis[row].iter().enumerate() {
1033
1026
  if i > 0 {
1034
1027
  out.text.push_str(sep);
@@ -1134,11 +1127,7 @@ impl Grid {
1134
1127
  }
1135
1128
  }
1136
1129
  // Lay the spanning cells' text over the line.
1137
- let mut out = Line {
1138
- text: String::new(),
1139
- width: 0,
1140
- kind: LineKind::Rule,
1141
- };
1130
+ let mut out = Line::of_kind(String::new(), 0, LineKind::Rule);
1142
1131
  texts.sort_by_key(|t| t.0);
1143
1132
  let mut ti = texts.into_iter().peekable();
1144
1133
  let mut x = 0;
@@ -7,6 +7,7 @@
7
7
  //! pending and only carried out when something is written after them, so
8
8
  //! empty blocks leave no trace and adjacent requests merge.
9
9
 
10
+ use std::collections::HashMap;
10
11
  use std::mem;
11
12
 
12
13
  use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
@@ -85,7 +86,11 @@ pub fn render(dom: &Dom, opts: &Options) -> Result<String, String> {
85
86
  parent.r.footnotes = mem::take(&mut done.r.footnotes);
86
87
  parent.r.table_sizes = mem::take(&mut done.r.table_sizes);
87
88
  let lines = done.r.bufs.pop().unwrap().finish();
88
- parent.table.as_mut().unwrap().add(lines);
89
+ parent
90
+ .table
91
+ .as_mut()
92
+ .unwrap()
93
+ .add(lines.into_iter().map(Line::flatten).collect());
89
94
  }
90
95
  Ok(levels.pop().unwrap().r.finish())
91
96
  }
@@ -113,6 +118,10 @@ pub struct Line {
113
118
  pub text: String,
114
119
  pub width: usize,
115
120
  pub kind: LineKind,
121
+ /// The prefixes of the blocks the line is in, still to be put in front
122
+ /// of `text`, written back to front: each block then adds its own in
123
+ /// time for its length, however deeply blocks are nested.
124
+ rprefix: String,
116
125
  }
117
126
 
118
127
  /// What a line is, for a table it ends up in.
@@ -127,16 +136,40 @@ pub enum LineKind {
127
136
 
128
137
  impl Line {
129
138
  pub fn new(text: String, width: usize) -> Line {
139
+ Line::of_kind(text, width, LineKind::Text)
140
+ }
141
+
142
+ pub fn of_kind(text: String, width: usize, kind: LineKind) -> Line {
130
143
  Line {
131
144
  text,
132
145
  width,
133
- kind: LineKind::Text,
146
+ kind,
147
+ rprefix: String::new(),
134
148
  }
135
149
  }
136
150
 
137
151
  fn empty() -> Line {
138
152
  Line::new(String::new(), 0)
139
153
  }
154
+
155
+ fn add_prefix(&mut self, prefix: &str) {
156
+ self.rprefix.extend(prefix.chars().rev());
157
+ self.width += prefix.len();
158
+ // A prefix makes a table's lines plain text.
159
+ self.kind = LineKind::Text;
160
+ }
161
+
162
+ /// The line with its prefixes in front of it.
163
+ fn flatten(mut self) -> Line {
164
+ if !self.rprefix.is_empty() {
165
+ let mut text = String::with_capacity(self.rprefix.len() + self.text.len());
166
+ text.extend(self.rprefix.chars().rev());
167
+ text.push_str(&self.text);
168
+ self.text = text;
169
+ self.rprefix.clear();
170
+ }
171
+ self
172
+ }
140
173
  }
141
174
 
142
175
  /// One buffer of output lines.
@@ -150,7 +183,7 @@ struct Buf {
150
183
  word: String,
151
184
  word_w: usize,
152
185
  pieces: Vec<usize>,
153
- word_style: Vec<u8>,
186
+ word_style: u32,
154
187
  /// The word has a character of non-zero width.
155
188
  word_base: bool,
156
189
  /// A space is due before the next word.
@@ -188,7 +221,7 @@ impl Buf {
188
221
  word: String::new(),
189
222
  word_w: 0,
190
223
  pieces: Vec::new(),
191
- word_style: Vec::new(),
224
+ word_style: 0,
192
225
  word_base: false,
193
226
  space: false,
194
227
  open: false,
@@ -410,8 +443,11 @@ pub struct Renderer<'a> {
410
443
  /// Sizes of the tables seen so far, shared with the cells' renderers.
411
444
  pub table_sizes: table::Sizes,
412
445
  /// The styled elements (emphasis, code, links...) the text is inside.
413
- /// Text in one style is measured as one piece.
414
- style: Vec<u8>,
446
+ /// Text in one style is measured as one piece. Each distinct stack of
447
+ /// them is numbered (`styles`, 0 for none), and this holds the number of
448
+ /// the stack up to each of them.
449
+ style: Vec<u32>,
450
+ styles: HashMap<(u32, u8), u32>,
415
451
  /// Width of the leading whitespace of a `<pre>` line, or of whitespace
416
452
  /// after a tab, held until something follows it.
417
453
  prews: usize,
@@ -427,6 +463,7 @@ impl<'a> Renderer<'a> {
427
463
  footnotes: Vec::new(),
428
464
  table_sizes: table::Sizes::new(),
429
465
  style: Vec::new(),
466
+ styles: HashMap::new(),
430
467
  prews: 0,
431
468
  after_tab: false,
432
469
  }
@@ -437,11 +474,26 @@ impl<'a> Renderer<'a> {
437
474
  }
438
475
 
439
476
  fn mark(&mut self) {
440
- for f in &mut self.frames {
477
+ // Frames are marked from the outermost in, so once one is found
478
+ // marked, so are all those outside it.
479
+ for f in self.frames.iter_mut().rev() {
480
+ if f.wrote {
481
+ break;
482
+ }
441
483
  f.wrote = true;
442
484
  }
443
485
  }
444
486
 
487
+ fn push_style(&mut self, id: u8) {
488
+ let next = self.styles.len() as u32 + 1;
489
+ let style = *self.styles.entry((self.cur_style(), id)).or_insert(next);
490
+ self.style.push(style);
491
+ }
492
+
493
+ fn cur_style(&self) -> u32 {
494
+ self.style.last().copied().unwrap_or(0)
495
+ }
496
+
445
497
  /// The first thing happens in a buffer: carry out the separator before
446
498
  /// its block, in the parent (and so on up).
447
499
  fn wake(&mut self) {
@@ -558,10 +610,11 @@ impl<'a> Renderer<'a> {
558
610
 
559
611
  fn word_char(&mut self, c: char) {
560
612
  let w = char_width(c);
613
+ let style = self.cur_style();
561
614
  let b = self.bufs.last_mut().unwrap();
562
- if b.word_style != self.style {
615
+ if b.word_style != style {
563
616
  b.piece();
564
- b.word_style.clone_from(&self.style);
617
+ b.word_style = style;
565
618
  }
566
619
  b.word.push(c);
567
620
  b.word_w += w;
@@ -599,6 +652,7 @@ impl<'a> Renderer<'a> {
599
652
  self.touch();
600
653
  }
601
654
  }
655
+ let style = self.cur_style();
602
656
  for c in seg.chars() {
603
657
  let b = self.bufs.last_mut().unwrap();
604
658
  let at_start = (b.cur.is_empty() || self.after_tab) && b.word.is_empty();
@@ -641,9 +695,9 @@ impl<'a> Renderer<'a> {
641
695
  }
642
696
  b.chop(&" ".repeat(n));
643
697
  }
644
- if b.word_style != self.style {
698
+ if b.word_style != style {
645
699
  b.piece();
646
- b.word_style.clone_from(&self.style);
700
+ b.word_style = style;
647
701
  }
648
702
  b.word.push(c);
649
703
  b.word_w += char_width(c);
@@ -731,22 +785,12 @@ impl<'a> Renderer<'a> {
731
785
  let lines = self.bufs.pop().unwrap().finish();
732
786
  if !lines.is_empty() {
733
787
  let b = self.b();
734
- for (i, line) in lines.into_iter().enumerate() {
788
+ for (i, mut line) in lines.into_iter().enumerate() {
735
789
  let prefix = if i == 0 { first } else { rest };
736
- let mut s = String::with_capacity(prefix.len() + line.text.len());
737
- s.push_str(prefix);
738
- s.push_str(&line.text);
739
- // A prefix makes a table's lines plain text.
740
- let kind = if prefix.is_empty() {
741
- line.kind
742
- } else {
743
- LineKind::Text
744
- };
745
- b.lines.push(Line {
746
- text: s,
747
- width: prefix.len() + line.width,
748
- kind,
749
- });
790
+ if !prefix.is_empty() {
791
+ line.add_prefix(prefix);
792
+ }
793
+ b.lines.push(line);
750
794
  }
751
795
  b.blank_after = false;
752
796
  b.content = true;
@@ -833,7 +877,7 @@ impl<'a> Renderer<'a> {
833
877
  self.start(Kind::Lead(LINE));
834
878
  stack.push(Step::Leave(Leave::Frame));
835
879
  // Unlike emphasis, the markers are part of the term's style.
836
- self.style.push(b'd');
880
+ self.push_style(b'd');
837
881
  stack.push(Step::Leave(Leave::Styled("")));
838
882
  if self.opts.decorate {
839
883
  self.text("*");
@@ -938,7 +982,7 @@ impl<'a> Renderer<'a> {
938
982
  "code" => {
939
983
  // Unlike emphasis, the markers are part of the code's
940
984
  // style.
941
- self.style.push(b'c');
985
+ self.push_style(b'c');
942
986
  if decorate {
943
987
  self.text("`");
944
988
  stack.push(Step::Leave(Leave::Styled("")));
@@ -953,7 +997,7 @@ impl<'a> Renderer<'a> {
953
997
  "ins" => ("", "", b'n'),
954
998
  _ if sup_digits(node).is_some() => {
955
999
  let digits = sup_digits(node).unwrap();
956
- self.style.push(b's');
1000
+ self.push_style(b's');
957
1001
  self.text(&digits);
958
1002
  self.style.pop();
959
1003
  return;
@@ -963,12 +1007,12 @@ impl<'a> Renderer<'a> {
963
1007
  if !open.is_empty() {
964
1008
  self.text(open);
965
1009
  }
966
- self.style.push(id);
1010
+ self.push_style(id);
967
1011
  Leave::Styled(close)
968
1012
  }
969
1013
  "del" | "s" => {
970
1014
  self.b().strike += 1;
971
- self.style.push(b'x');
1015
+ self.push_style(b'x');
972
1016
  Leave::Strike
973
1017
  }
974
1018
  "a" => match attr("href") {
@@ -982,7 +1026,7 @@ impl<'a> Renderer<'a> {
982
1026
  None => href,
983
1027
  };
984
1028
  self.text("[");
985
- self.style.push(b'a');
1029
+ self.push_style(b'a');
986
1030
  Leave::Link(url)
987
1031
  }
988
1032
  None => Leave::Nothing,
@@ -1179,6 +1223,7 @@ impl<'a> Renderer<'a> {
1179
1223
  let lines = b.finish();
1180
1224
  let mut out = String::new();
1181
1225
  for line in lines {
1226
+ out.extend(line.rprefix.chars().rev());
1182
1227
  out.push_str(&line.text);
1183
1228
  out.push('\n');
1184
1229
  }
@@ -97,6 +97,30 @@ def test_deep_nesting_is_linear():
97
97
  assert elapsed < 10, f"took {elapsed:.1f}s on {html[:20]!r}..."
98
98
 
99
99
 
100
+ def test_deep_nesting_renders_in_linear_time():
101
+ # In 0.4.0 each level of these cost time for every level around it: the
102
+ # lists took 49s and 28s, the others 6s. Now each takes under a second.
103
+ import platform
104
+ import sys
105
+ import time
106
+
107
+ n = 400_000
108
+ cases = [
109
+ ("<ul><li>" * n + "x", "* " * n + "x\n"),
110
+ ("<dl><dd>" * n + "x", " " * n + "x\n"),
111
+ ("<div><p>a" * (n // 2), "\n\n".join(["a"] * (n // 2)) + "\n"),
112
+ # Wrapped at 80 columns.
113
+ ("<b>" * n + "x", "**" * n + "x" + "**" * n),
114
+ ]
115
+ for html, expected in cases:
116
+ start = time.perf_counter()
117
+ out = html2text(html)
118
+ elapsed = time.perf_counter() - start
119
+ assert (out if expected.endswith("\n") else out.replace("\n", "")) == expected
120
+ if platform.machine().lower() in ("x86_64", "amd64") or sys.platform == "darwin":
121
+ assert elapsed < 10, f"took {elapsed:.1f}s on {html[:20]!r}..."
122
+
123
+
100
124
  def test_deeply_nested_tables():
101
125
  # Each table's cells are rendered on their own; nesting them mustn't
102
126
  # take a call stack as deep as the tables (or time quadratic in it).