@@ -62,6 +62,7 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
62
62
self .user_id : Optional [int ] = None
63
63
self .message_links : Dict [str , Tuple [str , int , bool ]] = dict ()
64
64
self .topic_links : Dict [str , Tuple [str , int , bool ]] = dict ()
65
+ self .code_snippets : List [Tuple [str , List [Tuple [str , str ]]]] = list ()
65
66
self .time_mentions : List [Tuple [str , str ]] = list ()
66
67
self .last_message = last_message
67
68
# if this is the first message
@@ -371,12 +372,22 @@ def footlinks_view(
371
372
@classmethod
372
373
def soup2markup (
373
374
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
+ ]:
375
381
# Ensure a string is provided, in case the soup finds none
376
382
# This could occur if eg. an image is removed or not shown
377
383
markup : List [Union [str , Tuple [Optional [str ], Any ]]] = ["" ]
378
384
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
+ )
380
391
unrendered_tags = { # In pairs of 'tag_name': 'text'
381
392
# TODO: Some of these could be implemented
382
393
"br" : "" , # No indicator of absence
@@ -551,7 +562,7 @@ def soup2markup(
551
562
# Ref: https://github.com/Python-Markdown/markdown/pull/862
552
563
if code_soup is None :
553
564
code_soup = element .pre
554
-
565
+ code_snippet = []
555
566
for code_element in code_soup .contents :
556
567
code_text = (
557
568
code_element .text
@@ -564,8 +575,15 @@ def soup2markup(
564
575
continue
565
576
css_style = code_element .attrs .get ("class" , ["w" ])
566
577
markup .append ((f"pygments:{ css_style [0 ]} " , code_text ))
578
+ code_snippet .append ((f"pygments:{ css_style [0 ]} " , code_text ))
567
579
else :
568
580
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 ))
569
587
elif tag in ("strong" , "em" ):
570
588
# BOLD & ITALIC
571
589
markup .append (("msg_bold" , tag_text ))
@@ -634,7 +652,12 @@ def soup2markup(
634
652
metadata ["time_mentions" ].append ((time_string , source_text ))
635
653
else :
636
654
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
+ )
638
661
639
662
def main_view (self ) -> List [Any ]:
640
663
# Recipient Header
@@ -730,11 +753,13 @@ def main_view(self) -> List[Any]:
730
753
)
731
754
732
755
# 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 )
736
762
self .content .set_text (content )
737
-
738
763
if self .message ["id" ] in self .model .index ["edited_messages" ]:
739
764
edited_label_size = 7
740
765
left_padding = 1
@@ -818,6 +843,7 @@ def transform_content(
818
843
) -> Tuple [
819
844
Tuple [None , Any ],
820
845
Dict [str , Tuple [str , int , bool ]],
846
+ List [Tuple [str , List [Tuple [str , str ]]]],
821
847
List [Tuple [str , str ]],
822
848
]:
823
849
soup = BeautifulSoup (content , "lxml" )
@@ -826,14 +852,17 @@ def transform_content(
826
852
metadata = dict (
827
853
server_url = server_url ,
828
854
message_links = dict (),
855
+ code_snippets = list (),
829
856
time_mentions = list (),
830
857
) # type: Dict[str, Any]
831
858
832
859
if isinstance (body , Tag ) and body .find (name = "blockquote" ):
833
860
metadata ["bq_len" ] = cls .indent_quoted_content (soup , QUOTED_TEXT_MARKER )
834
861
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
837
866
838
867
@staticmethod
839
868
def indent_quoted_content (soup : Any , padding_char : str ) -> int :
@@ -1121,7 +1150,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
1121
1150
self .model .controller .view .middle_column .set_focus ("footer" )
1122
1151
elif is_command_key ("MSG_INFO" , key ):
1123
1152
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 ,
1125
1158
)
1126
1159
elif is_command_key ("ADD_REACTION" , key ):
1127
1160
self .model .controller .show_emoji_picker (self .message )
0 commit comments