"""Verification script for the revised LumenDesk package (standard library only). Run from anywhere; it locates output/ and inputs/ relative to this file or the cwd.""" import json, os, re, sys here = os.path.dirname(os.path.abspath(__file__)) roots = [os.path.join(here, "..", ".."), os.getcwd(), os.path.join(here, "..")] root = next((r for r in roots if os.path.exists(os.path.join(r, "output", "article.md"))), None) if root is None: sys.exit("could not find output/article.md") out, inp = os.path.join(root, "output"), os.path.join(root, "inputs") art = open(os.path.join(out, "article.md"), encoding="utf-8").read() meta = json.load(open(os.path.join(out, "metadata.json"), encoding="utf-8")) src = {n: open(os.path.join(inp, n), encoding="utf-8").read() for n in sorted(os.listdir(inp))} fails = [] def check(label, ok, detail=""): print(("PASS " if ok else "FAIL ") + label, detail) if not ok: fails.append(label) wc = lambda t: len(t.split()) lines = art.splitlines() cite_re = re.compile(r"\((S\d-[a-z\-]+\.md), \"([^\"]+)\"\)") body = "\n".join(l for l in lines if not l.startswith("#")) counts = { "whole file": wc(art), "excl. H1": wc("\n".join(l for l in lines if not l.startswith("# "))), "excl. all headings": wc(body), "excl. headings+citations": wc(cite_re.sub("", body)), "excl. headings+citations+source note": wc(cite_re.sub("", "\n".join(l for l in body.splitlines() if not l.startswith("*Based")))), "whole file, dash/slash split": len(re.findall(r"[A-Za-z0-9][A-Za-z0-9'’.]*", art)), } for k, v in counts.items(): check("word count %s = %d in 600-800" % (k, v), 600 <= v <= 800) h, e, d = meta["headline"], meta["excerpt"], meta["search_description"] check("metadata keys", sorted(meta) == ["excerpt", "headline", "search_description"]) check("headline chars = %d in 50-70" % len(h), 50 <= len(h) <= 70) check("excerpt words = %d <= 45" % wc(e), wc(e) <= 45) check("search_description chars = %d in 140-160" % len(d), 140 <= len(d) <= 160) h1 = [l[2:] for l in lines if l.startswith("# ")] check("single H1 equals headline", h1 == [h]) cites = cite_re.findall(art) print("citations:", len(cites)) for f, s in sorted(set(cites)): check("cited heading exists: %s / %s" % (f, s), f in src and ("## " + s) in src[f]) # every body paragraph that contains a digit must carry a citation, unless whitelisted for para in [p for p in body.split("\n\n") if p.strip()]: p = para.strip() if re.search(r"\d", p) and not cite_re.search(p): allowed = p.startswith("*Based") or p.startswith("A trial makes sense") or p.startswith("- ") check("uncited numeric paragraph is a recap/source note: %r" % p[:50], allowed) # every numeral in article + metadata is either in a source file or a declared derived value all_src = " ".join(src[n] for n in src if n.startswith("S") and not n.startswith("S5")) derived = {"87": "3 x 29", "99": "87 + 12", "800": "illustrative volume", "300": "800 - 500", "2.0": "product name"} for label, text in [("article", art), ("metadata", " ".join([h, e, d]))]: for num in sorted(set(re.findall(r"\d+(?:\.\d+)?", cite_re.sub("", text)))): in_src = re.search(r"(?