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

79 lines
2.8 KiB
Python
Raw Normal View History

2024-05-23 16:16:02 +02:00
from json import loads
2024-05-26 14:53:09 +02:00
from math import ceil
2024-05-24 18:17:39 +02:00
from os import walk, mkdir
2024-05-21 17:43:53 +02:00
from os.path import isdir, join, exists
2024-05-19 10:26:52 +02:00
from shutil import copytree
2024-05-23 16:16:02 +02:00
from typing import Dict
2024-05-26 14:53:09 +02:00
from article import Article, Page, Site
2024-05-23 16:16:02 +02:00
from template import TemplateEnvironment
2024-05-19 10:26:52 +02:00
2024-05-21 17:43:53 +02:00
def compile(work_directory: str, template_directory: str=None, target_directory: str=None, force: bool=False):
if not isdir(work_directory):
raise FileNotFoundError("One or more of the directories you specified do not exist")
if template_directory is None:
template_directory = join(work_directory, 'template')
if not isdir(template_directory):
raise FileNotFoundError("Template doesn't exist. Add one to your project or specify one with -t")
if target_directory is None:
target_directory = join(work_directory, 'generated_out')
if exists(target_directory) and not force:
raise FileExistsError(target_directory + " already exists. Delete it, specify a different one with -o, or pass the -f flag to merge")
2024-05-19 10:26:52 +02:00
copytree(join(template_directory, 'static'), join(target_directory, 'static'), dirs_exist_ok=True)
2024-05-25 19:05:16 +02:00
copytree(join(work_directory, 'articles'), join(target_directory, 'article'), dirs_exist_ok=True)
2024-05-24 18:17:39 +02:00
try:
mkdir(join(target_directory, 'index'))
except FileExistsError:
pass
2024-05-19 10:26:52 +02:00
2024-05-22 14:42:05 +02:00
file = open(join(work_directory, 'config.json'))
2024-05-23 16:16:02 +02:00
site = Site.from_open_file(file)
2024-05-22 14:42:05 +02:00
file.close()
2024-05-23 16:16:02 +02:00
template = TemplateEnvironment(template_directory, site)
2024-05-25 19:05:16 +02:00
articles_per_page = template.config.articles_per_page
2024-05-23 16:16:02 +02:00
articles = []
2024-05-25 19:05:16 +02:00
for root, dirs, files in walk(join(target_directory, 'article')):
2024-05-19 10:26:52 +02:00
for fn in files:
if fn.endswith('.html'):
file = open(join(root, fn), 'r+')
2024-05-25 19:05:16 +02:00
id = fn.split('.')[0]
2024-05-19 10:26:52 +02:00
2024-05-25 19:05:16 +02:00
article = Article.from_open_file(id, file)
2024-05-23 16:16:02 +02:00
content = template.process_article(article)
2024-05-19 10:26:52 +02:00
file.seek(0)
file.write(content)
file.close()
2024-05-23 16:16:02 +02:00
articles += [article]
2024-05-19 10:26:52 +02:00
2024-05-26 14:53:09 +02:00
page_index = 1
pages = ceil(len(articles) / articles_per_page)
2024-05-24 18:17:39 +02:00
while len(articles) > 0:
2024-05-26 14:53:09 +02:00
page = Page(
page_index, pages,
f'/index/page{page_index - 1}.html' if page > 1 else None,
f'/index/page{page_index + 1}.html' if page < pages else None
)
if page_index == 1:
2024-05-24 18:17:39 +02:00
fn = join(target_directory, 'index.html')
else:
2024-05-26 14:53:09 +02:00
fn = join(target_directory, 'index', f'page{page_index}.html')
2024-05-24 18:17:39 +02:00
2024-05-25 19:05:16 +02:00
articles_on_page = articles[:articles_per_page] # TODO make this customizable
2024-05-26 14:53:09 +02:00
content = template.process_index(page, *articles_on_page)
2024-05-24 18:17:39 +02:00
file = open(fn, 'w')
file.write(content)
file.close()
2024-05-25 19:05:16 +02:00
articles = articles[articles_per_page:]
2024-05-26 14:53:09 +02:00
page_index += 1