#!/usr/bin/env node /* ========================================================================== Linux 专题 · SVG 图自检(SPEC v1 §8) 用法:node tools/check-svg.js # 全部章节 node tools/check-svg.js 06-proc.html # 指定章节 检查项: - 结构: 必须有 viewBox、role="img"、aria-label;不得写 width/height - id:marker id 形如 arNNx(章号 + 序号),同页不得重复;marker-end 必须能解析 - 配色:fill/stroke 只能用 CSS 变量,禁止写死颜色(暗色主题会看不见) - 几何:rect / text 不得越出 viewBox;文字估算宽度(CJK 1em、ASCII 0.55em)不得越界 - 图注:每个
必须紧跟一个 .figcap,且以「图 N」开头 ========================================================================== */ const fs = require("fs"); const path = require("path"); const root = path.resolve(__dirname, ".."); const args = process.argv.slice(2).filter(a => !a.startsWith("--")); const files = args.length ? args : fs.readdirSync(root).filter(f => /^\d\d-.*\.html$/.test(f)).sort(); const errors = []; const hints = []; const lineOf = (s, i) => s.slice(0, i).split("\n").length; /* 估算文字宽度:CJK 与全角标点按 1em,其余按 0.55em */ function textWidth(t, fs_) { let w = 0; for (const ch of t) { const c = ch.codePointAt(0); const wide = (c >= 0x4e00 && c <= 0x9fff) || (c >= 0x3000 && c <= 0x303f) || (c >= 0xff00 && c <= 0xffef) || (c >= 0x2018 && c <= 0x201d); w += wide ? fs_ : fs_ * 0.55; } return w; } const attr = (s, n) => { const m = s.match(new RegExp(n + '\\s*=\\s*"([^"]*)"')); return m ? m[1] : null; }; for (const f of files) { const html = fs.readFileSync(path.join(root, f), "utf8"); const art = (html.match(/
([\s\S]*?)<\/article>/) || [, ""])[1]; if (!art) continue; const figures = [...art.matchAll(/
([\s\S]*?)<\/figure>/g)]; const svgs = [...art.matchAll(/]*)>([\s\S]*?)<\/svg>/g)]; if (!figures.length && !svgs.length) continue; let figNo = 0; const chapNo = f.slice(0, 2); for (const fig of figures) { figNo++; const cap = fig[1].match(/
([\s\S]*?)<\/div>/); if (!cap) { errors.push(`${f}:${lineOf(art, fig.index)} 第 ${figNo} 个 figure 缺 .figcap`); } else { const t = cap[1].replace(/<[^>]+>/g, "").trim(); const m = t.match(/^图\s*(\d+)-(\d+)/); if (!m) errors.push(`${f}:${lineOf(art, fig.index)} 图注未以「图 N-M」开头(章号-序号):${t.slice(0, 20)}`); else { if (m[1] !== String(Number(chapNo))) errors.push(`${f}:${lineOf(art, fig.index)} 图注章号 ${m[1]} 与文件名第 ${chapNo} 章不符`); if (Number(m[2]) !== figNo) errors.push(`${f}:${lineOf(art, fig.index)} 图注序号 ${m[2]} 与本章第 ${figNo} 张图不一致`); const sentences = t.replace(/^图\s*\d+-\d+\s*·?\s*/, "").split(/[。!?]/).filter(x => x.trim()); if (sentences.length < 2) hints.push(`${f}:${lineOf(art, fig.index)} 图注只有一句,SPEC 要求两句(答什么 + 先看哪)`); } } } const markerIds = new Map(); for (const svg of svgs) { const head = svg[1] + " " + svg[2]; const at = lineOf(art, svg.index); const vb = attr(svg[1], "viewBox"); if (!vb) { errors.push(`${f}:${at} 缺 viewBox`); continue; } if (/\bwidth\s*=|\bheight\s*=/.test(svg[1])) errors.push(`${f}:${at} 不应写 width/height`); if (attr(svg[1], "role") !== "img") errors.push(`${f}:${at} 缺 role="img"`); if (!attr(svg[1], "aria-label")) errors.push(`${f}:${at} 缺 aria-label`); const [, , W, H] = vb.trim().split(/\s+/).map(Number); if (!W || !H) { errors.push(`${f}:${at} viewBox 无法解析:${vb}`); continue; } /* marker */ for (const m of svg[2].matchAll(/]*id="([^"]+)"/g)) { if (markerIds.has(m[1])) errors.push(`${f}:${at} marker id 重复:${m[1]}`); markerIds.set(m[1], at); if (!/^ar\d\d[a-z]$/.test(m[1])) errors.push(`${f}:${at} marker id 不符合 arNNx:${m[1]}`); } for (const m of svg[2].matchAll(/marker-end="url\(#([^)]+)\)"/g)) { if (!markerIds.has(m[1])) errors.push(`${f}:${at} marker-end 指向不存在的 id:${m[1]}`); } /* 颜色 */ for (const m of svg[2].matchAll(/(?:fill|stroke)\s*:\s*([^;"]+)/g)) { const v = m[1].trim(); if (/^(none|currentColor|transparent)$/.test(v)) continue; if (!/var\(--[a-z-]+\)/.test(v)) errors.push(`${f}:${at} 颜色未使用 CSS 变量:${v}`); } /* rect 边界 */ for (const m of svg[2].matchAll(/]*)\/?>/g)) { const x = Number(attr(m[1], "x") || 0), y = Number(attr(m[1], "y") || 0); const w = Number(attr(m[1], "width") || 0), h = Number(attr(m[1], "height") || 0); if (x < 0 || y < 0 || x + w > W + 1 || y + h > H + 1) { errors.push(`${f}:${at} rect 越出 viewBox:x=${x} y=${y} w=${w} h=${h}(画布 ${W}×${H})`); } } /* 文字边界 */ const rects = [...svg[2].matchAll(/]*)\/?>/g)].map(m => ({ x: Number(attr(m[1], "x") || 0), y: Number(attr(m[1], "y") || 0), w: Number(attr(m[1], "width") || 0), h: Number(attr(m[1], "height") || 0) })); for (const m of svg[2].matchAll(/]*)>([\s\S]*?)<\/text>/g)) { const x = Number(attr(m[1], "x") || 0), y = Number(attr(m[1], "y") || 0); const fsz = Number(attr(m[1], "font-size") || 12); const anchor = attr(m[1], "text-anchor") || "start"; const t = m[2].replace(/<[^>]+>/g, "").replace(/&[a-z]+;/gi, "x").trim(); if (!t) continue; const w = textWidth(t, fsz); const left = anchor === "middle" ? x - w / 2 : anchor === "end" ? x - w : x; const right = left + w; if (left < -1 || right > W + 1) errors.push(`${f}:${at} 文字越界(估算宽 ${Math.round(w)},右边界 ${Math.round(right)} > ${W}):「${t.slice(0, 18)}」`); if (y < -1 || y > H + 1) errors.push(`${f}:${at} 文字 y 越界:${y}(画布高 ${H})`); /* 落在某个 rect 里时,估算框不应明显超出该 rect */ const box = rects.find(r => x >= r.x && x <= r.x + r.w && y >= r.y && y <= r.y + r.h); if (box) { const pad = 6; if (left < box.x + pad - 2 || right > box.x + box.w - pad + 2) { hints.push(`${f}:${at} 文字可能压出方框:「${t.slice(0, 18)}」(框宽 ${box.w},估算 ${Math.round(w)})`); } } } } } if (!errors.length && !hints.length) { console.log(`OK SVG 全部合规(${files.length} 个文件)`); process.exit(0); } const byFile = new Map(); for (const [level, list] of [["error", errors], ["warn", hints]]) { for (const m of list) { const key = m.slice(0, m.indexOf(":")); if (!byFile.has(key)) byFile.set(key, []); byFile.get(key).push({ level, m }); } } for (const [f, list] of byFile) { console.log(`\n${f}`); for (const p of list) console.log(` ${p.level === "error" ? "✗" : "!"} ${p.m}`); } console.log(`\n结论:error ${errors.length} 项 · 提示 ${hints.length} 项 · ${errors.length ? "未通过" : "通过"}`); if (errors.length) process.exit(1);