mirror of https://gitlab.com/bashrc2/epicyon
Markdown emphasis
parent
830bab130e
commit
75249cf554
16
tests.py
16
tests.py
|
@ -3288,17 +3288,29 @@ def testMarkdownToHtml():
|
||||||
markdown = 'This is just plain text'
|
markdown = 'This is just plain text'
|
||||||
assert markdownToHtml(markdown) == markdown
|
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'
|
markdown = '# Title1\n### Title3\n## Title2\n'
|
||||||
assert markdownToHtml(markdown) == \
|
assert markdownToHtml(markdown) == \
|
||||||
'<h1>Title1</h1><h3>Title3</h3><h2>Title2</h2>'
|
'<h1>Title1</h1><h3>Title3</h3><h2>Title2</h2>'
|
||||||
|
|
||||||
markdown = \
|
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).'
|
'And [something else](https://cat.pic).'
|
||||||
assert markdownToHtml(markdown) == \
|
assert markdownToHtml(markdown) == \
|
||||||
'This is <a href="https://something.somewhere" ' + \
|
'This is <a href="https://something.somewhere" ' + \
|
||||||
'target="_blank" rel="nofollow noopener noreferrer">' + \
|
'target="_blank" rel="nofollow noopener noreferrer">' + \
|
||||||
'a link</a> to something<br>' + \
|
'a link</a> to something.<br>' + \
|
||||||
'And <a href="https://cat.pic" ' + \
|
'And <a href="https://cat.pic" ' + \
|
||||||
'target="_blank" rel="nofollow noopener noreferrer">' + \
|
'target="_blank" rel="nofollow noopener noreferrer">' + \
|
||||||
'something else</a>.'
|
'something else</a>.'
|
||||||
|
|
|
@ -21,9 +21,34 @@ from content import addHtmlTags
|
||||||
from content import replaceEmojiFromTags
|
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:
|
def markdownToHtml(markdown: str) -> str:
|
||||||
"""Converts markdown formatted text to html
|
"""Converts markdown formatted text to html
|
||||||
"""
|
"""
|
||||||
|
markdown = _markdownEmphasisHtml(markdown)
|
||||||
# replace markdown style links with html links
|
# replace markdown style links with html links
|
||||||
replaceLinks = {}
|
replaceLinks = {}
|
||||||
text = markdown
|
text = markdown
|
||||||
|
|
Loading…
Reference in New Issue