staticoso/Source/Build.py

386 lines
12 KiB
Python
Raw Normal View History

2022-05-16 20:12:06 +02:00
#!/usr/bin/env python3
""" ================================= |
| staticoso |
| Just a simple Static Site Generator |
2022-05-21 20:03:27 +02:00
| |
| Licensed under the AGPLv3 license |
| Copyright (C) 2022, OctoSpacc |
| ================================= """
2022-05-17 18:16:39 +02:00
2022-05-24 00:22:32 +02:00
import argparse
import json
from Libs import htmlmin
2022-05-16 20:12:06 +02:00
import os
import shutil
2022-06-07 13:42:32 +02:00
from ast import literal_eval
2022-05-17 18:16:39 +02:00
from markdown import Markdown
from pathlib import Path
2022-05-16 20:12:06 +02:00
Extensions = {
'Pages': ('md', 'pug')}
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 LoadLocale(Lang):
Lang = Lang + '.json'
Folder = os.path.dirname(os.path.abspath(__file__)) + '/../Locale/'
File = ReadFile(Folder + Lang)
if File:
return json.loads(File)
else:
return json.loads(ReadFile(Folder + 'en.json'))
def StripExt(Path):
return ".".join(Path.split('.')[:-1])
2022-05-16 20:12:06 +02:00
def ResetPublic():
try:
shutil.rmtree('public')
except FileNotFoundError:
pass
def GetLevels(Path, Sub=0, AsNum=False):
n = Path.count('/')
return n if AsNum else '../' * n
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
2022-05-30 20:10:43 +02:00
def GetTitle(Meta, Titles, Prefer='MetaTitle'):
if Prefer == 'Title':
Title = Titles[0].lstrip('#') if Titles else Meta['Title'] if Meta['Title'] else 'Untitled'
2022-05-30 20:10:43 +02:00
elif Prefer == 'MetaTitle':
Title = Meta['Title'] if Meta['Title'] else Titles[0].lstrip('#') if Titles else 'Untitled'
2022-05-30 20:10:43 +02:00
elif Prefer == 'HTMLTitle':
Title = Meta['HTMLTitle'] if Meta['HTMLTitle'] else Meta['Title'] if Meta['Title'] else Titles[0].lstrip('#') if Titles else 'Untitled'
if Meta['Type'] == 'Post':
# TODO: This hardcodes my blog name, bad, will fix asap
2022-06-15 12:01:59 +02:00
Title += ' - blogoctt'
return Title
2022-05-30 20:10:43 +02:00
2022-06-15 12:01:59 +02:00
def GetDescription(Meta, Prefer='MetaDescription'):
if Prefer == 'Description':
Description = Meta['Description']
elif Prefer == 'MetaDescription':
Description = Meta['Description']
return Description
def GetTitleIdLine(Line, Title, Type):
DashTitle = DashifyStr(Title.lstrip('#'))
if Type == 'md':
Index = Title.split(' ')[0].count('#')
return '<h{} id="{}">{}</h{}>'.format(Index, DashTitle, Title[Index+1:], Index)
elif Type == 'pug':
NewLine = ''
Index = Line.find('h')
NewLine += Line[:Index]
NewLine += "{}(id='{}')".format(Line[Index:Index+2], DashTitle)
NewLine += Line[Index+2:]
return NewLine
def MakeListTitle(File, Meta, Titles, Prefer, SiteRoot, PathPrefix=''):
2022-06-03 23:38:47 +02:00
Title = GetTitle(Meta, Titles, Prefer)
Link = False if Meta['Index'] == 'Unlinked' else True
if Link:
2022-06-03 23:38:47 +02:00
Title = '[{}]({})'.format(
Title,
'{}{}.html'.format(PathPrefix, StripExt(File)))
if Meta['Type'] == 'Post' and Meta['CreatedOn']:
Title = '[{}] {}'.format(
Meta['CreatedOn'],
Title)
2022-06-03 23:38:47 +02:00
return Title
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:
n = t.split(' ')[0].count('#')
2022-05-22 23:19:02 +02:00
Heading = '- ' * n
Title = t.lstrip('#')
Title = '[{}](#{})'.format(Title, DashifyStr(Title))
2022-05-22 23:19:02 +02:00
MDTitles += Heading + Title + '\n'
return Markdown().convert(MDTitles)
2022-05-16 20:12:06 +02:00
2022-05-27 11:11:33 +02:00
def LoadFromDir(Dir, Rglob):
2022-05-16 20:12:06 +02:00
Contents = {}
2022-05-27 11:11:33 +02:00
for File in Path(Dir).rglob(Rglob):
2022-05-16 20:12:06 +02:00
File = str(File)[len(Dir)+1:]
Contents.update({File: ReadFile('{}/{}'.format(Dir, File))})
return Contents
def PreProcessor(Path, SiteRoot):
File = ReadFile(Path)
2022-05-16 20:12:06 +02:00
Content, Titles, Meta = '', [], {
'Template': 'Standard.html',
'Style': '',
2022-06-03 23:38:47 +02:00
'Type': 'Page',
'Index': 'True',
'Title': '',
2022-05-30 20:10:43 +02:00
'HTMLTitle': '',
2022-06-15 12:01:59 +02:00
'Description': '',
'Image': '',
2022-06-18 23:40:01 +02:00
'Categories': [],
2022-06-03 23:38:47 +02:00
'CreatedOn': '',
'EditedOn': '',
'Order': None}
2022-05-16 20:12:06 +02:00
for l in File.splitlines():
ls = l.lstrip()
2022-05-30 20:10:43 +02:00
if ls.startswith('// '):
2022-06-01 00:08:49 +02:00
lss = ls[3:]
2022-06-15 12:01:59 +02:00
for Item in ('Template', 'Type', 'Index', 'Title', 'HTMLTitle', 'Description', 'Image', 'CreatedOn', 'EditedOn'):
2022-06-01 00:08:49 +02:00
ItemText = '{}: '.format(Item)
if lss.startswith(ItemText):
Meta[Item] = lss[len(ItemText):]
2022-06-18 23:40:01 +02:00
if lss.startswith('Categories: '):
for i in lss[len('Categories: '):].split(' '):
Meta['Categories'] += [i]
elif lss.startswith('Background: '):
Meta['Style'] += "#MainBox{Background:" + lss[len('Background: '):] + ";} "
2022-06-01 00:08:49 +02:00
elif lss.startswith('Style: '):
2022-06-18 23:40:01 +02:00
Meta['Style'] += lss[len('Style: '):] + ' '
2022-06-01 00:08:49 +02:00
elif lss.startswith('Order: '):
2022-06-18 23:40:01 +02:00
Meta['Order'] = int(lss[len('Order: '):])
else:
if Path.endswith('.md'):
if ls.startswith('#'):
Titles += [l]
Content += GetTitleIdLine(l, ls, 'md') + '\n'
else:
Content += l + '\n'
elif Path.endswith('.pug'):
if 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, 'pug') + '\n'
else:
Content += l + '\n'
2022-05-16 20:12:06 +02:00
return Content, Titles, Meta
def PugCompileList(Pages):
2022-05-29 13:10:48 +02:00
# Pug-cli seems to shit itself with folder paths as input, so we pass ALL the files as arguments
Paths = ''
for File, Content, Titles, Meta in Pages:
if File.endswith('.pug'):
Path = 'public/{}'.format(File)
WriteFile(Path, Content)
Paths += '"{}" '.format(Path)
2022-06-15 12:01:59 +02:00
os.system('pug -P {} > /dev/null'.format(Paths))
2022-05-17 18:16:39 +02:00
def MakeContentHeader(Meta, Locale):
Header = ''
if Meta['Type'] == 'Post':
if Meta['CreatedOn']:
Header += "{} {} \n".format(Locale['CreatedOn'], Meta['CreatedOn'])
if Meta['EditedOn']:
Header += "{} {} \n".format(Locale['EditedOn'], Meta['EditedOn'])
return Markdown().convert(Header)
def PatchHTML(Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList, PagePath, Content, Titles, Meta, SiteRoot, FolderRoots, Categories, Locale):
2022-05-16 20:12:06 +02:00
HTMLTitles = FormatTitles(Titles)
2022-06-07 13:42:32 +02:00
for Line in Template.splitlines():
Line = Line.lstrip().rstrip()
if Line.startswith('[HTML:ContextPart:') and Line.endswith(']'):
Path = Line[len('[HTML:ContextPart:'):-1]
Section = Path.split('/')[-1]
if Section in ContextParts:
Part = ContextParts[Section]
Text = ''
if type(Part) == list:
for i in Part:
Text += ContextPartsText['{}/{}'.format(Path, i)] + '\n'
elif type(Part) == str:
Text = ContextPartsText['{}/{}'.format(Path, Part)]
else:
Text = ''
Template = Template.replace('[HTML:ContextPart:{}]'.format(Path), Text)
for i in PartsText:
Template = Template.replace('[HTML:Part:{}]'.format(i), PartsText[i])
Template = Template.replace('[HTML:Page:LeftBox]', HTMLPagesList)
2022-05-16 21:16:36 +02:00
Template = Template.replace('[HTML:Page:RightBox]', HTMLTitles)
2022-05-30 20:10:43 +02:00
Template = Template.replace('[HTML:Page:Title]', GetTitle(Meta, Titles, 'MetaTitle'))
2022-06-15 12:01:59 +02:00
Template = Template.replace('[HTML:Page:Description]', GetDescription(Meta, 'MetaDescription'))
Template = Template.replace('[HTML:Page:Path]', PagePath)
2022-05-29 13:10:48 +02:00
Template = Template.replace('[HTML:Page:Style]', Meta['Style'])
Template = Template.replace('[HTML:Page:Content]', Content)
Template = Template.replace('[HTML:Page:ContentHeader]', MakeContentHeader(Meta, Locale))
2022-06-03 23:38:47 +02:00
Template = Template.replace('[HTML:Site:AbsoluteRoot]', SiteRoot)
2022-06-20 11:09:42 +02:00
Template = Template.replace('[HTML:Site:RelativeRoot]', GetLevels(PagePath))
2022-06-19 23:07:23 +02:00
for i in FolderRoots:
Template = Template.replace('[HTML:Folder:{}:AbsoluteRoot]'.format(i), FolderRoots[i])
for i in Categories:
Template = Template.replace('<span>[HTML:Category:{}]</span>'.format(i), Categories[i])
2022-05-16 20:12:06 +02:00
return Template
def FileToStr(File, Truncate=''):
return str(File)[len(Truncate):]
def OrderPages(Old):
New = []
Max = 0
for i,e in enumerate(Old):
Curr = e[3]['Order']
if Curr > Max:
Max = Curr
for i in range(Max+1):
New += [[]]
for i,e in enumerate(Old):
New[e[3]['Order']] = e
while [] in New:
New.remove([])
return New
def GetHTMLPagesList(Pages, SiteRoot, PathPrefix, Type='Page', Category=None):
List, ToPop, LastParent = '', [], []
2022-06-01 00:08:49 +02:00
IndexPages = Pages.copy()
for e in IndexPages:
if e[3]['Index'] == 'False' or e[3]['Index'] == 'None':
IndexPages.remove(e)
2022-06-03 23:38:47 +02:00
for i,e in enumerate(IndexPages):
if e[3]['Type'] != Type:
ToPop += [i]
ToPop = RevSort(ToPop)
for i in ToPop:
IndexPages.pop(i)
2022-06-03 23:38:47 +02:00
if Type == 'Page':
IndexPages = OrderPages(IndexPages)
2022-06-01 00:08:49 +02:00
for File, Content, Titles, Meta in IndexPages:
if Meta['Type'] == Type and (Meta['Index'] != 'False' or Meta['Index'] != 'None') and GetTitle(Meta, Titles, Prefer='HTMLTitle') != 'Untitled' and (not Category or Category in Meta['Categories']):
n = File.count('/') + 1
if n > 1:
CurParent = File.split('/')[:-1]
for i,s in enumerate(CurParent):
if LastParent != CurParent:
LastParent = CurParent
Levels = '- ' * (n-1+i)
2022-05-30 20:10:43 +02:00
if File[:-3].endswith('index.'):
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', SiteRoot, PathPrefix)
2022-05-30 20:10:43 +02:00
else:
Title = CurParent[n-2+i]
List += Levels + Title + '\n'
2022-05-30 20:10:43 +02:00
if not (n > 1 and File[:-3].endswith('index.')):
Levels = '- ' * n
Title = MakeListTitle(File, Meta, Titles, 'HTMLTitle', SiteRoot, PathPrefix)
2022-05-30 20:10:43 +02:00
List += Levels + Title + '\n'
return Markdown().convert(List)
def DelTmp():
for Ext in Extensions['Pages']:
for File in Path('public').rglob('*.{}'.format(Ext)):
os.remove(File)
def RevSort(List):
List.sort()
List.reverse()
return List
def DoMinify(HTML):
return htmlmin.minify(
input=HTML,
remove_comments=True,
remove_empty_space=True,
remove_all_empty_space=False,
reduce_empty_attributes=True,
reduce_boolean_attributes=True,
remove_optional_attribute_quotes=True,
convert_charrefs=True,
keep_pre=True)
def MakeSite(TemplatesText, PartsText, ContextParts, ContextPartsText, SiteRoot, FolderRoots, Locale, Minify):
Files, Pages, Categories = [], [], {}
for Ext in Extensions['Pages']:
for File in Path('Pages').rglob('*.{}'.format(Ext)):
Files += [FileToStr(File, 'Pages/')]
Files = RevSort(Files)
for File in Files:
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File), SiteRoot)
Pages += [[File, Content, Titles, Meta]]
for Category in Meta['Categories']:
Categories.update({Category:''})
PugCompileList(Pages)
for Category in Categories:
Categories[Category] = GetHTMLPagesList(
Pages=Pages,
SiteRoot=SiteRoot,
PathPrefix='../../', # This hardcodes paths, TODO make it somehow guess the path for every page containing the [HTML:Category] macro
Type='Post',
Category=Category)
for File, Content, Titles, Meta in Pages:
HTMLPagesList = GetHTMLPagesList(
Pages=Pages,
SiteRoot=SiteRoot,
PathPrefix=GetLevels(File),
Type='Page')
2022-06-15 12:01:59 +02:00
PagePath = 'public/{}.html'.format(StripExt(File))
if File.endswith('.md'):
Content = Markdown().convert(Content)
elif File.endswith('.pug'):
2022-06-15 12:01:59 +02:00
Content = ReadFile(PagePath)
HTML = PatchHTML(
Template=TemplatesText[Meta['Template']],
PartsText=PartsText,
ContextParts=ContextParts,
ContextPartsText=ContextPartsText,
HTMLPagesList=HTMLPagesList,
PagePath=PagePath[len('public/'):],
Content=Content,
Titles=Titles,
Meta=Meta,
SiteRoot=SiteRoot,
FolderRoots=FolderRoots,
Categories=Categories,
Locale=Locale)
if Minify != 'False' and Minify != 'None':
HTML = DoMinify(HTML)
WriteFile(PagePath, HTML)
DelTmp()
2022-05-16 20:12:06 +02:00
2022-05-24 00:22:32 +02:00
def Main(Args):
2022-05-16 20:12:06 +02:00
ResetPublic()
shutil.copytree('Pages', 'public')
2022-06-07 13:42:32 +02:00
MakeSite(
TemplatesText=LoadFromDir('Templates', '*.html'),
PartsText=LoadFromDir('Parts', '*.html'),
ContextParts=literal_eval(Args.ContextParts) if Args.ContextParts else {},
ContextPartsText=LoadFromDir('ContextParts', '*.html'),
SiteRoot=Args.SiteRoot if Args.SiteRoot else '/',
FolderRoots=literal_eval(Args.FolderRoots) if Args.FolderRoots else {},
Locale=LoadLocale(Args.SiteLang if Args.SiteLang else 'en'),
Minify=Args.Minify if Args.Minify else 'None')
os.system("cp -R Assets/* public/")
2022-05-16 20:12:06 +02:00
if __name__ == '__main__':
2022-05-24 00:22:32 +02:00
Parser = argparse.ArgumentParser()
Parser.add_argument('--SiteLang', type=str)
Parser.add_argument('--SiteRoot', type=str)
2022-06-19 23:07:23 +02:00
Parser.add_argument('--FolderRoots', type=str)
2022-06-07 13:42:32 +02:00
Parser.add_argument('--ContextParts', type=str)
Parser.add_argument('--Minify', type=str)
Main(
Args=Parser.parse_args())