Decrease line lengths

main
Bob Mottram 2020-03-29 09:59:54 +00:00
parent 233ae8e1ca
commit f9e8c09826
2 changed files with 73 additions and 27 deletions

View File

@ -95,13 +95,17 @@ def replaceEmojiFromTags(content: str,tag: [],messageType: str) -> str:
htmlClass='emojiheader' htmlClass='emojiheader'
if messageType=='profile': if messageType=='profile':
htmlClass='emojiprofile' htmlClass='emojiprofile'
emojiHtml="<img src=\""+tagItem['icon']['url']+"\" alt=\""+tagItem['name'].replace(':','')+"\" align=\"middle\" class=\""+htmlClass+"\"/>" emojiHtml= \
"<img src=\""+tagItem['icon']['url']+"\" alt=\""+ \
tagItem['name'].replace(':','')+ \
"\" align=\"middle\" class=\""+htmlClass+"\"/>"
content=content.replace(tagItem['name'],emojiHtml) content=content.replace(tagItem['name'],emojiHtml)
return content return content
def addMusicTag(content: str,tag: str) -> str: def addMusicTag(content: str,tag: str) -> str:
"""If a music link is found then ensure that the post is tagged appropriately """If a music link is found then ensure that the post is
tagged appropriately
""" """
if '#' not in tag: if '#' not in tag:
tag='#'+tag tag='#'+tag
@ -142,11 +146,16 @@ def addWebLinks(content: str) -> str:
markup+='<span class="invisible">i2p://</span>' markup+='<span class="invisible">i2p://</span>'
elif w.startswith('dat://'): elif w.startswith('dat://'):
markup+='<span class="invisible">dat://</span>' markup+='<span class="invisible">dat://</span>'
linkText=w.replace('https://','').replace('http://','').replace('dat://','').replace('i2p://','') linkText= \
w.replace('https://','').replace('http://','').replace('dat://','').replace('i2p://','')
# prevent links from becoming too long # prevent links from becoming too long
if len(linkText)>maxLinkLength: if len(linkText)>maxLinkLength:
markup+='<span class="ellipsis">'+linkText[:maxLinkLength]+'</span>' markup+= \
markup+='<span class="invisible">'+linkText[maxLinkLength:]+'</span></a>' '<span class="ellipsis">'+ \
linkText[:maxLinkLength]+'</span>'
markup+= \
'<span class="invisible">'+ \
linkText[maxLinkLength:]+'</span></a>'
else: else:
markup+='<span class="ellipsis">'+linkText+'</span></a>' markup+='<span class="ellipsis">'+linkText+'</span></a>'
replaceDict[w]=markup replaceDict[w]=markup
@ -181,7 +190,9 @@ def addHashTags(wordStr: str,httpPrefix: str,domain: str, \
'type': 'Hashtag' 'type': 'Hashtag'
} }
replaceHashTags[wordStr]= \ replaceHashTags[wordStr]= \
"<a href=\""+hashtagUrl+"\" class=\"mention hashtag\" rel=\"tag\">#<span>"+hashtag+"</span></a>" "<a href=\""+hashtagUrl+ \
"\" class=\"mention hashtag\" rel=\"tag\">#<span>"+ \
hashtag+"</span></a>"
return True return True
def loadEmojiDict(emojiDataFilename: str,emojiDict: {}) -> None: def loadEmojiDict(emojiDataFilename: str,emojiDict: {}) -> None:
@ -204,7 +215,8 @@ def loadEmojiDict(emojiDataFilename: str,emojiDict: {}) -> None:
continue continue
if '..' in emojiUnicode: if '..' in emojiUnicode:
emojiUnicode=emojiUnicode.split('..')[0] emojiUnicode=emojiUnicode.split('..')[0]
emojiName=line.split(')',1)[1].strip().replace('\n','').replace(' ','').replace('-','') emojiName= \
line.split(')',1)[1].strip().replace('\n','').replace(' ','').replace('-','')
if '..' in emojiName: if '..' in emojiName:
emojiName=emojiName.split('..')[0] emojiName=emojiName.split('..')[0]
emojiDict[emojiName.lower()]=emojiUnicode emojiDict[emojiName.lower()]=emojiUnicode
@ -249,8 +261,10 @@ def addEmoji(baseDir: str,wordStr: str, \
} }
return True return True
def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},recipients: [],tags: {}) -> bool: def addMention(wordStr: str,httpPrefix: str,following: str, \
"""Detects mentions and adds them to the replacements dict and recipients list replaceMentions: {},recipients: [],tags: {}) -> bool:
"""Detects mentions and adds them to the replacements dict and
recipients list
""" """
possibleHandle=wordStr[1:] possibleHandle=wordStr[1:]
# @nick # @nick
@ -261,7 +275,8 @@ def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},r
for follow in following: for follow in following:
if follow.startswith(possibleNickname+'@'): if follow.startswith(possibleNickname+'@'):
replaceDomain=follow.replace('\n','').split('@')[1] replaceDomain=follow.replace('\n','').split('@')[1]
recipientActor=httpPrefix+"://"+replaceDomain+"/users/"+possibleNickname recipientActor= \
httpPrefix+"://"+replaceDomain+"/users/"+possibleNickname
if recipientActor not in recipients: if recipientActor not in recipients:
recipients.append(recipientActor) recipients.append(recipientActor)
tags[wordStr]={ tags[wordStr]={
@ -269,7 +284,11 @@ def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},r
'name': wordStr, 'name': wordStr,
'type': 'Mention' 'type': 'Mention'
} }
replaceMentions[wordStr]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+replaceDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>" replaceMentions[wordStr]= \
"<span class=\"h-card\"><a href=\""+httpPrefix+ \
"://"+replaceDomain+"/@"+possibleNickname+ \
"\" class=\"u-url mention\">@<span>"+possibleNickname+ \
"</span></a></span>"
return True return True
return False return False
possibleNickname=None possibleNickname=None
@ -286,7 +305,8 @@ def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},r
for follow in following: for follow in following:
if follow.replace('\n','')!=possibleHandle: if follow.replace('\n','')!=possibleHandle:
continue continue
recipientActor=httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname recipientActor= \
httpPrefix+"://"+possibleDomain+"/users/"+possibleNickname
if recipientActor not in recipients: if recipientActor not in recipients:
recipients.append(recipientActor) recipients.append(recipientActor)
tags[wordStr]={ tags[wordStr]={
@ -294,7 +314,11 @@ def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},r
'name': wordStr, 'name': wordStr,
'type': 'Mention' 'type': 'Mention'
} }
replaceMentions[wordStr]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+possibleDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>" replaceMentions[wordStr]= \
"<span class=\"h-card\"><a href=\""+httpPrefix+ \
"://"+possibleDomain+"/@"+possibleNickname+ \
"\" class=\"u-url mention\">@<span>"+possibleNickname+ \
"</span></a></span>"
return True return True
# @nick@domain # @nick@domain
if not (possibleDomain=='localhost' or '.' in possibleDomain): if not (possibleDomain=='localhost' or '.' in possibleDomain):
@ -307,11 +331,16 @@ def addMention(wordStr: str,httpPrefix: str,following: str,replaceMentions: {},r
'name': wordStr, 'name': wordStr,
'type': 'Mention' 'type': 'Mention'
} }
replaceMentions[wordStr]="<span class=\"h-card\"><a href=\""+httpPrefix+"://"+possibleDomain+"/@"+possibleNickname+"\" class=\"u-url mention\">@<span>"+possibleNickname+"</span></a></span>" replaceMentions[wordStr]= \
"<span class=\"h-card\"><a href=\""+httpPrefix+ \
"://"+possibleDomain+"/@"+possibleNickname+ \
"\" class=\"u-url mention\">@<span>"+possibleNickname+ \
"</span></a></span>"
return True return True
def removeLongWords(content: str,maxWordLength: int,longWordsList: []) -> str: def removeLongWords(content: str,maxWordLength: int,longWordsList: []) -> str:
"""Breaks up long words so that on mobile screens this doesn't disrupt the layout """Breaks up long words so that on mobile screens this doesn't
disrupt the layout
""" """
if ' ' not in content: if ' ' not in content:
# handle a single very long string with no spaces # handle a single very long string with no spaces
@ -410,7 +439,8 @@ def addHtmlTags(baseDir: str,httpPrefix: str, \
originalDomain=domain originalDomain=domain
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain=domain.split(':')[0]
followingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/following.txt' followingFilename= \
baseDir+'/accounts/'+nickname+'@'+domain+'/following.txt'
# 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
@ -429,23 +459,29 @@ 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,replaceMentions,recipients,hashtags): if addMention(wordStr,httpPrefix,following, \
replaceMentions,recipients,hashtags):
continue continue
elif firstChar=='#': elif firstChar=='#':
if addHashTags(wordStr,httpPrefix,originalDomain,replaceHashTags,hashtags): if addHashTags(wordStr,httpPrefix,originalDomain, \
replaceHashTags,hashtags):
continue continue
elif ':' in wordStr: elif ':' in wordStr:
#print('TAG: emoji located - '+wordStr) #print('TAG: emoji located - '+wordStr)
wordStr2=wordStr.split(':')[1] wordStr2=wordStr.split(':')[1]
if not emojiDict: if not emojiDict:
# emoji.json is generated so that it can be customized and the changes # emoji.json is generated so that it can be customized and
# will be retained even if default_emoji.json is subsequently updated # the changes will be retained even if default_emoji.json
# is subsequently updated
if not os.path.isfile(baseDir+'/emoji/emoji.json'): if not os.path.isfile(baseDir+'/emoji/emoji.json'):
copyfile(baseDir+'/emoji/default_emoji.json',baseDir+'/emoji/emoji.json') copyfile(baseDir+'/emoji/default_emoji.json', \
baseDir+'/emoji/emoji.json')
emojiDict=loadJson(baseDir+'/emoji/emoji.json') emojiDict=loadJson(baseDir+'/emoji/emoji.json')
#print('TAG: looking up emoji for :'+wordStr2+':') #print('TAG: looking up emoji for :'+wordStr2+':')
addEmoji(baseDir,':'+wordStr2+':',httpPrefix,originalDomain,replaceEmoji,hashtags,emojiDict) addEmoji(baseDir,':'+wordStr2+':',httpPrefix, \
originalDomain,replaceEmoji,hashtags, \
emojiDict)
# replace words with their html versions # replace words with their html versions
for wordStr,replaceStr in replaceMentions.items(): for wordStr,replaceStr in replaceMentions.items():
@ -462,7 +498,8 @@ def addHtmlTags(baseDir: str,httpPrefix: str, \
content=content.replace(' --linebreak-- ','</p><p>') content=content.replace(' --linebreak-- ','</p><p>')
return '<p>'+content+'</p>' return '<p>'+content+'</p>'
def getMentionsFromHtml(htmlText: str,matchStr="<span class=\"h-card\"><a href=\"") -> []: def getMentionsFromHtml(htmlText: str, \
matchStr="<span class=\"h-card\"><a href=\"") -> []:
"""Extracts mentioned actors from the given html content string """Extracts mentioned actors from the given html content string
""" """
mentions=[] mentions=[]
@ -481,10 +518,13 @@ def getMentionsFromHtml(htmlText: str,matchStr="<span class=\"h-card\"><a href=\
return mentions return mentions
def extractMediaInFormPOST(postBytes,boundary,name: str): def extractMediaInFormPOST(postBytes,boundary,name: str):
"""Extracts the binary encoding for image/video/audio within a http form POST """Extracts the binary encoding for image/video/audio within a http
form POST
Returns the media bytes and the remaining bytes Returns the media bytes and the remaining bytes
""" """
imageStartBoundary=b'Content-Disposition: form-data; name="'+name.encode('utf8', 'ignore')+b'";' imageStartBoundary= \
b'Content-Disposition: form-data; name="'+ \
name.encode('utf8', 'ignore')+b'";'
imageStartLocation=postBytes.find(imageStartBoundary) imageStartLocation=postBytes.find(imageStartBoundary)
if imageStartLocation==-1: if imageStartLocation==-1:
return None,postBytes return None,postBytes
@ -566,7 +606,8 @@ def saveMediaInFormPOST(mediaBytes,debug: bool, \
for ex in extensionTypes: for ex in extensionTypes:
if ex==detectedExtension: if ex==detectedExtension:
continue continue
possibleOtherFormat=filename.replace('.temp','').replace('.'+detectedExtension,'.'+ex) possibleOtherFormat= \
filename.replace('.temp','').replace('.'+detectedExtension,'.'+ex)
if os.path.isfile(possibleOtherFormat): if os.path.isfile(possibleOtherFormat):
os.remove(possibleOtherFormat) os.remove(possibleOtherFormat)
@ -582,7 +623,8 @@ def extractTextFieldsInPOST(postBytes,boundary,debug: bool) -> {}:
""" """
msg=email.parser.BytesParser().parsebytes(postBytes) msg=email.parser.BytesParser().parsebytes(postBytes)
if debug: if debug:
print('DEBUG: POST arriving '+msg.get_payload(decode=True).decode('utf-8')) print('DEBUG: POST arriving '+ \
msg.get_payload(decode=True).decode('utf-8'))
messageFields=msg.get_payload(decode=True).decode('utf-8').split(boundary) messageFields=msg.get_payload(decode=True).decode('utf-8').split(boundary)
fields={} fields={}
# examine each section of the POST, separated by the boundary # examine each section of the POST, separated by the boundary

View File

@ -1402,6 +1402,10 @@ def testWebLinks():
resultText=removeLongWords(exampleText,40,[]) resultText=removeLongWords(exampleText,40,[])
assert resultText=='<p>ABCABCABCABCABCABCABCABCABCABCABCABCABCA<\p>' assert resultText=='<p>ABCABCABCABCABCABCABCABCABCABCABCABCABCA<\p>'
exampleText='"the nucleus of mutual-support institutions, habits, and customs remains alive with the millions; it keeps them together; and they prefer to cling to their customs, beliefs, and traditions rather than to accept the teachings of a war of each against all"\n\n--Peter Kropotkin'
resultText=removeLongWords(addWebLinks(exampleText),40,[])
assert resultText==exampleText
def testAddEmoji(): def testAddEmoji():
print('testAddEmoji') print('testAddEmoji')