Welcome text in markdown

merge-requests/21/head
Bob Mottram 2021-02-24 20:37:59 +00:00
parent 47788d5a43
commit 32eb256a49
5 changed files with 63 additions and 9 deletions

View File

@ -1 +0,0 @@
Welcome to Epicyon

View File

@ -98,6 +98,7 @@ from newswire import parseFeedDate
from mastoapiv1 import getMastoApiV1IdFromNickname
from mastoapiv1 import getNicknameFromMastoApiV1Id
from webapp_post import prepareHtmlPostNickname
from webapp_utils import markdownToHtml
testServerAliceRunning = False
testServerBobRunning = False
@ -3282,9 +3283,31 @@ def testValidHashTag():
assert not validHashTag('This=IsAlsoNotValid"')
def testMarkdownToHtml():
print('testMarkdownToHtml')
markdown = 'This is just plain text'
assert markdownToHtml(markdown) == markdown
markdown = '# Title1\n### Title3\n## Title2\n'
assert markdownToHtml(markdown) == \
'<h1>Title1</h1><br><h3>Title3</h3><br><h2>Title2</h2><br>'
markdown = \
'This is [a link](https://something.somewhere) to something\n' + \
'And [something else](https://cat.pic).'
assert markdownToHtml(markdown) == \
'This is <a href="https://something.somewhere" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \
'a link</a> to something<br>' + \
'And <a href="https://cat.pic" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \
'something else</a>.'
def runAllTests():
print('Running tests...')
testFunctions()
testMarkdownToHtml()
testValidHashTag()
testPrepareHtmlPostNickname()
testDomainHandling()

View File

@ -24,20 +24,52 @@ from content import replaceEmojiFromTags
def markdownToHtml(markdown: str) -> str:
"""Converts markdown formatted text to html
"""
# replace markdown style links with html links
replaceLinks = {}
text = markdown
while '[' in text:
if ')' not in text:
break
text = text.split('[', 1)[1]
markdownLink = '[' + text.split(')')[0] + ')'
if ']' not in markdownLink or \
'(' not in markdownLink:
text = text.split(')', 1)[1]
continue
replaceLinks[markdownLink] = \
'<a href="' + \
markdownLink.split('(')[1].split(')')[0] + \
'" target="_blank" rel="nofollow noopener noreferrer">' + \
markdownLink.split('[')[1].split(']')[0] + \
'</a>'
text = text.split(')', 1)[1]
for mdLink, htmlLink in replaceLinks.items():
markdown = markdown.replace(mdLink, htmlLink)
# replace headers
linesList = markdown.split('\n')
htmlStr = ''
ctr = 0
for line in linesList:
if ctr > 0:
htmlStr += '<br>'
if line.startswith('#####'):
line = line.replace('#####', '<h5>').strip() + '</h5>'
line = line.replace('#####', '').strip()
line = '<h5>' + line + '</h5>'
elif line.startswith('####'):
line = line.replace('####', '<h4>').strip() + '</h4>'
line = line.replace('####', '').strip()
line = '<h4>' + line + '</h4>'
elif line.startswith('###'):
line = line.replace('###', '<h3>').strip() + '</h3>'
line = line.replace('###', '').strip()
line = '<h3>' + line + '</h3>'
elif line.startswith('##'):
line = line.replace('##', '<h2>').strip() + '</h2>'
line = line.replace('##', '').strip()
line = '<h2>' + line + '</h2>'
elif line.startswith('#'):
line = line.replace('#', '<h1>').strip() + '</h1>'
line = line.replace('#', '').strip()
line = '<h1>' + line + '</h1>'
htmlStr += line
ctr += 1
return htmlStr

View File

@ -35,13 +35,13 @@ def htmlWelcomeScreen(baseDir: str, nickname: str, domain: str,
baseDir + '/accounts/welcome-background.jpg')
welcomeText = 'Welcome to Epicyon'
welcomeFilename = baseDir + '/accounts/welcome.txt'
welcomeFilename = baseDir + '/accounts/welcome.md'
if not os.path.isfile(welcomeFilename):
defaultFilename = baseDir + '/defaultwelcome/' + language + '.txt'
defaultFilename = baseDir + '/defaultwelcome/' + language + '.md'
if os.path.isfile(defaultFilename):
copyfile(defaultFilename, welcomeFilename)
if os.path.isfile(welcomeFilename):
with open(baseDir + '/accounts/welcome.txt', 'r') as welcomeFile:
with open(baseDir + '/accounts/welcome.md', 'r') as welcomeFile:
welcomeText = markdownToHtml(welcomeFile.read())
welcomeForm = ''