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

Render Spoilers in ZT #1529

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
46 changes: 46 additions & 0 deletions tests/ui_tools/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,52 @@ def test_private_message_to_self(self, mocker):
[("msg_emoji", ":github:")],
id="custom_emoji",
),
case(
'<div class="spoiler-block"><div class="spoiler-header"></div>'
'<div aria-hidden="true" class="spoiler-content">'
"<p>Test</p></div></div>",
[
"┌─",
"────────",
"─┬─",
"───────",
"─┐\n",
"│ ",
("msg_spoiler", "Spoiler:"),
" │ ",
"Spoiler",
" │\n",
"└─",
"────────",
"─┴─",
"───────",
"─┘",
],
id="spoiler_no_header",
),
case(
'<div class="spoiler-block"><div class="spoiler-header">'
'<p>Header</p></div><div aria-hidden="true" class="spoiler-content">'
"<p>Test</p></div></div>",
[
"┌─",
"────────",
"─┬─",
"──────",
"─┐\n",
"│ ",
("msg_spoiler", "Spoiler:"),
" │ ",
"Header",
" │\n",
"└─",
"────────",
"─┴─",
"──────",
"─┘",
],
id="spoiler_with_header",
),
],
)
def test_soup2markup(self, content, expected_markup, mocker):
Expand Down
1 change: 1 addition & 0 deletions zulipterminal/config/themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'msg_quote' : 'underline',
'msg_bold' : 'bold',
'msg_time' : 'bold',
'msg_spoiler' : 'bold',
'footer' : 'standout',
'footer_contrast' : 'standout',
'starred' : 'bold',
Expand Down
1 change: 1 addition & 0 deletions zulipterminal/themes/gruvbox_dark.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'msg_quote' : (Color.NEUTRAL_YELLOW, Color.DARK0_HARD),
'msg_bold' : (Color.LIGHT2__BOLD, Color.DARK0_HARD),
'msg_time' : (Color.DARK0_HARD, Color.LIGHT2),
'msg_spoiler' : (Color.BRIGHT_GREEN__BOLD, Color.DARK0_HARD),
'footer' : (Color.DARK0_HARD, Color.LIGHT4),
'footer_contrast' : (Color.LIGHT2, Color.DARK0_HARD),
'starred' : (Color.BRIGHT_RED__BOLD, Color.DARK0_HARD),
Expand Down
1 change: 1 addition & 0 deletions zulipterminal/themes/gruvbox_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'msg_quote' : (Color.NEUTRAL_YELLOW, Color.LIGHT0_HARD),
'msg_bold' : (Color.DARK2__BOLD, Color.LIGHT0_HARD),
'msg_time' : (Color.LIGHT0_HARD, Color.DARK2),
'msg_spoiler' : (Color.FADED_GREEN__BOLD, Color.LIGHT0_HARD),
'footer' : (Color.LIGHT0_HARD, Color.DARK4),
'footer_contrast' : (Color.DARK2, Color.LIGHT0_HARD),
'starred' : (Color.FADED_RED__BOLD, Color.LIGHT0_HARD),
Expand Down
1 change: 1 addition & 0 deletions zulipterminal/themes/zt_blue.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'msg_quote' : (Color.BROWN, Color.DARK_BLUE),
'msg_bold' : (Color.WHITE__BOLD, Color.DARK_BLUE),
'msg_time' : (Color.DARK_BLUE, Color.WHITE),
'msg_spoiler' : (Color.LIGHT_GREEN__BOLD, Color.LIGHT_BLUE),
'footer' : (Color.WHITE, Color.DARK_GRAY),
'footer_contrast' : (Color.BLACK, Color.WHITE),
'starred' : (Color.LIGHT_RED__BOLD, Color.LIGHT_BLUE),
Expand Down
1 change: 1 addition & 0 deletions zulipterminal/themes/zt_dark.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'msg_quote' : (Color.BROWN, Color.BLACK),
'msg_bold' : (Color.WHITE__BOLD, Color.BLACK),
'msg_time' : (Color.BLACK, Color.WHITE),
'msg_spoiler' : (Color.LIGHT_GREEN__BOLD, Color.BLACK),
'footer' : (Color.BLACK, Color.LIGHT_GRAY),
'footer_contrast' : (Color.WHITE, Color.BLACK),
'starred' : (Color.LIGHT_RED__BOLD, Color.BLACK),
Expand Down
1 change: 1 addition & 0 deletions zulipterminal/themes/zt_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
'msg_quote' : (Color.BLACK, Color.BROWN),
'msg_bold' : (Color.WHITE__BOLD, Color.DARK_GRAY),
'msg_time' : (Color.WHITE, Color.DARK_GRAY),
'msg_spoiler' : (Color.DARK_GREEN__BOLD, Color.WHITE),
'footer' : (Color.WHITE, Color.DARK_GRAY),
'footer_contrast' : (Color.BLACK, Color.WHITE),
'starred' : (Color.LIGHT_RED__BOLD, Color.WHITE),
Expand Down
41 changes: 40 additions & 1 deletion zulipterminal/ui_tools/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from zulipterminal.config.ui_mappings import STATE_ICON, STREAM_ACCESS_TYPE
from zulipterminal.helper import get_unused_fence
from zulipterminal.server_url import near_message_url
from zulipterminal.ui_tools.tables import render_table
from zulipterminal.ui_tools.tables import render_table, row_with_only_border
from zulipterminal.urwid_types import urwid_MarkupTuple, urwid_Size


Expand Down Expand Up @@ -632,6 +632,45 @@ def soup2markup(

source_text = f"Original text was {tag_text.strip()}"
metadata["time_mentions"].append((time_string, source_text))
elif tag == "div" and "spoiler-block" in tag_classes:
# SPOILERS
header = element.find(class_="spoiler-header")
header.contents = [part for part in header.contents if part != "\n"]

if not header.contents:
default = BeautifulSoup("<p>Spoiler</p>", "html.parser")
header.contents.append(default)

processed_header = cls.soup2markup(header, metadata)[0]

processed_header_text = "".join(
part[1] if isinstance(part, tuple) else part
for part in processed_header
)

# Limit to the first 10 characters and append "..."
if len(processed_header_text) > 10:
processed_header_text = processed_header_text[:10] + "..."
Comment on lines +677 to +679
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have a literal like 10, it's almost always useful to give it a name, particularly when it's referenced in multiple locations like here. Other than being descriptive, it ensures everything gets updated at the same time, which reduces one bug location.


processed_header_len = len(processed_header_text)
marker = "Spoiler:"

widths = [len(marker), processed_header_len]
top_border = row_with_only_border("┌", "─", "┬", "┐", widths)
bottom_border = row_with_only_border(
"└", "─", "┴", "┘", widths, newline=False
)
markup.extend(top_border)
markup.extend(
[
"│ ",
("msg_spoiler", marker),
" │ ",
processed_header_text,
" │\n",
]
)
markup.extend(bottom_border)
else:
markup.extend(cls.soup2markup(element, metadata)[0])
return markup, metadata["message_links"], metadata["time_mentions"]
Expand Down