book-text-util 0.1.2 → 0.1.3
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/dist/cli.js +60 -12
- package/dist/index.cjs +59 -14
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +58 -14
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -607,24 +607,54 @@ function diffRaw(a, b, maxD, same) {
|
|
|
607
607
|
}
|
|
608
608
|
|
|
609
609
|
// src/compare/volumes.ts
|
|
610
|
+
var DIGIT = { "\u3007": 0, \u96F6: 0, \u4E00: 1, \u4E8C: 2, \u4E09: 3, \u56DB: 4, \u4E94: 5, \u516D: 6, \u4E03: 7, \u516B: 8, \u4E5D: 9 };
|
|
611
|
+
function cnNumber(s) {
|
|
612
|
+
if (!s) return null;
|
|
613
|
+
if (/^\d+$/.test(s)) return Number(s);
|
|
614
|
+
if (![...s].every((ch) => ch in DIGIT || ch === "\u5341" || ch === "\u767E")) return null;
|
|
615
|
+
if (![...s].some((ch) => ch === "\u5341" || ch === "\u767E")) {
|
|
616
|
+
let n = 0;
|
|
617
|
+
for (const ch of s) n = n * 10 + DIGIT[ch];
|
|
618
|
+
return n;
|
|
619
|
+
}
|
|
620
|
+
let total = 0;
|
|
621
|
+
let cur = 0;
|
|
622
|
+
for (const ch of s) {
|
|
623
|
+
if (ch === "\u767E") {
|
|
624
|
+
total += (cur || 1) * 100;
|
|
625
|
+
cur = 0;
|
|
626
|
+
} else if (ch === "\u5341") {
|
|
627
|
+
total += (cur || 1) * 10;
|
|
628
|
+
cur = 0;
|
|
629
|
+
} else cur = DIGIT[ch];
|
|
630
|
+
}
|
|
631
|
+
return total + cur;
|
|
632
|
+
}
|
|
633
|
+
function volumeNo(label) {
|
|
634
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
|
|
635
|
+
return m ? cnNumber(m[1]) : null;
|
|
636
|
+
}
|
|
610
637
|
function wikiFileOf(no, chapters) {
|
|
611
638
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
612
|
-
|
|
639
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
640
|
+
if (exact) return exact.file;
|
|
641
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
613
642
|
}
|
|
614
643
|
|
|
615
644
|
// src/compare/wikitext.ts
|
|
616
645
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
617
646
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
618
647
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
648
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
649
|
+
var NOTE_MARK = /[<>|]/g;
|
|
619
650
|
function wikiChars(md) {
|
|
620
651
|
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*/, ""));
|
|
621
|
-
return Array.from(lines.join("").replace(STRIP, ""));
|
|
652
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
622
653
|
}
|
|
623
|
-
var NOTE_MARK = /[<>|]/g;
|
|
624
654
|
function chapterChars(md) {
|
|
625
655
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
626
656
|
if (lines.length && /^#{1,2}\s/.test(lines[0])) lines.shift();
|
|
627
|
-
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
657
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
628
658
|
}
|
|
629
659
|
|
|
630
660
|
// src/compare/compare.ts
|
|
@@ -667,16 +697,34 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
667
697
|
}
|
|
668
698
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
669
699
|
}
|
|
670
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
700
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
671
701
|
const res = { no: seg.no, label: seg.label, from: seg.from, to: seg.to, status: "ok", wikiFile: null };
|
|
672
|
-
|
|
673
|
-
if (
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
if (
|
|
702
|
+
let wiki;
|
|
703
|
+
if (whole) {
|
|
704
|
+
res.wikiFile = whole.file;
|
|
705
|
+
wiki = chapterChars(whole.md);
|
|
706
|
+
} else if (range) {
|
|
707
|
+
const files = [];
|
|
708
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
709
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
710
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
711
|
+
files.push(f);
|
|
712
|
+
}
|
|
713
|
+
res.wikiFile = files[0];
|
|
714
|
+
res.wikiFiles = files;
|
|
715
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
716
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
717
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
718
|
+
} else {
|
|
719
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
720
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
721
|
+
res.wikiFile = file;
|
|
722
|
+
const md = await input.getWikiMd(file);
|
|
723
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
724
|
+
wiki = wikiChars(md);
|
|
725
|
+
}
|
|
677
726
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
678
|
-
|
|
679
|
-
if (!whole) {
|
|
727
|
+
if (!whole && !range) {
|
|
680
728
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
681
729
|
wiki = wiki.slice(j0, j1);
|
|
682
730
|
}
|
package/dist/index.cjs
CHANGED
|
@@ -61,6 +61,7 @@ __export(index_exports, {
|
|
|
61
61
|
splitVolumes: () => splitVolumes,
|
|
62
62
|
trimWikiToBase: () => trimWikiToBase,
|
|
63
63
|
volumeNo: () => volumeNo,
|
|
64
|
+
volumeRangeOf: () => volumeRangeOf,
|
|
64
65
|
wikiChars: () => wikiChars,
|
|
65
66
|
wikiFileOf: () => wikiFileOf
|
|
66
67
|
});
|
|
@@ -682,7 +683,17 @@ function volumeNo(label) {
|
|
|
682
683
|
}
|
|
683
684
|
function wikiFileOf(no, chapters) {
|
|
684
685
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
685
|
-
|
|
686
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
687
|
+
if (exact) return exact.file;
|
|
688
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
689
|
+
}
|
|
690
|
+
function volumeRangeOf(title) {
|
|
691
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
692
|
+
if (!m) return null;
|
|
693
|
+
const a = cnNumber(m[1]);
|
|
694
|
+
const b = m[2] ? cnNumber(m[2]) : a;
|
|
695
|
+
if (a === null || b === null || b < a || b - a > 200) return null;
|
|
696
|
+
return [a, b];
|
|
686
697
|
}
|
|
687
698
|
var anchorKey = (a) => a;
|
|
688
699
|
function splitVolumes(cells, headings, prevVolNo, lastContinues) {
|
|
@@ -720,15 +731,16 @@ function nextContinues(nextHeadings) {
|
|
|
720
731
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
721
732
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
722
733
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
734
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
735
|
+
var NOTE_MARK = /[<>|]/g;
|
|
723
736
|
function wikiChars(md) {
|
|
724
737
|
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, ""));
|
|
738
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
726
739
|
}
|
|
727
|
-
var NOTE_MARK = /[<>|]/g;
|
|
728
740
|
function chapterChars(md) {
|
|
729
741
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
730
742
|
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, ""));
|
|
743
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
732
744
|
}
|
|
733
745
|
|
|
734
746
|
// src/compare/compare.ts
|
|
@@ -771,16 +783,34 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
771
783
|
}
|
|
772
784
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
773
785
|
}
|
|
774
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
786
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
775
787
|
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 (
|
|
788
|
+
let wiki;
|
|
789
|
+
if (whole) {
|
|
790
|
+
res.wikiFile = whole.file;
|
|
791
|
+
wiki = chapterChars(whole.md);
|
|
792
|
+
} else if (range) {
|
|
793
|
+
const files = [];
|
|
794
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
795
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
796
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
797
|
+
files.push(f);
|
|
798
|
+
}
|
|
799
|
+
res.wikiFile = files[0];
|
|
800
|
+
res.wikiFiles = files;
|
|
801
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
802
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
803
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
804
|
+
} else {
|
|
805
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
806
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
807
|
+
res.wikiFile = file;
|
|
808
|
+
const md = await input.getWikiMd(file);
|
|
809
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
810
|
+
wiki = wikiChars(md);
|
|
811
|
+
}
|
|
781
812
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
782
|
-
|
|
783
|
-
if (!whole) {
|
|
813
|
+
if (!whole && !range) {
|
|
784
814
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
785
815
|
wiki = wiki.slice(j0, j1);
|
|
786
816
|
}
|
|
@@ -817,6 +847,15 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
817
847
|
return res;
|
|
818
848
|
}
|
|
819
849
|
async function compareChapter(input) {
|
|
850
|
+
const range = input.volumeRange;
|
|
851
|
+
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
852
|
+
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
853
|
+
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
854
|
+
const items2 = [];
|
|
855
|
+
const res = await compareSeg(input, seg, items2, 0, void 0, range);
|
|
856
|
+
items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
|
|
857
|
+
return { items: items2, segments: [res] };
|
|
858
|
+
}
|
|
820
859
|
const segs = splitVolumes(input.cells, input.headings, input.prevVolNo, input.lastContinues);
|
|
821
860
|
const items = [];
|
|
822
861
|
const segments = [];
|
|
@@ -862,7 +901,9 @@ async function resolveTargetKey(transport, target) {
|
|
|
862
901
|
const m = await transport.getTextManifest?.(target.id);
|
|
863
902
|
const vs = m?.versions;
|
|
864
903
|
if (!Array.isArray(vs) || vs.length === 0) return null;
|
|
865
|
-
|
|
904
|
+
const main = vs.find((v) => v.key === "default") ?? vs[0];
|
|
905
|
+
if (main.kind !== "collated") return main.key;
|
|
906
|
+
return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
|
|
866
907
|
}
|
|
867
908
|
async function runCompare(input) {
|
|
868
909
|
const { base, target, cells } = input;
|
|
@@ -906,7 +947,10 @@ async function runCompare(input) {
|
|
|
906
947
|
lastContinues: nextContinues(at >= 0 && at + 1 < chapters.length ? headingsOf(chapters[at + 1]) : void 0),
|
|
907
948
|
wikiChapters,
|
|
908
949
|
getWikiMd,
|
|
909
|
-
isVariant
|
|
950
|
+
isVariant,
|
|
951
|
+
// 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
|
|
952
|
+
volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
|
|
953
|
+
volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
|
|
910
954
|
});
|
|
911
955
|
return { ...result, targetKey, wikiChapters };
|
|
912
956
|
}
|
|
@@ -1042,6 +1086,7 @@ function parseChapters(spec) {
|
|
|
1042
1086
|
splitVolumes,
|
|
1043
1087
|
trimWikiToBase,
|
|
1044
1088
|
volumeNo,
|
|
1089
|
+
volumeRangeOf,
|
|
1045
1090
|
wikiChars,
|
|
1046
1091
|
wikiFileOf
|
|
1047
1092
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -202,8 +202,16 @@ interface WikiChapter {
|
|
|
202
202
|
declare function cnNumber(s: string): number | null;
|
|
203
203
|
/** 一级标题「卷一 經部一」→ 1;不是「卷+数」开头的返回 null */
|
|
204
204
|
declare function volumeNo(label: string): number | null;
|
|
205
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* 卷序号 → 对照本章文件;没有返回 null。
|
|
207
|
+
* 认两种章名:维基文庫的「卷001」(数字补零到三位),和 Kanripo 等的「卷七」「卷二十三」(汉字数字,后面可带篇名)。
|
|
208
|
+
*/
|
|
206
209
|
declare function wikiFileOf(no: number, chapters: readonly WikiChapter[]): string | null;
|
|
210
|
+
/**
|
|
211
|
+
* 章名里的卷范围:「卷七至卷十」→ [7, 10],「卷七」→ [7, 7];认不出返回 null。
|
|
212
|
+
* 用于底本册没有一级标题(卷)时,按册名决定取对照本哪几卷(如全唐文自校本一册一章、章名写「卷七至卷十」)。
|
|
213
|
+
*/
|
|
214
|
+
declare function volumeRangeOf(title: string): [number, number] | null;
|
|
207
215
|
interface VolumeSeg {
|
|
208
216
|
/** 卷序号;认不出(卷首、序等)为 null */
|
|
209
217
|
no: number | null;
|
|
@@ -264,8 +272,10 @@ interface SegResult {
|
|
|
264
272
|
from: number;
|
|
265
273
|
to: number;
|
|
266
274
|
status: SegStatus;
|
|
267
|
-
/**
|
|
275
|
+
/** 对照本章文件(多卷拼成一段时为第一卷,全部见 wikiFiles) */
|
|
268
276
|
wikiFile: string | null;
|
|
277
|
+
/** 多卷拼成一段比对时的各卷章文件 */
|
|
278
|
+
wikiFiles?: string[];
|
|
269
279
|
/** 相似度 0..1(ok 时有) */
|
|
270
280
|
similarity?: number;
|
|
271
281
|
}
|
|
@@ -288,6 +298,13 @@ interface CompareInput {
|
|
|
288
298
|
/** 取对照本某章的 md 文本;取不到返回 null */
|
|
289
299
|
getWikiMd: (file: string) => Promise<string | null>;
|
|
290
300
|
isVariant: VariantTest;
|
|
301
|
+
/**
|
|
302
|
+
* 本册没有一级标题(卷)时,按册名(章 title)给出的卷范围,如「卷七至卷十」→ [7, 10]:
|
|
303
|
+
* 对照本这几卷拼起来与整册比对。有一级标题时不用。
|
|
304
|
+
*/
|
|
305
|
+
volumeRange?: [number, number] | null;
|
|
306
|
+
/** 那一段的名称(一般就是册名);不给按卷号写 */
|
|
307
|
+
volumeLabel?: string;
|
|
291
308
|
}
|
|
292
309
|
/** 比对一册:按卷分段,各卷分别取对照本、分别比对(不跨卷错位),结果按底本字序排列 */
|
|
293
310
|
declare function compareChapter(input: CompareInput): Promise<CompareResult>;
|
|
@@ -325,6 +342,7 @@ interface TextTransport {
|
|
|
325
342
|
getTextManifest?(id: string): Promise<{
|
|
326
343
|
versions: {
|
|
327
344
|
key: string;
|
|
345
|
+
kind?: string;
|
|
328
346
|
label?: string;
|
|
329
347
|
name?: string;
|
|
330
348
|
}[];
|
|
@@ -338,7 +356,11 @@ interface TextTransport {
|
|
|
338
356
|
md: string | null;
|
|
339
357
|
} | null>;
|
|
340
358
|
}
|
|
341
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* 对照本没指明版本时取哪一份:有 default 取它,否则第一份(与阅读页同规则);
|
|
361
|
+
* 但整理本(kind=collated)的 md 只有篇目、正文在 json 里,拿来逐字比没有意义,
|
|
362
|
+
* 这时改取第一份不是整理本的版本(如四庫總目维基条目的 wikisource)。都是整理本就仍取 default。
|
|
363
|
+
*/
|
|
342
364
|
declare function resolveTargetKey(transport: TextTransport, target: CompareTarget): Promise<string | null>;
|
|
343
365
|
interface RunInput {
|
|
344
366
|
/** 阅读页的传输层(IndexStorage);只用到 getTextManifest/getTextIndex/getChapter */
|
|
@@ -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,4 @@ 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
|
-
export { ALL_CATS, type BaseCell, 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 VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, wikiChars, wikiFileOf };
|
|
440
|
+
export { ALL_CATS, type BaseCell, 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 VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|
package/dist/index.d.ts
CHANGED
|
@@ -202,8 +202,16 @@ interface WikiChapter {
|
|
|
202
202
|
declare function cnNumber(s: string): number | null;
|
|
203
203
|
/** 一级标题「卷一 經部一」→ 1;不是「卷+数」开头的返回 null */
|
|
204
204
|
declare function volumeNo(label: string): number | null;
|
|
205
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* 卷序号 → 对照本章文件;没有返回 null。
|
|
207
|
+
* 认两种章名:维基文庫的「卷001」(数字补零到三位),和 Kanripo 等的「卷七」「卷二十三」(汉字数字,后面可带篇名)。
|
|
208
|
+
*/
|
|
206
209
|
declare function wikiFileOf(no: number, chapters: readonly WikiChapter[]): string | null;
|
|
210
|
+
/**
|
|
211
|
+
* 章名里的卷范围:「卷七至卷十」→ [7, 10],「卷七」→ [7, 7];认不出返回 null。
|
|
212
|
+
* 用于底本册没有一级标题(卷)时,按册名决定取对照本哪几卷(如全唐文自校本一册一章、章名写「卷七至卷十」)。
|
|
213
|
+
*/
|
|
214
|
+
declare function volumeRangeOf(title: string): [number, number] | null;
|
|
207
215
|
interface VolumeSeg {
|
|
208
216
|
/** 卷序号;认不出(卷首、序等)为 null */
|
|
209
217
|
no: number | null;
|
|
@@ -264,8 +272,10 @@ interface SegResult {
|
|
|
264
272
|
from: number;
|
|
265
273
|
to: number;
|
|
266
274
|
status: SegStatus;
|
|
267
|
-
/**
|
|
275
|
+
/** 对照本章文件(多卷拼成一段时为第一卷,全部见 wikiFiles) */
|
|
268
276
|
wikiFile: string | null;
|
|
277
|
+
/** 多卷拼成一段比对时的各卷章文件 */
|
|
278
|
+
wikiFiles?: string[];
|
|
269
279
|
/** 相似度 0..1(ok 时有) */
|
|
270
280
|
similarity?: number;
|
|
271
281
|
}
|
|
@@ -288,6 +298,13 @@ interface CompareInput {
|
|
|
288
298
|
/** 取对照本某章的 md 文本;取不到返回 null */
|
|
289
299
|
getWikiMd: (file: string) => Promise<string | null>;
|
|
290
300
|
isVariant: VariantTest;
|
|
301
|
+
/**
|
|
302
|
+
* 本册没有一级标题(卷)时,按册名(章 title)给出的卷范围,如「卷七至卷十」→ [7, 10]:
|
|
303
|
+
* 对照本这几卷拼起来与整册比对。有一级标题时不用。
|
|
304
|
+
*/
|
|
305
|
+
volumeRange?: [number, number] | null;
|
|
306
|
+
/** 那一段的名称(一般就是册名);不给按卷号写 */
|
|
307
|
+
volumeLabel?: string;
|
|
291
308
|
}
|
|
292
309
|
/** 比对一册:按卷分段,各卷分别取对照本、分别比对(不跨卷错位),结果按底本字序排列 */
|
|
293
310
|
declare function compareChapter(input: CompareInput): Promise<CompareResult>;
|
|
@@ -325,6 +342,7 @@ interface TextTransport {
|
|
|
325
342
|
getTextManifest?(id: string): Promise<{
|
|
326
343
|
versions: {
|
|
327
344
|
key: string;
|
|
345
|
+
kind?: string;
|
|
328
346
|
label?: string;
|
|
329
347
|
name?: string;
|
|
330
348
|
}[];
|
|
@@ -338,7 +356,11 @@ interface TextTransport {
|
|
|
338
356
|
md: string | null;
|
|
339
357
|
} | null>;
|
|
340
358
|
}
|
|
341
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* 对照本没指明版本时取哪一份:有 default 取它,否则第一份(与阅读页同规则);
|
|
361
|
+
* 但整理本(kind=collated)的 md 只有篇目、正文在 json 里,拿来逐字比没有意义,
|
|
362
|
+
* 这时改取第一份不是整理本的版本(如四庫總目维基条目的 wikisource)。都是整理本就仍取 default。
|
|
363
|
+
*/
|
|
342
364
|
declare function resolveTargetKey(transport: TextTransport, target: CompareTarget): Promise<string | null>;
|
|
343
365
|
interface RunInput {
|
|
344
366
|
/** 阅读页的传输层(IndexStorage);只用到 getTextManifest/getTextIndex/getChapter */
|
|
@@ -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,4 @@ 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
|
-
export { ALL_CATS, type BaseCell, 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 VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, wikiChars, wikiFileOf };
|
|
440
|
+
export { ALL_CATS, type BaseCell, 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 VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|
package/dist/index.js
CHANGED
|
@@ -614,7 +614,17 @@ function volumeNo(label) {
|
|
|
614
614
|
}
|
|
615
615
|
function wikiFileOf(no, chapters) {
|
|
616
616
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
617
|
-
|
|
617
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
618
|
+
if (exact) return exact.file;
|
|
619
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
620
|
+
}
|
|
621
|
+
function volumeRangeOf(title) {
|
|
622
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
623
|
+
if (!m) return null;
|
|
624
|
+
const a = cnNumber(m[1]);
|
|
625
|
+
const b = m[2] ? cnNumber(m[2]) : a;
|
|
626
|
+
if (a === null || b === null || b < a || b - a > 200) return null;
|
|
627
|
+
return [a, b];
|
|
618
628
|
}
|
|
619
629
|
var anchorKey = (a) => a;
|
|
620
630
|
function splitVolumes(cells, headings, prevVolNo, lastContinues) {
|
|
@@ -652,15 +662,16 @@ function nextContinues(nextHeadings) {
|
|
|
652
662
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
653
663
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
654
664
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
665
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
666
|
+
var NOTE_MARK = /[<>|]/g;
|
|
655
667
|
function wikiChars(md) {
|
|
656
668
|
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*/, ""));
|
|
657
|
-
return Array.from(lines.join("").replace(STRIP, ""));
|
|
669
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
658
670
|
}
|
|
659
|
-
var NOTE_MARK = /[<>|]/g;
|
|
660
671
|
function chapterChars(md) {
|
|
661
672
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
662
673
|
if (lines.length && /^#{1,2}\s/.test(lines[0])) lines.shift();
|
|
663
|
-
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
674
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
664
675
|
}
|
|
665
676
|
|
|
666
677
|
// src/compare/compare.ts
|
|
@@ -703,16 +714,34 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
703
714
|
}
|
|
704
715
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
705
716
|
}
|
|
706
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
717
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
707
718
|
const res = { no: seg.no, label: seg.label, from: seg.from, to: seg.to, status: "ok", wikiFile: null };
|
|
708
|
-
|
|
709
|
-
if (
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
if (
|
|
719
|
+
let wiki;
|
|
720
|
+
if (whole) {
|
|
721
|
+
res.wikiFile = whole.file;
|
|
722
|
+
wiki = chapterChars(whole.md);
|
|
723
|
+
} else if (range) {
|
|
724
|
+
const files = [];
|
|
725
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
726
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
727
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
728
|
+
files.push(f);
|
|
729
|
+
}
|
|
730
|
+
res.wikiFile = files[0];
|
|
731
|
+
res.wikiFiles = files;
|
|
732
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
733
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
734
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
735
|
+
} else {
|
|
736
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
737
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
738
|
+
res.wikiFile = file;
|
|
739
|
+
const md = await input.getWikiMd(file);
|
|
740
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
741
|
+
wiki = wikiChars(md);
|
|
742
|
+
}
|
|
713
743
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
714
|
-
|
|
715
|
-
if (!whole) {
|
|
744
|
+
if (!whole && !range) {
|
|
716
745
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
717
746
|
wiki = wiki.slice(j0, j1);
|
|
718
747
|
}
|
|
@@ -749,6 +778,15 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
749
778
|
return res;
|
|
750
779
|
}
|
|
751
780
|
async function compareChapter(input) {
|
|
781
|
+
const range = input.volumeRange;
|
|
782
|
+
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
783
|
+
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
784
|
+
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
785
|
+
const items2 = [];
|
|
786
|
+
const res = await compareSeg(input, seg, items2, 0, void 0, range);
|
|
787
|
+
items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
|
|
788
|
+
return { items: items2, segments: [res] };
|
|
789
|
+
}
|
|
752
790
|
const segs = splitVolumes(input.cells, input.headings, input.prevVolNo, input.lastContinues);
|
|
753
791
|
const items = [];
|
|
754
792
|
const segments = [];
|
|
@@ -794,7 +832,9 @@ async function resolveTargetKey(transport, target) {
|
|
|
794
832
|
const m = await transport.getTextManifest?.(target.id);
|
|
795
833
|
const vs = m?.versions;
|
|
796
834
|
if (!Array.isArray(vs) || vs.length === 0) return null;
|
|
797
|
-
|
|
835
|
+
const main = vs.find((v) => v.key === "default") ?? vs[0];
|
|
836
|
+
if (main.kind !== "collated") return main.key;
|
|
837
|
+
return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
|
|
798
838
|
}
|
|
799
839
|
async function runCompare(input) {
|
|
800
840
|
const { base, target, cells } = input;
|
|
@@ -838,7 +878,10 @@ async function runCompare(input) {
|
|
|
838
878
|
lastContinues: nextContinues(at >= 0 && at + 1 < chapters.length ? headingsOf(chapters[at + 1]) : void 0),
|
|
839
879
|
wikiChapters,
|
|
840
880
|
getWikiMd,
|
|
841
|
-
isVariant
|
|
881
|
+
isVariant,
|
|
882
|
+
// 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
|
|
883
|
+
volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
|
|
884
|
+
volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
|
|
842
885
|
});
|
|
843
886
|
return { ...result, targetKey, wikiChapters };
|
|
844
887
|
}
|
|
@@ -973,6 +1016,7 @@ export {
|
|
|
973
1016
|
splitVolumes,
|
|
974
1017
|
trimWikiToBase,
|
|
975
1018
|
volumeNo,
|
|
1019
|
+
volumeRangeOf,
|
|
976
1020
|
wikiChars,
|
|
977
1021
|
wikiFileOf
|
|
978
1022
|
};
|