@@ -63,6 +63,7 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> 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
65
self .time_mentions : List [Tuple [str , str ]] = list ()
66
+ self .code_blocks : List [Tuple [str , List [Tuple [str , str ]]]] = list ()
66
67
self .last_message = last_message
67
68
# if this is the first message
68
69
if self .last_message is None :
@@ -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 , str ]],
379
+ List [Tuple [str , 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 ["time_mentions" ],
389
+ metadata ["code_blocks" ],
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
@@ -552,6 +563,8 @@ def soup2markup(
552
563
if code_soup is None :
553
564
code_soup = element .pre
554
565
566
+ code_block = []
567
+
555
568
for code_element in code_soup .contents :
556
569
code_text = (
557
570
code_element .text
@@ -562,10 +575,18 @@ def soup2markup(
562
575
if code_element .name == "span" :
563
576
if len (code_text ) == 0 :
564
577
continue
565
- css_style = code_element .attrs .get ("class" , ["w" ])
566
- markup .append ((f"pygments:{ css_style [0 ]} " , code_text ))
578
+ css_style = (
579
+ f"pygments:{ code_element .attrs .get ('class' , ['w' ])[0 ]} "
580
+ )
567
581
else :
568
- markup .append (("pygments:w" , code_text ))
582
+ css_style = "pygments:w"
583
+
584
+ markup .append ((css_style , code_text ))
585
+ code_block .append ((css_style , code_text ))
586
+
587
+ code_language = element .attrs .get ("data-code-language" )
588
+
589
+ metadata ["code_blocks" ].append ((code_language , code_block ))
569
590
elif tag in ("strong" , "em" ):
570
591
# BOLD & ITALIC
571
592
markup .append (("msg_bold" , tag_text ))
@@ -634,7 +655,12 @@ def soup2markup(
634
655
metadata ["time_mentions" ].append ((time_string , source_text ))
635
656
else :
636
657
markup .extend (cls .soup2markup (element , metadata )[0 ])
637
- return markup , metadata ["message_links" ], metadata ["time_mentions" ]
658
+ return (
659
+ markup ,
660
+ metadata ["message_links" ],
661
+ metadata ["time_mentions" ],
662
+ metadata ["code_blocks" ],
663
+ )
638
664
639
665
def main_view (self ) -> List [Any ]:
640
666
# Recipient Header
@@ -730,11 +756,13 @@ def main_view(self) -> List[Any]:
730
756
)
731
757
732
758
# 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
- )
759
+ (
760
+ content ,
761
+ self .message_links ,
762
+ self .time_mentions ,
763
+ self .code_blocks ,
764
+ ) = self .transform_content (self .message ["content" ], self .model .server_url )
736
765
self .content .set_text (content )
737
-
738
766
if self .message ["id" ] in self .model .index ["edited_messages" ]:
739
767
edited_label_size = 7
740
768
left_padding = 1
@@ -819,6 +847,7 @@ def transform_content(
819
847
Tuple [None , Any ],
820
848
Dict [str , Tuple [str , int , bool ]],
821
849
List [Tuple [str , str ]],
850
+ List [Tuple [str , List [Tuple [str , str ]]]],
822
851
]:
823
852
soup = BeautifulSoup (content , "lxml" )
824
853
body = soup .find (name = "body" )
@@ -827,13 +856,16 @@ def transform_content(
827
856
server_url = server_url ,
828
857
message_links = dict (),
829
858
time_mentions = list (),
859
+ code_blocks = list (),
830
860
) # type: Dict[str, Any]
831
861
832
862
if isinstance (body , Tag ) and body .find (name = "blockquote" ):
833
863
metadata ["bq_len" ] = cls .indent_quoted_content (soup , QUOTED_TEXT_MARKER )
834
864
835
- markup , message_links , time_mentions = cls .soup2markup (body , metadata )
836
- return (None , markup ), message_links , time_mentions
865
+ markup , message_links , time_mentions , code_blocks = cls .soup2markup (
866
+ body , metadata
867
+ )
868
+ return (None , markup ), message_links , time_mentions , code_blocks
837
869
838
870
@staticmethod
839
871
def indent_quoted_content (soup : Any , padding_char : str ) -> int :
@@ -1121,7 +1153,11 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
1121
1153
self .model .controller .view .middle_column .set_focus ("footer" )
1122
1154
elif is_command_key ("MSG_INFO" , key ):
1123
1155
self .model .controller .show_msg_info (
1124
- self .message , self .topic_links , self .message_links , self .time_mentions
1156
+ self .message ,
1157
+ self .topic_links ,
1158
+ self .message_links ,
1159
+ self .time_mentions ,
1160
+ self .code_blocks ,
1125
1161
)
1126
1162
elif is_command_key ("ADD_REACTION" , key ):
1127
1163
self .model .controller .show_emoji_picker (self .message )
0 commit comments