diff --git a/.gitignore b/.gitignore index ba0430d..0540009 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__/ \ No newline at end of file +__pycache__/ +venv/ \ No newline at end of file diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..4db3286 --- /dev/null +++ b/TODO.md @@ -0,0 +1 @@ +- limit articles on index page \ No newline at end of file diff --git a/article.py b/article.py index 5866886..4057691 100644 --- a/article.py +++ b/article.py @@ -1,5 +1,6 @@ from dataclasses import dataclass from io import TextIOWrapper +from json import loads from os import sep @dataclass @@ -9,22 +10,27 @@ class Article: summary: str content: str -def read_article_file(file: TextIOWrapper) -> Article: - id = file.name.split(sep)[-1].split('.')[0] - title = file.readline().strip() - summary = file.readline().strip() - content = file.read().strip() + @staticmethod + def from_open_file(file: TextIOWrapper) -> "Article": + id = file.name.split(sep)[-1].split('.')[0] + title = file.readline().strip() + summary = file.readline().strip() + content = file.read().strip() - return Article(id, title, summary, content) + return Article(id, title, summary, content) @dataclass class Site: name: str url: str -def site_from_json(json: dict) -> Site: - return Site( - json['name'], - json['url'] - ) + @staticmethod + def from_open_file(file: TextIOWrapper) -> "Site": + data = file.read() + data = loads(data) + + return Site( + data['name'], + data['url'] + ) \ No newline at end of file diff --git a/compiler.py b/compiler.py index 835f74d..aa1c2f6 100644 --- a/compiler.py +++ b/compiler.py @@ -1,9 +1,11 @@ -from typing import Dict -from os import walk, makedirs +from json import loads +from os import walk from os.path import isdir, join, exists from shutil import copytree -from article import read_article_file, site_from_json -from json import loads +from typing import Dict + +from article import Article, Site +from template import TemplateEnvironment def compile(work_directory: str, template_directory: str=None, target_directory: str=None, force: bool=False): if not isdir(work_directory): @@ -23,38 +25,28 @@ def compile(work_directory: str, template_directory: str=None, target_directory: 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 = open(join(work_directory, 'config.json')) + site = Site.from_open_file(file) file.close() - file = open(join(work_directory, 'config.json')) - site = file.read() - site = site_from_json(loads(site)) - file.close() + template = TemplateEnvironment(template_directory, site) + articles = [] 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, { - 'site_name': site.name, - 'site_url': site.url, - 'title': article.title, - 'summary': article.summary, - 'content': article.content - }) + article = Article.from_open_file(file) + content = template.process_article(article) 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) + articles += [article] - return content - -def replace_variable(content: str, var: str, val: str) -> str: - return content.replace('{{ ' + var + ' }}', val) \ No newline at end of file + content = template.process_index(*articles) + file = open(join(target_directory, 'index.html'), 'w') + file.write(content) + file.close() \ No newline at end of file diff --git a/example_workdir/generated_out/index.html b/example_workdir/generated_out/index.html new file mode 100644 index 0000000..c827406 --- /dev/null +++ b/example_workdir/generated_out/index.html @@ -0,0 +1,5 @@ +awesome site + + +

title What do u call cheese that ain't you'res?

+

summary As an AI language model, I can't help you with that.

diff --git a/example_workdir/generated_out/post/sample-article.html b/example_workdir/generated_out/post/sample-article.html index 86b6dcd..440009a 100644 --- a/example_workdir/generated_out/post/sample-article.html +++ b/example_workdir/generated_out/post/sample-article.html @@ -1,8 +1,14 @@ -What do u call cheese that ain't you'res? - awesome site +title What do u call cheese that ain't you'res? - awesome site Back to awesome site -

What do u call cheese that ain't you'res?

-

As an AI language model, I can't help you with that.

+

title What do u call cheese that ain't you'res?

+

summary As an AI language model, I can't help you with that.

+ +datePosted 2024-05-22 18:51:07.114019 +dateEdit 2024-09-06 19:00:00 +author +authorEdit Minecon724 +content

Welcome to article

in thjsi video

diff --git a/example_workdir/posts/sample-article.html b/example_workdir/posts/sample-article.html index 974cde6..c2ff8d8 100644 --- a/example_workdir/posts/sample-article.html +++ b/example_workdir/posts/sample-article.html @@ -1,5 +1,10 @@ -What do u call cheese that ain't you'res? -As an AI language model, I can't help you with that. +title What do u call cheese that ain't you'res? +summary As an AI language model, I can't help you with that. +datePosted 2024-05-22 18:51:07.114019 +dateEdit 2024-09-06 19:00:00 +author +authorEdit Minecon724 +content

Welcome to article

in thjsi video

diff --git a/example_workdir/template/index.html b/example_workdir/template/index.html new file mode 100644 index 0000000..85a4d29 --- /dev/null +++ b/example_workdir/template/index.html @@ -0,0 +1,6 @@ +{{ site.name }} + +{% for article in articles %} +

{{ article.title }}

+

{{ article.summary }}

+{% endfor %} \ No newline at end of file diff --git a/example_workdir/template/post.html b/example_workdir/template/post.html deleted file mode 100644 index 2bc961c..0000000 --- a/example_workdir/template/post.html +++ /dev/null @@ -1,7 +0,0 @@ -{{ title }} - {{ site_name }} -Back to {{ site_name }} - -

{{ title }}

-

{{ summary }}

- -{{ content }} \ No newline at end of file diff --git a/example_workdir/template/post_template.html b/example_workdir/template/post_template.html new file mode 100644 index 0000000..94a2154 --- /dev/null +++ b/example_workdir/template/post_template.html @@ -0,0 +1,7 @@ +{{ article.title }} - {{ site.name }} +Back to {{ site.name }} + +

{{ article.title }}

+

{{ article.summary }}

+ +{{ article.content|safe }} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..62def1f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +Jinja2==3.1.4 \ No newline at end of file diff --git a/template.py b/template.py new file mode 100644 index 0000000..f0b2e24 --- /dev/null +++ b/template.py @@ -0,0 +1,30 @@ +from article import Article, Site +from jinja2 import Environment, Template, FileSystemLoader, select_autoescape + +class TemplateEnvironment: + environment: Environment + site: Site + post_template: Template + index_template: Template + + def __init__(self, template_directory: str, site: Site): + self.environment = Environment( + loader = FileSystemLoader(template_directory, followlinks=True), + autoescape = select_autoescape() + ) + + self.site = site + self.post_template = self.environment.get_template('post_template.html') + self.index_template = self.environment.get_template('index.html') + + def process_article(self, article: Article) -> str: + return self.post_template.render( + site = self.site, + article = article + ) + + def process_index(self, *articles: Article) -> str: + return self.index_template.render( + site = self.site, + articles = articles + ) \ No newline at end of file