-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #415 from ihabunek/danschwarz-richtext3
Add support for rich text
- Loading branch information
Showing
15 changed files
with
604 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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, url_to_widget | ||
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) | ||
] | ||
|
||
def url_to_widget(url: str): | ||
return urwid.Text(("link", url)) |
Oops, something went wrong.