commit b3827b3d5e19a325ab39944c5c19755906ced0fb Author: Minecon724 Date: Sun May 19 10:26:52 2024 +0200 committing work from yesterday diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..399e06d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +out/ \ No newline at end of file diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..8946796 --- /dev/null +++ b/__main__.py @@ -0,0 +1,28 @@ +from argparse import ArgumentParser +from dataclasses import dataclass + +from compiler import compile + +@dataclass +class Arguments: + templatedir: str + workdir: str + outdir: str + + def __init__(self): + pass + +def main(): + parser = ArgumentParser() + + parser.add_argument('templatedir', action='store') + parser.add_argument('workdir', action='store') + parser.add_argument('outdir', action='store') + + args = Arguments() + parser.parse_args(namespace=args) + + compile(args.templatedir, args.workdir, args.outdir) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/article.py b/article.py new file mode 100644 index 0000000..0e60945 --- /dev/null +++ b/article.py @@ -0,0 +1,18 @@ +from dataclasses import dataclass +from io import TextIOWrapper +from os import sep + +@dataclass +class Article: + id: str + title: str + summary: str + content: str + +def read_article_file(file: TextIOWrapper) -> Article: + id = file.name.split(sep)[-1].split('.')[0] + title = file.readline() + summary = file.readline() + content = file.read().strip() + + return Article(id, title, summary, content) \ No newline at end of file diff --git a/compiler.py b/compiler.py new file mode 100644 index 0000000..1dc98ed --- /dev/null +++ b/compiler.py @@ -0,0 +1,38 @@ +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) \ No newline at end of file diff --git a/example_template/post.html b/example_template/post.html new file mode 100644 index 0000000..8aa431f --- /dev/null +++ b/example_template/post.html @@ -0,0 +1,5 @@ +{{ title }} +

{{ title }}

+

{{ summary }}

+ +{{ content }} \ No newline at end of file diff --git a/example_workdir/posts/sample-article.html b/example_workdir/posts/sample-article.html new file mode 100644 index 0000000..974cde6 --- /dev/null +++ b/example_workdir/posts/sample-article.html @@ -0,0 +1,9 @@ +What do u call cheese that ain't you'res? +As an AI language model, I can't help you with that. + +

Welcome to article

+

in thjsi video

+ +

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ +

like and subcribe

\ No newline at end of file diff --git a/text.py b/text.py new file mode 100644 index 0000000..240b336 --- /dev/null +++ b/text.py @@ -0,0 +1,2 @@ +def replace_variable(var: str, content: str) -> str: + return content.replace('{{ ' + var + ' }}', content) \ No newline at end of file