Commit 0542b872 authored by berumuron's avatar berumuron
Browse files

Generate Atom feeds for series

parent 796ba09e
......@@ -161,6 +161,9 @@ class Page:
site_url = self.meta.get("SITE_URL", "")
return f"{site_url}{self.slug()}.html"
def uuid(self):
return str(uuid.uuid5(uuid.NAMESPACE_URL, self.url()))
def content(self):
return self.meta["PAGE_CONTENT"]
......@@ -270,6 +273,7 @@ def main(environment):
# STEP 1: we load all the Articles and Pages
pages = []
articles = []
articles_by_series = {}
# Check that index file exists
index_filepath = os.path.join(os.curdir, "index.html")
......@@ -313,6 +317,7 @@ def main(environment):
with open(serie_filepath, "r") as serie_file:
serie = Page(slug, serie_file.read(), configuration=configuration)
pages.append(serie)
articles_by_series[serie] = []
for filename in list_files(dirpath):
filename_no_ext, ext = os.path.splitext(filename)
......@@ -332,6 +337,9 @@ def main(environment):
)
articles.append(article)
if serie:
articles_by_series[serie].append(article)
for filename in list_files(articles_path):
filename_no_ext, ext = os.path.splitext(filename)
slug = os.path.basename(filename_no_ext)
......@@ -361,6 +369,8 @@ def main(environment):
# Sort the articles by publication date
articles.sort(key=lambda article: article.date(), reverse=True)
for serie_articles in articles_by_series.values():
serie_articles.sort(key=lambda article: article.date(), reverse=True)
# And write them in the ./site folder
for article in articles:
......@@ -381,7 +391,7 @@ def main(environment):
output_file.write(blog_template.render(context))
print(f"Written page: {configuration['SITE_URL']}blog.html")
# STEP 3: we write also the Atom feed.
# STEP 3: we write also the Atom feeds (the main one + the series').
# Just create some variables so it's easier to manipulate after
site_url = configuration["SITE_URL"]
site_uuid = configuration["SITE_UUID"]
......@@ -390,7 +400,10 @@ def main(environment):
timezone = pytz.timezone(site_timezone)
current_datetime = datetime.datetime.now(tz=timezone)
# Build the Atom feed
feed_dirpath = os.path.join(output_path, "feeds")
os.mkdir(feed_dirpath)
# Build the main Atom feed
feed = boopfeed.Atom()
feed.add_header(
{
......@@ -422,12 +435,49 @@ def main(environment):
feed.add_entry(entry)
# And write it on the filesystem
feed_dirpath = os.path.join(output_path, "feeds")
os.mkdir(feed_dirpath)
feed_filepath = os.path.join(feed_dirpath, "all.atom.xml")
feed.write(feed_filepath)
print(f"Written feed: {site_url}feeds/all.atom.xml")
# Now, build the feeds for series (same process)
for serie, serie_articles in articles_by_series.items():
feed_filename = f"{os.path.basename(serie.slug())}.atom.xml"
feed_filepath = os.path.join(feed_dirpath, feed_filename)
feed = boopfeed.Atom()
feed.add_header(
{
"title": f"{serie.title()} - {site_title}",
"self_link": f"{site_url}feeds/{feed_filename}",
"site_link": serie.url(),
"id": f"urn:uuid:{serie.uuid()}",
"updated": current_datetime,
}
)
for article in serie_articles:
# and complete it for each article
entry = {
"title": article.title(),
"id": f"urn:uuid:{article.uuid()}",
"article_link": article.url(),
"published": article.date(),
"content": article.content(),
}
article_author = article.author()
if article_author:
entry["author"] = article_author
article_update = article.update()
if article_update:
entry["updated"] = article_update
feed.add_entry(entry)
# And write it on the filesystem
feed.write(feed_filepath)
print(f"Written feed: {site_url}feeds/{feed_filename}")
# STEP 4: and we finish by copying the static folder.
static_dirpath = os.path.join(os.curdir, "static")
if os.path.isdir(static_dirpath):
......
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