Simplify text_to_widget
This was doing double regex matching, calling parse_text was not needed at all.
This commit is contained in:
parent
073dd3025c
commit
f50dea1175
|
@ -4,7 +4,7 @@ import unicodedata
|
||||||
|
|
||||||
from bs4.element import NavigableString, Tag
|
from bs4.element import NavigableString, Tag
|
||||||
from toot.tui.constants import PALETTE
|
from toot.tui.constants import PALETTE
|
||||||
from toot.tui.stubs.urwidgets import TextEmbed, Hyperlink, parse_text, has_urwidgets
|
from toot.tui.stubs.urwidgets import TextEmbed, Hyperlink, has_urwidgets
|
||||||
from toot.utils import parse_html, urlencode_url
|
from toot.utils import parse_html, urlencode_url
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple
|
||||||
from urwid.util import decompose_tagmarkup
|
from urwid.util import decompose_tagmarkup
|
||||||
|
@ -80,33 +80,26 @@ def process_inline_tag_children(tag) -> List:
|
||||||
return markups
|
return markups
|
||||||
|
|
||||||
|
|
||||||
|
URL_PATTERN = re.compile(r"(^.+)\x03(.+$)")
|
||||||
|
|
||||||
|
|
||||||
def text_to_widget(attr, markup) -> urwid.Widget:
|
def text_to_widget(attr, markup) -> urwid.Widget:
|
||||||
if not has_urwidgets:
|
if not has_urwidgets:
|
||||||
return urwid.Text((attr, markup))
|
return urwid.Text((attr, markup))
|
||||||
|
|
||||||
TRANSFORM = {
|
|
||||||
# convert http[s] URLs to Hyperlink widgets for nesting in a TextEmbed widget
|
|
||||||
re.compile(r"(^.+)\x03(.+$)"): lambda g: (
|
|
||||||
len(g[1]),
|
|
||||||
urwid.Filler(Hyperlink(g[2], anchor_attr, g[1])),
|
|
||||||
),
|
|
||||||
}
|
|
||||||
markup_list = []
|
markup_list = []
|
||||||
|
|
||||||
for run in markup:
|
for run in markup:
|
||||||
if isinstance(run, tuple):
|
if isinstance(run, tuple):
|
||||||
txt, attr_list = decompose_tagmarkup(run)
|
txt, attr_list = decompose_tagmarkup(run)
|
||||||
# find anchor titles with an ETX separator followed by href
|
# find anchor titles with an ETX separator followed by href
|
||||||
m = re.match(r"(^.+)\x03(.+$)", txt)
|
match = URL_PATTERN.match(txt)
|
||||||
if m:
|
if match:
|
||||||
|
label, url = match.groups()
|
||||||
anchor_attr = get_best_anchor_attr(attr_list)
|
anchor_attr = get_best_anchor_attr(attr_list)
|
||||||
markup_list.append(
|
markup_list.append((
|
||||||
parse_text(
|
len(label),
|
||||||
txt,
|
urwid.Filler(Hyperlink(url, anchor_attr, label)),
|
||||||
TRANSFORM,
|
))
|
||||||
lambda pattern, groups, span: TRANSFORM[pattern](groups),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
markup_list.append(run)
|
markup_list.append(run)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue