2
2
3
3
from __future__ import annotations
4
4
5
- import re
6
5
from typing import TYPE_CHECKING
7
6
8
7
from ._helpers import FILLER_CHAR , MKDOCS_INDENT_COUNT , get_conf , rstrip_result
13
12
FILLER = FILLER_CHAR * (MKDOCS_INDENT_COUNT - 2 ) # `mdformat` default is two spaces
14
13
"""A spacer that is inserted and then removed to ensure proper word wrap."""
15
14
16
- # Pattern to match HTML entities like
17
- _HTML_ENTITY_PATTERN = re .compile (r"&[a-zA-Z]+;" )
18
-
19
-
20
- def _preserve_html_entities (text : str ) -> tuple [str , list [tuple [str , str ]]]:
21
- """Replace HTML entities with placeholders and return a list of replacements."""
22
- replacements = []
23
- counter = 0
24
-
25
- def replace_entity (match : re .Match ) -> str :
26
- nonlocal counter
27
- placeholder = f"__HTML_ENTITY_{ counter } __"
28
- counter += 1
29
- replacements .append ((placeholder , match .group (0 )))
30
- return placeholder
31
-
32
- text = _HTML_ENTITY_PATTERN .sub (replace_entity , text )
33
- return text , replacements
34
-
35
-
36
- def _restore_html_entities (text : str , replacements : list [tuple [str , str ]]) -> str :
37
- """Restore HTML entities from their placeholders."""
38
- for placeholder , entity in replacements :
39
- text = text .replace (placeholder , entity )
40
- return text
41
-
42
15
43
16
@rstrip_result
44
17
def postprocess_list_wrap (
@@ -71,9 +44,6 @@ def postprocess_list_wrap(
71
44
):
72
45
return text
73
46
74
- # Preserve HTML entities before wrapping
75
- text_with_placeholders , replacements = _preserve_html_entities (text )
76
-
77
47
counter_ = - 1
78
48
parent = node .parent
79
49
while parent and parent .type == "paragraph" :
@@ -82,14 +52,14 @@ def postprocess_list_wrap(
82
52
indent_count = max (counter_ , 0 )
83
53
84
54
soft_break = "\x00 "
85
- text_with_placeholders = text_with_placeholders .lstrip (soft_break ).lstrip ()
55
+ text = text .lstrip (soft_break ).lstrip ()
86
56
filler = (FILLER * indent_count )[:- 1 ] if indent_count else ""
87
57
newline_filler = filler + FILLER if indent_count else FILLER [:- 1 ]
88
- if len (text_with_placeholders ) > wrap_mode :
58
+ if len (text ) > wrap_mode :
89
59
indent_length = MKDOCS_INDENT_COUNT * indent_count
90
60
wrapped_length = - 123
91
61
words : list [str ] = []
92
- for word in text_with_placeholders .split (soft_break ):
62
+ for word in text .split (soft_break ):
93
63
next_length = wrapped_length + len (word )
94
64
if not words :
95
65
words = [filler , word ]
@@ -100,9 +70,5 @@ def postprocess_list_wrap(
100
70
else :
101
71
words .append (word )
102
72
wrapped_length = next_length + 1
103
- text_with_placeholders = soft_break .join (_w for _w in words if _w )
104
- else :
105
- text_with_placeholders = f"{ filler } { soft_break } { text_with_placeholders } " if filler else text_with_placeholders
106
-
107
- # Restore HTML entities after wrapping
108
- return _restore_html_entities (text_with_placeholders , replacements )
73
+ return soft_break .join (_w for _w in words if _w )
74
+ return f"{ filler } { soft_break } { text } " if filler else text
0 commit comments