Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rich text simplification #412

Merged
merged 15 commits into from
Nov 16, 2023
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[flake8]
exclude=build,tests,tmp,venv,toot/tui/scroll.py
ignore=E128,W503
per-file-ignores=toot/tui/stubs/urwidgets.py:F401
max-line-length=120
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Programming Language :: Python :: 3',
],
packages=['toot', 'toot.tui', 'toot.utils'],
packages=['toot', 'toot.tui', 'toot.tui.richtext', 'toot.utils'],
python_requires=">=3.7",
install_requires=[
"requests>=2.13,<3.0",
Expand Down
45 changes: 45 additions & 0 deletions tests/tui/test_rich_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from urwid import Divider, Filler, Pile
from toot.tui.richtext import url_to_widget
from urwidgets import Hyperlink, TextEmbed

from toot.tui.richtext.richtext import html_to_widgets


def test_url_to_widget():
url = "http://foo.bar"
embed_widget = url_to_widget(url)
assert isinstance(embed_widget, TextEmbed)

[(filler, length)] = embed_widget.embedded
assert length == len(url)
assert isinstance(filler, Filler)

link_widget: Hyperlink = filler.base_widget
assert isinstance(link_widget, Hyperlink)

assert link_widget.attrib == "link"
assert link_widget.text == url
assert link_widget.uri == url


def test_html_to_widgets():
html = """
<p>foo</p>
<p>foo <b>bar</b> <i>baz</i></p>
""".strip()

[foo, divider, bar] = html_to_widgets(html)

assert isinstance(foo, Pile)
assert isinstance(divider, Divider)
assert isinstance(bar, Pile)

[foo_embed] = foo.widget_list
assert foo_embed.embedded == []
assert foo_embed.attrib == []
assert foo_embed.text == "foo"

[bar_embed] = bar.widget_list
assert bar_embed.embedded == []
assert bar_embed.attrib == [(None, 4), ("b", 3), (None, 1), ("i", 3)]
assert bar_embed.text == "foo bar baz"
4 changes: 2 additions & 2 deletions toot/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import lru_cache
from toot import settings
from toot.entities import Instance, Notification, Poll, Status
from toot.utils import get_text, parse_html
from toot.utils import get_text, html_to_paragraphs
from toot.wcstring import wc_wrap
from typing import List
from wcwidth import wcswidth
Expand Down Expand Up @@ -321,7 +321,7 @@ def print_status(status: Status, width: int = 80):

def print_html(text, width=80):
first = True
for paragraph in parse_html(text):
for paragraph in html_to_paragraphs(text):
if not first:
print_out("")
for line in paragraph:
Expand Down
8 changes: 3 additions & 5 deletions toot/tui/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from toot import api
from toot.tui.utils import highlight_keys
from toot.tui.widgets import Button, EditBox, SelectableText
from toot.tui.richtext import ContentParser
from toot.tui.richtext import html_to_widgets


class StatusSource(urwid.Padding):
Expand Down Expand Up @@ -255,8 +255,6 @@ def setup_listbox(self):
super().__init__(walker)

def generate_contents(self, account, relationship=None, last_action=None):
parser = ContentParser()

if self.last_action and not self.last_action.startswith("Confirm"):
yield Button(f"Confirm {self.last_action}", on_press=take_action, user_data=self)
yield Button("Cancel", on_press=cancel_action, user_data=self)
Expand All @@ -282,7 +280,7 @@ def generate_contents(self, account, relationship=None, last_action=None):
if account["note"]:
yield urwid.Divider()

widgetlist = parser.html_to_widgets(account["note"])
widgetlist = html_to_widgets(account["note"])
for line in widgetlist:
yield (line)

Expand Down Expand Up @@ -317,7 +315,7 @@ def generate_contents(self, account, relationship=None, last_action=None):
yield urwid.Divider()
yield urwid.Text([("bold", f"{name.rstrip(':')}"), ":"])

widgetlist = parser.html_to_widgets(field["value"])
widgetlist = html_to_widgets(field["value"])
for line in widgetlist:
yield (line)

Expand Down
5 changes: 2 additions & 3 deletions toot/tui/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from toot.exceptions import ApiError
from toot.utils.datetime import parse_datetime
from .widgets import Button, CheckBox, RadioButton
from .richtext import ContentParser
from .richtext import html_to_widgets


class Poll(urwid.ListBox):
Expand Down Expand Up @@ -86,8 +86,7 @@ def generate_poll_detail(self):
def generate_contents(self, status):
yield urwid.Divider()

parser = ContentParser()
widgetlist = parser.html_to_widgets(status.data["content"])
widgetlist = html_to_widgets(status.data["content"])

for line in widgetlist:
yield (line)
Expand Down
Loading
Loading