40 lines
1.7 KiB
JavaScript
40 lines
1.7 KiB
JavaScript
|
|
/* 详列 scan-escapes 命中的上下文,用于人工判定是否真损坏
|
|||
|
|
用法:node .sre-s23/scan-detail.js <dir> [<dir> ...]
|
|||
|
|
*/
|
|||
|
|
const fs = require("fs");
|
|||
|
|
const path = require("path");
|
|||
|
|
const DIR = path.resolve(__dirname, "content");
|
|||
|
|
|
|||
|
|
const NAMED = { n: "换行", t: "TAB", r: "回车", b: "退格", f: "换页", v: "垂直制表", "0": "NUL" };
|
|||
|
|
const DEGRADE = "+.*?$^[](){}|/dswSDW";
|
|||
|
|
|
|||
|
|
for (const name of process.argv.slice(2)) {
|
|||
|
|
const p = path.join(DIR, name + ".js");
|
|||
|
|
if (!fs.existsSync(p)) { console.log(`… ${name} 不存在`); continue; }
|
|||
|
|
const src = fs.readFileSync(p, "utf8");
|
|||
|
|
const tick = src.indexOf("`");
|
|||
|
|
const body = tick < 0 ? "" : src.slice(tick);
|
|||
|
|
const lines = body.split("\n");
|
|||
|
|
|
|||
|
|
const out = [];
|
|||
|
|
/* 逐行找,并记录是否处于 <pre>/<code> 区域内 */
|
|||
|
|
let inPre = false;
|
|||
|
|
lines.forEach((ln, i) => {
|
|||
|
|
const opens = (ln.match(/<pre[\s>]/g) || []).length;
|
|||
|
|
const closes = (ln.match(/<\/pre>/g) || []).length;
|
|||
|
|
const wasIn = inPre;
|
|||
|
|
if (opens) inPre = true;
|
|||
|
|
const here = wasIn || inPre;
|
|||
|
|
if (closes) inPre = false;
|
|||
|
|
if (!here) return;
|
|||
|
|
for (const m of ln.matchAll(/(?<!\\)\\(.)/g)) {
|
|||
|
|
const ch = m[1];
|
|||
|
|
if (ch === "\\" || ch === '"' || ch === "`" || ch === "$" ) continue;
|
|||
|
|
const s = Math.max(0, m.index - 45), e = Math.min(ln.length, m.index + 45);
|
|||
|
|
out.push(` L${i + 1} \\${ch}(${NAMED[ch] || (DEGRADE.includes(ch) ? "丢反斜杠" : "?")}) …${ln.slice(s, e).trim()}…`);
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
if (out.length) { console.log(`\n===== ${name}(${out.length} 处,含正文区) =====`); out.slice(0, 60).forEach(l => console.log(l)); if (out.length > 60) console.log(` … 另 ${out.length - 60} 处`); }
|
|||
|
|
else console.log(`\n✓ ${name} 无命中`);
|
|||
|
|
}
|