Commit c24335b6 authored by berumuron's avatar berumuron
Browse files

add: Allow to generate a sitemap page

parent 01a8b115
......@@ -133,6 +133,12 @@ def main(environment):
blog_page = utils.build_blog_page(blog_articles, series, configuration)
pages.append(blog_page)
# Load a "sitemap.xml" page
sitemap_template_filepath = os.path.join("templates", "sitemap.xml")
if os.path.exists(sitemap_template_filepath):
sitemap_page = utils.build_sitemap_page(blog_articles, series, configuration)
pages.append(sitemap_page)
# And load the main feed
if len(blog_articles) > 0 or configuration.get("SITE_FORCE_FEED", False):
feed = Feed("feeds/all", blog_articles, configuration=configuration)
......
import os
import uuid
import re
import yaml
......@@ -25,7 +26,13 @@ class Page:
def url(self):
site_url = self.meta.get("SITE_URL", "")
return f"{site_url}{self.slug()}.html"
template_name = self.template()
if "." in template_name:
extension = os.path.splitext(template_name)[1]
else:
extension = ".html"
return f"{site_url}{self.slug()}{extension}"
def uuid(self):
return str(uuid.uuid5(uuid.NAMESPACE_URL, self.url()))
......
......@@ -67,16 +67,33 @@ def build_blog_page(articles, series, configuration):
return blog_page
def build_sitemap_page(articles, series, configuration):
articles.sort(key=lambda article: article.date(), reverse=True)
series.sort(key=lambda serie: serie.title(), reverse=False)
sitemap_page = Page("sitemap", "", configuration=configuration)
sitemap_page.meta["PAGE_TEMPLATE"] = "sitemap.xml"
sitemap_page.meta["ARTICLES"] = articles
sitemap_page.meta["SERIES"] = series
return sitemap_page
templates_cache = {}
def write_content(content, output_path):
"""Write content (Article or Page) under output_path.
"""
output_filepath = os.path.join(output_path, f"{content.slug()}.html")
template_filepath = os.path.join(
os.curdir, "templates", f"{content.template()}.html"
)
template_name = content.template()
if "." in template_name:
template_ext = os.path.splitext(template_name)[1]
template_filepath = os.path.join(os.curdir, "templates", template_name)
output_filepath = os.path.join(output_path, f"{content.slug()}{template_ext}")
else:
template_filepath = os.path.join(
os.curdir, "templates", f"{template_name}.html"
)
output_filepath = os.path.join(output_path, f"{content.slug()}.html")
# Initialize the template defined within content
if template_filepath in templates_cache:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment