replace mentions when automatically translating

main
Bob Mottram 2021-07-20 18:49:12 +01:00
parent cb46a95593
commit 27da1189e3
2 changed files with 29 additions and 15 deletions

View File

@ -157,14 +157,14 @@ def _libretranslateLanguages(url: str, apiKey: str = None) -> []:
return langList return langList
def getLinksFromContent(content: str) -> []: def getLinksFromContent(content: str) -> {}:
"""Returns a list of links within the given content """Returns a list of links within the given content
""" """
if '<a href' not in content: if '<a href' not in content:
return [] return {}
sections = content.split('<a href') sections = content.split('<a href')
first = True first = True
links = [] links = {}
for subsection in sections: for subsection in sections:
if first: if first:
first = False first = False
@ -172,9 +172,13 @@ def getLinksFromContent(content: str) -> []:
if '"' not in subsection: if '"' not in subsection:
continue continue
url = subsection.split('"')[1].strip() url = subsection.split('"')[1].strip()
if '://' in url and '.' in url: if '://' in url and '.' in url and \
'>' in subsection:
if url not in links: if url not in links:
links.append(url) linkText = subsection.split('>')[1]
if '<' in linkText:
linkText = linkText.split('<')[0]
links[linkText] = url
return links return links
@ -216,10 +220,18 @@ def _libretranslate(url: str, text: str,
# append links form the original text # append links form the original text
if links: if links:
for url in links: for linkText, url in links.items():
urlDesc = url urlDesc = url
if len(urlDesc) > 30: if linkText.startswith('@') and linkText in translatedText:
urlDesc = urlDesc[:30] translatedText = \
translatedText.replace(linkText,
'<a href="' + url +
'" rel="nofollow noopener ' +
'noreferrer" target="_blank">' +
linkText + '</a>')
else:
if len(urlDesc) > 40:
urlDesc = urlDesc[:40]
translatedText += \ translatedText += \
'<p><a href="' + url + \ '<p><a href="' + url + \
'" rel="nofollow noopener noreferrer" target="_blank">' + \ '" rel="nofollow noopener noreferrer" target="_blank">' + \

View File

@ -4239,9 +4239,11 @@ def _testGetLinksFromContent():
'This is <a href="' + link1 + '">a link</a>. ' + \ 'This is <a href="' + link1 + '">a link</a>. ' + \
'And <a href="' + link2 + '">another</a>.' 'And <a href="' + link2 + '">another</a>.'
links = getLinksFromContent(content) links = getLinksFromContent(content)
assert len(links) == 2 assert len(links.items()) == 2
assert link1 in links assert links.get('a link')
assert link2 in links assert links['a link'] == link1
assert links.get('another')
assert links['another'] == link2
def runAllTests(): def runAllTests():