sort posts in rss feed by date

This commit is contained in:
Akbar Rahman 2024-04-13 20:28:46 +01:00
parent 8300c4ba45
commit 7bee57bcd5
Signed by: alvierahman90
GPG Key ID: 6217899F07CA2BDF

View File

@ -14,6 +14,7 @@ import time
import magic
import regex as re
import pprint
from datetime import datetime as dt
import frontmatter
import jinja2
@ -246,6 +247,13 @@ class FileMap:
return d
def rfc822_date_sorter_key(date):
if date is None:
return 0
int(dt.strptime(date, '%a, %d %b %Y %H:%M:%S %z').timestamp())
def update_required(src_filepath, output_filepath):
"""
check if file requires an update,
@ -531,6 +539,22 @@ def main(args):
}
posts.append(post)
posts.sort(
key=lambda p: rfc822_date_sorter_key(p.get('pub_date')),
reverse=True
)
# render rss feed
rss = JINJA_TEMPLATE_BLOG_FEED.render(
title=root_properties.get('title', ''),
description=root_properties.get('content', ''),
base_url=FILEMAP.get_base_url(),
link=f"{FILEMAP.get_base_url()}{root_properties['dst_path']['web']}",
language='en-GB',
posts=posts,
)
root_properties['dst_path']['raw'].joinpath('feed.xml').write_text(rss)
#pprint.pprint(root_properties)
# render index
html = (JINJA_TEMPLATE_BLOGINDEX if root_properties['blog'] else JINJA_TEMPLATE_INDEX).render(
@ -549,18 +573,6 @@ def main(args):
)
root_properties['dst_path']['raw'].joinpath('index.html').write_text(html)
# render rss feed if blog
if root_properties['blog']:
rss = JINJA_TEMPLATE_BLOG_FEED.render(
title=root_properties.get('title', ''),
description=root_properties.get('content', ''),
base_url=FILEMAP.get_base_url(),
link=f"{FILEMAP.get_base_url()}{root_properties['dst_path']['web']}",
language='en-GB',
posts=posts,
)
root_properties['dst_path']['raw'].joinpath('feed.xml').write_text(rss)
# render each file
for file in files:
render_file(root.joinpath(file))