2022-05-16 20:12:06 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-05-17 18:16:39 +02:00
|
|
|
|
2022-05-16 20:12:06 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
from pathlib import Path
|
2022-05-17 18:16:39 +02:00
|
|
|
from markdown import Markdown
|
2022-05-16 20:12:06 +02:00
|
|
|
|
|
|
|
def ReadFile(p):
|
|
|
|
try:
|
|
|
|
with open(p, 'r') as f:
|
|
|
|
return f.read()
|
|
|
|
except Exception:
|
|
|
|
print("Error reading file {}".format(p))
|
|
|
|
return None
|
|
|
|
|
|
|
|
def WriteFile(p, c):
|
|
|
|
try:
|
|
|
|
with open(p, 'w') as f:
|
|
|
|
f.write(c)
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
print("Error writing file {}".format(p))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def ResetPublic():
|
|
|
|
try:
|
|
|
|
shutil.rmtree('public')
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def FormatTitles(Titles):
|
2022-05-16 21:16:36 +02:00
|
|
|
HTMLTitles = ''
|
|
|
|
for t in Titles:
|
|
|
|
Heading = '- ' * (t.split(' ')[0].count('#')-1)
|
|
|
|
t = t.lstrip('#')
|
|
|
|
HTMLTitles += Heading + t + ' \n'
|
|
|
|
return Markdown().convert(HTMLTitles)
|
2022-05-16 20:12:06 +02:00
|
|
|
|
|
|
|
def LoadFromDir(Dir):
|
|
|
|
Contents = {}
|
|
|
|
for File in Path(Dir).rglob('*.html'):
|
|
|
|
File = str(File)[len(Dir)+1:]
|
|
|
|
Contents.update({File: ReadFile('{}/{}'.format(Dir, File))})
|
|
|
|
return Contents
|
|
|
|
|
|
|
|
def PreProcessor(File):
|
|
|
|
File = ReadFile(File)
|
|
|
|
Content, Titles, Meta = '', [], {
|
|
|
|
'Template': 'Standard.html',
|
|
|
|
'Style': ''}
|
|
|
|
for l in File.splitlines():
|
|
|
|
ls = l.lstrip()
|
2022-05-17 18:16:39 +02:00
|
|
|
if ls.startswith('//'):
|
|
|
|
if ls.startswith('// Template: '):
|
|
|
|
Meta['Template'] = ls[len('// Template: '):]
|
|
|
|
elif ls.startswith('// Background: '):
|
|
|
|
Meta['Style'] += "#MainBox{Background:" + ls[len('// Background: '):] + ";} "
|
|
|
|
elif ls.startswith('// Style: '):
|
|
|
|
Meta['Style'] += ls[len('// Style: '):] + ' '
|
2022-05-16 20:12:06 +02:00
|
|
|
else:
|
|
|
|
Content += l + '\n'
|
2022-05-17 18:16:39 +02:00
|
|
|
if ls.startswith(('h1', 'h2', 'h3', 'h4', 'h5', 'h6')):
|
|
|
|
if not ls.startswith(("h1(class='NoTitle", 'h1(class="NoTitle')):
|
|
|
|
Titles += ['#'*int(ls[1]) + ls]
|
2022-05-16 20:12:06 +02:00
|
|
|
return Content, Titles, Meta
|
|
|
|
|
2022-05-17 18:16:39 +02:00
|
|
|
def PugCompiler(c):
|
|
|
|
WriteFile('tmp.pug', c)
|
|
|
|
os.system('pug tmp.pug > /dev/null')
|
|
|
|
return ReadFile('tmp.html')
|
|
|
|
|
2022-05-16 20:12:06 +02:00
|
|
|
def PatchHTML(Template, Parts, Content, Titles, Meta):
|
|
|
|
HTMLTitles = FormatTitles(Titles)
|
|
|
|
|
2022-05-16 21:16:36 +02:00
|
|
|
Template = Template.replace('[HTML:Page:Title]', 'Untitled' if not Titles else Titles[0].lstrip('#'))
|
2022-05-16 20:12:06 +02:00
|
|
|
Template = Template.replace('[HTML:Page:Style]', Meta['Style'])
|
2022-05-16 21:16:36 +02:00
|
|
|
Template = Template.replace('[HTML:Page:RightBox]', HTMLTitles)
|
2022-05-16 20:12:06 +02:00
|
|
|
Template = Template.replace('[HTML:Page:MainBox]', Content)
|
|
|
|
|
|
|
|
for p in Parts:
|
|
|
|
Template = Template.replace('[HTML:Part:{}]'.format(p), Parts[p])
|
|
|
|
return Template
|
|
|
|
|
|
|
|
def MakeSite(Templates, Parts):
|
|
|
|
Pages = []
|
2022-05-17 18:16:39 +02:00
|
|
|
for File in Path('Pages').rglob('*.pug'):
|
2022-05-16 20:12:06 +02:00
|
|
|
File = str(File)[len('Pages/'):]
|
|
|
|
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File))
|
|
|
|
|
|
|
|
Template = Templates[Meta['Template']]
|
|
|
|
Template = Template.replace(
|
2022-05-16 21:16:36 +02:00
|
|
|
'[HTML:Page:CSS]',
|
|
|
|
'{}{}.css'.format('../'*File.count('/'), Meta['Template'][:-5]))
|
2022-05-16 20:12:06 +02:00
|
|
|
|
|
|
|
WriteFile(
|
2022-05-17 18:16:39 +02:00
|
|
|
'public/{}.html'.format(File.rstrip('.pug')),
|
|
|
|
PatchHTML(Template, Parts, PugCompiler(Content), Titles, Meta))
|
2022-05-16 20:12:06 +02:00
|
|
|
|
|
|
|
def IgnoreFiles(dir, files):
|
|
|
|
return [f for f in files if os.path.isfile(os.path.join(dir, f))]
|
|
|
|
def CopyAssets():
|
|
|
|
shutil.copytree('Pages', 'public', ignore=IgnoreFiles)
|
|
|
|
os.system("cp -R Assets/* public/")
|
|
|
|
|
|
|
|
def Main():
|
|
|
|
ResetPublic()
|
2022-05-16 21:16:36 +02:00
|
|
|
Templates = LoadFromDir('Templates')
|
|
|
|
Parts = LoadFromDir('Parts')
|
2022-05-16 20:12:06 +02:00
|
|
|
CopyAssets()
|
|
|
|
MakeSite(Templates, Parts)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
Main()
|