Skip to content

Commit

Permalink
use double_br to simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lopuhin committed Apr 24, 2024
1 parent 27f4c0f commit 31c42e8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
7 changes: 3 additions & 4 deletions clear_html/formatted_text/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from clear_html.formatted_text.headings import normalize_headings_level
from clear_html.formatted_text.utils import (
clean_incomplete_structures,
double_br,
kill_tag_content,
remove_empty_tags,
set_article_tag_as_root,
Expand Down Expand Up @@ -116,12 +117,10 @@ def paragraphy(doc: HtmlElement):
start, end = None, None
for idx, child in enumerate(doc):
if child.tag == "br":
prev_child = child.getprevious()
if prev_child is None or prev_child.tag != "br" or has_tail(prev_child):
if not double_br(child.getprevious()):
# A br without previous consecutive br was found
start = idx
next_child = child.getnext()
if next_child is None or next_child.tag != "br" or has_tail(child):
if not double_br(child):
# A br without next consecutive br was found
end = idx
if start == end:
Expand Down
6 changes: 3 additions & 3 deletions clear_html/formatted_text/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ def drop_tag_preserve_spacing(doc: HtmlElement, preserve_content=True):
prev_is_inline = (
doc_prev is not None
and doc_prev.tag in PHRASING_CONTENT
and not _double_br(doc_prev.getprevious())
and not double_br(doc_prev.getprevious())
)
after_is_inline = (
doc_next is not None
and doc_next.tag in PHRASING_CONTENT
and not _double_br(doc_next)
and not double_br(doc_next)
)

has_text_prev = bool(prev_text(doc).strip()) or prev_is_inline
Expand All @@ -192,7 +192,7 @@ def drop_tag_preserve_spacing(doc: HtmlElement, preserve_content=True):
doc.drop_tree()


def _double_br(doc: Optional[HtmlElement]):
def double_br(doc: Optional[HtmlElement]):
"""True if doc and next element are "br" tags without text in between."""
if doc is None or doc.tag != "br":
return False
Expand Down

0 comments on commit 31c42e8

Please sign in to comment.