main
Bob Mottram 2020-06-11 12:56:08 +01:00
parent 353f87f1aa
commit 136949752f
1 changed files with 47 additions and 53 deletions

View File

@ -112,7 +112,7 @@ def addMusicTag(content: str, tag: str) -> str:
tag = '#'+tag tag = '#'+tag
if tag in content: if tag in content:
return content return content
musicSites = ['soundcloud.com', 'bandcamp.com'] musicSites = ('soundcloud.com', 'bandcamp.com')
musicSiteFound = False musicSiteFound = False
for site in musicSites: for site in musicSites:
if site+'/' in content: if site+'/' in content:
@ -129,11 +129,18 @@ def addWebLinks(content: str) -> str:
if ':' not in content: if ':' not in content:
return content return content
if not ('https://' in content or 'http://' in content or prefixes = ('https://', 'http://', 'dat://', 'i2p://', 'gnunet://',
'i2p://' in content or 'gnunet://' in content or 'hyper://', 'gemini://', 'gopher://', 'briar:')
'gemini://' in content or 'gopher://' in content or
'hyper://' in content or 'dat://' in content or # do any of these prefixes exist within the content?
'briar:' in content): prefixFound = False
for prefix in prefixes:
if prefix in content:
prefixFound = True
break
# if there are no prefixes then just keep the content we have
if not prefixFound:
return content return content
maxLinkLength = 40 maxLinkLength = 40
@ -143,56 +150,43 @@ def addWebLinks(content: str) -> str:
for w in words: for w in words:
if ':' not in w: if ':' not in w:
continue continue
if w.startswith('https://') or \ # does the word begin with a prefix?
w.startswith('http://') or \ prefixFound = False
w.startswith('i2p://') or \ for prefix in prefixes:
w.startswith('briar:') or \ if w.startswith(prefix):
w.startswith('gnunet://') or \ prefixFound = True
w.startswith('gemini://') or \ break
w.startswith('gopher://') or \ if not prefixFound:
w.startswith('hyper://') or \ continue
w.startswith('dat://'): # the word contains a prefix
if w.endswith('.') or w.endswith(';'): if w.endswith('.') or w.endswith(';'):
w = w[:-1] w = w[:-1]
markup = '<a href="' + w + \ markup = '<a href="' + w + \
'" rel="nofollow noopener" target="_blank">' '" rel="nofollow noopener" target="_blank">'
if w.startswith('https://'): for prefix in prefixes:
markup += '<span class="invisible">https://</span>' if w.startswith(prefix):
elif w.startswith('http://'): markup += '<span class="invisible">' + prefix + '</span>'
markup += '<span class="invisible">http://</span>' break
elif w.startswith('i2p://'): linkText = w
markup += '<span class="invisible">i2p://</span>' for prefix in prefixes:
elif w.startswith('gnunet://'): linkText = linkText.replace(prefix, '')
markup += '<span class="invisible">gnunet://</span>' # prevent links from becoming too long
elif w.startswith('dat://'): if len(linkText) > maxLinkLength:
markup += '<span class="invisible">dat://</span>' markup += '<span class="ellipsis">' + \
elif w.startswith('hyper://'): linkText[:maxLinkLength] + '</span>'
markup += '<span class="invisible">hyper://</span>' markup += '<span class="invisible">' + \
elif w.startswith('gemini://'): linkText[maxLinkLength:] + '</span></a>'
markup += '<span class="invisible">gemini://</span>' else:
elif w.startswith('gopher://'): markup += '<span class="ellipsis">' + linkText + '</span></a>'
markup += '<span class="invisible">gopher://</span>' replaceDict[w] = markup
elif w.startswith('briar:'):
markup += '<span class="invisible">briar:</span>' # do the replacements
linkText = w.replace('https://', '').replace('http://', '')
linkText = linkText.replace('dat://', '').replace('i2p://', '')
linkText = linkText.replace('gnunet://', '')
linkText = linkText.replace('hyper://', '')
linkText = linkText.replace('gemini://', '')
linkText = linkText.replace('gopher://', '')
linkText = linkText.replace('briar:', '')
# prevent links from becoming too long
if len(linkText) > maxLinkLength:
markup += '<span class="ellipsis">' + \
linkText[:maxLinkLength] + '</span>'
markup += '<span class="invisible">' + \
linkText[maxLinkLength:] + '</span></a>'
else:
markup += '<span class="ellipsis">' + linkText + '</span></a>'
replaceDict[w] = markup
for url, markup in replaceDict.items(): for url, markup in replaceDict.items():
content = content.replace(url, markup) content = content.replace(url, markup)
# replace any line breaks
content = content.replace(' --linebreak-- ', '<br>') content = content.replace(' --linebreak-- ', '<br>')
return content return content