Add ContextParts support

This commit is contained in:
octospacc 2022-06-07 13:42:32 +02:00
parent 8b3112a2dc
commit 5716179d2d
2 changed files with 47 additions and 17 deletions

View File

@ -5,7 +5,7 @@
I'm making this because I need a simple and kind-of-minimal program to serve me as a static site generator with (limited) CMS features. Just the features I need.
This won't replace any of the big projects out there that do the same thing but, as all of my projects, I'm releasing it as free libre software, in the hope that someone would find it useful.
Also, this software is needed for someone to edit and compile my personal sub-website [postoctt](https://octtspacc.gitlab.io/postoctt) from its source. Being that site too released under a libre license that allows modifications, I have to also release the tools I use to build it.
Also, this software is needed for someone to edit and compile my personal sub-website [sitoctt](https://octtspacc.gitlab.io/sitoctt) from its source. Being that site too released under a libre license that allows modifications, I have to also release the tools I use to build it.
Feel free to experiment with all of this stuff!
@ -15,6 +15,7 @@ Feel free to experiment with all of this stuff!
- [pug-cli >= 1.0.0-alpha6](https://npmjs.com/package/pug-cli)
## Features roadmap
- [x] Custom static page parts programmable by context
- [ ] Handle showing creation and modified date for any page
- [x] Generation of category pages (ex. page with list of blog posts)
- [x] Custom title choosing type (HTML/Plaintext)
@ -28,6 +29,7 @@ Feel free to experiment with all of this stuff!
- [x] Generation of website page tree in left sidebar
- [x] Generation of titles in right sidebar with clickable links
- [x] Detections of titles in a page
- [x] Custom static page parts by template
- [x] Pug support for pages
- [x] Markdown support for pages
- [x] First working version

View File

@ -10,6 +10,7 @@
import argparse
import os
import shutil
from ast import literal_eval
from markdown import Markdown
from pathlib import Path
@ -141,10 +142,27 @@ def PugCompileList(Pages):
Paths += '"{}" '.format(FilePath)
os.system('pug {} > /dev/null'.format(Paths))
def PatchHTML(Template, Parts, HTMLPagesList, Content, Titles, Meta, SiteRoot, Macros):
def PatchHTML(Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList, Content, Titles, Meta, SiteRoot, Macros):
HTMLTitles = FormatTitles(Titles)
for i in Parts:
Template = Template.replace('[HTML:Part:{}]'.format(i), Parts[i])
for Line in Template.splitlines():
Line = Line.lstrip().rstrip()
print(ContextPartsText)
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)
Template = Template.replace('[HTML:Page:RightBox]', HTMLTitles)
Template = Template.replace('[HTML:Page:Title]', GetTitle(Meta, Titles, 'MetaTitle'))
@ -152,8 +170,6 @@ def PatchHTML(Template, Parts, HTMLPagesList, Content, Titles, Meta, SiteRoot, M
Template = Template.replace('[HTML:Page:MainBox]', Content)
Template = Template.replace('[HTML:Site:AbsoluteRoot]', SiteRoot)
for i in Macros:
#print(i)
#print(Macros[i])
Template = Template.replace('<span>[HTML:Macro:{}]</span>'.format(i), Macros[i])
return Template
@ -225,7 +241,7 @@ def DelTmp():
for File in Path('public').rglob('*.md'):
os.remove(File)
def MakeSite(Templates, Parts, SiteRoot):
def MakeSite(TemplatesText, PartsText, ContextParts, ContextPartsText, SiteRoot):
Pages = []
Macros = {
'BlogPosts': ''}
@ -238,7 +254,7 @@ def MakeSite(Templates, Parts, SiteRoot):
#print(GetHTMLPagesList(Pages, SiteRoot, 'Post'))
Macros['BlogPosts'] = GetHTMLPagesList(Pages, SiteRoot, 'Post')
for File, Content, Titles, Meta in Pages:
Template = Templates[Meta['Template']]
Template = TemplatesText[Meta['Template']]
Template = Template.replace(
'[HTML:Site:AbsoluteRoot]',
SiteRoot)
@ -248,13 +264,13 @@ def MakeSite(Templates, Parts, SiteRoot):
WriteFile(
'public/{}html'.format(File[:-3]),
PatchHTML(
Template, Parts, HTMLPagesList,
Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList,
ReadFile('public/{}html'.format(File[:-3])),
Titles, Meta, SiteRoot, Macros))
for File in Path('Pages').rglob('*.md'):
File = FileToStr(File, 'Pages/')
Content, Titles, Meta = PreProcessor('Pages/{}'.format(File), SiteRoot)
Template = Templates[Meta['Template']]
Template = TemplatesText[Meta['Template']]
Template = Template.replace(
'[HTML:Site:AbsoluteRoot]',
SiteRoot)
@ -264,25 +280,37 @@ def MakeSite(Templates, Parts, SiteRoot):
WriteFile(
'public/{}html'.format(File[:-2]),
PatchHTML(
Template, Parts, HTMLPagesList,
Template, PartsText, ContextParts, ContextPartsText, HTMLPagesList,
Markdown().convert(Content),
Titles, Meta, SiteRoot, Macros))
DelTmp()
"""
def GetContextPartsText(ContextParts):
List = {}
Contents = LoadFromDir('ContextParts', '*.html')
for i in ContextParts:
if type(ContextParts[i]) == str:
if
elif type(ContextParts[i]) == list:
"""
def Main(Args):
ResetPublic()
SiteRoot = Args.SiteRoot if Args.SiteRoot else '/'
Templates = LoadFromDir('Templates', '*.html')
Parts = LoadFromDir('Parts', '*.html')
shutil.copytree('Pages', 'public')
MakeSite(Templates, Parts, SiteRoot)
MakeSite(
LoadFromDir('Templates', '*.html'),
LoadFromDir('Parts', '*.html'),
literal_eval(Args.ContextParts) if Args.ContextParts else {},
LoadFromDir('ContextParts', '*.html'),
Args.SiteRoot if Args.SiteRoot else '/')
os.system("cp -R Assets/* public/")
if __name__ == '__main__':
Parser = argparse.ArgumentParser()
Parser.add_argument('--SiteRoot', type=str)
Parser.add_argument('--ContextParts', type=str)
Args = Parser.parse_args()
Main(Args)