#!/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('')