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
|
||||
|
||||
|
||||
def getLinksFromContent(content: str) -> []:
|
||||
def getLinksFromContent(content: str) -> {}:
|
||||
"""Returns a list of links within the given content
|
||||
"""
|
||||
if '<a href' not in content:
|
||||
return []
|
||||
return {}
|
||||
sections = content.split('<a href')
|
||||
first = True
|
||||
links = []
|
||||
links = {}
|
||||
for subsection in sections:
|
||||
if first:
|
||||
first = False
|
||||
|
@ -172,9 +172,13 @@ def getLinksFromContent(content: str) -> []:
|
|||
if '"' not in subsection:
|
||||
continue
|
||||
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:
|
||||
links.append(url)
|
||||
linkText = subsection.split('>')[1]
|
||||
if '<' in linkText:
|
||||
linkText = linkText.split('<')[0]
|
||||
links[linkText] = url
|
||||
return links
|
||||
|
||||
|
||||
|
@ -216,10 +220,18 @@ def _libretranslate(url: str, text: str,
|
|||
|
||||
# append links form the original text
|
||||
if links:
|
||||
for url in links:
|
||||
for linkText, url in links.items():
|
||||
urlDesc = url
|
||||
if len(urlDesc) > 30:
|
||||
urlDesc = urlDesc[:30]
|
||||
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">' + \
|
||||
|
|
8
tests.py
8
tests.py
|
@ -4239,9 +4239,11 @@ def _testGetLinksFromContent():
|
|||
'This is <a href="' + link1 + '">a link</a>. ' + \
|
||||
'And <a href="' + link2 + '">another</a>.'
|
||||
links = getLinksFromContent(content)
|
||||
assert len(links) == 2
|
||||
assert link1 in links
|
||||
assert link2 in links
|
||||
assert len(links.items()) == 2
|
||||
assert links.get('a link')
|
||||
assert links['a link'] == link1
|
||||
assert links.get('another')
|
||||
assert links['another'] == link2
|
||||
|
||||
|
||||
def runAllTests():
|
||||
|
|
Loading…
Reference in New Issue