book-text-util 0.0.0-stage → 0.1.0
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 +82 -2
- package/dist/browser.cjs +50 -0
- package/dist/browser.d.cts +11 -0
- package/dist/browser.d.ts +11 -0
- package/dist/browser.js +24 -0
- package/dist/cli.js +796 -0
- package/dist/export-Ct8PXBYj.d.cts +59 -0
- package/dist/export-Ct8PXBYj.d.ts +59 -0
- package/dist/index.cjs +935 -0
- package/dist/index.d.cts +379 -0
- package/dist/index.d.ts +379 -0
- package/dist/index.js +870 -0
- package/dist/node.cjs +51 -0
- package/dist/node.d.cts +11 -0
- package/dist/node.d.ts +11 -0
- package/dist/node.js +25 -0
- package/package.json +34 -4
package/README.md
CHANGED
|
@@ -1,3 +1,83 @@
|
|
|
1
|
-
#
|
|
1
|
+
# book-text-util
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
古籍文本处理:**下载导出(guji-markdown)**、**版本对校**、**字形转换**。纯 TypeScript,ESM + CJS 双发,网站与 Node 脚本通用。需要 Node ≥ 18。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm i book-text-util
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## 模块与入口
|
|
10
|
+
|
|
11
|
+
| 入口 | 内容 | 环境 |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `book-text-util` | 导出、对校、字形、id 解码 | 任意(无 DOM、无 Node 专有 API) |
|
|
14
|
+
| `book-text-util/browser` | `saveText`(Blob 下载)、`httpSource`(fetch 取数) | 浏览器 |
|
|
15
|
+
| `book-text-util/node` | `dirSource`(读本地目录)、`writeExport`(写文件) | Node |
|
|
16
|
+
| `book-text-util`(bin) | 命令行 `book-text-util export / compare` | Node |
|
|
17
|
+
|
|
18
|
+
## 一、导出 guji-markdown
|
|
19
|
+
|
|
20
|
+
规则照 guji-format spec/03,测试与 book-text 的 Python 生成器(`original_char.py`)逐字节比对(`tools/export-golden.py` 出标准答案)。
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { buildExport, availableFormats, createGlyphConverters } from 'book-text-util';
|
|
24
|
+
import { saveText } from 'book-text-util/browser';
|
|
25
|
+
|
|
26
|
+
// source:能按 (条目 id, 版本 key, 文件名) 取到 JSON 的对象;网站的 IndexStorage 本身就满足
|
|
27
|
+
const result = await buildExport({
|
|
28
|
+
id, versionKey: 'default', chapter, // chapter 是 index.json 的 chapters[] 一项
|
|
29
|
+
format: 'entity', links: true, glyph: 'simplified', // lines | punct | entity;original | traditional | simplified
|
|
30
|
+
bookTitle: '欽定四庫全書總目',
|
|
31
|
+
}, source, converters); // converters 仅 glyph≠original 需要
|
|
32
|
+
saveText(result); // { filename, text }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- `availableFormats(chapter)`:本册能出哪几种。
|
|
36
|
+
- `converters = createGlyphConverters({ opencc: Converter /* opencc-js/t2cn */, variants /* 异体字→正字表 */ })`。opencc-js、异体字表(`book-index-ui/variant-chars.json`)由调用方提供,本包不强依赖。
|
|
37
|
+
- 低层:`renderLines` / `renderRich` / `frontMatter`(纯函数,不取数)。
|
|
38
|
+
|
|
39
|
+
Node 脚本:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { buildExport } from 'book-text-util';
|
|
43
|
+
import { dirSource, writeExport } from 'book-text-util/node';
|
|
44
|
+
const r = await buildExport(req, dirSource('/path/to/book-text/.../version-dir'));
|
|
45
|
+
await writeExport('out', r);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## 二、版本对校
|
|
49
|
+
|
|
50
|
+
底本字格序列与对照本(维基文庫整理出的 md,或同书另一版本)现场比对,不入库。
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { compareWhole, buildVariantTest, runCompare } from 'book-text-util';
|
|
54
|
+
const cells = [{ c: '天', a: '1:1:1' }, /* … */]; // 阅读顺序,a = 页:列:序
|
|
55
|
+
const { items, segments } = await compareWhole({ cells, md, file: '001', label: '本册', isVariant: buildVariantTest(table) });
|
|
56
|
+
// item.cat: glyph 异体 | word 用字不同 | add 增 | omit 脱 | struct 结构
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
- `compareChapter`:按卷分段(册↔卷对应);`runCompare`:连取数一起做(传 `transport`,不给 `isVariant` 时按浏览器环境取 `/compare/glyph-variants.json`,Node 请自己传)。
|
|
60
|
+
- 异体字判定用 open-guji-cv 的 `edges.tsv` 抽出的表(`{ pairs: "葢蓋\n畧略…" }`)。
|
|
61
|
+
- 其余:`diffChars`、`classify`、`wikiChars`/`chapterChars`、`parseCmp`/`formatCmp`(`?cmp=` 链接)。
|
|
62
|
+
- 界面相关(标记装饰 `decorate`、偏好存储 `prefs`、React 钩子)留在网站,不在本包。
|
|
63
|
+
|
|
64
|
+
## 三、命令行
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
npx book-text-util export --root <版本目录> --chapter 020 --format entity --links --glyph simplified --out out/
|
|
68
|
+
npx book-text-util compare --base 底本.txt --ref 对照本.md [--variants glyph-variants.json] [--json]
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`--glyph` 非 original 需额外安装 `opencc-js` 与 `book-index-ui`。
|
|
72
|
+
|
|
73
|
+
## 开发
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
cd npm && npm ci && npm run typecheck && npm test && npm run build
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
改导出规则须与 book-text 的 Python 生成器一起改:`python3 tools/export-golden.py --book-text <book-text 检出>` 重出正文答案,`UPDATE_GOLDEN=1 npm test` 重出整文件答案。
|
|
80
|
+
|
|
81
|
+
## 发布
|
|
82
|
+
|
|
83
|
+
Actions →「Publish book-text-util」→ Run workflow(`dry_run` 可先试)。走 npm Trusted Publishing,不存 token;发布前把 `npm/package.json` 的 version 升好(默认只升 patch)。首次需在 npmjs.com 的包设置里登记 Trusted Publisher:`open-guji / book-text-util / publish.yml`。
|
package/dist/browser.cjs
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/browser.ts
|
|
21
|
+
var browser_exports = {};
|
|
22
|
+
__export(browser_exports, {
|
|
23
|
+
httpSource: () => httpSource,
|
|
24
|
+
saveText: () => saveText
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(browser_exports);
|
|
27
|
+
function saveText({ filename, text }) {
|
|
28
|
+
const blob = new Blob([text], { type: "text/markdown;charset=utf-8" });
|
|
29
|
+
const url = URL.createObjectURL(blob);
|
|
30
|
+
const a = document.createElement("a");
|
|
31
|
+
a.href = url;
|
|
32
|
+
a.download = filename;
|
|
33
|
+
document.body.appendChild(a);
|
|
34
|
+
a.click();
|
|
35
|
+
a.remove();
|
|
36
|
+
setTimeout(() => URL.revokeObjectURL(url), 1e4);
|
|
37
|
+
}
|
|
38
|
+
function httpSource(base, fetchImpl = fetch) {
|
|
39
|
+
return {
|
|
40
|
+
async getTextFile(id, key, file) {
|
|
41
|
+
const res = await fetchImpl(`${base.replace(/\/$/, "")}/${id}/${key}/${file}`);
|
|
42
|
+
return res.ok ? res.json() : null;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
httpSource,
|
|
49
|
+
saveText
|
|
50
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { E as ExportResult } from './export-Ct8PXBYj.cjs';
|
|
2
|
+
|
|
3
|
+
/** 浏览器端:把生成好的文件存成下载(Blob + 临时 <a download>)。需要 DOM。 */
|
|
4
|
+
|
|
5
|
+
declare function saveText({ filename, text }: ExportResult): void;
|
|
6
|
+
/** 同域/远端 HTTP 取数来源:`<base>/<id>/<key>/<file>`(网站是 `/data/items`)。取不到返回 null */
|
|
7
|
+
declare function httpSource(base: string, fetchImpl?: typeof fetch): {
|
|
8
|
+
getTextFile(id: string, key: string, file: string): Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { httpSource, saveText };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { E as ExportResult } from './export-Ct8PXBYj.js';
|
|
2
|
+
|
|
3
|
+
/** 浏览器端:把生成好的文件存成下载(Blob + 临时 <a download>)。需要 DOM。 */
|
|
4
|
+
|
|
5
|
+
declare function saveText({ filename, text }: ExportResult): void;
|
|
6
|
+
/** 同域/远端 HTTP 取数来源:`<base>/<id>/<key>/<file>`(网站是 `/data/items`)。取不到返回 null */
|
|
7
|
+
declare function httpSource(base: string, fetchImpl?: typeof fetch): {
|
|
8
|
+
getTextFile(id: string, key: string, file: string): Promise<unknown>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export { httpSource, saveText };
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/browser.ts
|
|
2
|
+
function saveText({ filename, text }) {
|
|
3
|
+
const blob = new Blob([text], { type: "text/markdown;charset=utf-8" });
|
|
4
|
+
const url = URL.createObjectURL(blob);
|
|
5
|
+
const a = document.createElement("a");
|
|
6
|
+
a.href = url;
|
|
7
|
+
a.download = filename;
|
|
8
|
+
document.body.appendChild(a);
|
|
9
|
+
a.click();
|
|
10
|
+
a.remove();
|
|
11
|
+
setTimeout(() => URL.revokeObjectURL(url), 1e4);
|
|
12
|
+
}
|
|
13
|
+
function httpSource(base, fetchImpl = fetch) {
|
|
14
|
+
return {
|
|
15
|
+
async getTextFile(id, key, file) {
|
|
16
|
+
const res = await fetchImpl(`${base.replace(/\/$/, "")}/${id}/${key}/${file}`);
|
|
17
|
+
return res.ok ? res.json() : null;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
httpSource,
|
|
23
|
+
saveText
|
|
24
|
+
};
|