2022-05-16 20:12:06 +02:00
|
|
|
#!/usr/bin/env python3
|
2022-05-17 22:58:51 +02:00
|
|
|
""" ================================= |
|
|
|
|
| staticoso |
|
|
|
|
| Just a simple Static Site Generator |
|
|
|
|
| Licensed under AGPLv3 by OctoSpacc |
|
|
|
|
| ================================= """
|
2022-05-17 18:16:39 +02:00
|
|
|
|
2022-05-16 20:12:06 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
2022-05-17 18:16:39 +02:00
|
|
|
from markdown import Markdown
|
2022-05-17 22:58:51 +02:00
|
|
|
from pathlib import Path
|
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
|
|
|
|
|
2022-05-17 22:58:51 +02:00
|
|
|
def DashifyStr(s, Limit=32):
|
|
|
|
Str, lc = '', Limit
|
|
|
|
for c in s[:Limit].replace(' ','-').replace(' ','-'):
|
|
|
|
if c.lower() in '0123456789qwfpbjluyarstgmneiozxcdvkh-':
|
|
|
|
Str += c
|
2022-05-21 19:14:01 +02:00
|
|
|
return '-' + Str
|
|
|
|
|
|
|
|
def GetTitleIdLine(Line, Title):
|
|
|
|
Title = DashifyStr(Title.lstrip('#'))
|
|
|
|
Index = Line.find('h')
|
|
|
|
NewLine = ''
|
|
|
|
NewLine += Line[:Index]
|
|
|
|
NewLine += "{}(id='{}')".format(Line[Index:Index+2], Title)
|
|
|
|
NewLine += Line[Index+2:]
|
|
|
|
print(NewLine)
|
|
|
|
return NewLine
|
2022-05-17 22:58:51 +02:00
|
|
|
|
2022-05-16 20:12:06 +02:00
|
|
|
def FormatTitles(Titles):
|
2022-05-21 19:14:01 +02:00
|
|
|
MDTitles = ''
|
2022-05-16 21:16:36 +02:00
|
|
|
for t in Titles:
|
2022-05-17 22:58:51 +02:00
|
|
|
n = t.split(' ')[0].count('#')
|
|
|
|
Heading = '\-'*n + ' '
|
2022-05-21 13:04:05 +02:00
|
|
|
Title = t.lstrip('#')
|
|
|
|
Title = '[{}](#{})'.format(Title, DashifyStr(Title))
|
|
|
|
MDTitles += Heading + Title + ' \n'
|
|
|
|
return Markdown().convert(MDTitles)
|
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
|
|
|
|
|
2022-05-17 22:58:51 +02:00
|
|
|
def PreProcessor(p):
|
|
|
|
File = ReadFile(p)
|
2022-05-16 20:12:06 +02:00
|
|
|
Content, Titles, Meta = '', [], {
|
|
|
|
'Template': 'Standard.html',
|
2022-05-21 13:04:05 +02:00
|
|
|
'Style': '',
|
2022-05-21 19:14:01 +02:00
|
|
|
'Index': 'True'}
|
2022-05-16 20:12:06 +02:00
|
|
|
for l in File.splitlines():
|
|
|
|
ls = l.lstrip()
|
2022-05-17 22:58:51 +02:00
|
|
|
if p.endswith('.pug'):
|
|
|
|
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-21 13:04:05 +02:00
|
|
|
elif ls.startswith('// Index: '):
|
|
|
|
Meta['Index'] += ls[len('// Index: '):] + ' '
|
2022-05-21 19:14:01 +02:00
|
|
|
elif ls.startswith(('h1', 'h2', 'h3', 'h4', 'h5', 'h6')):
|
|
|
|
if ls[2:].startswith(("(class='NoTitle", '(class="NoTitle')):
|
|
|
|
Content += l + '\n'
|
|
|
|
else:
|
|
|
|
Title = '#'*int(ls[1]) + str(ls[3:])
|
|
|
|
Titles += [Title]
|
|
|
|
# We should handle headers that for any reason already have parenthesis
|
|
|
|
if ls[2:] == '(':
|
|
|
|
Content += l + '\n'
|
|
|
|
else:
|
|
|
|
Content += GetTitleIdLine(l, Title) + '\n'
|
2022-05-17 22:58:51 +02:00
|
|
|
else:
|
|
|
|
Content += l + '\n'
|
|
|
|
elif p.endswith('.md'):
|
|
|
|
if ls.startswith('\%'):
|
|
|
|
Content += ls[1:] + '\n'
|
|
|
|
elif ls.startswith('% - '):
|
|
|
|
Content += '<!-- {} -->'.format(ls[4:]) + '\n'
|
|
|
|
elif 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'
|
|
|
|
Heading = ls.split(' ')[0].count('#')
|
|
|
|
if Heading > 0:
|
|
|
|
Titles += [ls]
|
2022-05-21 19:14:01 +02:00
|
|
|
print(Content)
|
2022-05-16 20:12:06 +02:00
|
|
|
return Content, Titles, Meta
|
|
|
|
|
2022-05-17 22:58:51 +02:00
|
|
|
def PugCompileList(Pages):
|
|
|
|
Paths = ''
|
|
|
|
for File, Content, Titles, Meta in Pages:
|
2022-05-21 19:14:01 +02:00
|
|
|
FilePath = 'public/{}'.format(File)
|
|
|
|
WriteFile(FilePath, Content)
|
|
|
|
Paths += '"{}" '.format(FilePath)
|
|
|
|
# Pug-cli seems to shit itself with folder paths as input, so we pass ALL the files as arguments
|
2022-05-17 22:58:51 +02:00
|
|
|
os.system('pug {} > /dev/null'.format(Paths))
|
2022-05-17 18:16:39 +02:00
|
|
|
|
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-21 13:04:05 +02:00
|
|
|
#Template = Template.replace('[HTML:Page:LeftBox]', HTMLSiteTree)
|
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
|
|
|
|
|
2022-05-17 22:58:51 +02:00
|
|
|
def DelTmp():
|
|
|
|
for File in Path('public').rglob('*.pug'):
|
|
|
|
os.remove(File)
|
|
|
|
for File in Path('public').rglob('*.md'):
|
|
|
|
os.remove(File)
|
|
|
|
|
2022-05-16 20:12:06 +02:00
|
|
|
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))
|
2022-05-21 19:14:01 +02:00
|
|
|
print(Content)
|
2022-05-17 22:58:51 +02:00
|
|
|
Pages += [[File, Content, Titles, Meta]]
|
|
|
|
PugCompileList(Pages)
|
|
|
|
for File, Content, Titles, Meta in Pages:
|
2022-05-16 20:12:06 +02:00
|
|
|
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')),
|
2022-05-17 22:58:51 +02:00
|
|
|
PatchHTML(
|
|
|
|
Template, Parts,
|
|
|
|
ReadFile('public/{}.html'.format(File.rstrip('.pug'))),
|
|
|
|
Titles, Meta))
|
|
|
|
for File in Path('Pages').rglob('*.md'):
|
|
|
|
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('.md')),
|
|
|
|
PatchHTML(
|
|
|
|
Template, Parts,
|
|
|
|
Markdown().convert(Content),
|
|
|
|
Titles, Meta))
|
|
|
|
DelTmp()
|
2022-05-16 20:12:06 +02:00
|
|
|
|
|
|
|
def Main():
|
|
|
|
ResetPublic()
|
2022-05-16 21:16:36 +02:00
|
|
|
Templates = LoadFromDir('Templates')
|
|
|
|
Parts = LoadFromDir('Parts')
|
2022-05-17 22:58:51 +02:00
|
|
|
shutil.copytree('Pages', 'public')
|
2022-05-16 20:12:06 +02:00
|
|
|
MakeSite(Templates, Parts)
|
2022-05-17 22:58:51 +02:00
|
|
|
os.system("cp -R Assets/* public/")
|
2022-05-16 20:12:06 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
Main()
|