My personal blog's source repository.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

45 lines
1.1 KiB

#!/usr/bin/env python3
# Post-processing script that will grab everything in public/ and scan for
# `<!-- POST-PROCESS(.*) -->` to execute its function
from pathlib import Path
from zlib import crc32
import re
ROOT = Path.cwd() / 'public'
def LGBT(data):
LGBT_THEMES = list(ROOT.glob('.-/lgbt-*.css'))
if len(LGBT_THEMES) != 0:
theme = crc32(data['path'].__str__().encode('ascii')) % len(LGBT_THEMES)
theme_path = ('/' / LGBT_THEMES[theme].relative_to(ROOT)).__str__().replace('\\', '/')
return f'<link rel="stylesheet" href="{theme_path}" />'
else:
return ''
def post_process(root: Path, path: Path):
def _(keyword):
return f'<!-- POST-PROCESS({keyword}) -->'
filters = {
_('LGBT'): LGBT,
}
page = ''
with open(root / path, 'r', encoding='utf8') as f:
page = f.read()
for keyword in filters:
filtered = filters[keyword]({
'content': page,
'path': path,
})
page = page.replace(keyword, filtered)
with open(root / path, 'w', encoding='utf8') as f:
f.write(page)
def grab_all_files(path: Path):
[post_process(path, x.relative_to(path)) for x in sorted(path.glob('**/*.html'))]
grab_all_files(ROOT)