Fallback mentions replacement

main
Bob Mottram 2021-01-29 21:33:23 +00:00
parent 2de0f83f5c
commit 9195e57c13
1 changed files with 40 additions and 5 deletions

View File

@ -14,6 +14,7 @@ from utils import getImageExtensions
from utils import loadJson from utils import loadJson
from utils import fileLastModified from utils import fileLastModified
from utils import getLinkPrefixes from utils import getLinkPrefixes
from petnames import getPetName
def removeHtmlTag(htmlStr: str, tag: str) -> str: def removeHtmlTag(htmlStr: str, tag: str) -> str:
@ -489,7 +490,7 @@ def tagExists(tagType: str, tagName: str, tags: {}) -> bool:
return False return False
def _addMention(wordStr: str, httpPrefix: str, following: str, def _addMention(wordStr: str, httpPrefix: str, following: str, petnames: str,
replaceMentions: {}, recipients: [], tags: {}) -> bool: replaceMentions: {}, recipients: [], tags: {}) -> bool:
"""Detects mentions and adds them to the replacements dict and """Detects mentions and adds them to the replacements dict and
recipients list recipients list
@ -501,9 +502,12 @@ def _addMention(wordStr: str, httpPrefix: str, following: str,
# if no domain was specified. eg. @nick # if no domain was specified. eg. @nick
possibleNickname = possibleHandle possibleNickname = possibleHandle
for follow in following: for follow in following:
if follow.startswith(possibleNickname + '@'): if '@' not in follow:
replaceDomain = \ continue
follow.replace('\n', '').replace('\r', '').split('@')[1] followNick = follow.split('@')[0]
if possibleNickname == followNick:
followStr = follow.replace('\n', '').replace('\r', '')
replaceDomain = followStr.split('@')[1]
recipientActor = httpPrefix + "://" + \ recipientActor = httpPrefix + "://" + \
replaceDomain + "/users/" + possibleNickname replaceDomain + "/users/" + possibleNickname
if recipientActor not in recipients: if recipientActor not in recipients:
@ -519,6 +523,33 @@ def _addMention(wordStr: str, httpPrefix: str, following: str,
"\" class=\"u-url mention\">@<span>" + possibleNickname + \ "\" class=\"u-url mention\">@<span>" + possibleNickname + \
"</span></a></span>" "</span></a></span>"
return True return True
# try replacing petnames with mentions
followCtr = 0
for follow in following:
if '@' not in follow:
continue
pet = petnames[followCtr].replace('\n', '')
if pet:
if possibleNickname == pet:
followStr = follow.replace('\n', '').replace('\r', '')
replaceNickname = followStr.split('@')[0]
replaceDomain = followStr.split('@')[1]
recipientActor = httpPrefix + "://" + \
replaceDomain + "/users/" + replaceNickname
if recipientActor not in recipients:
recipients.append(recipientActor)
tags[wordStr] = {
'href': recipientActor,
'name': wordStr,
'type': 'Mention'
}
replaceMentions[wordStr] = \
"<span class=\"h-card\"><a href=\"" + httpPrefix + \
"://" + replaceDomain + "/@" + replaceNickname + \
"\" class=\"u-url mention\">@<span>" + \
replaceNickname + "</span></a></span>"
return True
followCtr += 1
return False return False
possibleNickname = None possibleNickname = None
possibleDomain = None possibleDomain = None
@ -752,10 +783,14 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
# read the following list so that we can detect just @nick # read the following list so that we can detect just @nick
# in addition to @nick@domain # in addition to @nick@domain
following = None following = None
petnames = None
if '@' in words: if '@' in words:
if os.path.isfile(followingFilename): if os.path.isfile(followingFilename):
with open(followingFilename, "r") as f: with open(followingFilename, "r") as f:
following = f.readlines() following = f.readlines()
for handle in following:
pet = getPetName(baseDir, nickname, domain, handle)
petnames.append(pet + '\n')
# extract mentions and tags from words # extract mentions and tags from words
longWordsList = [] longWordsList = []
@ -769,7 +804,7 @@ def addHtmlTags(baseDir: str, httpPrefix: str,
longWordsList.append(wordStr) longWordsList.append(wordStr)
firstChar = wordStr[0] firstChar = wordStr[0]
if firstChar == '@': if firstChar == '@':
if _addMention(wordStr, httpPrefix, following, if _addMention(wordStr, httpPrefix, following, petnames,
replaceMentions, recipients, hashtags): replaceMentions, recipients, hashtags):
prevWordStr = '' prevWordStr = ''
continue continue