@@ -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 .spoilers : List [Tuple [int , List [Any ], List [Any ]]] = 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 [int , List [Any ], List [Any ]]],
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 ["spoilers" ],
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
@@ -675,9 +686,33 @@ def soup2markup(
675
686
]
676
687
)
677
688
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
+ )
678
708
else :
679
709
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
+ )
681
716
682
717
def main_view (self ) -> List [Any ]:
683
718
# Recipient Header
@@ -773,9 +808,12 @@ def main_view(self) -> List[Any]:
773
808
)
774
809
775
810
# 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 )
779
817
self .content .set_text (content )
780
818
781
819
if self .message ["id" ] in self .model .index ["edited_messages" ]:
@@ -862,6 +900,7 @@ def transform_content(
862
900
Tuple [None , Any ],
863
901
Dict [str , Tuple [str , int , bool ]],
864
902
List [Tuple [str , str ]],
903
+ List [Tuple [int , List [Any ], List [Any ]]],
865
904
]:
866
905
soup = BeautifulSoup (content , "lxml" )
867
906
body = soup .find (name = "body" )
@@ -870,13 +909,14 @@ def transform_content(
870
909
server_url = server_url ,
871
910
message_links = dict (),
872
911
time_mentions = list (),
912
+ spoilers = list (),
873
913
) # type: Dict[str, Any]
874
914
875
915
if isinstance (body , Tag ) and body .find (name = "blockquote" ):
876
916
metadata ["bq_len" ] = cls .indent_quoted_content (soup , QUOTED_TEXT_MARKER )
877
917
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
880
920
881
921
@staticmethod
882
922
def indent_quoted_content (soup : Any , padding_char : str ) -> int :
0 commit comments