Support pages inside folders

This commit is contained in:
Govindas 2022-09-06 14:48:04 +03:00
parent f81271097c
commit 87bace72a1
1 changed files with 22 additions and 16 deletions

View File

@ -86,7 +86,7 @@ def generateFooter(lines):
#parse markdown
if not rawhtml:
footer = footer.li(HTML(parseMarkdown(footer, line)))
footer = footer.li(HTML(parseMarkdown(line)))
def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines):
@ -112,13 +112,13 @@ def parseRawHTML(doc, line, htmlstring, rawhtml, id, maxlines):
return rawhtml, doc, htmlstring
def parseMarkdown(doc, line):
def parseMarkdown(line):
#do not allow HTML in markdown
line = html.escape(line.strip())
return markdown.markdown(line)
def generateLines(title, lines):
def generateLines(title, lines, path):
title = title.replace(".page", "")
doc = Document(title, lang="en")
rawhtml = False
@ -130,17 +130,20 @@ def generateLines(title, lines):
#parse markdown
if not rawhtml:
doc = doc(HTML(parseMarkdown(doc, line)))
doc = doc(HTML(parseMarkdown(line)))
generatePage(title, doc)
generatePage(title, doc, path)
def generatePage(title, doc):
def generatePage(title, doc, path):
global pages
global titles
global paths
if 'pages' not in globals():
pages = []
if 'titles' not in globals():
titles = []
if 'paths' not in globals():
paths = []
navbarfile = Path("./").parent.joinpath('navbar')
@ -161,10 +164,12 @@ def generatePage(title, doc):
pages.append(str(doc))
titles.append(title)
paths.append(path)
def writePages():
global pages
global titles
global paths
if 'pages' not in globals():
print("Error: no page files found")
exit()
@ -184,8 +189,9 @@ def writePages():
foldername = titles[id].replace(" ", "-").lower()
outputpath = Path("./").parent.joinpath("website-output")
pageoutput = outputpath.joinpath(paths[id])
dirpath = outputpath.joinpath(foldername)
dirpath = pageoutput.joinpath(foldername)
dirpath.mkdir(parents=True, exist_ok=True)
@ -224,6 +230,14 @@ def writePages():
shutil.rmtree(folder)
else:
shutil.move(str(folder), newpath)
def findPages(dir):
for file in dir.iterdir():
if file.is_file():
if file.suffix == ".page":
with file.open('r') as page:
generateLines(file.stem, page.readlines(), dir)
elif file.stem != "resources" and file.stem != "website-output":
findPages(file)
def main():
@ -239,15 +253,7 @@ def main():
else:
print("No 'footer' file found, there will be no footer.")
pagescount = 0
for file in Path("./").parent.iterdir():
if file.is_file():
if file.suffix == ".page":
pagescount += 1
with file.open('r') as page:
generateLines(file.stem, page.readlines())
print("Found " + str(pagescount) + " pages")
findPages(Path("./").parent)
#write all pages to files
writePages()