Extra test for mentions

main
Bob Mottram 2021-07-20 19:02:42 +01:00
parent 27da1189e3
commit c8925141ba
2 changed files with 31 additions and 19 deletions

View File

@ -182,6 +182,28 @@ def getLinksFromContent(content: str) -> {}:
return links return links
def addLinksToContent(content: str, links: {}) -> str:
"""Adds links back into plain text
"""
for linkText, url in links.items():
urlDesc = url
if linkText.startswith('@') and linkText in content:
content = \
content.replace(linkText,
'<a href="' + url +
'" rel="nofollow noopener ' +
'noreferrer" target="_blank">' +
linkText + '</a>')
else:
if len(urlDesc) > 40:
urlDesc = urlDesc[:40]
content += \
'<p><a href="' + url + \
'" rel="nofollow noopener noreferrer" target="_blank">' + \
urlDesc + '</a></p>'
return content
def _libretranslate(url: str, text: str, def _libretranslate(url: str, text: str,
source: str, target: str, apiKey: str = None) -> str: source: str, target: str, apiKey: str = None) -> str:
"""Translate string using libretranslate """Translate string using libretranslate
@ -220,22 +242,7 @@ def _libretranslate(url: str, text: str,
# append links form the original text # append links form the original text
if links: if links:
for linkText, url in links.items(): translatedText = addLinksToContent(translatedText, links)
urlDesc = url
if linkText.startswith('@') and linkText in translatedText:
translatedText = \
translatedText.replace(linkText,
'<a href="' + url +
'" rel="nofollow noopener ' +
'noreferrer" target="_blank">' +
linkText + '</a>')
else:
if len(urlDesc) > 40:
urlDesc = urlDesc[:40]
translatedText += \
'<p><a href="' + url + \
'" rel="nofollow noopener noreferrer" target="_blank">' + \
urlDesc + '</a></p>'
return translatedText return translatedText

View File

@ -126,6 +126,7 @@ from markdown import markdownToHtml
from languages import setActorLanguages from languages import setActorLanguages
from languages import getActorLanguages from languages import getActorLanguages
from languages import getLinksFromContent from languages import getLinksFromContent
from languages import addLinksToContent
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -4236,15 +4237,19 @@ def _testGetLinksFromContent():
link1 = 'https://somewebsite.net' link1 = 'https://somewebsite.net'
link2 = 'http://somewhere.or.other' link2 = 'http://somewhere.or.other'
content = \ content = \
'This is <a href="' + link1 + '">a link</a>. ' + \ 'This is <a href="' + link1 + '">@linked</a>. ' + \
'And <a href="' + link2 + '">another</a>.' 'And <a href="' + link2 + '">another</a>.'
links = getLinksFromContent(content) links = getLinksFromContent(content)
assert len(links.items()) == 2 assert len(links.items()) == 2
assert links.get('a link') assert links.get('@linked')
assert links['a link'] == link1 assert links['@linked'] == link1
assert links.get('another') assert links.get('another')
assert links['another'] == link2 assert links['another'] == link2
contentPlain = '<p>' + removeHtml(content) + '</p>'
content = addLinksToContent(contentPlain, links)
assert '>@linked</a>' in content
def runAllTests(): def runAllTests():
print('Running tests...') print('Running tests...')