@cn-math-notation/core 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +36 -2
- package/dist/dengshi/expr.d.ts +21 -0
- package/dist/dengshi/expr.js +156 -0
- package/dist/dengshi/index.d.ts +13 -0
- package/dist/dengshi/index.js +41 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +18 -0
- package/dist/model.d.ts +47 -0
- package/dist/model.js +7 -0
- package/dist/options.d.ts +41 -0
- package/dist/options.js +69 -0
- package/dist/parse-layout.d.ts +31 -0
- package/dist/parse-layout.js +273 -0
- package/dist/render-svg.d.ts +29 -0
- package/dist/render-svg.js +219 -0
- package/dist/semantic/add-sub.d.ts +6 -0
- package/dist/semantic/add-sub.js +76 -0
- package/dist/semantic/answer.d.ts +9 -0
- package/dist/semantic/answer.js +35 -0
- package/dist/semantic/div.d.ts +8 -0
- package/dist/semantic/div.js +87 -0
- package/dist/semantic/index.d.ts +10 -0
- package/dist/semantic/index.js +29 -0
- package/dist/semantic/mul.d.ts +14 -0
- package/dist/semantic/mul.js +107 -0
- package/dist/semantic/numbers.d.ts +37 -0
- package/dist/semantic/numbers.js +66 -0
- package/dist/semantic/parse-expr.d.ts +16 -0
- package/dist/semantic/parse-expr.js +40 -0
- package/package.json +46 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 cn-math-notation contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cn-math-notation/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
中国中小学数学写法库的核心:把竖式写成几行文字,渲染成和课本一样的 SVG。**不依赖 KaTeX**,可以单独使用,也可以给其他编辑器写插件。
|
|
4
|
+
|
|
5
|
+
<img src="https://raw.githubusercontent.com/n7tequila/cn-math-notation/main/docs/images/sem-sub-zero.svg" alt="403−167 竖式" height="150" /> <img src="https://raw.githubusercontent.com/n7tequila/cn-math-notation/main/docs/images/sem-div.svg" alt="812÷4 竖式" height="250" />
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cn-math-notation/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { renderShushi, renderShushiLayout } from "@cn-math-notation/core";
|
|
13
|
+
|
|
14
|
+
// 语义式:只写算式,程序按课本步骤算出竖式
|
|
15
|
+
const { svg, label } = renderShushi("403-167");
|
|
16
|
+
|
|
17
|
+
// 直写布局:逐行写出,写什么画什么(例如孩子写错的过程)
|
|
18
|
+
const layout = renderShushiLayout(String.raw`\tui{4} 0 3 \\ - 1 6 7 \\ \hline \err{34} 6`);
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- `svg`:SVG 字符串,颜色跟随外层文字(`currentColor`),尺寸以 em 计。
|
|
22
|
+
- `label`:给屏幕阅读器的文字描述。
|
|
23
|
+
|
|
24
|
+
递等式(脱式计算)返回 TeX,交给 KaTeX、MathJax 等任何 TeX 渲染器:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { buildDengshi, dengshiSteps } from "@cn-math-notation/core";
|
|
28
|
+
|
|
29
|
+
dengshiSteps("25×(40+4)"); // ["25\\times (40+4)", "25\\times 44", "1100"]
|
|
30
|
+
buildDengshi("25×(40+4)"); // "\\begin{aligned}&25\\times (40+4)\\\\[6pt]={}&…\\end{aligned}"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
在网页里配合 KaTeX 使用,请用 [@cn-math-notation/katex](https://www.npmjs.com/package/@cn-math-notation/katex)。
|
|
34
|
+
|
|
35
|
+
完整写法、样式选项与例子见 [GitHub](https://github.com/n7tequila/cn-math-notation) 与[使用说明](https://github.com/n7tequila/cn-math-notation/blob/main/docs/usage.md)。
|
|
36
|
+
|
|
37
|
+
许可:MIT
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type BinOp = "+" | "-" | "×" | "÷";
|
|
2
|
+
export type Bracket = "(" | "[";
|
|
3
|
+
export type Node = {
|
|
4
|
+
readonly k: "num";
|
|
5
|
+
readonly v: bigint;
|
|
6
|
+
} | {
|
|
7
|
+
readonly k: "op";
|
|
8
|
+
readonly op: BinOp;
|
|
9
|
+
readonly l: Node;
|
|
10
|
+
readonly r: Node;
|
|
11
|
+
} | {
|
|
12
|
+
readonly k: "paren";
|
|
13
|
+
readonly br: Bracket;
|
|
14
|
+
readonly inner: Node;
|
|
15
|
+
};
|
|
16
|
+
/** 递归下降:expr := term (± term)*;term := factor (×÷ factor)*;factor := 数 | (expr) | [expr]。 */
|
|
17
|
+
export declare function parseArith(source: string): Node;
|
|
18
|
+
/** 逐步化简,返回每一步的语法树(第一项是原式,最后一项是结果)。 */
|
|
19
|
+
export declare function simplifySteps(root: Node): Node[];
|
|
20
|
+
/** 语法树 → TeX(括号都来自原式,照原样写回)。 */
|
|
21
|
+
export declare function toTex(n: Node): string;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 递等式(脱式计算)的算式:解析成语法树、按课本运算顺序逐步化简、写回 TeX。
|
|
3
|
+
*
|
|
4
|
+
* 运算顺序(课本):有括号先算括号里面的(小括号 → 中括号,由里往外);
|
|
5
|
+
* 同一层先乘除、后加减;同级从左往右。互不影响的几处在同一步里一起算
|
|
6
|
+
* (如 25×4+36÷6 → 100+6;(12+8)×(5-3) → 20×2),但括号外的运算要等括号都算完。
|
|
7
|
+
* 第一期只做整数;减法不能出现负数,除法要能整除。
|
|
8
|
+
*/
|
|
9
|
+
import { ExpressionError } from "../semantic/numbers.js";
|
|
10
|
+
const SYMBOLS = [
|
|
11
|
+
[/^\d+/, (m) => ({ t: "num", v: BigInt(m) })],
|
|
12
|
+
[/^\+/, () => ({ t: "op", op: "+" })],
|
|
13
|
+
[/^(?:-|−)/, () => ({ t: "op", op: "-" })],
|
|
14
|
+
[/^(?:\\times|×|\*)/, () => ({ t: "op", op: "×" })],
|
|
15
|
+
[/^(?:\\div|÷|\/)/, () => ({ t: "op", op: "÷" })],
|
|
16
|
+
[/^(?:\\left)?(?:\(|()/, () => ({ t: "open", br: "(" })],
|
|
17
|
+
[/^(?:\\right)?(?:\)|))/, () => ({ t: "close", br: "(" })],
|
|
18
|
+
[/^(?:\\left)?(?:\[|[|\\lbrack)/, () => ({ t: "open", br: "[" })],
|
|
19
|
+
[/^(?:\\right)?(?:\]|]|\\rbrack)/, () => ({ t: "close", br: "[" })],
|
|
20
|
+
];
|
|
21
|
+
function tokenize(source) {
|
|
22
|
+
let rest = source.replace(/\\nobreakspace|~|\s+/g, "");
|
|
23
|
+
if (rest === "")
|
|
24
|
+
throw new ExpressionError("算式是空的");
|
|
25
|
+
const tokens = [];
|
|
26
|
+
while (rest !== "") {
|
|
27
|
+
// 小数要先于整数判断
|
|
28
|
+
const decimal = /^\d+\.\d+/.exec(rest);
|
|
29
|
+
if (decimal !== null)
|
|
30
|
+
throw new ExpressionError("小数的递等式属第二期");
|
|
31
|
+
const hit = SYMBOLS.find(([re]) => re.test(rest));
|
|
32
|
+
if (hit === undefined)
|
|
33
|
+
throw new ExpressionError(`不认识的符号:“${rest}”`);
|
|
34
|
+
const m = hit[0].exec(rest)[0];
|
|
35
|
+
tokens.push(hit[1](m));
|
|
36
|
+
rest = rest.slice(m.length);
|
|
37
|
+
}
|
|
38
|
+
return tokens;
|
|
39
|
+
}
|
|
40
|
+
/** 递归下降:expr := term (± term)*;term := factor (×÷ factor)*;factor := 数 | (expr) | [expr]。 */
|
|
41
|
+
export function parseArith(source) {
|
|
42
|
+
const tokens = tokenize(source);
|
|
43
|
+
let pos = 0;
|
|
44
|
+
const peek = () => tokens[pos];
|
|
45
|
+
const factor = () => {
|
|
46
|
+
const tok = peek();
|
|
47
|
+
if (tok === undefined)
|
|
48
|
+
throw new ExpressionError("算式不完整:结尾少了一个数");
|
|
49
|
+
if (tok.t === "num") {
|
|
50
|
+
pos += 1;
|
|
51
|
+
return { k: "num", v: tok.v };
|
|
52
|
+
}
|
|
53
|
+
if (tok.t === "open") {
|
|
54
|
+
pos += 1;
|
|
55
|
+
const inner = expr();
|
|
56
|
+
const close = peek();
|
|
57
|
+
if (close?.t !== "close" || close.br !== tok.br) {
|
|
58
|
+
throw new ExpressionError(tok.br === "(" ? "小括号没有配对" : "中括号没有配对");
|
|
59
|
+
}
|
|
60
|
+
pos += 1;
|
|
61
|
+
return { k: "paren", br: tok.br, inner };
|
|
62
|
+
}
|
|
63
|
+
throw new ExpressionError(tok.t === "close" ? "多了一个右括号" : "运算符前面少了一个数");
|
|
64
|
+
};
|
|
65
|
+
const chain = (next, ops) => () => {
|
|
66
|
+
let node = next();
|
|
67
|
+
for (let tok = peek(); tok?.t === "op" && ops.includes(tok.op); tok = peek()) {
|
|
68
|
+
pos += 1;
|
|
69
|
+
node = { k: "op", op: tok.op, l: node, r: next() };
|
|
70
|
+
}
|
|
71
|
+
return node;
|
|
72
|
+
};
|
|
73
|
+
const term = chain(factor, ["×", "÷"]);
|
|
74
|
+
const expr = chain(term, ["+", "-"]);
|
|
75
|
+
const root = expr();
|
|
76
|
+
if (pos < tokens.length) {
|
|
77
|
+
const tok = tokens[pos];
|
|
78
|
+
throw new ExpressionError(tok.t === "close" ? "多了一个右括号" : "两个数之间少了运算符");
|
|
79
|
+
}
|
|
80
|
+
return root;
|
|
81
|
+
}
|
|
82
|
+
function compute(op, a, b) {
|
|
83
|
+
switch (op) {
|
|
84
|
+
case "+":
|
|
85
|
+
return a + b;
|
|
86
|
+
case "-":
|
|
87
|
+
if (a < b)
|
|
88
|
+
throw new ExpressionError(`${a}−${b} 得负数,小学递等式不处理负数`);
|
|
89
|
+
return a - b;
|
|
90
|
+
case "×":
|
|
91
|
+
return a * b;
|
|
92
|
+
case "÷":
|
|
93
|
+
if (b === 0n)
|
|
94
|
+
throw new ExpressionError("除数不能是 0");
|
|
95
|
+
if (a % b !== 0n)
|
|
96
|
+
throw new ExpressionError(`${a}÷${b} 除不尽,递等式里的除法要能整除`);
|
|
97
|
+
return a / b;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function containsParen(n) {
|
|
101
|
+
return n.k === "paren" || (n.k === "op" && (containsParen(n.l) || containsParen(n.r)));
|
|
102
|
+
}
|
|
103
|
+
function containsOp(n, ops) {
|
|
104
|
+
return n.k === "op" && (ops.includes(n.op) || containsOp(n.l, ops) || containsOp(n.r, ops));
|
|
105
|
+
}
|
|
106
|
+
/** 把两个子节点都是数、且属于 ops 的运算全部算掉(同一步里互不影响的几处一起算)。 */
|
|
107
|
+
function reduceReady(n, ops) {
|
|
108
|
+
if (n.k !== "op")
|
|
109
|
+
return n;
|
|
110
|
+
if (ops.includes(n.op) && n.l.k === "num" && n.r.k === "num")
|
|
111
|
+
return { k: "num", v: compute(n.op, n.l.v, n.r.v) };
|
|
112
|
+
return { ...n, l: reduceReady(n.l, ops), r: reduceReady(n.r, ops) };
|
|
113
|
+
}
|
|
114
|
+
/** 一层(括号外或某个括号内)往前算一步。 */
|
|
115
|
+
function stepScope(n) {
|
|
116
|
+
if (containsParen(n))
|
|
117
|
+
return stepParens(n);
|
|
118
|
+
return containsOp(n, ["×", "÷"]) ? reduceReady(n, ["×", "÷"]) : reduceReady(n, ["+", "-"]);
|
|
119
|
+
}
|
|
120
|
+
/** 有括号时只算括号里面:各个括号各自往前一步,算成一个数时去掉括号。 */
|
|
121
|
+
function stepParens(n) {
|
|
122
|
+
if (n.k === "paren") {
|
|
123
|
+
const inner = stepScope(n.inner);
|
|
124
|
+
return inner.k === "num" ? inner : { ...n, inner };
|
|
125
|
+
}
|
|
126
|
+
if (n.k === "op")
|
|
127
|
+
return { ...n, l: stepParens(n.l), r: stepParens(n.r) };
|
|
128
|
+
return n;
|
|
129
|
+
}
|
|
130
|
+
const MAX_STEPS = 100;
|
|
131
|
+
/** 逐步化简,返回每一步的语法树(第一项是原式,最后一项是结果)。 */
|
|
132
|
+
export function simplifySteps(root) {
|
|
133
|
+
const steps = [root];
|
|
134
|
+
let cur = root;
|
|
135
|
+
while (cur.k !== "num") {
|
|
136
|
+
if (steps.length > MAX_STEPS)
|
|
137
|
+
throw new ExpressionError("算式太长了");
|
|
138
|
+
cur = stepScope(cur);
|
|
139
|
+
steps.push(cur);
|
|
140
|
+
}
|
|
141
|
+
if (steps.length === 1)
|
|
142
|
+
throw new ExpressionError("只有一个数,不需要递等式");
|
|
143
|
+
return steps;
|
|
144
|
+
}
|
|
145
|
+
const OP_TEX = { "+": "+", "-": "-", "×": "\\times ", "÷": "\\div " };
|
|
146
|
+
/** 语法树 → TeX(括号都来自原式,照原样写回)。 */
|
|
147
|
+
export function toTex(n) {
|
|
148
|
+
switch (n.k) {
|
|
149
|
+
case "num":
|
|
150
|
+
return n.v.toString();
|
|
151
|
+
case "op":
|
|
152
|
+
return `${toTex(n.l)}${OP_TEX[n.op]}${toTex(n.r)}`;
|
|
153
|
+
case "paren":
|
|
154
|
+
return n.br === "(" ? `(${toTex(n.inner)})` : `[${toTex(n.inner)}]`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { parseArith, simplifySteps, toTex as arithToTex, type Node as ArithNode } from "./expr.ts";
|
|
2
|
+
/** 把若干行算式(不含等号)排成递等式的 TeX。 */
|
|
3
|
+
export declare function dengshiTex(lines: readonly string[]): string;
|
|
4
|
+
/** 语义式:只写算式,按运算顺序逐步化简,每一步一行。返回每行的 TeX(不含等号)。 */
|
|
5
|
+
export declare function dengshiSteps(expression: string): string[];
|
|
6
|
+
/** 语义式一步到位:算式 → 递等式 TeX。 */
|
|
7
|
+
export declare function buildDengshi(expression: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* 直写:`25\times(40+4) \\ 25\times44 \\ 1100`,用 `\\` 分行,每行一步。
|
|
10
|
+
* 行首写不写等号都可以(写了会去掉,统一由排版加上悬在左边的等号)。
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseDengshiLines(source: string): string[];
|
|
13
|
+
export declare function buildDengshiLayout(source: string): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 递等式:等号悬在算式左边(维护者 2026-11-26 定),各行算式左端对齐,第一行不写等号。
|
|
3
|
+
* 排版交给 TeX 的 aligned 环境(第一列放等号、右对齐,第二列放算式、左对齐),
|
|
4
|
+
* 所以本模块只产出 TeX 字符串,KaTeX、MathJax 都能排。
|
|
5
|
+
*/
|
|
6
|
+
import { ExpressionError } from "../semantic/numbers.js";
|
|
7
|
+
import { parseArith, simplifySteps, toTex } from "./expr.js";
|
|
8
|
+
export { parseArith, simplifySteps, toTex as arithToTex } from "./expr.js";
|
|
9
|
+
/** 行距,与课本里每步一行的疏朗程度接近。 */
|
|
10
|
+
const ROW_GAP = "6pt";
|
|
11
|
+
/** 把若干行算式(不含等号)排成递等式的 TeX。 */
|
|
12
|
+
export function dengshiTex(lines) {
|
|
13
|
+
if (lines.length === 0)
|
|
14
|
+
throw new ExpressionError("递等式至少要有一行");
|
|
15
|
+
const rows = lines.map((line, i) => (i === 0 ? `&${line}` : `={}&${line}`));
|
|
16
|
+
return `\\begin{aligned}${rows.join(`\\\\[${ROW_GAP}]`)}\\end{aligned}`;
|
|
17
|
+
}
|
|
18
|
+
/** 语义式:只写算式,按运算顺序逐步化简,每一步一行。返回每行的 TeX(不含等号)。 */
|
|
19
|
+
export function dengshiSteps(expression) {
|
|
20
|
+
return simplifySteps(parseArith(expression)).map(toTex);
|
|
21
|
+
}
|
|
22
|
+
/** 语义式一步到位:算式 → 递等式 TeX。 */
|
|
23
|
+
export function buildDengshi(expression) {
|
|
24
|
+
return dengshiTex(dengshiSteps(expression));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 直写:`25\times(40+4) \\ 25\times44 \\ 1100`,用 `\\` 分行,每行一步。
|
|
28
|
+
* 行首写不写等号都可以(写了会去掉,统一由排版加上悬在左边的等号)。
|
|
29
|
+
*/
|
|
30
|
+
export function parseDengshiLines(source) {
|
|
31
|
+
const lines = source
|
|
32
|
+
.split(/\\\\(?:\[[^\]]*\])?/)
|
|
33
|
+
.map((line) => line.trim().replace(/^=\s*/, "").trim())
|
|
34
|
+
.filter((line) => line !== "");
|
|
35
|
+
if (lines.length === 0)
|
|
36
|
+
throw new ExpressionError("递等式至少要有一行");
|
|
37
|
+
return lines;
|
|
38
|
+
}
|
|
39
|
+
export function buildDengshiLayout(source) {
|
|
40
|
+
return dengshiTex(parseDengshiLines(source));
|
|
41
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type * from "./model.ts";
|
|
2
|
+
export { buildDengshi, buildDengshiLayout, dengshiSteps, dengshiTex, parseDengshiLines, parseArith, simplifySteps, arithToTex, type ArithNode, } from "./dengshi/index.ts";
|
|
3
|
+
export { buildSemantic, parseExpression, ExpressionError, DIGIT_COLORS, type Expression } from "./semantic/index.ts";
|
|
4
|
+
export { parseLayout, tokenize, assignColumns, LayoutSyntaxError } from "./parse-layout.ts";
|
|
5
|
+
export { renderLayout, describe, FONT_SIZE, COLORS, type RenderedSvg, type RenderOptions, type RootDisplay, } from "./render-svg.ts";
|
|
6
|
+
export { DEFAULT_STYLE, parseOptions, OptionError, type StyleOptions, type CarryAt, type OperatorPlacement, type MulMode, type ProcessMode, } from "./options.ts";
|
|
7
|
+
import { type StyleOptions } from "./options.ts";
|
|
8
|
+
import { type RenderedSvg, type RenderOptions } from "./render-svg.ts";
|
|
9
|
+
/** 直写布局一步到位:原文 + 选项原文 → SVG。base 是未写选项时的默认样式。 */
|
|
10
|
+
export declare function renderShushiLayout(source: string, options?: string, base?: StyleOptions, render?: RenderOptions): RenderedSvg;
|
|
11
|
+
/** 语义式一步到位:算式(如 `403-167`)+ 选项原文 → SVG。竖式过程由程序算出。 */
|
|
12
|
+
export declare function renderShushi(expression: string, options?: string, base?: StyleOptions, render?: RenderOptions): RenderedSvg;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { buildDengshi, buildDengshiLayout, dengshiSteps, dengshiTex, parseDengshiLines, parseArith, simplifySteps, arithToTex, } from "./dengshi/index.js";
|
|
2
|
+
export { buildSemantic, parseExpression, ExpressionError, DIGIT_COLORS } from "./semantic/index.js";
|
|
3
|
+
export { parseLayout, tokenize, assignColumns, LayoutSyntaxError } from "./parse-layout.js";
|
|
4
|
+
export { renderLayout, describe, FONT_SIZE, COLORS, } from "./render-svg.js";
|
|
5
|
+
export { DEFAULT_STYLE, parseOptions, OptionError, } from "./options.js";
|
|
6
|
+
import { parseLayout } from "./parse-layout.js";
|
|
7
|
+
import { buildSemantic } from "./semantic/index.js";
|
|
8
|
+
import { DEFAULT_STYLE, parseOptions } from "./options.js";
|
|
9
|
+
import { renderLayout } from "./render-svg.js";
|
|
10
|
+
/** 直写布局一步到位:原文 + 选项原文 → SVG。base 是未写选项时的默认样式。 */
|
|
11
|
+
export function renderShushiLayout(source, options, base = DEFAULT_STYLE, render = {}) {
|
|
12
|
+
return renderLayout(parseLayout(source), parseOptions(options, base), render);
|
|
13
|
+
}
|
|
14
|
+
/** 语义式一步到位:算式(如 `403-167`)+ 选项原文 → SVG。竖式过程由程序算出。 */
|
|
15
|
+
export function renderShushi(expression, options, base = DEFAULT_STYLE, render = {}) {
|
|
16
|
+
const style = parseOptions(options, base);
|
|
17
|
+
return renderLayout(buildSemantic(expression, style), style, render);
|
|
18
|
+
}
|
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 竖式布局模型(第一期:整数与小数竖式、长除法)。
|
|
3
|
+
*
|
|
4
|
+
* 列号:个位为 0,向高位为正,向小数位为负;小数点本身占 -0.5 这一“窄列”。
|
|
5
|
+
* 设计依据:docs/design/2026-11-19-设计草案.md §3、§11。
|
|
6
|
+
*/
|
|
7
|
+
export type CellMark = "tui" | "err" | "hong" | "bu" | "hua";
|
|
8
|
+
export type CellContent = {
|
|
9
|
+
readonly kind: "digit";
|
|
10
|
+
readonly text: string;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: "point";
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: "blank";
|
|
15
|
+
};
|
|
16
|
+
export interface Cell {
|
|
17
|
+
readonly col: number;
|
|
18
|
+
readonly content: CellContent;
|
|
19
|
+
readonly marks: readonly CellMark[];
|
|
20
|
+
readonly color?: string;
|
|
21
|
+
}
|
|
22
|
+
export type Operator = "+" | "-" | "×" | "÷";
|
|
23
|
+
/** 普通数字行(加数、部分积、结果、除法各步)。 */
|
|
24
|
+
export interface DigitRow {
|
|
25
|
+
readonly kind: "digits";
|
|
26
|
+
readonly cells: readonly Cell[];
|
|
27
|
+
readonly operator?: Operator;
|
|
28
|
+
/** 长除法:商行。 */
|
|
29
|
+
readonly quotient?: true;
|
|
30
|
+
/** 长除法:被除数行,带除数。 */
|
|
31
|
+
readonly divisor?: string;
|
|
32
|
+
}
|
|
33
|
+
/** 进位行:每个数字记在“进到的那一列”。 */
|
|
34
|
+
export interface CarryRow {
|
|
35
|
+
readonly kind: "carry";
|
|
36
|
+
readonly cells: readonly Cell[];
|
|
37
|
+
}
|
|
38
|
+
/** 横线:画在前一行与后一行之间。 */
|
|
39
|
+
export interface RuleRow {
|
|
40
|
+
readonly kind: "hline";
|
|
41
|
+
}
|
|
42
|
+
export type Row = DigitRow | CarryRow | RuleRow;
|
|
43
|
+
export interface Layout {
|
|
44
|
+
readonly rows: readonly Row[];
|
|
45
|
+
/** 是否出现过小数点(决定按小数点对齐、是否画数位虚线等)。 */
|
|
46
|
+
readonly hasDecimal: boolean;
|
|
47
|
+
}
|
package/dist/model.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 写法样式选项。默认值即沪教版(维护者 2026-11-25 裁决,设计草案 §11.1)。
|
|
3
|
+
*/
|
|
4
|
+
export type CarryAt = "s" | "sw";
|
|
5
|
+
export type OperatorPlacement = "far" | "adjacent";
|
|
6
|
+
/** 一位数乘法:简写(进位小字)或分步式(每位分别乘再相加)。 */
|
|
7
|
+
export type MulMode = "compact" | "expanded";
|
|
8
|
+
/**
|
|
9
|
+
* 语义式是否画计算过程(进位、退位点、补的 0、部分积、除法各步)。
|
|
10
|
+
* auto:没写孩子答案时画;写了孩子答案时不画——程序按正确算法画出的过程会被误当成孩子写的。
|
|
11
|
+
*/
|
|
12
|
+
export type ProcessMode = "auto" | "show" | "hide";
|
|
13
|
+
export interface StyleOptions {
|
|
14
|
+
/** 进位水平位置:s = 进到的那一列正下;sw = 产生进位那一位的左下。 */
|
|
15
|
+
readonly carryAt: CarryAt;
|
|
16
|
+
/** 数位虚线。 */
|
|
17
|
+
readonly guides: boolean;
|
|
18
|
+
/** 多位乘法是否画进位(语义式用;直写布局按写的为准)。 */
|
|
19
|
+
readonly multiCarry: boolean;
|
|
20
|
+
/** 小数位数不够时补 0(语义式用)。 */
|
|
21
|
+
readonly padZero: boolean;
|
|
22
|
+
/** 按乘数位着色(语义式用)。 */
|
|
23
|
+
readonly colorByDigit: boolean;
|
|
24
|
+
/** 运算符放最左(far)还是紧挨数字(adjacent)。 */
|
|
25
|
+
readonly operatorPlacement: OperatorPlacement;
|
|
26
|
+
/** 一位数乘法的写法(语义式用)。 */
|
|
27
|
+
readonly mode: MulMode;
|
|
28
|
+
/** 小数结果末尾的 0 划掉(语义式用,维护者 2026-11-25 定默认开)。 */
|
|
29
|
+
readonly strikeZero: boolean;
|
|
30
|
+
/** 是否画计算过程(语义式用)。 */
|
|
31
|
+
readonly process: ProcessMode;
|
|
32
|
+
}
|
|
33
|
+
export declare const DEFAULT_STYLE: StyleOptions;
|
|
34
|
+
export declare class OptionError extends Error {
|
|
35
|
+
readonly name = "OptionError";
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 解析方括号里的选项,如 `carryAt=sw, guides=false, multiCarry`。
|
|
39
|
+
* 只写键名表示 true。未知键或非法值抛 OptionError。
|
|
40
|
+
*/
|
|
41
|
+
export declare function parseOptions(source: string | undefined, base?: StyleOptions): StyleOptions;
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 写法样式选项。默认值即沪教版(维护者 2026-11-25 裁决,设计草案 §11.1)。
|
|
3
|
+
*/
|
|
4
|
+
export const DEFAULT_STYLE = {
|
|
5
|
+
carryAt: "s",
|
|
6
|
+
guides: true,
|
|
7
|
+
multiCarry: false,
|
|
8
|
+
padZero: true,
|
|
9
|
+
colorByDigit: false,
|
|
10
|
+
operatorPlacement: "far",
|
|
11
|
+
mode: "compact",
|
|
12
|
+
strikeZero: true,
|
|
13
|
+
process: "auto",
|
|
14
|
+
};
|
|
15
|
+
const BOOLEAN_KEYS = ["guides", "multiCarry", "padZero", "colorByDigit", "strikeZero"];
|
|
16
|
+
export class OptionError extends Error {
|
|
17
|
+
name = "OptionError";
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 解析方括号里的选项,如 `carryAt=sw, guides=false, multiCarry`。
|
|
21
|
+
* 只写键名表示 true。未知键或非法值抛 OptionError。
|
|
22
|
+
*/
|
|
23
|
+
export function parseOptions(source, base = DEFAULT_STYLE) {
|
|
24
|
+
if (source === undefined || source.trim() === "")
|
|
25
|
+
return base;
|
|
26
|
+
let result = base;
|
|
27
|
+
for (const part of source.split(",")) {
|
|
28
|
+
const item = part.trim();
|
|
29
|
+
if (item === "")
|
|
30
|
+
continue;
|
|
31
|
+
const eq = item.indexOf("=");
|
|
32
|
+
const key = (eq === -1 ? item : item.slice(0, eq)).trim();
|
|
33
|
+
const value = eq === -1 ? "true" : item.slice(eq + 1).trim();
|
|
34
|
+
result = applyOption(result, key, value);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
function applyOption(opts, key, value) {
|
|
39
|
+
if (BOOLEAN_KEYS.includes(key)) {
|
|
40
|
+
if (value !== "true" && value !== "false") {
|
|
41
|
+
throw new OptionError(`选项 ${key} 只能是 true 或 false,收到 "${value}"`);
|
|
42
|
+
}
|
|
43
|
+
return { ...opts, [key]: value === "true" };
|
|
44
|
+
}
|
|
45
|
+
if (key === "carryAt") {
|
|
46
|
+
if (value !== "s" && value !== "sw")
|
|
47
|
+
throw new OptionError(`carryAt 只能是 s 或 sw,收到 "${value}"`);
|
|
48
|
+
return { ...opts, carryAt: value };
|
|
49
|
+
}
|
|
50
|
+
if (key === "operatorPlacement") {
|
|
51
|
+
if (value !== "far" && value !== "adjacent") {
|
|
52
|
+
throw new OptionError(`operatorPlacement 只能是 far 或 adjacent,收到 "${value}"`);
|
|
53
|
+
}
|
|
54
|
+
return { ...opts, operatorPlacement: value };
|
|
55
|
+
}
|
|
56
|
+
if (key === "mode") {
|
|
57
|
+
if (value !== "compact" && value !== "expanded") {
|
|
58
|
+
throw new OptionError(`mode 只能是 compact 或 expanded,收到 "${value}"`);
|
|
59
|
+
}
|
|
60
|
+
return { ...opts, mode: value };
|
|
61
|
+
}
|
|
62
|
+
if (key === "process") {
|
|
63
|
+
if (value !== "auto" && value !== "show" && value !== "hide") {
|
|
64
|
+
throw new OptionError(`process 只能是 auto、show 或 hide,收到 "${value}"`);
|
|
65
|
+
}
|
|
66
|
+
return { ...opts, process: value };
|
|
67
|
+
}
|
|
68
|
+
throw new OptionError(`未知选项 "${key}"`);
|
|
69
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 直写布局解析:`\shushi*{…}` 花括号里的原文 → Layout。
|
|
3
|
+
* 语法见 docs/design/2026-11-25-直写布局语法试写.md(R1~R9;R10 的 & 分格属第三期,暂报错)。
|
|
4
|
+
*/
|
|
5
|
+
import type { Cell, CellContent, CellMark, Layout } from "./model.ts";
|
|
6
|
+
export declare class LayoutSyntaxError extends Error {
|
|
7
|
+
readonly name = "LayoutSyntaxError";
|
|
8
|
+
}
|
|
9
|
+
type Token = {
|
|
10
|
+
readonly t: "cmd";
|
|
11
|
+
readonly name: string;
|
|
12
|
+
} | {
|
|
13
|
+
readonly t: "open";
|
|
14
|
+
} | {
|
|
15
|
+
readonly t: "close";
|
|
16
|
+
} | {
|
|
17
|
+
readonly t: "newrow";
|
|
18
|
+
} | {
|
|
19
|
+
readonly t: "char";
|
|
20
|
+
readonly ch: string;
|
|
21
|
+
};
|
|
22
|
+
export declare function tokenize(source: string): Token[];
|
|
23
|
+
interface RawCell {
|
|
24
|
+
readonly content: CellContent;
|
|
25
|
+
readonly marks: readonly CellMark[];
|
|
26
|
+
readonly color?: string;
|
|
27
|
+
}
|
|
28
|
+
/** 给一行的格子编列号:有小数点按小数点,否则按最右一格为个位。 */
|
|
29
|
+
export declare function assignColumns(raw: readonly RawCell[]): Cell[];
|
|
30
|
+
export declare function parseLayout(source: string): Layout;
|
|
31
|
+
export {};
|