book-text-util 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/cli.js +204 -33
- package/dist/index.cjs +180 -39
- package/dist/index.d.cts +72 -10
- package/dist/index.d.ts +72 -10
- package/dist/index.js +174 -36
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -9,9 +9,9 @@ var __export = (target, all) => {
|
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let
|
|
13
|
-
if (!__hasOwnProp.call(to,
|
|
14
|
-
__defProp(to,
|
|
12
|
+
for (let key2 of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key2) && key2 !== except)
|
|
14
|
+
__defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
@@ -31,6 +31,7 @@ __export(index_exports, {
|
|
|
31
31
|
availableFormats: () => availableFormats,
|
|
32
32
|
buildExport: () => buildExport,
|
|
33
33
|
buildReport: () => buildReport,
|
|
34
|
+
buildVariantTable: () => buildVariantTable,
|
|
34
35
|
buildVariantTest: () => buildVariantTest,
|
|
35
36
|
cellLane: () => cellLane,
|
|
36
37
|
chapterChars: () => chapterChars,
|
|
@@ -49,6 +50,7 @@ __export(index_exports, {
|
|
|
49
50
|
figAlts: () => figAlts,
|
|
50
51
|
formatCmp: () => formatCmp,
|
|
51
52
|
frontMatter: () => frontMatter,
|
|
53
|
+
isRareHan: () => isRareHan,
|
|
52
54
|
loadVariantTest: () => loadVariantTest,
|
|
53
55
|
nextContinues: () => nextContinues,
|
|
54
56
|
normalizeVariants: () => normalizeVariants,
|
|
@@ -61,6 +63,7 @@ __export(index_exports, {
|
|
|
61
63
|
splitVolumes: () => splitVolumes,
|
|
62
64
|
trimWikiToBase: () => trimWikiToBase,
|
|
63
65
|
volumeNo: () => volumeNo,
|
|
66
|
+
volumeRangeOf: () => volumeRangeOf,
|
|
64
67
|
wikiChars: () => wikiChars,
|
|
65
68
|
wikiFileOf: () => wikiFileOf
|
|
66
69
|
});
|
|
@@ -228,9 +231,9 @@ function cmpKey(a, b) {
|
|
|
228
231
|
for (let i = 1; i < 5; i++) if (a[i] !== b[i]) return a[i] ? 1 : -1;
|
|
229
232
|
return 0;
|
|
230
233
|
}
|
|
231
|
-
function resolveOffsets(pos, items,
|
|
234
|
+
function resolveOffsets(pos, items, key2) {
|
|
232
235
|
return items.map((it, i) => {
|
|
233
|
-
const a =
|
|
236
|
+
const a = key2(it);
|
|
234
237
|
const off = a === void 0 ? void 0 : pos.get(a);
|
|
235
238
|
if (off === void 0) throw new GujiFormatError(`\u7B2C ${i} \u6761\u7684\u683C\u4F4D ${String(a)} \u4E0D\u5728 char \u91CC`);
|
|
236
239
|
return off;
|
|
@@ -373,21 +376,21 @@ function availableFormats(ch) {
|
|
|
373
376
|
function safeFile(v) {
|
|
374
377
|
return typeof v === "string" && /^[\w.-]+$/.test(v) ? v : null;
|
|
375
378
|
}
|
|
376
|
-
async function getJson(source, id,
|
|
379
|
+
async function getJson(source, id, key2, file) {
|
|
377
380
|
try {
|
|
378
|
-
return await source.getTextFile(id,
|
|
381
|
+
return await source.getTextFile(id, key2, file) ?? null;
|
|
379
382
|
} catch {
|
|
380
383
|
return null;
|
|
381
384
|
}
|
|
382
385
|
}
|
|
383
|
-
async function getPreferred(source, id,
|
|
386
|
+
async function getPreferred(source, id, key2, slim, original) {
|
|
384
387
|
const s = safeFile(slim);
|
|
385
388
|
if (s) {
|
|
386
|
-
const v = await getJson(source, id,
|
|
389
|
+
const v = await getJson(source, id, key2, s);
|
|
387
390
|
if (v != null) return v;
|
|
388
391
|
}
|
|
389
392
|
const o = safeFile(original);
|
|
390
|
-
return o ? getJson(source, id,
|
|
393
|
+
return o ? getJson(source, id, key2, o) : null;
|
|
391
394
|
}
|
|
392
395
|
function glyphConverter(glyph2, converters) {
|
|
393
396
|
if (glyph2 === "original") return (s) => s;
|
|
@@ -396,14 +399,14 @@ function glyphConverter(glyph2, converters) {
|
|
|
396
399
|
}
|
|
397
400
|
async function buildExport(req, source, converters) {
|
|
398
401
|
const ch = req.chapter;
|
|
399
|
-
const { id, versionKey:
|
|
402
|
+
const { id, versionKey: key2 } = req;
|
|
400
403
|
const needPunct = req.format !== "lines";
|
|
401
404
|
const [char, punct, entity, fig] = await Promise.all([
|
|
402
|
-
getPreferred(source, id,
|
|
403
|
-
needPunct ? getPreferred(source, id,
|
|
405
|
+
getPreferred(source, id, key2, ch.reader_char_file, ch.char_file),
|
|
406
|
+
needPunct ? getPreferred(source, id, key2, ch.reader_punct_file, ch.punct_file) : null,
|
|
404
407
|
// 专名层取原文件:瘦身副本有 entity_id 时去掉了 href,而下载要原样写仓里的 book-index:// 链接
|
|
405
|
-
needPunct && safeFile(ch.entity_file) ? getJson(source, id,
|
|
406
|
-
safeFile(ch.fig_file) ? getJson(source, id,
|
|
408
|
+
needPunct && safeFile(ch.entity_file) ? getJson(source, id, key2, ch.entity_file) : null,
|
|
409
|
+
safeFile(ch.fig_file) ? getJson(source, id, key2, ch.fig_file) : null
|
|
407
410
|
]);
|
|
408
411
|
if (!char) throw new Error("\u672C\u518A\u6587\u5B57\u53D6\u4E0D\u5230");
|
|
409
412
|
if (needPunct && !punct) throw new Error("\u672C\u518A\u6A19\u9EDE\u53D6\u4E0D\u5230");
|
|
@@ -505,9 +508,15 @@ function createGlyphConverters(opts) {
|
|
|
505
508
|
|
|
506
509
|
// src/compare/classify.ts
|
|
507
510
|
var ALL_CATS = ["word", "add", "omit", "glyph", "struct"];
|
|
511
|
+
var NUMERAL = /^[一二三四五六七八九十百]+$/;
|
|
512
|
+
var PLACEHOLDER = /無法顯示|无法显示/;
|
|
513
|
+
var NUM_CTX = /* @__PURE__ */ new Set([..."\u5377\u7B2C\u5E74\u6708\u65E5\u6B72\u5C81\u4E4B\u5341\u767E\u5343\u842C\u4E07\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D"]);
|
|
508
514
|
function classify(h, base, wiki, opts) {
|
|
509
515
|
if (h.i1 === h.i2) return "add";
|
|
510
516
|
if (h.j1 === h.j2) return opts.structEnd !== void 0 && h.i2 <= opts.structEnd ? "struct" : "omit";
|
|
517
|
+
const b = base.slice(h.i1, h.i2).join("");
|
|
518
|
+
const r = wiki.slice(h.j1, h.j2).join("");
|
|
519
|
+
if (b === "\u4E00" && r !== b && NUMERAL.test(r) && !NUM_CTX.has(base[h.i1 - 1] ?? "") || PLACEHOLDER.test(r)) return "struct";
|
|
511
520
|
const len = h.i2 - h.i1;
|
|
512
521
|
if (len === h.j2 - h.j1 && opts.isVariant) {
|
|
513
522
|
let all = true;
|
|
@@ -682,7 +691,17 @@ function volumeNo(label) {
|
|
|
682
691
|
}
|
|
683
692
|
function wikiFileOf(no, chapters) {
|
|
684
693
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
685
|
-
|
|
694
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
695
|
+
if (exact) return exact.file;
|
|
696
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
697
|
+
}
|
|
698
|
+
function volumeRangeOf(title) {
|
|
699
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
700
|
+
if (!m) return null;
|
|
701
|
+
const a = cnNumber(m[1]);
|
|
702
|
+
const b = m[2] ? cnNumber(m[2]) : a;
|
|
703
|
+
if (a === null || b === null || b < a || b - a > 200) return null;
|
|
704
|
+
return [a, b];
|
|
686
705
|
}
|
|
687
706
|
var anchorKey = (a) => a;
|
|
688
707
|
function splitVolumes(cells, headings, prevVolNo, lastContinues) {
|
|
@@ -720,15 +739,16 @@ function nextContinues(nextHeadings) {
|
|
|
720
739
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
721
740
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
722
741
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
742
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
743
|
+
var NOTE_MARK = /[<>|]/g;
|
|
723
744
|
function wikiChars(md) {
|
|
724
745
|
const lines = md.split("\n").map((s) => s.trim()).filter((s) => s && !VOLUME_LINE.test(s) && !STATUS_LINE.test(s)).map((s) => s.replace(/^#+\s*/, ""));
|
|
725
|
-
return Array.from(lines.join("").replace(STRIP, ""));
|
|
746
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
726
747
|
}
|
|
727
|
-
var NOTE_MARK = /[<>|]/g;
|
|
728
748
|
function chapterChars(md) {
|
|
729
749
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
730
750
|
if (lines.length && /^#{1,2}\s/.test(lines[0])) lines.shift();
|
|
731
|
-
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
751
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
732
752
|
}
|
|
733
753
|
|
|
734
754
|
// src/compare/compare.ts
|
|
@@ -771,16 +791,34 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
771
791
|
}
|
|
772
792
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
773
793
|
}
|
|
774
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
794
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
775
795
|
const res = { no: seg.no, label: seg.label, from: seg.from, to: seg.to, status: "ok", wikiFile: null };
|
|
776
|
-
|
|
777
|
-
if (
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
if (
|
|
796
|
+
let wiki;
|
|
797
|
+
if (whole) {
|
|
798
|
+
res.wikiFile = whole.file;
|
|
799
|
+
wiki = chapterChars(whole.md);
|
|
800
|
+
} else if (range) {
|
|
801
|
+
const files = [];
|
|
802
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
803
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
804
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
805
|
+
files.push(f);
|
|
806
|
+
}
|
|
807
|
+
res.wikiFile = files[0];
|
|
808
|
+
res.wikiFiles = files;
|
|
809
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
810
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
811
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
812
|
+
} else {
|
|
813
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
814
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
815
|
+
res.wikiFile = file;
|
|
816
|
+
const md = await input.getWikiMd(file);
|
|
817
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
818
|
+
wiki = wikiChars(md);
|
|
819
|
+
}
|
|
781
820
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
782
|
-
|
|
783
|
-
if (!whole) {
|
|
821
|
+
if (!whole && !range) {
|
|
784
822
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
785
823
|
wiki = wiki.slice(j0, j1);
|
|
786
824
|
}
|
|
@@ -817,6 +855,15 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
817
855
|
return res;
|
|
818
856
|
}
|
|
819
857
|
async function compareChapter(input) {
|
|
858
|
+
const range = input.volumeRange;
|
|
859
|
+
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
860
|
+
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
861
|
+
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
862
|
+
const items2 = [];
|
|
863
|
+
const res = await compareSeg(input, seg, items2, 0, void 0, range);
|
|
864
|
+
items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
|
|
865
|
+
return { items: items2, segments: [res] };
|
|
866
|
+
}
|
|
820
867
|
const segs = splitVolumes(input.cells, input.headings, input.prevVolNo, input.lastContinues);
|
|
821
868
|
const items = [];
|
|
822
869
|
const segments = [];
|
|
@@ -834,14 +881,18 @@ async function compareWhole(input) {
|
|
|
834
881
|
}
|
|
835
882
|
|
|
836
883
|
// src/compare/variants.ts
|
|
837
|
-
function buildVariantTest(table) {
|
|
884
|
+
function buildVariantTest(table, opts = {}) {
|
|
838
885
|
const set = /* @__PURE__ */ new Set();
|
|
839
|
-
|
|
840
|
-
const
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
886
|
+
const add = (text) => {
|
|
887
|
+
for (const line of text.split("\n")) {
|
|
888
|
+
const cp = Array.from(line);
|
|
889
|
+
if (cp.length !== 2) continue;
|
|
890
|
+
set.add(cp[0] + cp[1]);
|
|
891
|
+
set.add(cp[1] + cp[0]);
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
add(table.pairs);
|
|
895
|
+
if (table.simp && opts.simp !== false) add(table.simp);
|
|
845
896
|
return (a, b) => set.has(a + b);
|
|
846
897
|
}
|
|
847
898
|
var loading = null;
|
|
@@ -862,7 +913,9 @@ async function resolveTargetKey(transport, target) {
|
|
|
862
913
|
const m = await transport.getTextManifest?.(target.id);
|
|
863
914
|
const vs = m?.versions;
|
|
864
915
|
if (!Array.isArray(vs) || vs.length === 0) return null;
|
|
865
|
-
|
|
916
|
+
const main = vs.find((v) => v.key === "default") ?? vs[0];
|
|
917
|
+
if (main.kind !== "collated") return main.key;
|
|
918
|
+
return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
|
|
866
919
|
}
|
|
867
920
|
async function runCompare(input) {
|
|
868
921
|
const { base, target, cells } = input;
|
|
@@ -906,7 +959,10 @@ async function runCompare(input) {
|
|
|
906
959
|
lastContinues: nextContinues(at >= 0 && at + 1 < chapters.length ? headingsOf(chapters[at + 1]) : void 0),
|
|
907
960
|
wikiChapters,
|
|
908
961
|
getWikiMd,
|
|
909
|
-
isVariant
|
|
962
|
+
isVariant,
|
|
963
|
+
// 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
|
|
964
|
+
volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
|
|
965
|
+
volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
|
|
910
966
|
});
|
|
911
967
|
return { ...result, targetKey, wikiChapters };
|
|
912
968
|
}
|
|
@@ -919,10 +975,10 @@ function parseCmp(raw) {
|
|
|
919
975
|
const first = raw.split(",")[0].trim();
|
|
920
976
|
const dash = first.indexOf("-");
|
|
921
977
|
const id = dash < 0 ? first : first.slice(0, dash);
|
|
922
|
-
const
|
|
978
|
+
const key2 = dash < 0 ? void 0 : first.slice(dash + 1);
|
|
923
979
|
if (!ID_RE.test(id)) return null;
|
|
924
|
-
if (
|
|
925
|
-
return
|
|
980
|
+
if (key2 !== void 0 && !KEY_RE.test(key2)) return null;
|
|
981
|
+
return key2 ? { id, key: key2 } : { id };
|
|
926
982
|
}
|
|
927
983
|
function formatCmp(t) {
|
|
928
984
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
@@ -999,6 +1055,88 @@ function parseChapters(spec) {
|
|
|
999
1055
|
}
|
|
1000
1056
|
return out;
|
|
1001
1057
|
}
|
|
1058
|
+
|
|
1059
|
+
// src/compare/variant-build.ts
|
|
1060
|
+
var VARIANT_REL = /* @__PURE__ */ new Set(["variant", "variant_partial", "simp", "z"]);
|
|
1061
|
+
var isOpen = (src) => src.startsWith("unihan") || src.startsWith("local:keben") || src.startsWith("yibai");
|
|
1062
|
+
var TRUSTED_EXTRA = /* @__PURE__ */ new Set(["yitizi", "twedu", "dypytz", "hydzd", "cjkvi-simplified"]);
|
|
1063
|
+
var isTrusted = (src) => isOpen(src) || TRUSTED_EXTRA.has(src);
|
|
1064
|
+
var isStrong = (rel, src) => rel === "z" || src === "unihan:kSemanticVariant" || src === "unihan:kZVariant" || src === "twedu";
|
|
1065
|
+
function isRareHan(ch) {
|
|
1066
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
1067
|
+
return cp > 65535 || cp >= 13312 && cp <= 19903;
|
|
1068
|
+
}
|
|
1069
|
+
var key = (a, b) => a < b ? a + b : b + a;
|
|
1070
|
+
function buildVariantTable(edgesTsv, opts = {}) {
|
|
1071
|
+
const mode = opts.mode ?? "collate";
|
|
1072
|
+
const pickSrc = mode === "open" ? isOpen : isTrusted;
|
|
1073
|
+
const yes = /* @__PURE__ */ new Set();
|
|
1074
|
+
const strong = /* @__PURE__ */ new Set();
|
|
1075
|
+
const loose = /* @__PURE__ */ new Set();
|
|
1076
|
+
const no = /* @__PURE__ */ new Set();
|
|
1077
|
+
const borrow = /* @__PURE__ */ new Set();
|
|
1078
|
+
const used = /* @__PURE__ */ new Set();
|
|
1079
|
+
const chars = /* @__PURE__ */ new Set();
|
|
1080
|
+
for (const line of edgesTsv.split("\n")) {
|
|
1081
|
+
if (!line) continue;
|
|
1082
|
+
const [a, b, rel, , src = ""] = line.split(" ");
|
|
1083
|
+
if (!a || !b || a === b || [...a].length !== 1 || [...b].length !== 1) continue;
|
|
1084
|
+
const k = key(a, b);
|
|
1085
|
+
chars.add(a);
|
|
1086
|
+
chars.add(b);
|
|
1087
|
+
if (rel === "distinct") no.add(k);
|
|
1088
|
+
else if (rel === "borrow") borrow.add(k);
|
|
1089
|
+
else if (VARIANT_REL.has(rel)) {
|
|
1090
|
+
if (pickSrc(src)) {
|
|
1091
|
+
yes.add(k);
|
|
1092
|
+
used.add(src);
|
|
1093
|
+
}
|
|
1094
|
+
if (isStrong(rel, src)) strong.add(k);
|
|
1095
|
+
if (rel !== "simp") loose.add(k);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
const out = /* @__PURE__ */ new Set();
|
|
1099
|
+
for (const k of yes) if (!no.has(k) && !borrow.has(k)) out.add(k);
|
|
1100
|
+
let simp;
|
|
1101
|
+
if (mode === "collate") {
|
|
1102
|
+
for (const k of strong) if (!no.has(k)) out.add(k);
|
|
1103
|
+
for (const k of loose) {
|
|
1104
|
+
if (no.has(k)) continue;
|
|
1105
|
+
const [a, b] = [...k];
|
|
1106
|
+
if (isRareHan(a) || isRareHan(b)) out.add(k);
|
|
1107
|
+
}
|
|
1108
|
+
if (opts.toSimplified) {
|
|
1109
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1110
|
+
for (let cp = 19968; cp <= 40959; cp++) chars.add(String.fromCodePoint(cp));
|
|
1111
|
+
for (const c of chars) {
|
|
1112
|
+
const s = opts.toSimplified(c);
|
|
1113
|
+
if ([...s].length !== 1) continue;
|
|
1114
|
+
const g = groups.get(s) ?? [];
|
|
1115
|
+
g.push(c);
|
|
1116
|
+
groups.set(s, g);
|
|
1117
|
+
}
|
|
1118
|
+
const sp = /* @__PURE__ */ new Set();
|
|
1119
|
+
for (const [s, g] of groups) {
|
|
1120
|
+
const all = g.includes(s) ? g : [...g, s];
|
|
1121
|
+
if (all.length < 2 || all.length > 12) continue;
|
|
1122
|
+
for (let i = 0; i < all.length; i++)
|
|
1123
|
+
for (let j = i + 1; j < all.length; j++) {
|
|
1124
|
+
const k = key(all[i], all[j]);
|
|
1125
|
+
if (!no.has(k) && !out.has(k)) sp.add(k);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
simp = [...sp].sort();
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
const table = {
|
|
1132
|
+
version: opts.version ?? (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
|
|
1133
|
+
mode,
|
|
1134
|
+
sources: [...used].sort(),
|
|
1135
|
+
pairs: [...out].sort().join("\n")
|
|
1136
|
+
};
|
|
1137
|
+
if (simp) table.simp = simp.join("\n");
|
|
1138
|
+
return table;
|
|
1139
|
+
}
|
|
1002
1140
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1003
1141
|
0 && (module.exports = {
|
|
1004
1142
|
ALL_CATS,
|
|
@@ -1012,6 +1150,7 @@ function parseChapters(spec) {
|
|
|
1012
1150
|
availableFormats,
|
|
1013
1151
|
buildExport,
|
|
1014
1152
|
buildReport,
|
|
1153
|
+
buildVariantTable,
|
|
1015
1154
|
buildVariantTest,
|
|
1016
1155
|
cellLane,
|
|
1017
1156
|
chapterChars,
|
|
@@ -1030,6 +1169,7 @@ function parseChapters(spec) {
|
|
|
1030
1169
|
figAlts,
|
|
1031
1170
|
formatCmp,
|
|
1032
1171
|
frontMatter,
|
|
1172
|
+
isRareHan,
|
|
1033
1173
|
loadVariantTest,
|
|
1034
1174
|
nextContinues,
|
|
1035
1175
|
normalizeVariants,
|
|
@@ -1042,6 +1182,7 @@ function parseChapters(spec) {
|
|
|
1042
1182
|
splitVolumes,
|
|
1043
1183
|
trimWikiToBase,
|
|
1044
1184
|
volumeNo,
|
|
1185
|
+
volumeRangeOf,
|
|
1045
1186
|
wikiChars,
|
|
1046
1187
|
wikiFileOf
|
|
1047
1188
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -167,11 +167,6 @@ type DiffCat = 'glyph' | 'word' | 'add' | 'omit' | 'struct';
|
|
|
167
167
|
declare const ALL_CATS: readonly DiffCat[];
|
|
168
168
|
/** 判定两个字是不是异体字关系:由 glyph-variants.json 加载后给出(见 variants.ts);null=表还没载入,按「不是」算 */
|
|
169
169
|
type VariantTest = ((a: string, b: string) => boolean) | null;
|
|
170
|
-
/**
|
|
171
|
-
* 给一处差异定类别。
|
|
172
|
-
* - 纯增:add;纯删:落在「卷端署题区」(structEnd 之前)算 struct,否则 omit;
|
|
173
|
-
* - 等长替换且逐字都是异体:glyph;其余(含通假,按/案)word。
|
|
174
|
-
*/
|
|
175
170
|
declare function classify(h: Hunk, base: readonly string[], wiki: readonly string[], opts: {
|
|
176
171
|
isVariant: VariantTest;
|
|
177
172
|
structEnd?: number;
|
|
@@ -202,8 +197,16 @@ interface WikiChapter {
|
|
|
202
197
|
declare function cnNumber(s: string): number | null;
|
|
203
198
|
/** 一级标题「卷一 經部一」→ 1;不是「卷+数」开头的返回 null */
|
|
204
199
|
declare function volumeNo(label: string): number | null;
|
|
205
|
-
/**
|
|
200
|
+
/**
|
|
201
|
+
* 卷序号 → 对照本章文件;没有返回 null。
|
|
202
|
+
* 认两种章名:维基文庫的「卷001」(数字补零到三位),和 Kanripo 等的「卷七」「卷二十三」(汉字数字,后面可带篇名)。
|
|
203
|
+
*/
|
|
206
204
|
declare function wikiFileOf(no: number, chapters: readonly WikiChapter[]): string | null;
|
|
205
|
+
/**
|
|
206
|
+
* 章名里的卷范围:「卷七至卷十」→ [7, 10],「卷七」→ [7, 7];认不出返回 null。
|
|
207
|
+
* 用于底本册没有一级标题(卷)时,按册名决定取对照本哪几卷(如全唐文自校本一册一章、章名写「卷七至卷十」)。
|
|
208
|
+
*/
|
|
209
|
+
declare function volumeRangeOf(title: string): [number, number] | null;
|
|
207
210
|
interface VolumeSeg {
|
|
208
211
|
/** 卷序号;认不出(卷首、序等)为 null */
|
|
209
212
|
no: number | null;
|
|
@@ -264,8 +267,10 @@ interface SegResult {
|
|
|
264
267
|
from: number;
|
|
265
268
|
to: number;
|
|
266
269
|
status: SegStatus;
|
|
267
|
-
/**
|
|
270
|
+
/** 对照本章文件(多卷拼成一段时为第一卷,全部见 wikiFiles) */
|
|
268
271
|
wikiFile: string | null;
|
|
272
|
+
/** 多卷拼成一段比对时的各卷章文件 */
|
|
273
|
+
wikiFiles?: string[];
|
|
269
274
|
/** 相似度 0..1(ok 时有) */
|
|
270
275
|
similarity?: number;
|
|
271
276
|
}
|
|
@@ -288,6 +293,13 @@ interface CompareInput {
|
|
|
288
293
|
/** 取对照本某章的 md 文本;取不到返回 null */
|
|
289
294
|
getWikiMd: (file: string) => Promise<string | null>;
|
|
290
295
|
isVariant: VariantTest;
|
|
296
|
+
/**
|
|
297
|
+
* 本册没有一级标题(卷)时,按册名(章 title)给出的卷范围,如「卷七至卷十」→ [7, 10]:
|
|
298
|
+
* 对照本这几卷拼起来与整册比对。有一级标题时不用。
|
|
299
|
+
*/
|
|
300
|
+
volumeRange?: [number, number] | null;
|
|
301
|
+
/** 那一段的名称(一般就是册名);不给按卷号写 */
|
|
302
|
+
volumeLabel?: string;
|
|
291
303
|
}
|
|
292
304
|
/** 比对一册:按卷分段,各卷分别取对照本、分别比对(不跨卷错位),结果按底本字序排列 */
|
|
293
305
|
declare function compareChapter(input: CompareInput): Promise<CompareResult>;
|
|
@@ -325,6 +337,7 @@ interface TextTransport {
|
|
|
325
337
|
getTextManifest?(id: string): Promise<{
|
|
326
338
|
versions: {
|
|
327
339
|
key: string;
|
|
340
|
+
kind?: string;
|
|
328
341
|
label?: string;
|
|
329
342
|
name?: string;
|
|
330
343
|
}[];
|
|
@@ -338,7 +351,11 @@ interface TextTransport {
|
|
|
338
351
|
md: string | null;
|
|
339
352
|
} | null>;
|
|
340
353
|
}
|
|
341
|
-
/**
|
|
354
|
+
/**
|
|
355
|
+
* 对照本没指明版本时取哪一份:有 default 取它,否则第一份(与阅读页同规则);
|
|
356
|
+
* 但整理本(kind=collated)的 md 只有篇目、正文在 json 里,拿来逐字比没有意义,
|
|
357
|
+
* 这时改取第一份不是整理本的版本(如四庫總目维基条目的 wikisource)。都是整理本就仍取 default。
|
|
358
|
+
*/
|
|
342
359
|
declare function resolveTargetKey(transport: TextTransport, target: CompareTarget): Promise<string | null>;
|
|
343
360
|
interface RunInput {
|
|
344
361
|
/** 阅读页的传输层(IndexStorage);只用到 getTextManifest/getTextIndex/getChapter */
|
|
@@ -371,9 +388,14 @@ interface VariantTable {
|
|
|
371
388
|
version: string;
|
|
372
389
|
sources: string[];
|
|
373
390
|
pairs: string;
|
|
391
|
+
/** 仅「转简后相同」的字对(collate 表才有,见 variant-build.ts);默认也当异体 */
|
|
392
|
+
simp?: string;
|
|
393
|
+
mode?: string;
|
|
374
394
|
}
|
|
375
395
|
/** 由表建判定函数(双向);两字相同视为同字,不在这里处理 */
|
|
376
|
-
declare function buildVariantTest(table: Pick<VariantTable, 'pairs'
|
|
396
|
+
declare function buildVariantTest(table: Pick<VariantTable, 'pairs' | 'simp'>, opts?: {
|
|
397
|
+
simp?: boolean;
|
|
398
|
+
}): (a: string, b: string) => boolean;
|
|
377
399
|
/** 取表并建判定函数;取不到返回 null(对校照常,只是异体字都归入「用字不同」)。只取一次 */
|
|
378
400
|
declare function loadVariantTest(url?: string): Promise<((a: string, b: string) => boolean) | null>;
|
|
379
401
|
|
|
@@ -383,6 +405,12 @@ declare function loadVariantTest(url?: string): Promise<((a: string, b: string)
|
|
|
383
405
|
* 其余(含各篇标题文字)保留,因为底本的标题也是字层里的字。
|
|
384
406
|
*/
|
|
385
407
|
declare function wikiChars(md: string): string[];
|
|
408
|
+
/**
|
|
409
|
+
* 同一本书的另一个版本(如光盘本)的章 md → 参与比对的字序列。
|
|
410
|
+
* 与维基文庫那份不同:章文件与底本一一对应(同一个文件 stem),第一行 `## 章标题` 是章名、不是正文,去掉;
|
|
411
|
+
* 其余照常去掉标点与空白(这类版本多数没有标点,保持原分行)。
|
|
412
|
+
* 双行小注的标记 `<…|…>`(`<` `>` 起止,`|` 换列)不是字,去掉(它们属符号类 Sm,不在 STRIP 里)。
|
|
413
|
+
*/
|
|
386
414
|
declare function chapterChars(md: string): string[];
|
|
387
415
|
|
|
388
416
|
/**
|
|
@@ -409,4 +437,38 @@ declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOpt
|
|
|
409
437
|
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
410
438
|
declare function parseChapters(spec: string): string[];
|
|
411
439
|
|
|
412
|
-
|
|
440
|
+
/**
|
|
441
|
+
* 从 open-guji-cv 的 config/hanzi/core/edges.tsv 生成对校用的异体字表(glyph-variants.json)。
|
|
442
|
+
* 纯函数,不读文件:调用方把 edges.tsv 的文本和(可选的)繁→简转换函数传进来。命令行见 `book-text-util variants`。
|
|
443
|
+
*
|
|
444
|
+
* 判法分三种 mode:
|
|
445
|
+
* - open/trusted:与网站原先的 scripts/build-glyph-variants.mjs 相同。两字之间有 variant/variant_partial/simp/z 边,
|
|
446
|
+
* 而且来源在 open(Unihan、本项目自有、yi-bai)或 trusted(再加 yitizi、twedu、dypytz、hydzd、cjkvi-simplified)之内;
|
|
447
|
+
* 有 distinct(人审定永不并)或 borrow(通假)边的不算。
|
|
448
|
+
* - collate(默认,2026-11-14 殿本参校查漏用过、用户认可):在 trusted 之上再加三条,
|
|
449
|
+
* 1. 强证据压过通假:Unihan kSemanticVariant/kZVariant 或教育部异体字字典(twedu)认定是异体的,即使另有 borrow 边也算异体
|
|
450
|
+
* (辨/辯、奕/弈、筭/算、寖/浸);distinct 仍然一票否决。
|
|
451
|
+
* 2. 僻字放宽:两字中有一个是扩展区字(CJK 扩展 A 或 BMP 以外),任何来源的 variant 边都算(𠉀/候、𭧂/暴、𨒪/速)。
|
|
452
|
+
* 常用字之间只靠宽松来源(zi:link、charcomp 等)的不算,如 令/命、壁/璧、宇/字 都留作「用字不同」。
|
|
453
|
+
* 3. 繁简同形:给了 toSimplified 时,转简后相同的两个字也算(範/范、乾/幹、閒/閑、捜/蒐),放在表的 `simp` 栏,
|
|
454
|
+
* 用的时候可以选择要不要并进异体(对照本是简繁转换来的,比如维基文库,就该并)。
|
|
455
|
+
*/
|
|
456
|
+
type VariantMode = 'open' | 'trusted' | 'collate';
|
|
457
|
+
interface BuiltVariantTable {
|
|
458
|
+
version: string;
|
|
459
|
+
mode: VariantMode;
|
|
460
|
+
sources: string[];
|
|
461
|
+
/** 每行一对,两个码点;判为异体 */
|
|
462
|
+
pairs: string;
|
|
463
|
+
/** 每行一对;仅「转简后相同」(collate 且给了 toSimplified 时才有) */
|
|
464
|
+
simp?: string;
|
|
465
|
+
}
|
|
466
|
+
/** 扩展区字:CJK 扩展 A(U+3400–4DBF)或 BMP 以外 */
|
|
467
|
+
declare function isRareHan(ch: string): boolean;
|
|
468
|
+
declare function buildVariantTable(edgesTsv: string, opts?: {
|
|
469
|
+
mode?: VariantMode;
|
|
470
|
+
toSimplified?: (s: string) => string;
|
|
471
|
+
version?: string;
|
|
472
|
+
}): BuiltVariantTable;
|
|
473
|
+
|
|
474
|
+
export { ALL_CATS, type BaseCell, type BuiltVariantTable, type CharCell, type CharColumn, type CharDoc, type CharEq, type CharPage, type CompareInput, type CompareItem, type CompareResult, type CompareTarget, type DiffCat, type EntityDoc, type EntityItem, type EntityMode, type FigDoc, GlyphConverters, GujiFormatError, type Heading, type Hunk, type IndexType, MAX_EDIT_DISTANCE, type PunctDoc, type PunctItem, type ReportChapter, type ReportOptions, type RunError, type RunInput, SOLO_ATTR, type SegResult, type SegStatus, type VariantMode, type VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTable, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, isRareHan, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|