File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -27,28 +27,40 @@ def define_link_data(usernames):
27
27
logging .info (f"{ e } ---define_link_data" )
28
28
return []
29
29
30
+
30
31
def remove_unmatched_tags (text ):
31
32
try :
32
- # Remove unmatched closing tags at the beginning of the string
33
+ # Remove unmatched closing tags at the beginning of the string
33
34
text = re .sub (r'^\s*</[^>]+>\s*' , '' , text )
34
-
35
35
# Regex pattern to find matched or unmatched tags
36
- pattern = re .compile (r'(<([^>]+)>.*?</\2>)|(<[^/][^>]*>.*)' , re .DOTALL )
36
+ pattern = re .compile (r'(<([^>]+)>.*?</\2>)|(<[^/][^>]*>.*?)(?=<[^/][^>]*>|$ )' , re .DOTALL )
37
37
matches = pattern .findall (text )
38
-
38
+
39
39
cleaned_text = ''
40
+ open_tags = []
41
+
40
42
for match in matches :
41
43
if match [0 ]: # Full matched <tag>...</tag> pairs
42
44
cleaned_text += match [0 ]
43
45
elif match [2 ]: # Unmatched opening <tag> tags
46
+ # Add the tag to the list of open tags
47
+ tag = re .match (r'<([^/][^>]*)>' , match [2 ])
48
+ if tag :
49
+ tag_name = tag .group (1 ).split ()[0 ]
50
+ open_tags .append (tag_name )
44
51
cleaned_text += match [2 ]
45
-
52
+
53
+ # Close any unmatched opening tags
54
+ while open_tags :
55
+ tag = open_tags .pop ()
56
+ cleaned_text += f'</{ tag } >'
57
+
46
58
return cleaned_text
59
+
47
60
except Exception as e :
48
61
print (e )
49
62
return text
50
63
51
-
52
64
53
65
54
66
def week_data_formatter (html_content , type ):
You can’t perform that action at this time.
0 commit comments