2018-12-07 19:02:27 -05:00
|
|
|
#!/usr/bin/env python3
|
2018-12-05 23:10:18 -05:00
|
|
|
import errno
|
|
|
|
import os
|
2018-12-07 19:02:27 -05:00
|
|
|
from html import escape
|
2018-12-05 23:10:18 -05:00
|
|
|
from hashlib import sha256
|
2018-12-08 20:42:11 -05:00
|
|
|
from shutil import copyfile
|
2018-12-05 23:10:18 -05:00
|
|
|
|
2018-12-07 19:02:27 -05:00
|
|
|
import yaml
|
2018-12-05 23:10:18 -05:00
|
|
|
from rcssmin import cssmin
|
|
|
|
|
|
|
|
DIR = os.path.dirname(__file__)
|
|
|
|
SRC_DIR = os.path.join(DIR, 'src')
|
|
|
|
DIST_DIR = os.path.join(DIR, 'dist')
|
|
|
|
ASSETS_SRC = os.path.join(SRC_DIR, 'assets')
|
|
|
|
ASSETS_DIST = os.path.join(DIST_DIR, 'assets')
|
2018-12-08 20:42:11 -05:00
|
|
|
ICONS_SRC = os.path.join(SRC_DIR, 'icons')
|
2018-12-05 23:10:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def build_assets():
|
|
|
|
name_map = []
|
|
|
|
for asset in os.listdir(ASSETS_SRC):
|
|
|
|
name, ext = os.path.splitext(asset)
|
|
|
|
if not ext:
|
|
|
|
continue
|
|
|
|
|
|
|
|
with open(os.path.join(ASSETS_SRC, asset), 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
hash = sha256(content).hexdigest()[:20]
|
|
|
|
dist_name = '%s-%s%s' % (name, hash, ext)
|
|
|
|
|
|
|
|
if ext == '.css':
|
2018-12-07 19:02:27 -05:00
|
|
|
content = cssmin(content.decode('utf-8')).encode('utf-8')
|
2018-12-05 23:10:18 -05:00
|
|
|
|
|
|
|
with open(os.path.join(ASSETS_DIST, dist_name), 'wb') as f:
|
|
|
|
f.write(content)
|
2018-12-07 19:02:27 -05:00
|
|
|
name_map.append((asset, dist_name))
|
2018-12-05 23:10:18 -05:00
|
|
|
return name_map
|
|
|
|
|
|
|
|
|
2018-12-08 20:42:11 -05:00
|
|
|
def build_icons():
|
|
|
|
for icon in os.listdir(ICONS_SRC):
|
|
|
|
copyfile(os.path.join(ICONS_SRC, icon), os.path.join(DIST_DIR, icon))
|
|
|
|
|
|
|
|
|
2018-12-07 19:02:27 -05:00
|
|
|
def build_links(links):
|
|
|
|
output = []
|
|
|
|
for section in links['sections']:
|
|
|
|
output.append(' <h2 id="%s">%s</h2>' % (section['id'], escape(section['name'])))
|
|
|
|
output.append(' <ul>')
|
2018-12-05 23:10:18 -05:00
|
|
|
|
2018-12-07 19:02:27 -05:00
|
|
|
for link in section['links']:
|
|
|
|
output.append(' <li><a href="{url}">{url}</a> — {description}</li>'.format(
|
|
|
|
url=escape(link['name']), description=escape(link['description'])
|
|
|
|
))
|
2018-12-05 23:10:18 -05:00
|
|
|
|
2018-12-07 19:02:27 -05:00
|
|
|
output.append(' </ul>')
|
|
|
|
output.append('')
|
2018-12-05 23:10:18 -05:00
|
|
|
|
2018-12-07 19:02:27 -05:00
|
|
|
return '\n'.join(output)
|
|
|
|
|
|
|
|
|
|
|
|
def build_redirects(links):
|
|
|
|
output = []
|
|
|
|
|
|
|
|
def build_link(link):
|
|
|
|
output.append('%s "%s";' % (link['name'], link['target']))
|
|
|
|
|
|
|
|
if 'other_links' in links:
|
|
|
|
for link in links['other_links']:
|
|
|
|
build_link(link)
|
|
|
|
output.append('')
|
|
|
|
|
|
|
|
for section in links['sections']:
|
|
|
|
output.append('# %s' % (section['name'],))
|
|
|
|
for link in section['links']:
|
|
|
|
build_link(link)
|
|
|
|
output.append('')
|
|
|
|
|
|
|
|
with open(os.path.join(DIST_DIR, 'redirects.conf'), 'w', encoding='utf-8') as f:
|
|
|
|
f.write('\n'.join(output))
|
|
|
|
|
|
|
|
|
|
|
|
def build_index(html_replace, links):
|
|
|
|
with open(os.path.join(SRC_DIR, 'index.html'), encoding='utf-8') as f:
|
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
for old, new in html_replace:
|
|
|
|
content = content.replace(old, new)
|
|
|
|
content = content.replace('{listing}', build_links(links))
|
|
|
|
|
|
|
|
with open(os.path.join(DIST_DIR, 'index.html'), 'w', encoding='utf-8') as f:
|
|
|
|
f.write(content)
|
2018-12-05 23:10:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
os.makedirs(ASSETS_DIST)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
|
|
|
|
2018-12-07 19:02:27 -05:00
|
|
|
with open(os.path.join(SRC_DIR, 'links.yml'), encoding='utf-8') as f:
|
|
|
|
links = yaml.safe_load(f)
|
|
|
|
|
2018-12-05 23:10:18 -05:00
|
|
|
name_map = build_assets()
|
2018-12-08 20:42:11 -05:00
|
|
|
build_icons()
|
2018-12-07 19:02:27 -05:00
|
|
|
build_index(name_map, links)
|
|
|
|
build_redirects(links)
|
|
|
|
|
2018-12-05 23:10:18 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|