Add fallback for html_to_widgets

Remove has_urwidgets since we no longer need to worry if we have
urwidgets in the richtext module.
This commit is contained in:
Ivan Habunek 2023-11-16 11:35:44 +01:00
parent f96b1b722c
commit e5ac82bb01
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
3 changed files with 29 additions and 7 deletions

View File

@ -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)
]

View File

@ -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

View File

@ -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.