#!/usr/bin/env python3
import html
from pathlib import Path
import tomli
EMAIL = ''.join(['{:02x};'.format(ord(c)) for c in 'mailto:martin@push-f.com'])
out_dir = Path('build')
out_dir.mkdir(exist_ok=True)
html_f = open(out_dir / 'index.html', 'w')
nginx_f = open(out_dir / 'nginx_rules', 'w')
html_f.write('''
spec.pub
spec.pub
Redirects to specification publications.
Maintained by Martin Fischer.
Suggestions for new redirects are welcome.
Note that the redirects are lowercase (the links on this page
are capitalized for readability).
''' % EMAIL)
redirects = {}
def link(key, css_class=''):
return '{}'.format(
html.escape(key.lower()),
f' class={css_class}' if css_class else '',
html.escape(key))
with open('specs.toml', 'rb') as f:
data = tomli.load(f)
html_f.write('')
for section, redirects in data.items():
html_f.write(f'
{section}
')
html_f.write('
')
for key, config in sorted(redirects.items(), key=lambda t: t[0].lower()):
if isinstance(config, str):
config = {'url': config}
if not config.get('hidden'):
html_f.write('- ')
html_f.write(link(key, css_class='for-purchase' if 'closed' in config else ''))
if 'aliases' in config:
html_f.write(
' ('
+ ', '.join([link(k) for k in config['aliases']])
+ ') '
)
html_f.write('
')
for path in [key, *config.get('aliases', ())]:
closed = config.get('closed')
if closed:
text = closed.replace('ISO.', 'ISO.
')
response = f"return 402 '{text}'; add_header Content-Type text/html always;"
else:
response = f'return 302 {config["url"]};'
nginx_f.write('location = /%s { %s }\n' % (path.lower(), response))
html_f.write('
')
html_f.write('''
''')
html_f.close()
nginx_f.close()