md-validator / resolve.py
Pannuru's picture
Upload 6 files
cd6dbae verified
Raw History Blame Contribute Delete
3.11 kB
#!/usr/bin/env python3
"""Merge reviewed content into report.json.
Writing recovered Markdown (LaTeX, tables, diagrams) inside JSON means escaping every
backslash and quote, which is error-prone. Instead, write a plain Markdown "resolutions"
file and let this script merge it:
### @FIG-p4-1 done
**Figure 1 - Proposed Spam Detection Model.** ...transcription / Mermaid / description...
### @EQ-p5-1 done
$$\\chi^2 = \\sum \\frac{(O_i - E_i)^2}{E_i} \\tag{1}$$
### @TB-p6-1 done
(empty body = accept the auto-drafted table exactly as generated)
### @GFX-p1-1 skip: publisher logo, decorative
### @T-p3-2 skip: page furniture, not content
Rules
* `done` + body -> body becomes the recovered Markdown for that item
* `done` + no body -> keep what is already in the item (auto draft / auto text)
* `skip: reason` -> item is not appended (reason is logged in the appendix)
* Unknown ids are reported and ignored.
Usage: python resolve.py --report work/report.json --resolutions work/resolutions.md
"""
import argparse
import json
import re
import sys
HDR = re.compile(r"^###\s+@(\S+)\s+(done|skip)\b\s*:?\s*(.*)$", re.I)
def parse(text):
out, cur = {}, None
for line in text.replace("\r\n", "\n").split("\n"):
m = HDR.match(line)
if m:
cur = m.group(1)
out[cur] = {"verb": m.group(2).lower(), "reason": m.group(3).strip(), "body": []}
elif cur:
out[cur]["body"].append(line)
for v in out.values():
v["body"] = "\n".join(v["body"]).strip("\n").strip()
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--report", required=True)
ap.add_argument("--resolutions", required=True)
a = ap.parse_args()
rep = json.load(open(a.report, encoding="utf8"))
res = parse(open(a.resolutions, encoding="utf8").read())
by_id = {i["id"]: i for i in rep["items"]}
done = skipped = 0
for rid, r in res.items():
it = by_id.get(rid)
if not it:
print(f" ! unknown id {rid} (ignored)")
continue
if r["verb"] == "skip":
it["status"], it["skip_reason"] = "skipped", r["reason"] or "skipped by reviewer"
skipped += 1
continue
body = r["body"] or it.get("recovered_markdown") or it.get("draft_markdown") or ""
if not body.strip():
print(f" ! {rid}: marked done but there is no content (add a body) -> left open")
continue
it["recovered_markdown"], it["status"] = body, "done"
it.pop("skip_reason", None)
done += 1
rep["summary"]["items_open"] = sum(1 for i in rep["items"] if i["status"] == "open")
json.dump(rep, open(a.report, "w", encoding="utf8"), ensure_ascii=False, indent=2)
print(f"resolved: {done} done, {skipped} skipped | still open: {rep['summary']['items_open']}")
for i in rep["items"]:
if i["status"] == "open":
print(f" open: {i['id']} ({i['type']}, p.{i['page']}, {i.get('label','')})")
if __name__ == "__main__":
sys.exit(main())