"""Glue between the page and Groundline. Runs inside the browser (Pyodide); Groundline's own code is loaded from GitHub.""" import html import json import os from groundline.checks import check_all from groundline.cli import draft_sentences from groundline.report import LABEL, METHOD_HINT, METHOD_TITLE, counts, methodology_body, to_html, to_markdown from groundline.text import passages_from TONE = {"matched": "green", "check": "amber", "no match": "red", "contradicted": "purple"} def run(source_paths_json, names_json, draft_path): e = html.escape paths, names = json.loads(source_paths_json), json.loads(names_json) try: passages = passages_from(paths) except (SystemExit, Exception): return json.dumps({"error": "One of the source files could not be read. Check it opens normally, or paste its text instead."}) if not passages: return json.dumps({"error": "No text was found in the sources. Scanned PDFs need OCR first."}) results = check_all(draft_sentences(draft_path), passages) c = counts(results) out = ['
' + f"{len(results)} sentences checked: " + ", ".join( f'{v} {LABEL[k].lower()}' for k, v in c.items() if v) + "
"] for i, r in enumerate(results, 1): out.append(f'
{i}. {LABEL[r.verdict]}
{e(r.sentence)}
') if r.flags: out.append("") if r.matches and r.verdict != "matched": s, p = r.matches[0] out.append(f'
Closest passage ({e(p.where)}): {e(p.text)}
') out.append("
") out.append(f'
{e(METHOD_TITLE)}. {e(METHOD_HINT)}
{e(methodology_body(names, False))}
') return json.dumps({"html": "\n".join(out), "report_html": to_html(results, names, "draft"), "report_md": to_markdown(results, names, "draft")})