Start rendering templates w/ Jinja

This commit is contained in:
Fred Pauchet 2023-01-10 20:58:21 +01:00
parent b5fcc92d19
commit b5af5d20da
2 changed files with 17 additions and 4 deletions

View File

@ -4,9 +4,14 @@ import re
from datetime import datetime
from typing import List
import jinja2
import markdown
import yaml
jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader("templates"),
)
LOGGER = logging.getLogger(__name__)
@ -38,14 +43,14 @@ class Article:
self.relative_file_path = relative_file_path
if self.relative_file_path:
self.sections = categories = [
*(self.sections), article_folder = categories = [
x for x in relative_file_path.split(os.sep) if x
][:-1]
]
else:
self.sections = []
try:
self.published_date = datetime.strptime(self.sections[-1][0:10], "%Y-%m-%d")
self.published_date = datetime.strptime(article_folder[0:10], "%Y-%m-%d")
except (IndexError, TypeError, ValueError):
self.published_date = None
@ -76,11 +81,12 @@ class Section:
def write(self, root_path):
section_path = os.path.join(root_path, self.key)
content = jinja_env.get_template("section.html").render(section=self)
LOGGER.warning("Writing section to {}".format(section_path))
with open(os.path.join(section_path, "index.html"), "w") as html_file:
html_file.write("""<html><body><p>{}</p></body></html>""".format(self.articles))
html_file.write(content)
for child in self.sections.values():
child.write(section_path)

7
templates/section.html Normal file
View File

@ -0,0 +1,7 @@
<h1>{{ section.parent }} >> {{ section.key }}</h1>
<ul>
{% for article in section.articles %}
<li>{{ article.published_date }} {{ article.title }}</li>
{% endfor %}
</ul>