Write site index

This commit is contained in:
Fred Pauchet 2023-01-10 21:40:31 +01:00
parent 0fe3c62f45
commit 74a5faae48
3 changed files with 43 additions and 12 deletions

View File

@ -69,7 +69,17 @@ class Article:
output_file.write(self.to_prose())
class Section:
class PublishedUnpublished:
@property
def published_articles(self):
return [x for x in self.articles if x.published_date]
@property
def articles_without_date(self):
return [x for x in self.articles if not x.published_date]
class Section(PublishedUnpublished):
def __init__(self, key, parent=None):
self.key = key
self.parent = parent
@ -110,7 +120,7 @@ class Tag:
return False
class Site:
class Site(PublishedUnpublished):
"""A site contains articles, categories and all related data."""
def __init__(self, root_directory: str):
@ -154,13 +164,9 @@ class Site:
def write(self):
LOGGER.info("Writing site articles")
for article in self.articles:
with open(
os.path.join(self.root_directory, "index.html"), "w"
) as html_file:
html_file.write(
"""<html><body><p>{}</p></body></html>""".format(self.articles)
)
content = jinja_env.get_template("index.html").render(site=self)
with open(os.path.join(self.root_directory, "index.html"), "w") as html_file:
html_file.write(content)
LOGGER.info("Writing site categories")
for section in self.sections.values():

19
templates/index.html Normal file
View File

@ -0,0 +1,19 @@
<h1>{{ site.name }}</h1>
<ul>
{% for section in site.sections %}
<li>{{ section }}</li>
{% endfor %}
</ul>
<ul>
{% for year, list in site.published_articles|groupby('published_date.year') %}
<li><b>{{ year }}</b>
<ul>
{% for event in list %}
<li>{{event.title}}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>

View File

@ -1,7 +1,13 @@
<h1>{{ section.parent.key }} >> {{ section.key }}</h1>
<ul>
{% for article in section.articles %}
<li>{{ article.published_date }} {{ article.title }}</li>
{% for year, list in section.published_articles|groupby('published_date.year') %}
<li><b>{{ year }}</b>
<ul>
{% for event in list %}
<li>{{event.title}}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
</ul>