|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Convert a Vale JSON report into a standalone HTML file.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import html |
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | +from typing import Iterable |
| 11 | + |
| 12 | + |
| 13 | +def parse_args() -> argparse.Namespace: |
| 14 | + parser = argparse.ArgumentParser(description=__doc__) |
| 15 | + parser.add_argument("--input", type=Path, required=True, help="Path to the Vale JSON report.") |
| 16 | + parser.add_argument("--output", type=Path, required=True, help="Destination path for the HTML report.") |
| 17 | + return parser.parse_args() |
| 18 | + |
| 19 | + |
| 20 | +def load_alerts(report: Path) -> list[dict[str, object]]: |
| 21 | + if not report.is_file(): |
| 22 | + return [] |
| 23 | + try: |
| 24 | + data = json.loads(report.read_text(encoding="utf-8")) |
| 25 | + except json.JSONDecodeError: |
| 26 | + return [] |
| 27 | + |
| 28 | + alerts: list[dict[str, object]] = [] |
| 29 | + if isinstance(data, dict): |
| 30 | + if isinstance(data.get("alerts"), list): |
| 31 | + alerts.extend(data["alerts"]) |
| 32 | + files = data.get("files") |
| 33 | + if isinstance(files, dict): |
| 34 | + for file_result in files.values(): |
| 35 | + if not isinstance(file_result, dict): |
| 36 | + continue |
| 37 | + file_alerts = file_result.get("alerts") |
| 38 | + if isinstance(file_alerts, list): |
| 39 | + alerts.extend(file_alerts) |
| 40 | + return alerts |
| 41 | + |
| 42 | + |
| 43 | +def render_alert_rows(alerts: Iterable[dict[str, object]]) -> str: |
| 44 | + normalized: list[dict[str, str]] = [] |
| 45 | + for alert in alerts: |
| 46 | + if not isinstance(alert, dict): |
| 47 | + continue |
| 48 | + span = alert.get("Span") |
| 49 | + line = column = "" |
| 50 | + if isinstance(span, dict): |
| 51 | + start = span.get("Start") |
| 52 | + if isinstance(start, dict): |
| 53 | + line = str(start.get("Line", "")) |
| 54 | + column = str(start.get("Column", "")) |
| 55 | + elif isinstance(span, list) and span: |
| 56 | + line = str(span[0]) |
| 57 | + if len(span) > 1: |
| 58 | + column = str(span[1]) |
| 59 | + normalized.append( |
| 60 | + { |
| 61 | + "file": str(alert.get("Path", "")), |
| 62 | + "line": line, |
| 63 | + "column": column, |
| 64 | + "severity": str(alert.get("Severity", "")), |
| 65 | + "rule": str(alert.get("Check", "")), |
| 66 | + "message": str(alert.get("Message", "")), |
| 67 | + } |
| 68 | + ) |
| 69 | + |
| 70 | + if not normalized: |
| 71 | + return "<tr><td colspan='6'>No alerts found.</td></tr>" |
| 72 | + |
| 73 | + def sort_key(entry: dict[str, str]) -> tuple: |
| 74 | + def as_int(value: str) -> int: |
| 75 | + try: |
| 76 | + return int(value) |
| 77 | + except (TypeError, ValueError): |
| 78 | + return 0 |
| 79 | + |
| 80 | + return ( |
| 81 | + entry["file"], |
| 82 | + as_int(entry["line"]), |
| 83 | + as_int(entry["column"]), |
| 84 | + entry["rule"], |
| 85 | + ) |
| 86 | + |
| 87 | + normalized.sort(key=sort_key) |
| 88 | + |
| 89 | + rows: list[str] = [] |
| 90 | + for entry in normalized: |
| 91 | + severity_value = entry["severity"] |
| 92 | + severity = html.escape(severity_value) |
| 93 | + severity_class = f"severity-{severity_value.lower()}" if severity_value else "" |
| 94 | + rows.append( |
| 95 | + "<tr>" |
| 96 | + f"<td>{html.escape(entry['file'])}</td>" |
| 97 | + f"<td>{html.escape(entry['line'])}</td>" |
| 98 | + f"<td>{html.escape(entry['column'])}</td>" |
| 99 | + f"<td class='{severity_class}'>{severity}</td>" |
| 100 | + f"<td>{html.escape(entry['rule'])}</td>" |
| 101 | + f"<td>{html.escape(entry['message'])}</td>" |
| 102 | + "</tr>" |
| 103 | + ) |
| 104 | + |
| 105 | + return "\n".join(rows) |
| 106 | + |
| 107 | + |
| 108 | +def main() -> None: |
| 109 | + args = parse_args() |
| 110 | + alerts = load_alerts(args.input) |
| 111 | + counts = {"error": 0, "warning": 0, "suggestion": 0} |
| 112 | + for alert in alerts: |
| 113 | + if not isinstance(alert, dict): |
| 114 | + continue |
| 115 | + severity = str(alert.get("Severity", "")).lower() |
| 116 | + if severity in counts: |
| 117 | + counts[severity] += 1 |
| 118 | + args.output.parent.mkdir(parents=True, exist_ok=True) |
| 119 | + table_rows = render_alert_rows(alerts) |
| 120 | + html_content = f""" |
| 121 | +<!DOCTYPE html> |
| 122 | +<html lang="en"> |
| 123 | +<head> |
| 124 | + <meta charset="utf-8" /> |
| 125 | + <title>Vale Report</title> |
| 126 | + <style> |
| 127 | + body {{ font-family: Arial, sans-serif; margin: 2rem; }} |
| 128 | + table {{ border-collapse: collapse; width: 100%; }} |
| 129 | + th, td {{ border: 1px solid #ccc; padding: 0.5rem; text-align: left; }} |
| 130 | + th {{ background-color: #f0f0f0; }} |
| 131 | + tbody tr:nth-child(even) {{ background-color: #fafafa; }} |
| 132 | + .severity-error {{ color: #c62828; font-weight: bold; }} |
| 133 | + .severity-warning {{ color: #ef6c00; font-weight: bold; }} |
| 134 | + .severity-suggestion {{ color: #1565c0; font-weight: bold; }} |
| 135 | + </style> |
| 136 | +</head> |
| 137 | +<body> |
| 138 | + <h1>Vale Report</h1> |
| 139 | + <p>Total alerts: {len(alerts)}</p> |
| 140 | + <ul> |
| 141 | + <li>Errors: {counts['error']}</li> |
| 142 | + <li>Warnings: {counts['warning']}</li> |
| 143 | + <li>Suggestions: {counts['suggestion']}</li> |
| 144 | + </ul> |
| 145 | + <table> |
| 146 | + <thead> |
| 147 | + <tr> |
| 148 | + <th>File</th> |
| 149 | + <th>Line</th> |
| 150 | + <th>Column</th> |
| 151 | + <th>Severity</th> |
| 152 | + <th>Rule</th> |
| 153 | + <th>Message</th> |
| 154 | + </tr> |
| 155 | + </thead> |
| 156 | + <tbody> |
| 157 | + {table_rows} |
| 158 | + </tbody> |
| 159 | + </table> |
| 160 | +</body> |
| 161 | +</html> |
| 162 | +""" |
| 163 | + args.output.write_text(html_content, encoding="utf-8") |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + main() |
0 commit comments