#!/usr/bin/env python3 import os import shutil from pathlib import Path from markdown import Markdown 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): HTMLTitles = '' for t in Titles: Heading = '- ' * (t.split(' ')[0].count('#')-1) t = t.lstrip('#') HTMLTitles += Heading + t + ' \n' return Markdown().convert(HTMLTitles) 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() 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: '):] + ' ' else: Content += l + '\n' if ls.startswith(('h1', 'h2', 'h3', 'h4', 'h5', 'h6')): if not ls.startswith(("h1(class='NoTitle", 'h1(class="NoTitle')): Titles += ['#'*int(ls[1]) + ls] return Content, Titles, Meta def PugCompiler(c): WriteFile('tmp.pug', c) os.system('pug tmp.pug > /dev/null') return ReadFile('tmp.html') def PatchHTML(Template, Parts, Content, Titles, Meta): HTMLTitles = FormatTitles(Titles) Template = Template.replace('[HTML:Page:Title]', 'Untitled' if not Titles else Titles[0].lstrip('#')) Template = Template.replace('[HTML:Page:Style]', Meta['Style']) Template = Template.replace('[HTML:Page:RightBox]', HTMLTitles) 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 = [] for File in Path('Pages').rglob('*.pug'): File = str(File)[len('Pages/'):] Content, Titles, Meta = PreProcessor('Pages/{}'.format(File)) Template = Templates[Meta['Template']] Template = Template.replace( '[HTML:Page:CSS]', '{}{}.css'.format('../'*File.count('/'), Meta['Template'][:-5])) WriteFile( 'public/{}.html'.format(File.rstrip('.pug')), PatchHTML(Template, Parts, PugCompiler(Content), Titles, Meta)) 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() Templates = LoadFromDir('Templates') Parts = LoadFromDir('Parts') CopyAssets() MakeSite(Templates, Parts) if __name__ == '__main__': Main()