switch to jinja
This commit is contained in:
parent
7aedec5254
commit
76851fa48d
12 changed files with 102 additions and 49 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
__pycache__/
|
||||
venv/
|
1
TODO.md
Normal file
1
TODO.md
Normal file
|
@ -0,0 +1 @@
|
|||
- limit articles on index page
|
28
article.py
28
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']
|
||||
)
|
||||
|
42
compiler.py
42
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)
|
||||
content = template.process_index(*articles)
|
||||
file = open(join(target_directory, 'index.html'), 'w')
|
||||
file.write(content)
|
||||
file.close()
|
5
example_workdir/generated_out/index.html
Normal file
5
example_workdir/generated_out/index.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
<title>awesome site</title>
|
||||
|
||||
|
||||
<h3>title What do u call cheese that ain't you'res?</h3>
|
||||
<p>summary As an AI language model, I can't help you with that.</p>
|
|
@ -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't you'res? - awesome site</title>
|
||||
<a href="https://awesome.example">Back to awesome site</a>
|
||||
|
||||
<h1>What do u call cheese that ain't you'res?</h1>
|
||||
<h4>As an AI language model, I can't help you with that.</h4>
|
||||
<h1>title What do u call cheese that ain't you'res?</h1>
|
||||
<h4>summary As an AI language model, I can'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>in thjsi video</p>
|
||||
|
|
|
@ -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
|
||||
|
||||
<p>Welcome to article</p>
|
||||
<p>in thjsi video</p>
|
||||
|
|
6
example_workdir/template/index.html
Normal file
6
example_workdir/template/index.html
Normal file
|
@ -0,0 +1,6 @@
|
|||
<title>{{ site.name }}</title>
|
||||
|
||||
{% for article in articles %}
|
||||
<h3>{{ article.title }}</h3>
|
||||
<p>{{ article.summary }}</p>
|
||||
{% endfor %}
|
|
@ -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 }}
|
7
example_workdir/template/post_template.html
Normal file
7
example_workdir/template/post_template.html
Normal 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
1
requirements.txt
Normal file
|
@ -0,0 +1 @@
|
|||
Jinja2==3.1.4
|
30
template.py
Normal file
30
template.py
Normal 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
|
||||
)
|
Reference in a new issue