|
| 1 | +#!/usr/bin/env -S quarto run |
| 2 | +/** |
| 3 | + * filter-pdf-errors.ts |
| 4 | + * |
| 5 | + * Parse a quarto render log and extract PDF validation errors, |
| 6 | + * showing which files failed and why. Aggregates errors by type at the end. |
| 7 | + * |
| 8 | + * Handles two error formats: |
| 9 | + * - Typst compiler errors: "error: PDF/UA-1 error: missing alt text" |
| 10 | + * - verapdf validation failures: "WARN: PDF validation failed for ua-2:\n<rule text>" |
| 11 | + * |
| 12 | + * Usage: |
| 13 | + * quarto run tools/filter-pdf-errors.ts <logfile> |
| 14 | + * |
| 15 | + * Reads from stdin if no file is given. |
| 16 | + */ |
| 17 | + |
| 18 | +// Strip ANSI escape codes |
| 19 | +function stripAnsi(s: string): string { |
| 20 | + return s.replace(/\x1b\[[0-9;]*m/g, ""); |
| 21 | +} |
| 22 | + |
| 23 | +// Unescape HTML entities from verapdf output |
| 24 | +function unescapeHtml(s: string): string { |
| 25 | + return s |
| 26 | + .replace(/</g, "<") |
| 27 | + .replace(/>/g, ">") |
| 28 | + .replace(/&/g, "&") |
| 29 | + .replace(/"/g, '"') |
| 30 | + .replace(/'/g, "'"); |
| 31 | +} |
| 32 | + |
| 33 | +interface RenderBlock { |
| 34 | + inputFile: string; |
| 35 | + outputFile: string; |
| 36 | + lines: string[]; |
| 37 | +} |
| 38 | + |
| 39 | +interface ErrorEntry { |
| 40 | + file: string; |
| 41 | + errorType: string; |
| 42 | + context: string[]; |
| 43 | +} |
| 44 | + |
| 45 | +function extractErrors(block: RenderBlock): { |
| 46 | + seenErrors: Set<string>; |
| 47 | + context: string[]; |
| 48 | +} { |
| 49 | + const context: string[] = []; |
| 50 | + let inStack = false; |
| 51 | + const seenErrors = new Set<string>(); |
| 52 | + |
| 53 | + for (let i = 0; i < block.lines.length; i++) { |
| 54 | + const line = block.lines[i]; |
| 55 | + |
| 56 | + // Skip download lines |
| 57 | + if (/^Download /.test(line.trim())) continue; |
| 58 | + // Skip blank-ish lines at start |
| 59 | + if (context.length === 0 && line.trim() === "") continue; |
| 60 | + |
| 61 | + // Detect start of stack trace |
| 62 | + if (/^Stack trace:/.test(line.trim())) { |
| 63 | + inStack = true; |
| 64 | + continue; |
| 65 | + } |
| 66 | + if (inStack) { |
| 67 | + if (/^\s+at /.test(line)) continue; |
| 68 | + inStack = false; |
| 69 | + } |
| 70 | + |
| 71 | + // Skip duplicate ERROR: lines from typst |
| 72 | + if (/^ERROR: error: PDF\//.test(line.trim())) continue; |
| 73 | + if (/^ERROR: Typst compilation failed/.test(line.trim())) continue; |
| 74 | + |
| 75 | + // Typst compiler errors: "error: PDF/UA-1 error: missing alt text" |
| 76 | + const typstMatch = line.match(/error: (PDF\/\S+ error: .+)/); |
| 77 | + if (typstMatch) { |
| 78 | + const errType = typstMatch[1].trim(); |
| 79 | + seenErrors.add(errType); |
| 80 | + } |
| 81 | + |
| 82 | + // verapdf failures: "WARN: PDF validation failed for <standard>:" |
| 83 | + // followed by one or more rule description lines until a blank line or "Output created" |
| 84 | + const verapdfMatch = line.match( |
| 85 | + /^WARN: PDF validation failed for ([\w-]+):$/, |
| 86 | + ); |
| 87 | + if (verapdfMatch) { |
| 88 | + const standard = verapdfMatch[1]; |
| 89 | + // Collect the rule lines that follow |
| 90 | + for (let j = i + 1; j < block.lines.length; j++) { |
| 91 | + const ruleLine = block.lines[j].trim(); |
| 92 | + if (ruleLine === "" || /^Output created/.test(ruleLine)) break; |
| 93 | + const errType = `${standard}: ${ruleLine}`; |
| 94 | + seenErrors.add(errType); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + context.push(line); |
| 99 | + } |
| 100 | + |
| 101 | + // Trim trailing blank lines |
| 102 | + while (context.length > 0 && context[context.length - 1].trim() === "") { |
| 103 | + context.pop(); |
| 104 | + } |
| 105 | + |
| 106 | + return { seenErrors, context }; |
| 107 | +} |
| 108 | + |
| 109 | +async function main() { |
| 110 | + const path = Deno.args[0]; |
| 111 | + let text: string; |
| 112 | + if (path) { |
| 113 | + text = await Deno.readTextFile(path); |
| 114 | + } else { |
| 115 | + const buf = await new Response(Deno.stdin.readable).text(); |
| 116 | + text = buf; |
| 117 | + } |
| 118 | + |
| 119 | + const rawLines = text.split("\n"); |
| 120 | + const lines = rawLines.map((l) => unescapeHtml(stripAnsi(l))); |
| 121 | + |
| 122 | + // Parse into render blocks. Each block starts with either a |
| 123 | + // "Rendering <path>.qmd" line or a "pandoc" header. |
| 124 | + const blocks: RenderBlock[] = []; |
| 125 | + let current: RenderBlock | null = null; |
| 126 | + let pendingInputFile = ""; |
| 127 | + |
| 128 | + for (const line of lines) { |
| 129 | + // "Rendering docs/smoke-all/.../foo.qmd" precedes the pandoc block |
| 130 | + const renderMatch = line.match(/^Rendering\s+(\S+\.qmd)\s*$/); |
| 131 | + if (renderMatch) { |
| 132 | + pendingInputFile = renderMatch[1]; |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + if (/^pandoc\s*$/.test(line.trim())) { |
| 137 | + if (current) blocks.push(current); |
| 138 | + current = { inputFile: pendingInputFile, outputFile: "", lines: [] }; |
| 139 | + pendingInputFile = ""; |
| 140 | + current.lines.push(line); |
| 141 | + continue; |
| 142 | + } |
| 143 | + if (current) { |
| 144 | + current.lines.push(line); |
| 145 | + const m = line.match(/^\s*output-file:\s*(.+)/); |
| 146 | + if (m) { |
| 147 | + current.outputFile = m[1].trim(); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + if (current) blocks.push(current); |
| 152 | + |
| 153 | + // Extract errors from each block |
| 154 | + const errors: ErrorEntry[] = []; |
| 155 | + const errorCounts = new Map<string, number>(); |
| 156 | + const errorFiles = new Map<string, string[]>(); |
| 157 | + |
| 158 | + for (const block of blocks) { |
| 159 | + // Check for either error format |
| 160 | + const hasError = block.lines.some( |
| 161 | + (l) => |
| 162 | + l.includes("error: PDF/") || |
| 163 | + l.includes("ERROR: error: PDF/") || |
| 164 | + l.includes("PDF validation failed"), |
| 165 | + ); |
| 166 | + if (!hasError) continue; |
| 167 | + |
| 168 | + const { seenErrors, context } = extractErrors(block); |
| 169 | + const displayFile = block.inputFile || block.outputFile; |
| 170 | + |
| 171 | + for (const errType of seenErrors) { |
| 172 | + errorCounts.set(errType, (errorCounts.get(errType) || 0) + 1); |
| 173 | + const files = errorFiles.get(errType) || []; |
| 174 | + if (!files.includes(displayFile)) { |
| 175 | + files.push(displayFile); |
| 176 | + } |
| 177 | + errorFiles.set(errType, files); |
| 178 | + |
| 179 | + errors.push({ |
| 180 | + file: displayFile, |
| 181 | + errorType: errType, |
| 182 | + context, |
| 183 | + }); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // Print per-file errors |
| 188 | + const printedFiles = new Set<string>(); |
| 189 | + for (const err of errors) { |
| 190 | + if (printedFiles.has(err.file)) continue; |
| 191 | + printedFiles.add(err.file); |
| 192 | + |
| 193 | + const fileErrors = errors.filter((e) => e.file === err.file); |
| 194 | + const types = [...new Set(fileErrors.map((e) => e.errorType))]; |
| 195 | + |
| 196 | + console.log("─".repeat(72)); |
| 197 | + console.log(`FILE: ${err.file}`); |
| 198 | + console.log(`ERRORS: ${types.join(", ")}`); |
| 199 | + console.log(""); |
| 200 | + for (const line of err.context) { |
| 201 | + console.log(" " + line); |
| 202 | + } |
| 203 | + console.log(""); |
| 204 | + } |
| 205 | + |
| 206 | + // Print summary |
| 207 | + console.log("═".repeat(72)); |
| 208 | + console.log("SUMMARY"); |
| 209 | + console.log("═".repeat(72)); |
| 210 | + console.log(""); |
| 211 | + console.log(`Total files with errors: ${printedFiles.size}`); |
| 212 | + console.log(`Total files rendered: ${blocks.length}`); |
| 213 | + console.log(""); |
| 214 | + |
| 215 | + // Sort by count descending |
| 216 | + const sorted = [...errorCounts.entries()].sort((a, b) => b[1] - a[1]); |
| 217 | + for (const [errType, count] of sorted) { |
| 218 | + console.log(` ${count.toString().padStart(4)} ${errType}`); |
| 219 | + } |
| 220 | + console.log(""); |
| 221 | + |
| 222 | + // List files per error type |
| 223 | + for (const [errType] of sorted) { |
| 224 | + const files = errorFiles.get(errType) || []; |
| 225 | + console.log(`${errType} (${files.length} files):`); |
| 226 | + for (const f of files) { |
| 227 | + console.log(` - ${f}`); |
| 228 | + } |
| 229 | + console.log(""); |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +main(); |
0 commit comments