diff --git a/content.py b/content.py index 3f85eff5b..3db21aa92 100644 --- a/content.py +++ b/content.py @@ -14,6 +14,25 @@ from utils import fileLastModified from utils import getLinkPrefixes +def htmlReplaceEmailQuote(content: str) -> str: + """Replaces an email style quote "> Some quote" with html blockquote + """ + if '>> ' not in content: + return content + contentStr = content.replace('
', '') + contentLines = contentStr.split('
') + newContent = '' + for lineStr in contentLines: + if not lineStr: + continue + if '>> ' not in lineStr: + newContent += '' + lineStr + '
' + else: + lineStr = lineStr.replace('>> ', '>') + newContent += '' + return newContent + + def htmlReplaceQuoteMarks(content: str) -> str: """Replaces quotes with html formatting "hello" becomes' + lineStr + '
hello@@ -612,6 +631,7 @@ def addHtmlTags(baseDir: str, httpPrefix: str, by matching against known following accounts """ if content.startswith('
'): + content = htmlReplaceEmailQuote(content) return htmlReplaceQuoteMarks(content) maxWordLength = 40 content = content.replace('\r', '') @@ -718,6 +738,7 @@ def addHtmlTags(baseDir: str, httpPrefix: str, if longWordsList: content = removeLongWords(content, maxWordLength, longWordsList) content = content.replace(' --linebreak-- ', '
') + content = htmlReplaceEmailQuote(content) return '
' + htmlReplaceQuoteMarks(content) + '
' diff --git a/tests.py b/tests.py index 51b00cbd4..ecb58134a 100644 --- a/tests.py +++ b/tests.py @@ -68,6 +68,7 @@ from delete import sendDeleteViaServer from inbox import jsonPostAllowsComments from inbox import validInbox from inbox import validInboxFilenames +from content import htmlReplaceEmailQuote from content import htmlReplaceQuoteMarks from content import dangerousMarkup from content import removeHtml @@ -2124,8 +2125,33 @@ def testConstantTimeStringCheck(): assert timeDiffMicroseconds < 10 +def testReplaceEmailQuote(): + print('testReplaceEmailQuote') + testStr='This content has no quote.
' + assert htmlReplaceEmailQuote(testStr) == testStr + + testStr='This content has no quote.\nWith multiple\nlines
' + assert htmlReplaceEmailQuote(testStr) == testStr + + testStr = "" + \
+ "@nickname " + \
+ "
> This is a quote
Some other text.
" + expectedStr = "" + \
+ "@nickname " + \
+ "
This is a quote" + \ + "
Some other text.
" + resultStr = htmlReplaceEmailQuote(testStr) + if resultStr != expectedStr: + print('Result: ' + resultStr) + print('Expect: ' + expectedStr) + assert resultStr == expectedStr + + def runAllTests(): print('Running tests...') + testReplaceEmailQuote() testConstantTimeStringCheck() testTranslations() testValidContentWarning()