From c3b0ec3aa9dc6b8b0727113dfd9bf8fdb3f1dded Mon Sep 17 00:00:00 2001 From: official-techsupport Date: Sun, 22 Jan 2023 21:49:50 +0200 Subject: [PATCH] showmore on too many newlines (works now) --- files/helpers/regex.py | 5 ++--- files/helpers/sanitize.py | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/files/helpers/regex.py b/files/helpers/regex.py index aaec0161c..0eee98294 100644 --- a/files/helpers/regex.py +++ b/files/helpers/regex.py @@ -105,10 +105,9 @@ reddit_domain_regex = re.compile("(^|\s|\()https?:\/\/(reddit\.com|(?:(?:[A-z]{2 color_regex = re.compile("[a-f0-9]{6}", flags=re.A) -# lazy match on the {}?, only match if there is trailing stuff +# lazy match on the .*?, only match if there is trailing stuff # Specifically match Snappy's way of formatting, this might break some losers' comments. -# showmore_regex = re.compile(r"^((.{3000,}?|(.*?<.*?){10,})?<\/p>(?:<\/li><\/ul>)?)(\s*

.*)", flags=re.A|re.DOTALL) -showmore_regex = re.compile(r"^(.{3000,}?

(?:)?)(\s*

.*)", flags=re.A|re.DOTALL) +showmore_regex = re.compile(r"^(.*?

(?:)?)(\s*

.*)", flags=re.A|re.DOTALL) search_token_regex = re.compile('"([^"]*)"|(\S+)', flags=re.A) diff --git a/files/helpers/sanitize.py b/files/helpers/sanitize.py index c026c4430..d0176cc2f 100644 --- a/files/helpers/sanitize.py +++ b/files/helpers/sanitize.py @@ -410,10 +410,19 @@ def sanitize(sanitized, golden=True, limit_pings=0, showmore=True, count_marseys if '

' not in sanitized and not sidebar:
 		sanitized = sanitized.replace('\n','')
 
-	# if showmore and len(sanitized) > 3500 or sanitized.count('<') > 15:
-	# 	sanitized = showmore_regex.sub(r'\1

\4', sanitized, count=1) - if showmore and len(sanitized) > 3500: - sanitized = showmore_regex.sub(r'\1

\2', sanitized, count=1) + if showmore: + # Insert a show more button if the text is too long or has too many paragraphs + pos = 0 + for _ in range(20): + pos = sanitized.find('

', pos + 4) + if pos < 0: + break + if pos < 0 and len(sanitized) > 3000: + pos = 2500 + if pos >= 0: + sanitized = (sanitized[:pos] + + showmore_regex.sub(r'\1

\2', + sanitized[pos:], count=1)) return sanitized.strip() -- 2.34.1