From 16fb70aaeb89311e4b4442e3bbf989a9ca5e1052 Mon Sep 17 00:00:00 2001 From: Govindas Date: Mon, 24 Jan 2022 14:43:28 +0200 Subject: [PATCH] escape HTML in markdown, more functions --- Home.page | 2 +- generate.py | 45 ++++++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Home.page b/Home.page index dbb9839..f21e6a8 100644 --- a/Home.page +++ b/Home.page @@ -3,4 +3,4 @@ This is the homepage of our website. > Hello Another line -Another line 2 +**Another line 2** diff --git a/generate.py b/generate.py index 632d476..20c53b5 100644 --- a/generate.py +++ b/generate.py @@ -2,6 +2,7 @@ import os from html5tagger import Document, E, HTML from shutil import rmtree import markdown +import html #if homepage is at Home.page, set homepage to "Home" homepage = "Home" @@ -26,33 +27,43 @@ def generateNavigationBar(lines): title, link = line.strip().split(";", 1) navbar = navbar.li(HTML("" + title + "")) +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): title = title.replace(".page", "") doc = Document(title, lang="en") rawhtml = False + htmlstring = "" for line in lines: - #raw html start - if line.strip() == ">": - 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 raw HTML + rawhtml, doc, htmlstring = parseRawHTML(doc, line, htmlstring, rawhtml) #parse markdown - if rawhtml == False: - doc(HTML( markdown.markdown(line.strip()) )) + if not rawhtml: + doc = parseMarkdown(doc, line) #if indented html was the last line, this is needed for it to not be ignored - if rawhtml == True: - rawhtml = False - doc.div(HTML(html)) + if rawhtml: + doc.div(HTML(htmlstring)) generatePage(title, doc)