forked from indymedia/epicyon
Refactor mentions replacements
parent
8fdcb93546
commit
af32aa08d6
66
content.py
66
content.py
|
@ -9,34 +9,19 @@ __status__ = "Production"
|
|||
import os
|
||||
import commentjson
|
||||
|
||||
def addMentions(baseDir: str,httpPrefix: str, \
|
||||
nickname: str,domain: str,content: str, \
|
||||
recipients: []) -> str:
|
||||
""" Replaces plaintext mentions such as @nick@domain into html
|
||||
by matching against known following accounts
|
||||
def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},recipients: []) -> bool:
|
||||
"""Detects mentions and adds them to the replacements dict and recipients list
|
||||
"""
|
||||
if content.startswith('<p>'):
|
||||
return content
|
||||
wordsOnly=content.replace(',',' ').replace(';',' ').replace('.',' ').replace(':',' ')
|
||||
words=wordsOnly.split(' ')
|
||||
replaceMentions={}
|
||||
if ':' in domain:
|
||||
domain=domain.split(':')[0]
|
||||
followingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/following.txt'
|
||||
if not os.path.isfile(followingFilename):
|
||||
content=content.replace('\n','</p><p>')
|
||||
content='<p>'+content+'</p>'
|
||||
return content.replace('<p></p>','')
|
||||
with open(followingFilename, "r") as f:
|
||||
following = f.readlines()
|
||||
for wordStr in words:
|
||||
if wordStr.startswith('@'):
|
||||
if len(wordStr)>1:
|
||||
if not wordStr.startswith('@'):
|
||||
return False
|
||||
if len(wordStr)<2:
|
||||
return False
|
||||
possibleHandle=wordStr[1:]
|
||||
if '@' in possibleHandle:
|
||||
if '@' not in possibleHandle:
|
||||
return False
|
||||
replaceFound=False
|
||||
possibleNickname=possibleHandle.split('@')[0]
|
||||
possibleDomain=possibleHandle.split('@')[1]
|
||||
replaceFound=False
|
||||
for follow in following:
|
||||
if follow.replace('\n','')==possibleHandle:
|
||||
recipientActor=httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname
|
||||
|
@ -56,7 +41,38 @@ def addMentions(baseDir: str,httpPrefix: str, \
|
|||
replaceMentions[wordStr]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+replaceDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>"
|
||||
replaceFound=True
|
||||
break
|
||||
# do the mention replacements
|
||||
return replaceFound
|
||||
|
||||
def addHtmlTags(baseDir: str,httpPrefix: str, \
|
||||
nickname: str,domain: str,content: str, \
|
||||
recipients: []) -> str:
|
||||
""" Replaces plaintext mentions such as @nick@domain into html
|
||||
by matching against known following accounts
|
||||
"""
|
||||
if content.startswith('<p>'):
|
||||
return content
|
||||
wordsOnly=content.replace(',',' ').replace(';',' ').replace('.',' ').replace(':',' ')
|
||||
words=wordsOnly.split(' ')
|
||||
replaceMentions={}
|
||||
if ':' in domain:
|
||||
domain=domain.split(':')[0]
|
||||
followingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/following.txt'
|
||||
if not os.path.isfile(followingFilename):
|
||||
content=content.replace('\n','</p><p>')
|
||||
content='<p>'+content+'</p>'
|
||||
return content.replace('<p></p>','')
|
||||
|
||||
# read the following list so that we can detect just @nick
|
||||
# in addition to @nick@domain
|
||||
with open(followingFilename, "r") as f:
|
||||
following = f.readlines()
|
||||
|
||||
# extract mentions and tags from words
|
||||
for wordStr in words:
|
||||
if addMention(wordStr,httpPrefix,following,replaceMentions,recipients):
|
||||
continue
|
||||
|
||||
# replace words with their html versions
|
||||
for wordStr,replaceStr in replaceMentions.items():
|
||||
content=content.replace(wordStr,replaceStr)
|
||||
content=content.replace('\n','</p><p>')
|
||||
|
|
|
@ -86,7 +86,7 @@ from manualapprove import manualDenyFollowRequest
|
|||
from manualapprove import manualApproveFollowRequest
|
||||
from announce import createAnnounce
|
||||
from announce import outboxAnnounce
|
||||
from content import addMentions
|
||||
from content import addHtmlTags
|
||||
from media import removeMetaData
|
||||
import os
|
||||
import sys
|
||||
|
@ -1835,7 +1835,7 @@ class PubServer(BaseHTTPRequestHandler):
|
|||
if fields.get('bio'):
|
||||
if fields['bio']!=actorJson['summary']:
|
||||
actorJson['summary']= \
|
||||
addMentions(self.server.baseDir, \
|
||||
addHtmlTags(self.server.baseDir, \
|
||||
self.server.httpPrefix, \
|
||||
nickname, \
|
||||
self.server.domain,fields['bio'],[])
|
||||
|
|
4
posts.py
4
posts.py
|
@ -39,7 +39,7 @@ from utils import validNickname
|
|||
from capabilities import getOcapFilename
|
||||
from capabilities import capabilitiesUpdate
|
||||
from media import attachImage
|
||||
from content import addMentions
|
||||
from content import addHtmlTags
|
||||
from auth import createBasicAuthHeader
|
||||
try:
|
||||
from BeautifulSoup import BeautifulSoup
|
||||
|
@ -381,7 +381,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
|
|||
mentionedRecipients=[]
|
||||
if not clientToServer:
|
||||
# convert content to html
|
||||
content=addMentions(baseDir,httpPrefix, \
|
||||
content=addHtmlTags(baseDir,httpPrefix, \
|
||||
nickname,domain,content, \
|
||||
mentionedRecipients)
|
||||
|
||||
|
|
Loading…
Reference in New Issue