"""Files written from the declarative specs, so each rule has one source of truth: .github/ISSUE_TEMPLATE/report-a-run.yml from collection/specs/report_form.yaml (report_form.render) the CROSSWALK block of core/graph.js from collection/specs/bot.yaml (Ken's bot uses it at run time) """ import json from . import forge from .common import path, read_yaml LINKS = "data/public/links.json" def links(): """The links the page shows, from forge.yaml and pipeline.yaml (site/build.js fills them into the page).""" f = forge.config() src = read_yaml("collection/pipeline.yaml")["sources"] host = f["url"].split("://", 1)[-1].rstrip("/") return { "repo": forge.repo_url(f), "repo_label": f"{host}/{f['repo']}", "host": host, "kind": f["kind"], "visibility": f.get("visibility", "public"), "report": forge.new_issue_url(".github/ISSUE_TEMPLATE/report-a-run.yml", f), "form": src["reddit_spreadsheet"]["links"]["form"], "sheet": src["reddit_spreadsheet"]["links"]["sheet"], } GRAPH_JS = "core/graph.js" BEGIN = " // BEGIN CROSSWALK" END = " // END CROSSWALK\n" def graph_js(): """core/graph.js with its CROSSWALK block rewritten from the bot spec.""" text = path(GRAPH_JS).read_text(encoding="utf-8") a = text.index(BEGIN) a = text.index("\n", a) + 1 b = text.index(END, a) rules = read_yaml("collection/specs/bot.yaml")["crosswalk"] lines = [" const CROSSWALK = ["] for i, (rx, col, val) in enumerate(rules): sep = "," if i < len(rules) - 1 else "" lines.append(f" [/{rx.replace('/', chr(92) + '/')}/, {json.dumps(col)}, {json.dumps(val, ensure_ascii=False)}]{sep}") lines.append(" ];") return text[:a] + "\n".join(lines) + "\n" + text[b:]