Remove formatting such as bold and italics from posts

This is to be conformant with what Mastodon is doing. Probably markdown should be an optional extra
main
Bob Mottram 2020-06-14 14:25:38 +01:00
parent f5e9db1dd6
commit 5ba0e38090
4 changed files with 34 additions and 0 deletions

View File

@ -385,6 +385,21 @@ def replaceContentDuplicates(content: str) -> str:
return content
def removeTextFormatting(content: str) -> str:
"""Removes markup for bold, italics, etc
"""
if '<' not in content:
return content
content = content.replace('<b>', '').replace('</b>', '')
content = content.replace('<i>', '').replace('</i>', '')
content = content.replace('<ul>', '').replace('</ul>', '')
content = content.replace('<ol>', '').replace('</ol>', '')
for level in range(1, 5):
content = content.replace('<h' + str(level) + '>', '')
content = content.replace('</h' + str(level) + '>', '')
return content
def removeLongWords(content: str, maxWordLength: int,
longWordsList: []) -> str:
"""Breaks up long words so that on mobile screens this doesn't

View File

@ -46,6 +46,7 @@ from media import replaceYouTube
from content import removeLongWords
from content import addHtmlTags
from content import replaceEmojiFromTags
from content import removeTextFormatting
from auth import createBasicAuthHeader
from config import getConfigParam
from blocking import isBlocked
@ -3086,6 +3087,10 @@ def downloadAnnounce(session, baseDir: str, httpPrefix: str,
announcedJson['content'] = \
removeLongWords(announcedJson['content'], 40, [])
# remove text formatting, such as bold/italics
announcedJson['content'] = \
removeTextFormatting(announcedJson['content'])
# wrap in create to be consistent with other posts
announcedJson = \
outboxMessageCreateWrap(httpPrefix,

View File

@ -67,6 +67,7 @@ from content import replaceEmojiFromTags
from content import addHtmlTags
from content import removeLongWords
from content import replaceContentDuplicates
from content import removeTextFormatting
from theme import setCSSparam
from semantic import isAccusatory
@ -1798,8 +1799,19 @@ def testAccusatory():
assert(not isAccusatory(testStr, None, 3))
def testRemoveTextFormatting():
print('testRemoveTextFormatting')
testStr = '<p>Text without formatting</p>'
resultStr = removeTextFormatting(testStr)
assert(resultStr == testStr)
testStr = '<p>Text <i>with</i> <h3>formatting</h3></p>'
resultStr = removeTextFormatting(testStr)
assert(resultStr == '<p>Text with formatting</p>')
def runAllTests():
print('Running tests...')
testRemoveTextFormatting()
testAccusatory()
testWebLinks()
testRecentPostsCache()

View File

@ -56,6 +56,7 @@ from bookmarks import bookmarkedByPerson
from announce import announcedByPerson
from blocking import isBlocked
from blocking import isBlockedHashtag
from content import removeTextFormatting
from content import switchWords
from content import getMentionsFromHtml
from content import addHtmlTags
@ -4099,6 +4100,7 @@ def individualPostAsHtml(recentPostsCache: {}, maxRecentPosts: int,
if not isPatch:
objectContent = \
removeLongWords(postJsonObject['object']['content'], 40, [])
objectContent = removeTextFormatting(objectContent)
objectContent = \
switchWords(baseDir, nickname, domain, objectContent)
else: