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

Support to display a limited set of HTML tags, aligned with Mastodon 4.2 #404

Merged
merged 3 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[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 requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ requests>=2.13,<3.0
beautifulsoup4>=4.5.0,<5.0
wcwidth>=0.1.7
urwid>=2.0.0,<3.0

urwidgets>=0.1,<0.2
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"urwid>=2.0.0,<3.0",
"tomlkit>=0.10.0,<1.0"
],
extras_require={
"hyperlinks": ['urwidgets>=0.1,<0.2'],
},
entry_points={
'console_scripts': [
'toot=toot.console:main',
Expand Down
17 changes: 0 additions & 17 deletions toot/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ def __init__(self, app, user, screen, args):
def run(self):
self.loop.set_alarm_in(0, lambda *args: self.async_load_instance())
self.loop.set_alarm_in(0, lambda *args: self.async_load_followed_accounts())
self.loop.set_alarm_in(0, lambda *args: self.async_load_followed_tags())
self.loop.set_alarm_in(0, lambda *args: self.async_load_timeline(
is_initial=True, timeline_name="home"))
self.loop.run()
Expand Down Expand Up @@ -339,22 +338,6 @@ def _done_accounts(accounts):

self.run_in_thread(_load_accounts, done_callback=_done_accounts)

def async_load_followed_tags(self):
def _load_tag_list():
try:
return api.followed_tags(self.app, self.user)
except ApiError:
# not supported by all Mastodon servers so fail silently if necessary
return []

def _done_tag_list(tags):
if len(tags) > 0:
self.followed_tags = [t["name"] for t in tags]
else:
self.followed_tags = []

self.run_in_thread(_load_tag_list, done_callback=_done_tag_list)

def refresh_footer(self, timeline):
"""Show status details in footer."""
status, index, count = timeline.get_focused_status_with_counts()
Expand Down
23 changes: 23 additions & 0 deletions toot/tui/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@
('dim', 'dark gray', ''),
('highlight', 'yellow', ''),
('success', 'dark green', ''),

# HTML tag styling
('a', ',italics', '', 'italics'),
# em tag is mapped to i
('i', ',italics', '', 'italics'),
# strong tag is mapped to b
('b', ',bold', '', 'bold'),
# special case for bold + italic nested tags
('bi', ',bold,italics', '', ',bold,italics'),
('u', ',underline', '', ',underline'),
('del', ',strikethrough', '', ',strikethrough'),
('code', 'light gray, standout', '', ',standout'),
('pre', 'light gray, standout', '', ',standout'),
('blockquote', 'light gray', '', ''),
('h1', ',bold', '', ',bold'),
('h2', ',bold', '', ',bold'),
('h3', ',bold', '', ',bold'),
('h4', ',bold', '', ',bold'),
('h5', ',bold', '', ',bold'),
('h6', ',bold', '', ',bold'),
('class_mention_hashtag', 'light cyan', '', ''),
('class_hashtag', 'light cyan', '', ''),

]

VISIBILITY_OPTIONS = [
Expand Down
21 changes: 14 additions & 7 deletions toot/tui/overlays.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import webbrowser

from toot import __version__
from toot.utils import format_content
from .utils import highlight_hashtags, highlight_keys
from .widgets import Button, EditBox, SelectableText
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


class StatusSource(urwid.Padding):
Expand Down Expand Up @@ -255,6 +255,8 @@ 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 @@ -279,8 +281,10 @@ def generate_contents(self, account, relationship=None, last_action=None):

if account["note"]:
yield urwid.Divider()
for line in format_content(account["note"]):
yield urwid.Text(highlight_hashtags(line, followed_tags=set()))

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

yield urwid.Divider()
yield urwid.Text(["ID: ", ("highlight", f"{account['id']}")])
Expand Down Expand Up @@ -312,8 +316,11 @@ def generate_contents(self, account, relationship=None, last_action=None):
name = field["name"].title()
yield urwid.Divider()
yield urwid.Text([("bold", f"{name.rstrip(':')}"), ":"])
for line in format_content(field["value"]):
yield urwid.Text(highlight_hashtags(line, followed_tags=set()))

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

if field["verified_at"]:
yield urwid.Text(("success", "✓ Verified"))

Expand Down
12 changes: 7 additions & 5 deletions toot/tui/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

from toot import api
from toot.exceptions import ApiError
from toot.utils import format_content
from toot.utils.datetime import parse_datetime

from .utils import highlight_hashtags
from .widgets import Button, CheckBox, RadioButton
from .richtext import ContentParser


class Poll(urwid.ListBox):
Expand Down Expand Up @@ -87,8 +85,12 @@ def generate_poll_detail(self):

def generate_contents(self, status):
yield urwid.Divider()
for line in format_content(status.data["content"]):
yield urwid.Text(highlight_hashtags(line, set()))

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

for line in widgetlist:
yield (line)

yield urwid.Divider()
yield self.build_linebox(self.generate_poll_detail())
Expand Down
Loading