diff --git a/newswire.py b/newswire.py index dca813fb2..63379a605 100644 --- a/newswire.py +++ b/newswire.py @@ -93,22 +93,43 @@ def getNewswireTags(text: str, maxTags: int) -> []: words = textSimplified.split(' ') tags = [] for wrd in words: - if wrd.startswith('#'): - if len(wrd) > 1: - if wrd not in tags: - tags.append(wrd) - if len(tags) >= maxTags: - break + if not wrd.startswith('#'): + continue + if len(wrd) <= 1: + continue + if wrd in tags: + continue + tags.append(wrd) + if len(tags) >= maxTags: + break return tags +def limitWordLengths(text: str, maxWordLength: int) -> str: + """Limits the maximum length of words so that the newswire + column cannot become too wide + """ + if ' ' not in text: + return text + words = text.split(' ') + result = '' + for wrd in words: + if len(wrd) > maxWordLength: + wrd = wrd[:maxWordLength] + if result: + result += ' ' + result += wrd + return result + + def _addNewswireDictEntry(baseDir: str, domain: str, newswire: {}, dateStr: str, title: str, link: str, votesStatus: str, postFilename: str, description: str, moderated: bool, mirrored: bool, - tags=[], maxTags=32) -> None: + tags: [] = [], + maxTags: int = 32) -> None: """Update the newswire dictionary """ # remove any markup @@ -121,6 +142,8 @@ def _addNewswireDictEntry(baseDir: str, domain: str, if isFiltered(baseDir, None, None, allText): return + title = limitWordLengths(title, 13) + if tags is None: tags = [] @@ -129,9 +152,10 @@ def _addNewswireDictEntry(baseDir: str, domain: str, # combine the tags into a single list for tag in tags: - if tag not in postTags: - if len(postTags) < maxTags: - postTags.append(tag) + if tag in postTags: + continue + if len(postTags) < maxTags: + postTags.append(tag) # check that no tags are blocked for tag in postTags: diff --git a/tests.py b/tests.py index 9fe956d83..cbcec1c6c 100644 --- a/tests.py +++ b/tests.py @@ -115,6 +115,7 @@ from newsdaemon import hashtagRuleTree from newsdaemon import hashtagRuleResolve from newswire import getNewswireTags from newswire import parseFeedDate +from newswire import limitWordLengths from mastoapiv1 import getMastoApiV1IdFromNickname from mastoapiv1 import getNicknameFromMastoApiV1Id from webapp_post import prepareHtmlPostNickname @@ -4141,9 +4142,22 @@ def _testSwitchWords() -> None: assert result == 'This is a test hamster' +def _testLimitWordLengths() -> None: + print('testLimitWordLengths') + maxWordLength = 13 + text = "This is a test" + result = limitWordLengths(text, maxWordLength) + assert result == text + + text = "This is an exceptionallylongword test" + result = limitWordLengths(text, maxWordLength) + assert result == "This is an exceptionally test" + + def runAllTests(): print('Running tests...') updateDefaultThemesList(os.getcwd()) + _testLimitWordLengths() _testSwitchWords() _testFunctions() _testUserAgentDomain()