Files
materials/knowledge/linux/tools/check-svg.js
huanghaosheng 2354b3d704 docs(linux): 17 章按编写规范完成 P0–P4 改造
- 新增 Linux 专属规范 tools/SPEC.md v1:分档指标(原理 3.5 / 操作 4.5 / 速查不设),
  另加发行版锚定、输出保真、危险命令、跨章一致性四条事实纪律,附整章 HTML 模板
- 新增 tools/check-metrics.js、metrics.config.json、check-svg.js:把密度、take、
  章骨架、图、易错点、<mark> 从"人工约定"变成可执行红线;assets/doc.css 补
  .tag/.figure/.figcaption/p.take 样式
- P1:17 章补齐章骨架(30 秒直答 / 术语速查 / 怎么读 / chN-sum + 症状表),
  126 节各补 1 条 take(机制节写结论复述,排障节写判据句)
- P2:密度均值 5.03 → 3.63,参数枚举改为"命令 / 看什么 / 典型误读 / 给错了会怎样"
  四列表且未删任何命令;"段落含 ≥5 个 code" 232 处 → 4 处;超长句 130 → 4 章各 1–2 句
- P3:新增 19 张内联 SVG(图号"章号-序号",几何由 check-svg 校验)、45 处四要素
  易错点、30 处可跳过;95 处 <mark> 清零
- P4:修正 49 条会误导读者的技术错误与 8 条跨章矛盾(容器 kill 1、ext4 缩容、
  -maxdepth 位置效应、ss -tnp 语义、tcp_tw_recycle 已移除、/dev/sda2 容量、
  /data 归属、systemd cgroup 版本)
- 新增 linux-文档审查报告.md:量化体检、7 类系统性问题、分章问题清单与全过程记录
2026-10-27 22:05:47 +08:00

150 lines
7.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/* ==========================================================================
Linux 专题 · SVG 图自检(SPEC v1 §8)
用法:node tools/check-svg.js # 全部章节
node tools/check-svg.js 06-proc.html # 指定章节
检查项:
- 结构:<svg> 必须有 viewBox、role="img"、aria-label;不得写 width/height
- id:marker id 形如 arNNx(章号 + 序号),同页不得重复;marker-end 必须能解析
- 配色:fill/stroke 只能用 CSS 变量,禁止写死颜色(暗色主题会看不见)
- 几何:rect / text 不得越出 viewBox;文字估算宽度(CJK 1em、ASCII 0.55em)不得越界
- 图注:每个 <figure class="figure"> 必须紧跟一个 .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(/<article>([\s\S]*?)<\/article>/) || [, ""])[1];
if (!art) continue;
const figures = [...art.matchAll(/<figure class="figure">([\s\S]*?)<\/figure>/g)];
const svgs = [...art.matchAll(/<svg\b([^>]*)>([\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(/<div class="figcap">([\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} <svg> 缺 viewBox`); continue; }
if (/\bwidth\s*=|\bheight\s*=/.test(svg[1])) errors.push(`${f}:${at} <svg> 不应写 width/height`);
if (attr(svg[1], "role") !== "img") errors.push(`${f}:${at} <svg> 缺 role="img"`);
if (!attr(svg[1], "aria-label")) errors.push(`${f}:${at} <svg> 缺 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(/<marker\b[^>]*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(/<rect\b([^>]*)\/?>/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(/<rect\b([^>]*)\/?>/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(/<text\b([^>]*)>([\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);