#!/usr/bin/env python3 """Idempotently inject the HomeDash stylesheet and script into Dashy's index.html. Dashy's index.html does not always contain a `` tag. A naive `sed s##...#` therefore silently drops the injected script (and the `` marker itself). This script inserts before ``/`` when those exist, otherwise before ``, and restores a missing ``. """ import re import sys CSS_TAG = '' JS_TAG = '' def patch(html: str) -> str: html = re.sub(r'[ \t]*\s*', '', html) html = re.sub(r'[ \t]*\s*', '', html) if '' in html: html = html.replace('', CSS_TAG + '', 1) if '' in html: html = html.replace('', JS_TAG + '', 1) elif '' in html: html = html.replace('', JS_TAG + '\n\n', 1) else: html = html + '\n' + JS_TAG + '\n' return html def main(path: str) -> int: with open(path, encoding='utf-8') as fh: original = fh.read() patched = patch(original) if 'href="/navi.css"' not in patched or 'src="/navi.js"' not in patched: print('patch failed: tags missing after patch', file=sys.stderr) return 1 with open(path, 'w', encoding='utf-8') as fh: fh.write(patched) print('patched %s (%s)' % (path, 'changed' if patched != original else 'unchanged')) return 0 if __name__ == '__main__': raise SystemExit(main(sys.argv[1] if len(sys.argv) > 1 else '/app/dashy/index.html'))