Markdown emphasis

merge-requests/21/head
Bob Mottram 2021-02-25 10:54:38 +00:00
parent 830bab130e
commit 75249cf554
2 changed files with 39 additions and 2 deletions

View File

@ -3288,17 +3288,29 @@ def testMarkdownToHtml():
markdown = 'This is just plain text'
assert markdownToHtml(markdown) == markdown
markdown = 'This is **bold**'
assert markdownToHtml(markdown) == 'This is <b>bold</b>'
markdown = 'This is *italic*'
assert markdownToHtml(markdown) == 'This is <i>italic</i>'
markdown = 'This is _underlined_'
assert markdownToHtml(markdown) == 'This is <ul>underlined</ul>'
markdown = 'This is **just** plain text'
assert markdownToHtml(markdown) == 'This is <b>just</b> plain text'
markdown = '# Title1\n### Title3\n## Title2\n'
assert markdownToHtml(markdown) == \
'<h1>Title1</h1><h3>Title3</h3><h2>Title2</h2>'
markdown = \
'This is [a link](https://something.somewhere) to something\n' + \
'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>' + \
'a link</a> to something.<br>' + \
'And <a href="https://cat.pic" ' + \
'target="_blank" rel="nofollow noopener noreferrer">' + \
'something else</a>.'

View File

@ -21,9 +21,34 @@ from content import addHtmlTags
from content import replaceEmojiFromTags
def _markdownEmphasisHtml(markdown: str) -> str:
"""Add italics and bold html markup to the given markdown
"""
punctuation = ('.', ';', ':')
noPunctuation = markdown
for ch in punctuation:
noPunctuation = noPunctuation.replace(ch, ' ')
wordList = noPunctuation.split(' ')
replacements = {}
for word in wordList:
if word.startswith('**') and word.endswith('**'):
replacements[word] = \
'<b>' + word.replace('*', '') + '</b>'
elif word.startswith('*') and word.endswith('*'):
replacements[word] = \
'<i>' + word.replace('*', '') + '</i>'
elif word.startswith('_') and word.endswith('_'):
replacements[word] = \
'<ul>' + word.replace('_', '') + '</ul>'
for md, html in replacements.items():
markdown = markdown.replace(md, html)
return markdown
def markdownToHtml(markdown: str) -> str:
"""Converts markdown formatted text to html
"""
markdown = _markdownEmphasisHtml(markdown)
# replace markdown style links with html links
replaceLinks = {}
text = markdown