diff --git a/toot/tui/richtext/__init__.py b/toot/tui/richtext/__init__.py index ba857ab..6359c24 100644 --- a/toot/tui/richtext/__init__.py +++ b/toot/tui/richtext/__init__.py @@ -1 +1,15 @@ -from .richtext import html_to_widgets +import urwid + +from toot.tui.utils import highlight_hashtags +from toot.utils import format_content +from typing import List + +try: + from .richtext import html_to_widgets +except ImportError: + # Fallback if urwidgets are not available + def html_to_widgets(html: str) -> List[urwid.Widget]: + return [ + urwid.Text(highlight_hashtags(line)) + for line in format_content(html) + ] diff --git a/toot/tui/richtext/richtext.py b/toot/tui/richtext/richtext.py index ae463ae..9db7e73 100644 --- a/toot/tui/richtext/richtext.py +++ b/toot/tui/richtext/richtext.py @@ -4,10 +4,10 @@ import unicodedata from bs4.element import NavigableString, Tag from toot.tui.constants import PALETTE -from toot.tui.stubs.urwidgets import TextEmbed, Hyperlink, has_urwidgets from toot.utils import parse_html, urlencode_url from typing import List, Tuple from urwid.util import decompose_tagmarkup +from urwidgets import Hyperlink, TextEmbed STYLE_NAMES = [p[0] for p in PALETTE] @@ -84,9 +84,6 @@ URL_PATTERN = re.compile(r"(^.+)\x03(.+$)") def text_to_widget(attr, markup) -> urwid.Widget: - if not has_urwidgets: - return urwid.Text((attr, markup)) - markup_list = [] for run in markup: if isinstance(run, tuple): @@ -242,8 +239,7 @@ def render_anchor(tag) -> Tuple: title, attrib_list = decompose_tagmarkup(markups) if not attrib_list: attrib_list = [tag] - if href and has_urwidgets: - # only if we have urwidgets loaded for OCS 8 hyperlinks: + if href: # urlencode the path and query portions of the URL href = urlencode_url(href) # use ASCII ETX (end of record) as a diff --git a/toot/tui/utils.py b/toot/tui/utils.py index 0ccff9d..734ae32 100644 --- a/toot/tui/utils.py +++ b/toot/tui/utils.py @@ -35,6 +35,18 @@ def highlight_keys(text, high_attr, low_attr=""): return list(_gen()) +def highlight_hashtags(line): + hline = [] + + for p in re.split(HASHTAG_PATTERN, line): + if p.startswith("#"): + hline.append(("hashtag", p)) + else: + hline.append(p) + + return hline + + def show_media(paths): """ Attempt to open an image viewer to show given media files.