()
| 540 | |
| 541 | |
| 542 | def main(): |
| 543 | parser = argparse.ArgumentParser(description=__doc__) |
| 544 | parser.add_argument("--html", required=True, help="Rendered HTML input") |
| 545 | parser.add_argument("--output", required=True, help="JSON output path") |
| 546 | parser.add_argument("--language", default="en-US") |
| 547 | parser.add_argument( |
| 548 | "--accept-list", |
| 549 | default=os.path.join( |
| 550 | os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), |
| 551 | "docs", |
| 552 | "developer-guide", |
| 553 | "languagetool-accept.txt", |
| 554 | ), |
| 555 | help="Path to the LanguageTool accept-list file (one regex per line).", |
| 556 | ) |
| 557 | args = parser.parse_args() |
| 558 | |
| 559 | text = extract_text(args.html) |
| 560 | accept_re = load_accept_patterns(args.accept_list) |
| 561 | |
| 562 | report = {"status": "unknown", "matches": [], "total": 0} |
| 563 | try: |
| 564 | try: |
| 565 | matches = run_languagetool(text, language=args.language, accept_re=accept_re) |
| 566 | except Exception as exc: # noqa: BLE001 — advisory check must not crash CI |
| 567 | print(f"LanguageTool failed to start: {exc}", file=sys.stderr) |
| 568 | report = {"status": "error", "reason": str(exc), "matches": [], "total": 0} |
| 569 | else: |
| 570 | if matches is None: |
| 571 | report = { |
| 572 | "status": "skipped", |
| 573 | "reason": "language_tool_python not installed", |
| 574 | "matches": [], |
| 575 | "total": 0, |
| 576 | } |
| 577 | else: |
| 578 | try: |
| 579 | serialized = matches_to_json(matches, text) |
| 580 | except Exception as exc: # noqa: BLE001 |
| 581 | print( |
| 582 | f"LanguageTool: failed to serialize {len(matches)} match(es): {exc}", |
| 583 | file=sys.stderr, |
| 584 | ) |
| 585 | report = { |
| 586 | "status": "error", |
| 587 | "reason": f"serialization failed: {exc}", |
| 588 | "matches": [], |
| 589 | "total": len(matches), |
| 590 | } |
| 591 | else: |
| 592 | report = {"status": "ok", "matches": serialized, "total": len(serialized)} |
| 593 | finally: |
| 594 | os.makedirs(os.path.dirname(args.output) or ".", exist_ok=True) |
| 595 | with open(args.output, "w", encoding="utf-8") as fh: |
| 596 | json.dump(report, fh, indent=2) |
| 597 | print( |
| 598 | f"LanguageTool report written to {args.output} " |
| 599 | f"({report.get('total', 0)} match(es), status={report.get('status')})." |
no test coverage detected