diff --git a/defaultwelcome/en.txt b/defaultwelcome/en.md similarity index 100% rename from defaultwelcome/en.txt rename to defaultwelcome/en.md diff --git a/defaultwelcome/en.txt~ b/defaultwelcome/en.txt~ deleted file mode 100644 index 334cab178..000000000 --- a/defaultwelcome/en.txt~ +++ /dev/null @@ -1 +0,0 @@ -Welcome to Epicyon diff --git a/tests.py b/tests.py index 02fe2b604..79ba90062 100644 --- a/tests.py +++ b/tests.py @@ -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) == \ + '

Title1


Title3


Title2


' + + markdown = \ + 'This is [a link](https://something.somewhere) to something\n' + \ + 'And [something else](https://cat.pic).' + assert markdownToHtml(markdown) == \ + 'This is ' + \ + 'a link to something
' + \ + 'And ' + \ + 'something else.' + + def runAllTests(): print('Running tests...') testFunctions() + testMarkdownToHtml() testValidHashTag() testPrepareHtmlPostNickname() testDomainHandling() diff --git a/webapp_utils.py b/webapp_utils.py index 1ab1afd40..52b83d424 100644 --- a/webapp_utils.py +++ b/webapp_utils.py @@ -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] = \ + '' + \ + markdownLink.split('[')[1].split(']')[0] + \ + '' + 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 += '
' if line.startswith('#####'): - line = line.replace('#####', '
').strip() + '
' + line = line.replace('#####', '').strip() + line = '
' + line + '
' elif line.startswith('####'): - line = line.replace('####', '

').strip() + '

' + line = line.replace('####', '').strip() + line = '

' + line + '

' elif line.startswith('###'): - line = line.replace('###', '

').strip() + '

' + line = line.replace('###', '').strip() + line = '

' + line + '

' elif line.startswith('##'): - line = line.replace('##', '

').strip() + '

' + line = line.replace('##', '').strip() + line = '

' + line + '

' elif line.startswith('#'): - line = line.replace('#', '

').strip() + '

' + line = line.replace('#', '').strip() + line = '

' + line + '

' htmlStr += line + ctr += 1 return htmlStr diff --git a/webapp_welcome.py b/webapp_welcome.py index 41f0f4d8f..2ab528d87 100644 --- a/webapp_welcome.py +++ b/webapp_welcome.py @@ -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 = ''