Turn email style quotes into blockquotes

main
Bob Mottram 2020-09-14 10:33:42 +01:00
parent 74a6f286fc
commit 21573b6ca7
2 changed files with 47 additions and 0 deletions

View File

@ -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('<p>', '')
contentLines = contentStr.split('</p>')
newContent = ''
for lineStr in contentLines:
if not lineStr:
continue
if '>&gt; ' not in lineStr:
newContent += '<p>' + lineStr + '</p>'
else:
lineStr = lineStr.replace('>&gt; ', '><blockquote>')
newContent += '<p>' + lineStr + '</blockquote></p>'
return newContent
def htmlReplaceQuoteMarks(content: str) -> str:
"""Replaces quotes with html formatting
"hello" becomes <q>hello</q>
@ -612,6 +631,7 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
by matching against known following accounts
"""
if content.startswith('<p>'):
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-- ', '</p><p>')
content = htmlReplaceEmailQuote(content)
return '<p>' + htmlReplaceQuoteMarks(content) + '</p>'

View File

@ -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='<p>This content has no quote.</p>'
assert htmlReplaceEmailQuote(testStr) == testStr
testStr='<p>This content has no quote.\nWith multiple\nlines</p>'
assert htmlReplaceEmailQuote(testStr) == testStr
testStr = "<p><span class=\"h-card\">" + \
"<a href=\"https://somewebsite/@nickname\" " + \
"class=\"u-url mention\">@<span>nickname</span></a></span> " + \
"<br />&gt; This is a quote</p><p>Some other text.</p>"
expectedStr = "<p><span class=\"h-card\">" + \
"<a href=\"https://somewebsite/@nickname\" " + \
"class=\"u-url mention\">@<span>nickname</span></a></span> " + \
"<br /><blockquote>This is a quote</blockquote></p>" + \
"<p>Some other text.</p>"
resultStr = htmlReplaceEmailQuote(testStr)
if resultStr != expectedStr:
print('Result: ' + resultStr)
print('Expect: ' + expectedStr)
assert resultStr == expectedStr
def runAllTests():
print('Running tests...')
testReplaceEmailQuote()
testConstantTimeStringCheck()
testTranslations()
testValidContentWarning()