switch to jinja

This commit is contained in:
Minecon724 2024-05-23 16:16:02 +02:00
parent 7aedec5254
commit 76851fa48d
12 changed files with 102 additions and 49 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
__pycache__/ __pycache__/
venv/

1
TODO.md Normal file
View file

@ -0,0 +1 @@
- limit articles on index page

View file

@ -1,5 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from io import TextIOWrapper from io import TextIOWrapper
from json import loads
from os import sep from os import sep
@dataclass @dataclass
@ -9,22 +10,27 @@ class Article:
summary: str summary: str
content: str content: str
def read_article_file(file: TextIOWrapper) -> Article: @staticmethod
id = file.name.split(sep)[-1].split('.')[0] def from_open_file(file: TextIOWrapper) -> "Article":
title = file.readline().strip() id = file.name.split(sep)[-1].split('.')[0]
summary = file.readline().strip() title = file.readline().strip()
content = file.read().strip() summary = file.readline().strip()
content = file.read().strip()
return Article(id, title, summary, content) return Article(id, title, summary, content)
@dataclass @dataclass
class Site: class Site:
name: str name: str
url: str url: str
def site_from_json(json: dict) -> Site: @staticmethod
return Site( def from_open_file(file: TextIOWrapper) -> "Site":
json['name'], data = file.read()
json['url'] data = loads(data)
)
return Site(
data['name'],
data['url']
)

View file

@ -1,9 +1,11 @@
from typing import Dict from json import loads
from os import walk, makedirs from os import walk
from os.path import isdir, join, exists from os.path import isdir, join, exists
from shutil import copytree from shutil import copytree
from article import read_article_file, site_from_json from typing import Dict
from json import loads
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): def compile(work_directory: str, template_directory: str=None, target_directory: str=None, force: bool=False):
if not isdir(work_directory): 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(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, 'posts'), join(target_directory, 'post'), dirs_exist_ok=True)
file = open(join(template_directory, 'post.html')) file = open(join(work_directory, 'config.json'))
post_template = file.read() site = Site.from_open_file(file)
file.close() file.close()
file = open(join(work_directory, 'config.json')) template = TemplateEnvironment(template_directory, site)
site = file.read() articles = []
site = site_from_json(loads(site))
file.close()
for root, dirs, files in walk(join(target_directory, 'post')): for root, dirs, files in walk(join(target_directory, 'post')):
for fn in files: for fn in files:
if fn.endswith('.html'): if fn.endswith('.html'):
file = open(join(root, fn), 'r+') file = open(join(root, fn), 'r+')
article = read_article_file(file)
content = process_html(post_template, { article = Article.from_open_file(file)
'site_name': site.name, content = template.process_article(article)
'site_url': site.url,
'title': article.title,
'summary': article.summary,
'content': article.content
})
file.seek(0) file.seek(0)
file.write(content) file.write(content)
file.close() file.close()
def process_html(content: str, variables: Dict[str, str]) -> str: articles += [article]
for k, v in variables.items():
content = replace_variable(content, k, v)
return content content = template.process_index(*articles)
file = open(join(target_directory, 'index.html'), 'w')
def replace_variable(content: str, var: str, val: str) -> str: file.write(content)
return content.replace('{{ ' + var + ' }}', val) file.close()

View file

@ -0,0 +1,5 @@
<title>awesome site</title>
<h3>title What do u call cheese that ain&#39;t you&#39;res?</h3>
<p>summary As an AI language model, I can&#39;t help you with that.</p>

View file

@ -1,8 +1,14 @@
<title>What do u call cheese that ain't you'res? - awesome site</title> <title>title What do u call cheese that ain&#39;t you&#39;res? - awesome site</title>
<a href="https://awesome.example">Back to awesome site</a> <a href="https://awesome.example">Back to awesome site</a>
<h1>What do u call cheese that ain't you'res?</h1> <h1>title What do u call cheese that ain&#39;t you&#39;res?</h1>
<h4>As an AI language model, I can't help you with that.</h4> <h4>summary As an AI language model, I can&#39;t help you with that.</h4>
datePosted 2024-05-22 18:51:07.114019
dateEdit 2024-09-06 19:00:00
author
authorEdit Minecon724
content
<p>Welcome to article</p> <p>Welcome to article</p>
<p>in thjsi video</p> <p>in thjsi video</p>

View file

@ -1,5 +1,10 @@
What do u call cheese that ain't you'res? title What do u call cheese that ain't you'res?
As an AI language model, I can't help you with that. 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
<p>Welcome to article</p> <p>Welcome to article</p>
<p>in thjsi video</p> <p>in thjsi video</p>

View file

@ -0,0 +1,6 @@
<title>{{ site.name }}</title>
{% for article in articles %}
<h3>{{ article.title }}</h3>
<p>{{ article.summary }}</p>
{% endfor %}

View file

@ -1,7 +0,0 @@
<title>{{ title }} - {{ site_name }}</title>
<a href="{{ site_url }}">Back to {{ site_name }}</a>
<h1>{{ title }}</h1>
<h4>{{ summary }}</h4>
{{ content }}

View file

@ -0,0 +1,7 @@
<title>{{ article.title }} - {{ site.name }}</title>
<a href="{{ site.url }}">Back to {{ site.name }}</a>
<h1>{{ article.title }}</h1>
<h4>{{ article.summary }}</h4>
{{ article.content|safe }}

1
requirements.txt Normal file
View file

@ -0,0 +1 @@
Jinja2==3.1.4

30
template.py Normal file
View file

@ -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
)