Minecon724
209a9e6adc
I legit forgot to commit today and then there was (still kinda is) a thunderstorm and power got rekt it's back online but ssh is broken so kinda sad I'll probably have to fix this
39 lines
No EOL
1.4 KiB
Python
39 lines
No EOL
1.4 KiB
Python
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)
|
|
|
|
print(post_template)
|
|
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(content, k, v)
|
|
|
|
return content
|
|
|
|
def replace_variable(content: str, var: str, val: str) -> str:
|
|
return content.replace('{{ ' + var + ' }}', val) |