Try to remove long words

main2
Bob Mottram 2019-11-04 20:39:14 +00:00
parent e6bd4cf1f4
commit ed2e97ab55
3 changed files with 29 additions and 7 deletions

View File

@ -240,22 +240,38 @@ 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
""" """
words=content.split(' ') words=content.split(' ')
separator='\n'
if not longWordsList:
longWordsList=[]
separator='<br>'
for wordStr in words:
if len(wordStr)>maxWordLength:
if wordStr not in longWordsList:
longWordsList.append(wordStr)
for wordStr in longWordsList: for wordStr in longWordsList:
if wordStr.startswith('<'): if wordStr.startswith('<'):
continue continue
if wordStr.startswith('http'): if 'https:' in wordStr:
continue continue
elif 'http:' in wordStr:
continue
elif 'dat:' in wordStr:
continue
suffix=''
if '<' in wordStr:
suffix='<'+wordStr.split('<',1)[1]
wordStr=wordStr.split('<',1)[0]
if '/' in wordStr: if '/' in wordStr:
continue continue
if len(wordStr[maxWordLength:])<maxWordLength: if len(wordStr[maxWordLength:])<maxWordLength:
content= \ content= \
content.replace(wordStr, \ content.replace(wordStr, \
wordStr[:maxWordLength]+'\n'+ \ wordStr[:maxWordLength]+separator+ \
wordStr[maxWordLength:]) wordStr[maxWordLength:]+suffix)
else: else:
content= \ content= \
content.replace(wordStr, \ content.replace(wordStr, \
wordStr[:maxWordLength]) wordStr[:maxWordLength]+suffix)
return content return content
def addHtmlTags(baseDir: str,httpPrefix: str, \ def addHtmlTags(baseDir: str,httpPrefix: str, \

View File

@ -69,6 +69,7 @@ from inbox import validInboxFilenames
from content import addWebLinks from content import addWebLinks
from content import replaceEmojiFromTags from content import replaceEmojiFromTags
from content import addHtmlTags from content import addHtmlTags
from content import removeLongWords
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -1528,6 +1529,9 @@ def testWebLinks():
exampleText='This post has a very long web link\n\nhttp://cbwebewuvfuftdiudbqd33dddbbyuef23fyug3bfhcyu2fct2cuyqbcbucuwvckiwyfgewfvqejbchevbhwevuevwbqebqekveqvuvjfkf.onion\n\nAnd some other text' exampleText='This post has a very long web link\n\nhttp://cbwebewuvfuftdiudbqd33dddbbyuef23fyug3bfhcyu2fct2cuyqbcbucuwvckiwyfgewfvqejbchevbhwevuevwbqebqekveqvuvjfkf.onion\n\nAnd some other text'
linkedText=addWebLinks(exampleText) linkedText=addWebLinks(exampleText)
assert 'ellipsis' in linkedText assert 'ellipsis' in linkedText
exampleText='<p>1. HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHHAHAHAHAHAHAHAHAHAHAHAHAHHAHAHAHAHAHAHAHAH</p>'
resultText=removeLongWords(exampleText,40,[])
assert resultText=='<p>1. HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA</p></p>'
def testAddEmoji(): def testAddEmoji():
print('testAddEmoji') print('testAddEmoji')

View File

@ -47,6 +47,7 @@ from blocking import isBlocked
from content import getMentionsFromHtml from content import getMentionsFromHtml
from content import addHtmlTags from content import addHtmlTags
from content import replaceEmojiFromTags from content import replaceEmojiFromTags
from content import removeLongWords
from config import getConfigParam from config import getConfigParam
from skills import getSkills from skills import getSkills
from cache import getPersonFromCache from cache import getPersonFromCache
@ -2206,8 +2207,9 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \
if not postJsonObject['object'].get('summary'): if not postJsonObject['object'].get('summary'):
postJsonObject['object']['summary']='' postJsonObject['object']['summary']=''
objectContent=removeLongWords(postJsonObject['object']['content'],40,[])
if not postJsonObject['object']['sensitive']: if not postJsonObject['object']['sensitive']:
contentStr=postJsonObject['object']['content']+attachmentStr contentStr=objectContent+attachmentStr
contentStr=addEmbeddedElements(translate,contentStr) contentStr=addEmbeddedElements(translate,contentStr)
contentStr=insertQuestion(translate,nickname,contentStr,postJsonObject,pageNumber) contentStr=insertQuestion(translate,nickname,contentStr,postJsonObject,pageNumber)
else: else:
@ -2219,7 +2221,7 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \
containerClass='container report' containerClass='container report'
contentStr+='<button class="cwButton" onclick="showContentWarning('+"'"+postID+"'"+')">'+translate['SHOW MORE']+'</button>' contentStr+='<button class="cwButton" onclick="showContentWarning('+"'"+postID+"'"+')">'+translate['SHOW MORE']+'</button>'
contentStr+='<div class="cwText" id="'+postID+'">' contentStr+='<div class="cwText" id="'+postID+'">'
contentStr+=postJsonObject['object']['content']+attachmentStr contentStr+=objectContent+attachmentStr
contentStr=addEmbeddedElements(translate,contentStr) contentStr=addEmbeddedElements(translate,contentStr)
contentStr=insertQuestion(translate,nickname,contentStr,postJsonObject,pageNumber) contentStr=insertQuestion(translate,nickname,contentStr,postJsonObject,pageNumber)
contentStr+='</div>' contentStr+='</div>'