Skip to content

Commit 55784b5

Browse files
committed
messages/views: Add markup to code snippets.
Tests updated.
1 parent 0968af4 commit 55784b5

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

tests/ui_tools/test_messages.py

+1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ def test_soup2markup(self, content, expected_markup, mocker):
679679
metadata = dict(
680680
server_url=SERVER_URL,
681681
message_links=OrderedDict(),
682+
code_snippets=list(),
682683
time_mentions=list(),
683684
bq_len=0,
684685
)

zulipterminal/ui_tools/messages.py

+44-11
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
6262
self.user_id: Optional[int] = None
6363
self.message_links: Dict[str, Tuple[str, int, bool]] = dict()
6464
self.topic_links: Dict[str, Tuple[str, int, bool]] = dict()
65+
self.code_snippets: List[Tuple[str, List[Tuple[str, str]]]] = list()
6566
self.time_mentions: List[Tuple[str, str]] = list()
6667
self.last_message = last_message
6768
# if this is the first message
@@ -371,12 +372,22 @@ def footlinks_view(
371372
@classmethod
372373
def soup2markup(
373374
cls, soup: Any, metadata: Dict[str, Any], **state: Any
374-
) -> Tuple[List[Any], Dict[str, Tuple[str, int, bool]], List[Tuple[str, str]]]:
375+
) -> Tuple[
376+
List[Any],
377+
Dict[str, Tuple[str, int, bool]],
378+
List[Tuple[str, List[Tuple[str, str]]]],
379+
List[Tuple[str, str]],
380+
]:
375381
# Ensure a string is provided, in case the soup finds none
376382
# This could occur if eg. an image is removed or not shown
377383
markup: List[Union[str, Tuple[Optional[str], Any]]] = [""]
378384
if soup is None: # This is not iterable, so return promptly
379-
return markup, metadata["message_links"], metadata["time_mentions"]
385+
return (
386+
markup,
387+
metadata["message_links"],
388+
metadata["code_snippets"],
389+
metadata["time_mentions"],
390+
)
380391
unrendered_tags = { # In pairs of 'tag_name': 'text'
381392
# TODO: Some of these could be implemented
382393
"br": "", # No indicator of absence
@@ -551,7 +562,7 @@ def soup2markup(
551562
# Ref: https://github.com/Python-Markdown/markdown/pull/862
552563
if code_soup is None:
553564
code_soup = element.pre
554-
565+
code_snippet = []
555566
for code_element in code_soup.contents:
556567
code_text = (
557568
code_element.text
@@ -564,8 +575,15 @@ def soup2markup(
564575
continue
565576
css_style = code_element.attrs.get("class", ["w"])
566577
markup.append((f"pygments:{css_style[0]}", code_text))
578+
code_snippet.append((f"pygments:{css_style[0]}", code_text))
567579
else:
568580
markup.append(("pygments:w", code_text))
581+
code_snippet.append(("pygments:w", code_text))
582+
583+
code_language = element.attrs.get("data-code-language")
584+
585+
if code_language != "Text only":
586+
metadata["code_snippets"].append((code_language, code_snippet))
569587
elif tag in ("strong", "em"):
570588
# BOLD & ITALIC
571589
markup.append(("msg_bold", tag_text))
@@ -634,7 +652,12 @@ def soup2markup(
634652
metadata["time_mentions"].append((time_string, source_text))
635653
else:
636654
markup.extend(cls.soup2markup(element, metadata)[0])
637-
return markup, metadata["message_links"], metadata["time_mentions"]
655+
return (
656+
markup,
657+
metadata["message_links"],
658+
metadata["code_snippets"],
659+
metadata["time_mentions"],
660+
)
638661

639662
def main_view(self) -> List[Any]:
640663
# Recipient Header
@@ -730,11 +753,13 @@ def main_view(self) -> List[Any]:
730753
)
731754

732755
# Transform raw message content into markup (As needed by urwid.Text)
733-
content, self.message_links, self.time_mentions = self.transform_content(
734-
self.message["content"], self.model.server_url
735-
)
756+
(
757+
content,
758+
self.message_links,
759+
self.code_snippets,
760+
self.time_mentions,
761+
) = self.transform_content(self.message["content"], self.model.server_url)
736762
self.content.set_text(content)
737-
738763
if self.message["id"] in self.model.index["edited_messages"]:
739764
edited_label_size = 7
740765
left_padding = 1
@@ -818,6 +843,7 @@ def transform_content(
818843
) -> Tuple[
819844
Tuple[None, Any],
820845
Dict[str, Tuple[str, int, bool]],
846+
List[Tuple[str, List[Tuple[str, str]]]],
821847
List[Tuple[str, str]],
822848
]:
823849
soup = BeautifulSoup(content, "lxml")
@@ -826,14 +852,17 @@ def transform_content(
826852
metadata = dict(
827853
server_url=server_url,
828854
message_links=dict(),
855+
code_snippets=list(),
829856
time_mentions=list(),
830857
) # type: Dict[str, Any]
831858

832859
if isinstance(body, Tag) and body.find(name="blockquote"):
833860
metadata["bq_len"] = cls.indent_quoted_content(soup, QUOTED_TEXT_MARKER)
834861

835-
markup, message_links, time_mentions = cls.soup2markup(body, metadata)
836-
return (None, markup), message_links, time_mentions
862+
markup, message_links, code_snippets, time_mentions = cls.soup2markup(
863+
body, metadata
864+
)
865+
return (None, markup), message_links, code_snippets, time_mentions
837866

838867
@staticmethod
839868
def indent_quoted_content(soup: Any, padding_char: str) -> int:
@@ -1121,7 +1150,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
11211150
self.model.controller.view.middle_column.set_focus("footer")
11221151
elif is_command_key("MSG_INFO", key):
11231152
self.model.controller.show_msg_info(
1124-
self.message, self.topic_links, self.message_links, self.time_mentions
1153+
self.message,
1154+
self.topic_links,
1155+
self.message_links,
1156+
self.code_snippets,
1157+
self.time_mentions,
11251158
)
11261159
elif is_command_key("ADD_REACTION", key):
11271160
self.model.controller.show_emoji_picker(self.message)

zulipterminal/ui_tools/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ def __init__(self, controller: Any, stream_id: int) -> None:
13971397

13981398
title = f"{stream_marker} {stream['name']}"
13991399
rendered_desc = stream["rendered_description"]
1400-
self.markup_desc, message_links, _ = MessageBox.transform_content(
1400+
self.markup_desc, message_links, _, _ = MessageBox.transform_content(
14011401
rendered_desc,
14021402
self.controller.model.server_url,
14031403
)

0 commit comments

Comments
 (0)