diff --git a/.gitignore b/.gitignore index 399e06d..ba0430d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -__pycache__/ -out/ \ No newline at end of file +__pycache__/ \ No newline at end of file diff --git a/__main__.py b/__main__.py index 8946796..a372420 100644 --- a/__main__.py +++ b/__main__.py @@ -5,9 +5,10 @@ from compiler import compile @dataclass class Arguments: - templatedir: str workdir: str - outdir: str + template: str + out: str + force: str def __init__(self): pass @@ -15,14 +16,15 @@ class Arguments: def main(): parser = ArgumentParser() - parser.add_argument('templatedir', action='store') parser.add_argument('workdir', action='store') - parser.add_argument('outdir', action='store') + parser.add_argument('-t' , '--template', action='store') + parser.add_argument('-o', '--out', action='store') + parser.add_argument('-f', '--force', action='store_true', default=False) args = Arguments() parser.parse_args(namespace=args) - compile(args.templatedir, args.workdir, args.outdir) + compile(args.workdir, args.template, args.out, args.force) if __name__ == "__main__": main() \ No newline at end of file diff --git a/article.py b/article.py index c62b70f..89b2f77 100644 --- a/article.py +++ b/article.py @@ -15,4 +15,16 @@ def read_article_file(file: TextIOWrapper) -> Article: summary = file.readline().strip() content = file.read().strip() - return Article(id, title, summary, content) \ No newline at end of file + return Article(id, title, summary, content) + +@dataclass +class Blog: + name: str + url: str + +def blog_from_json(json: dict) -> Blog: + return Blog( + json['name'], + json['url'] + ) + \ No newline at end of file diff --git a/compiler.py b/compiler.py index 239094b..2eb0556 100644 --- a/compiler.py +++ b/compiler.py @@ -1,10 +1,24 @@ from typing import Dict from os import walk, makedirs -from os.path import isdir, join +from os.path import isdir, join, exists from shutil import copytree from article import read_article_file -def compile(template_directory: str, work_directory: str, target_directory: str): +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") + 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) @@ -18,7 +32,6 @@ def compile(template_directory: str, work_directory: str, target_directory: str) 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, diff --git a/example_workdir/generated_out/post/sample-article.html b/example_workdir/generated_out/post/sample-article.html new file mode 100644 index 0000000..62f252c --- /dev/null +++ b/example_workdir/generated_out/post/sample-article.html @@ -0,0 +1,10 @@ +What do u call cheese that ain't you'res? +

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/example_template/post.html b/example_workdir/template/post.html similarity index 100% rename from example_template/post.html rename to example_workdir/template/post.html