@@ -63,6 +63,7 @@ def __init__(self, message: Message, model: "Model", last_message: Any) -> None:
6363 self .message_links : Dict [str , Tuple [str , int , bool ]] = dict ()
6464 self .topic_links : Dict [str , Tuple [str , int , bool ]] = dict ()
6565 self .time_mentions : List [Tuple [str , str ]] = list ()
66+ self .spoilers : List [Tuple [int , List [Any ], List [Any ]]] = list ()
6667 self .last_message = last_message
6768 # if this is the first message
6869 if self .last_message is None :
@@ -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 , str ]],
379+ List [Tuple [int , List [Any ], List [Any ]]],
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 ["time_mentions" ],
389+ metadata ["spoilers" ],
390+ )
380391 unrendered_tags = { # In pairs of 'tag_name': 'text'
381392 # TODO: Some of these could be implemented
382393 "br" : "" , # No indicator of absence
@@ -675,9 +686,33 @@ def soup2markup(
675686 ]
676687 )
677688 markup .extend (bottom_border )
689+ # Spoiler content
690+ content = element .find (class_ = "spoiler-content" )
691+
692+ # Remove surrounding newlines.
693+ content_contents = content .contents
694+ if len (content_contents ) > 2 :
695+ if content_contents [- 1 ] == "\n " :
696+ content .contents .pop (- 1 )
697+ if content_contents [0 ] == "\n " :
698+ content .contents .pop (0 )
699+ if len (content_contents ) == 1 and content_contents [0 ] == "\n " :
700+ content .contents .pop (0 )
701+
702+ # FIXME: Do not soup2markup content in the MessageBox as it
703+ # will render 'sensitive' spoiler anchor tags in the footlinks.
704+ processed_content = cls .soup2markup (content , metadata )[0 ]
705+ metadata ["spoilers" ].append (
706+ (header_len , processed_header , processed_content )
707+ )
678708 else :
679709 markup .extend (cls .soup2markup (element , metadata )[0 ])
680- return markup , metadata ["message_links" ], metadata ["time_mentions" ]
710+ return (
711+ markup ,
712+ metadata ["message_links" ],
713+ metadata ["time_mentions" ],
714+ metadata ["spoilers" ],
715+ )
681716
682717 def main_view (self ) -> List [Any ]:
683718 # Recipient Header
@@ -773,9 +808,12 @@ def main_view(self) -> List[Any]:
773808 )
774809
775810 # Transform raw message content into markup (As needed by urwid.Text)
776- content , self .message_links , self .time_mentions = self .transform_content (
777- self .message ["content" ], self .model .server_url
778- )
811+ (
812+ content ,
813+ self .message_links ,
814+ self .time_mentions ,
815+ self .spoilers ,
816+ ) = self .transform_content (self .message ["content" ], self .model .server_url )
779817 self .content .set_text (content )
780818
781819 if self .message ["id" ] in self .model .index ["edited_messages" ]:
@@ -862,6 +900,7 @@ def transform_content(
862900 Tuple [None , Any ],
863901 Dict [str , Tuple [str , int , bool ]],
864902 List [Tuple [str , str ]],
903+ List [Tuple [int , List [Any ], List [Any ]]],
865904 ]:
866905 soup = BeautifulSoup (content , "lxml" )
867906 body = soup .find (name = "body" )
@@ -870,13 +909,14 @@ def transform_content(
870909 server_url = server_url ,
871910 message_links = dict (),
872911 time_mentions = list (),
912+ spoilers = list (),
873913 ) # type: Dict[str, Any]
874914
875915 if isinstance (body , Tag ) and body .find (name = "blockquote" ):
876916 metadata ["bq_len" ] = cls .indent_quoted_content (soup , QUOTED_TEXT_MARKER )
877917
878- markup , message_links , time_mentions = cls .soup2markup (body , metadata )
879- return (None , markup ), message_links , time_mentions
918+ markup , message_links , time_mentions , spoilers = cls .soup2markup (body , metadata )
919+ return (None , markup ), message_links , time_mentions , spoilers
880920
881921 @staticmethod
882922 def indent_quoted_content (soup : Any , padding_char : str ) -> int :
0 commit comments