Skip to content

Commit 65640fc

Browse files
committed
Replace BEGIN/END with Position
1 parent 46bf7ce commit 65640fc

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

quotequail/_html.py

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# HTML utils
2+
import enum
23
from collections.abc import Iterator
34
from typing import TYPE_CHECKING, TypeAlias
45

@@ -10,8 +11,14 @@
1011

1112
from ._patterns import FORWARD_LINE, FORWARD_STYLES, MULTIPLE_WHITESPACE_RE
1213

14+
15+
class Position(enum.Enum):
16+
Begin = "begin"
17+
End = "end"
18+
19+
1320
Element: TypeAlias = "HtmlElement"
14-
ElementRef = tuple["Element", str]
21+
ElementRef = tuple["Element", Position]
1522

1623
INLINE_TAGS = [
1724
"a",
@@ -31,9 +38,6 @@
3138
"th",
3239
]
3340

34-
BEGIN = "begin"
35-
END = "end"
36-
3741

3842
def trim_tree_after(element: Element, include_element: bool = True):
3943
"""
@@ -184,9 +188,9 @@ def slice_tree(
184188
new_tree = tree
185189

186190
if start_ref:
187-
include_start = start_ref[1] == BEGIN
191+
include_start = start_ref[1] is Position.Begin
188192
if end_ref:
189-
include_end = end_ref[1] == END
193+
include_end = end_ref[1] is Position.End
190194

191195
# If start_ref is the same as end_ref, and we don't include the element,
192196
# we are removing the entire tree. We need to handle this separately,
@@ -283,14 +287,14 @@ def is_indentation_element(element: Element) -> bool:
283287

284288
def tree_token_generator(
285289
el: Element, indentation_level: int = 0
286-
) -> Iterator[None | tuple[Element, str, int] | str]:
290+
) -> Iterator[None | tuple[Element, Position, int] | str]:
287291
"""
288292
Yield tokens for the given HTML element as follows:
289293
290-
- A tuple (LXML element, BEGIN, indentation_level)
294+
- A tuple (LXML element, Begin, indentation_level)
291295
- Text right after the start of the tag, or None.
292296
- Recursively calls the token generator for all child objects
293-
- A tuple (LXML element, END, indentation_level)
297+
- A tuple (LXML element, End, indentation_level)
294298
- Text right after the end of the tag, or None.
295299
"""
296300
if not isinstance(el.tag, str):
@@ -301,7 +305,7 @@ def tree_token_generator(
301305
if is_indentation:
302306
indentation_level += 1
303307

304-
yield (el, BEGIN, indentation_level)
308+
yield (el, Position.Begin, indentation_level)
305309

306310
yield el.text
307311

@@ -311,7 +315,7 @@ def tree_token_generator(
311315
if is_indentation:
312316
indentation_level -= 1
313317

314-
yield (el, END, indentation_level)
318+
yield (el, Position.End, indentation_level)
315319

316320
yield el.tail
317321

@@ -320,7 +324,10 @@ def tree_line_generator(
320324
el: Element, max_lines: int | None = None
321325
) -> Iterator[
322326
tuple[
323-
tuple[ElementRef, str] | None, tuple[ElementRef, str] | None, int, str
327+
tuple[ElementRef, Position] | None,
328+
tuple[ElementRef, Position] | None,
329+
int,
330+
str,
324331
]
325332
]:
326333
"""
@@ -343,14 +350,14 @@ def tree_line_generator(
343350
344351
For example, the HTML tree "<div>foo <span>bar</span><br>baz</div>" yields:
345352
346-
- ((<Element div>, 'begin'), (<Element br>, 'begin'), 0, 'foo bar')
347-
- ((<Element br>, 'end'), (<Element div>, 'end'), 0, 'baz').
353+
- ((<Element div>, Begin), (<Element br>, Begin), 0, 'foo bar')
354+
- ((<Element br>, End), (<Element div>, End), 0, 'baz').
348355
349356
To illustrate the indentation level, the HTML tree
350357
'<div><blockquote>hi</blockquote>world</div>' yields:
351358
352-
- ((<Element blockquote>, 'begin'), (<Element blockquote>, 'end'), 1, 'hi')
353-
- ((<Element blockquote>, 'end'), (<Element div>, 'end'), 0, 'world')
359+
- ((<Element blockquote>, Begin), (<Element blockquote>, End), 1, 'hi')
360+
- ((<Element blockquote>, End), (<Element div>, End), 0, 'world')
354361
"""
355362

356363
def _trim_spaces(text: str) -> str:
@@ -378,11 +385,11 @@ def _trim_spaces(text: str) -> str:
378385

379386
tag_name = el.tag.lower()
380387

381-
line_break = tag_name == "br" and state == BEGIN
388+
line_break = tag_name == "br" and state is Position.Begin
382389
is_block = tag_name not in INLINE_TAGS
383390
is_forward = (
384391
is_block
385-
and state == BEGIN
392+
and state is Position.Begin
386393
and (style := el.attrib.get("style"))
387394
and any(style_re.match(style) for style_re in FORWARD_STYLES)
388395
)

0 commit comments

Comments
 (0)