This repository has been archived on 2025-01-08. You can view files and clone it, but cannot push or open issues or pull requests.
blog-software/compiler.py

38 lines
1.3 KiB
Python
Raw Normal View History

2024-05-19 10:26:52 +02:00
from typing import Dict
from os import walk, makedirs
from os.path import isdir, join
from shutil import copytree
from article import read_article_file
def compile(template_directory: str, work_directory: str, target_directory: str):
copytree(join(template_directory, 'static'), join(target_directory, 'static'), dirs_exist_ok=True)
copytree(join(work_directory, 'posts'), join(target_directory, 'post'), dirs_exist_ok=True)
file = open(join(template_directory, 'post.html'))
post_template = file.read()
file.close()
for root, dirs, files in walk(join(target_directory, 'post')):
for fn in files:
if fn.endswith('.html'):
file = open(join(root, fn), 'r+')
article = read_article_file(file)
content = process_html(post_template, {
'title': article.title,
'summary': article.summary,
'content': article.content
})
file.seek(0)
file.write(content)
file.close()
def process_html(content: str, variables: Dict[str, str]) -> str:
for k, v in variables.items():
content = replace_variable(k, v)
return content
def replace_variable(var: str, content: str) -> str:
return content.replace('{{ ' + var + ' }}', content)