mirror of
https://github.com/ihabunek/toot.git
synced 2024-09-29 04:35:54 -04:00
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:
parent
f96b1b722c
commit
e5ac82bb01
@ -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)
|
||||||
|
]
|
||||||
|
@ -4,10 +4,10 @@ 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, 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
|
||||||
|
from urwidgets import Hyperlink, TextEmbed
|
||||||
|
|
||||||
|
|
||||||
STYLE_NAMES = [p[0] for p in PALETTE]
|
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:
|
def text_to_widget(attr, markup) -> urwid.Widget:
|
||||||
if not has_urwidgets:
|
|
||||||
return urwid.Text((attr, markup))
|
|
||||||
|
|
||||||
markup_list = []
|
markup_list = []
|
||||||
for run in markup:
|
for run in markup:
|
||||||
if isinstance(run, tuple):
|
if isinstance(run, tuple):
|
||||||
@ -242,8 +239,7 @@ def render_anchor(tag) -> Tuple:
|
|||||||
title, attrib_list = decompose_tagmarkup(markups)
|
title, attrib_list = decompose_tagmarkup(markups)
|
||||||
if not attrib_list:
|
if not attrib_list:
|
||||||
attrib_list = [tag]
|
attrib_list = [tag]
|
||||||
if href and has_urwidgets:
|
if href:
|
||||||
# only if we have urwidgets loaded for OCS 8 hyperlinks:
|
|
||||||
# urlencode the path and query portions of the URL
|
# urlencode the path and query portions of the URL
|
||||||
href = urlencode_url(href)
|
href = urlencode_url(href)
|
||||||
# use ASCII ETX (end of record) as a
|
# use ASCII ETX (end of record) as a
|
||||||
|
@ -35,6 +35,18 @@ def highlight_keys(text, high_attr, low_attr=""):
|
|||||||
return list(_gen())
|
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):
|
def show_media(paths):
|
||||||
"""
|
"""
|
||||||
Attempt to open an image viewer to show given media files.
|
Attempt to open an image viewer to show given media files.
|
||||||
|
Loading…
Reference in New Issue
Block a user