forked from indymedia/epicyon
Turn email style quotes into blockquotes
parent
74a6f286fc
commit
21573b6ca7
21
content.py
21
content.py
|
@ -14,6 +14,25 @@ from utils import fileLastModified
|
||||||
from utils import getLinkPrefixes
|
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 '>> ' not in lineStr:
|
||||||
|
newContent += '<p>' + lineStr + '</p>'
|
||||||
|
else:
|
||||||
|
lineStr = lineStr.replace('>> ', '><blockquote>')
|
||||||
|
newContent += '<p>' + lineStr + '</blockquote></p>'
|
||||||
|
return newContent
|
||||||
|
|
||||||
|
|
||||||
def htmlReplaceQuoteMarks(content: str) -> str:
|
def htmlReplaceQuoteMarks(content: str) -> str:
|
||||||
"""Replaces quotes with html formatting
|
"""Replaces quotes with html formatting
|
||||||
"hello" becomes <q>hello</q>
|
"hello" becomes <q>hello</q>
|
||||||
|
@ -612,6 +631,7 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
|
||||||
by matching against known following accounts
|
by matching against known following accounts
|
||||||
"""
|
"""
|
||||||
if content.startswith('<p>'):
|
if content.startswith('<p>'):
|
||||||
|
content = htmlReplaceEmailQuote(content)
|
||||||
return htmlReplaceQuoteMarks(content)
|
return htmlReplaceQuoteMarks(content)
|
||||||
maxWordLength = 40
|
maxWordLength = 40
|
||||||
content = content.replace('\r', '')
|
content = content.replace('\r', '')
|
||||||
|
@ -718,6 +738,7 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
|
||||||
if longWordsList:
|
if longWordsList:
|
||||||
content = removeLongWords(content, maxWordLength, longWordsList)
|
content = removeLongWords(content, maxWordLength, longWordsList)
|
||||||
content = content.replace(' --linebreak-- ', '</p><p>')
|
content = content.replace(' --linebreak-- ', '</p><p>')
|
||||||
|
content = htmlReplaceEmailQuote(content)
|
||||||
return '<p>' + htmlReplaceQuoteMarks(content) + '</p>'
|
return '<p>' + htmlReplaceQuoteMarks(content) + '</p>'
|
||||||
|
|
||||||
|
|
||||||
|
|
26
tests.py
26
tests.py
|
@ -68,6 +68,7 @@ from delete import sendDeleteViaServer
|
||||||
from inbox import jsonPostAllowsComments
|
from inbox import jsonPostAllowsComments
|
||||||
from inbox import validInbox
|
from inbox import validInbox
|
||||||
from inbox import validInboxFilenames
|
from inbox import validInboxFilenames
|
||||||
|
from content import htmlReplaceEmailQuote
|
||||||
from content import htmlReplaceQuoteMarks
|
from content import htmlReplaceQuoteMarks
|
||||||
from content import dangerousMarkup
|
from content import dangerousMarkup
|
||||||
from content import removeHtml
|
from content import removeHtml
|
||||||
|
@ -2124,8 +2125,33 @@ def testConstantTimeStringCheck():
|
||||||
assert timeDiffMicroseconds < 10
|
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 />> 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():
|
def runAllTests():
|
||||||
print('Running tests...')
|
print('Running tests...')
|
||||||
|
testReplaceEmailQuote()
|
||||||
testConstantTimeStringCheck()
|
testConstantTimeStringCheck()
|
||||||
testTranslations()
|
testTranslations()
|
||||||
testValidContentWarning()
|
testValidContentWarning()
|
||||||
|
|
Loading…
Reference in New Issue