From 3515d993f5dd911200643923b3ae50c381e87d30 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 6 Oct 2020 10:56:33 +0200 Subject: add mkfeed.py --- mkfeed.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100755 mkfeed.py (limited to 'mkfeed.py') diff --git a/mkfeed.py b/mkfeed.py new file mode 100755 index 0000000..7ba298d --- /dev/null +++ b/mkfeed.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +# Written by Martin Fischer and licensed under MIT. +# The embedded SVG is licensed under Mozilla's MPL / GPL / LGPL and +# from https://commons.wikimedia.org/wiki/File:Generic_Feed-icon.svg. +import os +import sys +import xml.etree.ElementTree as ET +from xml.sax.saxutils import escape + +FEED_SVG = 'Feed' + +URL = escape(os.environ['URL']) +TITLE = escape(os.environ['TITLE']) + +posts = [] + +DIRNAME = sys.argv[1].strip('/') +DIR = '/' + DIRNAME + '/' +OUTDIR = 'build' + DIR + +for filename in sorted(os.listdir(OUTDIR), reverse=True): + if filename in ('atom.xml', 'index.html'): + continue + tree = ET.parse(OUTDIR + filename + '/index.html').getroot() + title = tree.find('.//title') + data = dict( + title = escape(title.text), + html = ''.join(ET.tostring(c, 'unicode') for c in tree.find('.//div[@id="content"]')), + path = DIR + escape(filename) + ) + dataEl = tree.find('.//meta[@name="dcterms.date"]') + if dataEl is not None: + data['date'] = dataEl.attrib['content'] + data['dateMeta'] = dataEl.attrib['content'] + 'T00:00:00Z' + else: + print('[warning] no date for', filename) + posts.append(data) + +last_updated = max([p['dateMeta'] for p in posts if 'dateMeta' in p]) + +with open(OUTDIR + 'index.html', 'w') as f: + f.write(f'

{escape(DIRNAME.title())} {FEED_SVG}

') + f.write('') + +with open(OUTDIR + 'atom.xml', 'w') as f: + f.write(f'''\ + + + + {URL} + {TITLE} + + {last_updated} + ''') + for post in posts[:10]: + f.write(f''' + + {post['title']} + + {URL.rstrip('/')}{post['path']} + {post.get('dateMeta')} + + + ''') + f.write('') -- cgit v1.2.3