osdict_project/spider/normalutils/utils/contenttohtml.py

22 lines
734 B
Python

from markdown import markdown
import html
from normalutils.choices.htmlchoices import HtmlContentType
def content_to_html(content: str, content_type=HtmlContentType.TEXT_MARKDOWN, title=None):
if content_type == HtmlContentType.TEXT_MARKDOWN:
content = markdown(content)
return content
elif content_type == HtmlContentType.TEXT_PLAIN:
content = html.escape(content)
content = content.split('\n')
ret = ''
for sentence in content:
ret += ''.join(["<p>", sentence, "</p>\n"])
# add title
if title is not None:
title = html.escape(title)
return ''.join(['<h1>', title, '</h1>\n', ret])
return ret