book-text-util 0.1.4 → 0.1.6

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 CHANGED
@@ -96,3 +96,9 @@ book-text-util report --base-root <底本版本目录> --ref-root <对照版本
96
96
  ```
97
97
 
98
98
  逐册整章对整章,输出 Markdown:汇总表(字数、相似度、各类条数)+每册「用字不同/对照本多/对照本少」逐条(位置、前后文,差异字加粗);`--list word,add,omit,glyph` 可改列哪几类。库内对应 `buildReport`、`parseChapters`。
99
+
100
+ ### 册与卷的对应:章条目的 `juan` 字段
101
+
102
+ 底本一册一章而对照本按卷分章时(如全唐文对 Kanripo),在底本 `index.json` 的章条目里写固定字段
103
+ `"juan": [起, 止]`(本册含的原书卷号,闭区间,如 `[3, 6]`)。对校只认它:卷号即对照本章文件号,
104
+ 不看章名,也不看册内一级标题。没有这个字段才退回按章名「卷三至卷六」解析(旧书兼容)。
package/dist/cli.js CHANGED
@@ -637,7 +637,7 @@ function cnNumber(s) {
637
637
  return total + cur;
638
638
  }
639
639
  function volumeNo(label) {
640
- const m = /^卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
640
+ const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
641
641
  return m ? cnNumber(m[1]) : null;
642
642
  }
643
643
  function wikiFileOf(no, chapters) {
@@ -730,7 +730,7 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
730
730
  wiki = wikiChars(md);
731
731
  }
732
732
  const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
733
- if (!whole && !range) {
733
+ if (!whole && (!range || seg.startsPartial || seg.endsPartial)) {
734
734
  const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
735
735
  wiki = wiki.slice(j0, j1);
736
736
  }
package/dist/index.cjs CHANGED
@@ -51,6 +51,7 @@ __export(index_exports, {
51
51
  formatCmp: () => formatCmp,
52
52
  frontMatter: () => frontMatter,
53
53
  isRareHan: () => isRareHan,
54
+ juanOf: () => juanOf,
54
55
  loadVariantTest: () => loadVariantTest,
55
56
  nextContinues: () => nextContinues,
56
57
  normalizeVariants: () => normalizeVariants,
@@ -686,7 +687,7 @@ function cnNumber(s) {
686
687
  return total + cur;
687
688
  }
688
689
  function volumeNo(label) {
689
- const m = /^卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
690
+ const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
690
691
  return m ? cnNumber(m[1]) : null;
691
692
  }
692
693
  function wikiFileOf(no, chapters) {
@@ -696,7 +697,7 @@ function wikiFileOf(no, chapters) {
696
697
  return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
697
698
  }
698
699
  function volumeRangeOf(title) {
699
- const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
700
+ const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
700
701
  if (!m) return null;
701
702
  const a = cnNumber(m[1]);
702
703
  const b = m[2] ? cnNumber(m[2]) : a;
@@ -818,7 +819,7 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
818
819
  wiki = wikiChars(md);
819
820
  }
820
821
  const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
821
- if (!whole && !range) {
822
+ if (!whole && (!range || seg.startsPartial || seg.endsPartial)) {
822
823
  const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
823
824
  wiki = wiki.slice(j0, j1);
824
825
  }
@@ -856,9 +857,9 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
856
857
  }
857
858
  async function compareChapter(input) {
858
859
  const range = input.volumeRange;
859
- if (range && !input.headings.some((h) => h.level === 1)) {
860
+ if (range && (input.volumeRangeFixed || !input.headings.some((h) => h.level === 1 && volumeNo(h.label) !== null))) {
860
861
  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 seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: !!input.rangeStartsPartial, endsPartial: !!input.rangeEndsPartial, headEnd: 0 };
862
863
  const items2 = [];
863
864
  const res = await compareSeg(input, seg, items2, 0, void 0, range);
864
865
  items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
@@ -917,6 +918,12 @@ async function resolveTargetKey(transport, target) {
917
918
  if (main.kind !== "collated") return main.key;
918
919
  return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
919
920
  }
921
+ function juanOf(ch) {
922
+ const j = ch?.juan;
923
+ if (!Array.isArray(j) || j.length !== 2) return null;
924
+ const [a, b] = j;
925
+ return Number.isInteger(a) && Number.isInteger(b) && a >= 1 && b >= a ? [a, b] : null;
926
+ }
920
927
  async function runCompare(input) {
921
928
  const { base, target, cells } = input;
922
929
  const transport = input.transport;
@@ -961,7 +968,12 @@ async function runCompare(input) {
961
968
  getWikiMd,
962
969
  isVariant,
963
970
  // 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
964
- volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
971
+ // 章条目的 `juan: [起, 止]` 是固定字段(本册含的原书卷号,闭区间),有就只认它;没有才退回章名解析(旧书)
972
+ volumeRange: juanOf(chapters[at]) ?? (typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null),
973
+ volumeRangeFixed: juanOf(chapters[at]) !== null,
974
+ // 一卷占多册:与前一册的 juan 止卷、后一册的 juan 起卷相同,就是接续/延续,对照本要按首尾对齐裁
975
+ rangeStartsPartial: at > 0 && juanOf(chapters[at]) !== null && juanOf(chapters[at - 1])?.[1] === juanOf(chapters[at])[0],
976
+ rangeEndsPartial: at >= 0 && at + 1 < chapters.length && juanOf(chapters[at]) !== null && juanOf(chapters[at + 1])?.[0] === juanOf(chapters[at])[1],
965
977
  volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
966
978
  });
967
979
  return { ...result, targetKey, wikiChapters };
@@ -1170,6 +1182,7 @@ function buildVariantTable(edgesTsv, opts = {}) {
1170
1182
  formatCmp,
1171
1183
  frontMatter,
1172
1184
  isRareHan,
1185
+ juanOf,
1173
1186
  loadVariantTest,
1174
1187
  nextContinues,
1175
1188
  normalizeVariants,
package/dist/index.d.cts CHANGED
@@ -298,6 +298,11 @@ interface CompareInput {
298
298
  * 对照本这几卷拼起来与整册比对。有一级标题时不用。
299
299
  */
300
300
  volumeRange?: [number, number] | null;
301
+ /** 卷范围来自章条目的固定字段 juan:不管册内一级标题怎么写,一律按这个范围取对照本 */
302
+ volumeRangeFixed?: boolean;
303
+ /** 起始卷从上一册接续/末卷延续到下一册(一卷占多册):对照本按本册首尾对齐裁掉不属于本册的部分 */
304
+ rangeStartsPartial?: boolean;
305
+ rangeEndsPartial?: boolean;
301
306
  /** 那一段的名称(一般就是册名);不给按卷号写 */
302
307
  volumeLabel?: string;
303
308
  }
@@ -374,6 +379,8 @@ interface RunInput {
374
379
  }
375
380
  type RunError = 'no-target' | 'no-base-index';
376
381
  /** 取齐输入并比对一册。对照本或底本目录取不到时抛 Error(RunError),调用方静默退回普通阅读 */
382
+ /** 章条目的 juan 字段:`[起, 止]`,正整数、起≤止;不合格式当没有 */
383
+ declare function juanOf(ch: Record<string, unknown> | undefined): [number, number] | null;
377
384
  declare function runCompare(input: RunInput): Promise<CompareResult & {
378
385
  targetKey: string;
379
386
  wikiChapters: WikiChapter[];
@@ -471,4 +478,4 @@ declare function buildVariantTable(edgesTsv: string, opts?: {
471
478
  version?: string;
472
479
  }): BuiltVariantTable;
473
480
 
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 };
481
+ 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, juanOf, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
package/dist/index.d.ts CHANGED
@@ -298,6 +298,11 @@ interface CompareInput {
298
298
  * 对照本这几卷拼起来与整册比对。有一级标题时不用。
299
299
  */
300
300
  volumeRange?: [number, number] | null;
301
+ /** 卷范围来自章条目的固定字段 juan:不管册内一级标题怎么写,一律按这个范围取对照本 */
302
+ volumeRangeFixed?: boolean;
303
+ /** 起始卷从上一册接续/末卷延续到下一册(一卷占多册):对照本按本册首尾对齐裁掉不属于本册的部分 */
304
+ rangeStartsPartial?: boolean;
305
+ rangeEndsPartial?: boolean;
301
306
  /** 那一段的名称(一般就是册名);不给按卷号写 */
302
307
  volumeLabel?: string;
303
308
  }
@@ -374,6 +379,8 @@ interface RunInput {
374
379
  }
375
380
  type RunError = 'no-target' | 'no-base-index';
376
381
  /** 取齐输入并比对一册。对照本或底本目录取不到时抛 Error(RunError),调用方静默退回普通阅读 */
382
+ /** 章条目的 juan 字段:`[起, 止]`,正整数、起≤止;不合格式当没有 */
383
+ declare function juanOf(ch: Record<string, unknown> | undefined): [number, number] | null;
377
384
  declare function runCompare(input: RunInput): Promise<CompareResult & {
378
385
  targetKey: string;
379
386
  wikiChapters: WikiChapter[];
@@ -471,4 +478,4 @@ declare function buildVariantTable(edgesTsv: string, opts?: {
471
478
  version?: string;
472
479
  }): BuiltVariantTable;
473
480
 
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 };
481
+ 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, juanOf, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
package/dist/index.js CHANGED
@@ -615,7 +615,7 @@ function cnNumber(s) {
615
615
  return total + cur;
616
616
  }
617
617
  function volumeNo(label) {
618
- const m = /^卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
618
+ const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
619
619
  return m ? cnNumber(m[1]) : null;
620
620
  }
621
621
  function wikiFileOf(no, chapters) {
@@ -625,7 +625,7 @@ function wikiFileOf(no, chapters) {
625
625
  return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
626
626
  }
627
627
  function volumeRangeOf(title) {
628
- const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
628
+ const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
629
629
  if (!m) return null;
630
630
  const a = cnNumber(m[1]);
631
631
  const b = m[2] ? cnNumber(m[2]) : a;
@@ -747,7 +747,7 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
747
747
  wiki = wikiChars(md);
748
748
  }
749
749
  const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
750
- if (!whole && !range) {
750
+ if (!whole && (!range || seg.startsPartial || seg.endsPartial)) {
751
751
  const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
752
752
  wiki = wiki.slice(j0, j1);
753
753
  }
@@ -785,9 +785,9 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
785
785
  }
786
786
  async function compareChapter(input) {
787
787
  const range = input.volumeRange;
788
- if (range && !input.headings.some((h) => h.level === 1)) {
788
+ if (range && (input.volumeRangeFixed || !input.headings.some((h) => h.level === 1 && volumeNo(h.label) !== null))) {
789
789
  const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
790
- const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
790
+ const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: !!input.rangeStartsPartial, endsPartial: !!input.rangeEndsPartial, headEnd: 0 };
791
791
  const items2 = [];
792
792
  const res = await compareSeg(input, seg, items2, 0, void 0, range);
793
793
  items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
@@ -846,6 +846,12 @@ async function resolveTargetKey(transport, target) {
846
846
  if (main.kind !== "collated") return main.key;
847
847
  return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
848
848
  }
849
+ function juanOf(ch) {
850
+ const j = ch?.juan;
851
+ if (!Array.isArray(j) || j.length !== 2) return null;
852
+ const [a, b] = j;
853
+ return Number.isInteger(a) && Number.isInteger(b) && a >= 1 && b >= a ? [a, b] : null;
854
+ }
849
855
  async function runCompare(input) {
850
856
  const { base, target, cells } = input;
851
857
  const transport = input.transport;
@@ -890,7 +896,12 @@ async function runCompare(input) {
890
896
  getWikiMd,
891
897
  isVariant,
892
898
  // 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
893
- volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
899
+ // 章条目的 `juan: [起, 止]` 是固定字段(本册含的原书卷号,闭区间),有就只认它;没有才退回章名解析(旧书)
900
+ volumeRange: juanOf(chapters[at]) ?? (typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null),
901
+ volumeRangeFixed: juanOf(chapters[at]) !== null,
902
+ // 一卷占多册:与前一册的 juan 止卷、后一册的 juan 起卷相同,就是接续/延续,对照本要按首尾对齐裁
903
+ rangeStartsPartial: at > 0 && juanOf(chapters[at]) !== null && juanOf(chapters[at - 1])?.[1] === juanOf(chapters[at])[0],
904
+ rangeEndsPartial: at >= 0 && at + 1 < chapters.length && juanOf(chapters[at]) !== null && juanOf(chapters[at + 1])?.[0] === juanOf(chapters[at])[1],
894
905
  volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
895
906
  });
896
907
  return { ...result, targetKey, wikiChapters };
@@ -1098,6 +1109,7 @@ export {
1098
1109
  formatCmp,
1099
1110
  frontMatter,
1100
1111
  isRareHan,
1112
+ juanOf,
1101
1113
  loadVariantTest,
1102
1114
  nextContinues,
1103
1115
  normalizeVariants,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "book-text-util",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "古籍文本处理工具:下载导出 guji-markdown、版本对校、字形转换。网站与命令行脚本共用。",
5
5
  "license": "CC-BY-SA-4.0",
6
6
  "publishConfig": { "access": "public", "provenance": true },