mirror of https://gitlab.com/bashrc2/epicyon
replace mentions when automatically translating
parent
cb46a95593
commit
27da1189e3
28
languages.py
28
languages.py
|
@ -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">' + \
|
||||||
|
|
8
tests.py
8
tests.py
|
@ -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():
|
||||||
|
|
Loading…
Reference in New Issue