escape HTML in markdown, more functions

This commit is contained in:
Govindas 2022-01-24 14:43:28 +02:00
parent bdb6422612
commit 16fb70aaeb
2 changed files with 29 additions and 18 deletions

View File

@ -3,4 +3,4 @@ This is the homepage of our website.
> >
<b>Hello</b> <b>Hello</b>
Another line Another line
Another line 2 <a href=/test>**Another line 2**</a>

View File

@ -2,6 +2,7 @@ import os
from html5tagger import Document, E, HTML from html5tagger import Document, E, HTML
from shutil import rmtree from shutil import rmtree
import markdown import markdown
import html
#if homepage is at Home.page, set homepage to "Home" #if homepage is at Home.page, set homepage to "Home"
homepage = "Home" homepage = "Home"
@ -26,33 +27,43 @@ def generateNavigationBar(lines):
title, link = line.strip().split(";", 1) title, link = line.strip().split(";", 1)
navbar = navbar.li(HTML("<a href='" + link + "'>" + title + "</a>")) navbar = navbar.li(HTML("<a href='" + link + "'>" + title + "</a>"))
def parseRawHTML(doc, line, htmlstring, rawhtml):
#raw html start
if line.strip() == ">":
rawhtml = True
htmlstring = ""
#parse indented raw html
elif rawhtml:
if line.startswith(" "):
htmlstring = htmlstring + line.strip()
else:
rawhtml = False
doc.div(HTML(html))
htmlstring = ""
return rawhtml, doc, htmlstring
def parseMarkdown(doc, line):
line = html.escape(line.strip())
return doc(HTML(markdown.markdown(line)))
def generateLines(title, lines): def generateLines(title, lines):
title = title.replace(".page", "") title = title.replace(".page", "")
doc = Document(title, lang="en") doc = Document(title, lang="en")
rawhtml = False rawhtml = False
htmlstring = ""
for line in lines: for line in lines:
#raw html start #parse raw HTML
if line.strip() == ">": rawhtml, doc, htmlstring = parseRawHTML(doc, line, htmlstring, rawhtml)
rawhtml = True
html = ""
#parse indented raw html
elif rawhtml == True:
if line.startswith(" "):
html = html + line.strip()
else:
rawhtml = False
doc.div(HTML(html))
#parse markdown #parse markdown
if rawhtml == False: if not rawhtml:
doc(HTML( markdown.markdown(line.strip()) )) doc = parseMarkdown(doc, line)
#if indented html was the last line, this is needed for it to not be ignored #if indented html was the last line, this is needed for it to not be ignored
if rawhtml == True: if rawhtml:
rawhtml = False doc.div(HTML(htmlstring))
doc.div(HTML(html))
generatePage(title, doc) generatePage(title, doc)