diff --git a/TODO.md b/TODO.md index 67b6a60..59b0029 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1 @@ -- navigating pages -- customizable articles per page \ No newline at end of file +- navigating pages \ No newline at end of file diff --git a/__main__.py b/__main__.py index a372420..7413ed9 100644 --- a/__main__.py +++ b/__main__.py @@ -1,9 +1,7 @@ from argparse import ArgumentParser -from dataclasses import dataclass from compiler import compile -@dataclass class Arguments: workdir: str template: str diff --git a/article.py b/article.py index 4057691..8074bf2 100644 --- a/article.py +++ b/article.py @@ -2,6 +2,7 @@ from dataclasses import dataclass from io import TextIOWrapper from json import loads from os import sep +from typing import Any, Dict @dataclass class Article: @@ -9,28 +10,50 @@ class Article: title: str summary: str content: str + custom: Dict[str, str] @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() + def from_open_file(id: str, file: TextIOWrapper) -> "Article": + kwargs = {'id': id} + custom = {} - return Article(id, title, summary, content) + for line in file: + if line.strip() == 'content': + break + + kv = line.strip().split(' ', 1) + + key = kv[0] + value = kv[1] if len(kv) > 1 else '' + + if key in Article.__annotations__: + kwargs[key] = value + else: + custom[key] = value + + content = file.read() + + return Article(content=content, custom=custom, **kwargs) @dataclass class Site: name: str url: str + custom: Dict[str, str] @staticmethod def from_open_file(file: TextIOWrapper) -> "Site": + kwargs = {} + custom = {} + data = file.read() data = loads(data) - return Site( - data['name'], - data['url'] - ) + for key, value in data.items(): + if key in Site.__annotations__: + kwargs[key] = value + else: + custom[key] = value + + return Site(custom=custom, **kwargs) \ No newline at end of file diff --git a/compiler.py b/compiler.py index 6163ec1..a2b1a3a 100644 --- a/compiler.py +++ b/compiler.py @@ -23,7 +23,8 @@ def compile(work_directory: str, template_directory: str=None, target_directory: 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) + copytree(join(work_directory, 'articles'), join(target_directory, 'article'), dirs_exist_ok=True) + try: mkdir(join(target_directory, 'index')) except FileExistsError: @@ -34,14 +35,16 @@ def compile(work_directory: str, template_directory: str=None, target_directory: file.close() template = TemplateEnvironment(template_directory, site) + articles_per_page = template.config.articles_per_page articles = [] - for root, dirs, files in walk(join(target_directory, 'post')): + for root, dirs, files in walk(join(target_directory, 'article')): for fn in files: if fn.endswith('.html'): file = open(join(root, fn), 'r+') + id = fn.split('.')[0] - article = Article.from_open_file(file) + article = Article.from_open_file(id, file) content = template.process_article(article) file.seek(0) @@ -57,12 +60,12 @@ def compile(work_directory: str, template_directory: str=None, target_directory: else: fn = join(target_directory, 'index', f'page{page}.html') - articles_on_page = articles[:10] # TODO make this customizable + articles_on_page = articles[:articles_per_page] # TODO make this customizable content = template.process_index(*articles_on_page) file = open(fn, 'w') file.write(content) file.close() - articles = articles[10:] + articles = articles[articles_per_page:] page += 1 \ No newline at end of file diff --git a/example_workdir/posts/sample-article.html b/example_workdir/articles/sample-article.html similarity index 100% rename from example_workdir/posts/sample-article.html rename to example_workdir/articles/sample-article.html diff --git a/example_workdir/generated_out/article/sample-article.html b/example_workdir/generated_out/article/sample-article.html new file mode 100644 index 0000000..788a04a --- /dev/null +++ b/example_workdir/generated_out/article/sample-article.html @@ -0,0 +1,13 @@ +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.

+ + +

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_workdir/generated_out/index.html b/example_workdir/generated_out/index.html index c827406..35d0fde 100644 --- a/example_workdir/generated_out/index.html +++ b/example_workdir/generated_out/index.html @@ -1,5 +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.

+

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

+

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

diff --git a/example_workdir/template/post_template.html b/example_workdir/template/article_template.html similarity index 100% rename from example_workdir/template/post_template.html rename to example_workdir/template/article_template.html diff --git a/example_workdir/template/template.json b/example_workdir/template/template.json new file mode 100644 index 0000000..391a771 --- /dev/null +++ b/example_workdir/template/template.json @@ -0,0 +1,5 @@ +{ + "version": 1, + "name": "basic template", + "articles_per_page": 10 +} \ No newline at end of file diff --git a/template.py b/template.py index f0b2e24..bdf26e4 100644 --- a/template.py +++ b/template.py @@ -1,10 +1,34 @@ from article import Article, Site +from dataclasses import dataclass +from io import TextIOWrapper from jinja2 import Environment, Template, FileSystemLoader, select_autoescape +from json import loads +from os.path import join + +TEMPLATE_VERSIONS = [1] + +@dataclass +class TemplateConfig: + version: int + name: str + articles_per_page: int + + @staticmethod + def from_open_file(file: TextIOWrapper) -> "TemplateConfig": + content = file.read() + data = loads(content) + + if data['version'] not in TEMPLATE_VERSIONS: + raise ValueError('Template version' + data['version'] + ' is not compatible. ' + # TODO right exception? + 'Supported versions: ' + ', '.join(str(i) for i in TEMPLATE_VERSIONS)) + + return TemplateConfig(**data) class TemplateEnvironment: environment: Environment + config: TemplateConfig site: Site - post_template: Template + article_template: Template index_template: Template def __init__(self, template_directory: str, site: Site): @@ -14,11 +38,19 @@ class TemplateEnvironment: ) self.site = site - self.post_template = self.environment.get_template('post_template.html') + self.article_template = self.environment.get_template('article_template.html') self.index_template = self.environment.get_template('index.html') + self.__load_config(template_directory) + + def __load_config(self, template_directory: str): + fn = join(template_directory, 'template.json') + file = open(fn) + self.config = TemplateConfig.from_open_file(file) + file.close() + def process_article(self, article: Article) -> str: - return self.post_template.render( + return self.article_template.render( site = self.site, article = article )