wikiparser-node 1.3.3-b → 1.3.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/config/.schema.json +172 -0
- package/config/llwiki.json +35 -1
- package/config/moegirl.json +44 -1
- package/config/zhwiki.json +466 -1
- package/dist/addon/table.d.ts +6 -0
- package/dist/addon/table.js +564 -0
- package/dist/base.d.ts +45 -0
- package/dist/bin/toc.js +18 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +209 -0
- package/dist/internal.d.ts +44 -0
- package/dist/lib/element.d.ts +155 -0
- package/dist/lib/element.js +634 -0
- package/dist/lib/node.d.ts +146 -0
- package/dist/lib/node.js +419 -0
- package/dist/lib/range.d.ts +104 -0
- package/dist/lib/range.js +385 -0
- package/dist/lib/ranges.d.ts +26 -0
- package/dist/lib/ranges.js +117 -0
- package/dist/lib/text.d.ts +62 -0
- package/dist/lib/text.js +248 -0
- package/dist/lib/title.d.ts +38 -0
- package/dist/lib/title.js +162 -0
- package/dist/mixin/attributesParent.js +94 -0
- package/dist/mixin/fixed.js +32 -0
- package/dist/mixin/flagsParent.js +70 -0
- package/dist/mixin/hidden.js +26 -0
- package/dist/mixin/magicLinkParent.js +41 -0
- package/dist/mixin/singleLine.js +25 -0
- package/dist/mixin/sol.js +43 -0
- package/dist/mixin/syntax.js +56 -0
- package/dist/parser/braces.js +121 -0
- package/dist/parser/commentAndExt.js +69 -0
- package/dist/parser/converter.js +40 -0
- package/dist/parser/externalLinks.js +28 -0
- package/dist/parser/hrAndDoubleUnderscore.js +38 -0
- package/dist/parser/html.js +36 -0
- package/dist/parser/links.js +94 -0
- package/dist/parser/list.js +59 -0
- package/dist/parser/magicLinks.js +40 -0
- package/dist/parser/quotes.js +67 -0
- package/dist/parser/selector.js +162 -0
- package/dist/parser/table.js +112 -0
- package/dist/src/arg.d.ts +58 -0
- package/dist/src/arg.js +189 -0
- package/dist/src/atom.d.ts +12 -0
- package/dist/src/atom.js +27 -0
- package/dist/src/attribute.d.ts +87 -0
- package/dist/src/attribute.js +392 -0
- package/dist/src/attributes.d.ts +107 -0
- package/dist/src/attributes.js +341 -0
- package/dist/src/converter.d.ts +52 -0
- package/dist/src/converter.js +79 -0
- package/dist/src/converterFlags.d.ts +87 -0
- package/dist/src/converterFlags.js +224 -0
- package/dist/src/converterRule.d.ts +81 -0
- package/dist/src/converterRule.js +212 -0
- package/dist/src/extLink.d.ts +54 -0
- package/dist/src/extLink.js +122 -0
- package/dist/src/gallery.d.ts +55 -0
- package/dist/src/gallery.js +122 -0
- package/dist/src/heading.d.ts +74 -0
- package/dist/src/heading.js +127 -0
- package/dist/src/hidden.d.ts +22 -0
- package/dist/src/hidden.js +23 -0
- package/dist/src/html.d.ts +101 -0
- package/dist/src/html.js +227 -0
- package/dist/src/imageParameter.d.ts +66 -0
- package/dist/src/imageParameter.js +247 -0
- package/dist/src/imagemap.d.ts +57 -0
- package/dist/src/imagemap.js +148 -0
- package/dist/src/imagemapLink.d.ts +59 -0
- package/dist/src/imagemapLink.js +40 -0
- package/dist/src/index.d.ts +146 -0
- package/dist/src/index.js +777 -0
- package/dist/src/link/base.d.ts +52 -0
- package/dist/src/link/base.js +209 -0
- package/dist/src/link/category.d.ts +13 -0
- package/dist/src/link/category.js +29 -0
- package/dist/src/link/file.d.ts +96 -0
- package/dist/src/link/file.js +255 -0
- package/dist/src/link/galleryImage.d.ts +44 -0
- package/dist/src/link/galleryImage.js +106 -0
- package/dist/src/link/index.d.ts +53 -0
- package/dist/src/link/index.js +123 -0
- package/dist/src/magicLink.d.ts +70 -0
- package/dist/src/magicLink.js +147 -0
- package/dist/src/nested.d.ts +44 -0
- package/dist/src/nested.js +87 -0
- package/dist/src/nowiki/base.d.ts +47 -0
- package/dist/src/nowiki/base.js +42 -0
- package/dist/src/nowiki/comment.d.ts +40 -0
- package/dist/src/nowiki/comment.js +67 -0
- package/dist/src/nowiki/dd.d.ts +8 -0
- package/dist/src/nowiki/dd.js +25 -0
- package/dist/src/nowiki/doubleUnderscore.d.ts +45 -0
- package/dist/src/nowiki/doubleUnderscore.js +50 -0
- package/dist/src/nowiki/hr.d.ts +31 -0
- package/dist/src/nowiki/hr.js +14 -0
- package/dist/src/nowiki/index.d.ts +16 -0
- package/dist/src/nowiki/index.js +21 -0
- package/dist/src/nowiki/list.d.ts +30 -0
- package/dist/src/nowiki/list.js +48 -0
- package/dist/src/nowiki/listBase.d.ts +19 -0
- package/dist/src/nowiki/listBase.js +12 -0
- package/dist/src/nowiki/noinclude.d.ts +26 -0
- package/dist/src/nowiki/noinclude.js +25 -0
- package/dist/src/nowiki/quote.d.ts +23 -0
- package/dist/src/nowiki/quote.js +45 -0
- package/dist/src/onlyinclude.d.ts +16 -0
- package/dist/src/onlyinclude.js +57 -0
- package/dist/src/paramTag/index.d.ts +37 -0
- package/dist/src/paramTag/index.js +68 -0
- package/dist/src/paramTag/inputbox.d.ts +8 -0
- package/dist/src/paramTag/inputbox.js +23 -0
- package/dist/src/parameter.d.ts +85 -0
- package/dist/src/parameter.js +205 -0
- package/dist/src/pre.d.ts +28 -0
- package/dist/src/pre.js +51 -0
- package/dist/src/syntax.d.ts +28 -0
- package/dist/src/syntax.js +34 -0
- package/dist/src/table/base.d.ts +53 -0
- package/dist/src/table/base.js +78 -0
- package/dist/src/table/index.d.ts +229 -0
- package/dist/src/table/index.js +379 -0
- package/dist/src/table/td.d.ts +104 -0
- package/dist/src/table/td.js +261 -0
- package/dist/src/table/tr.d.ts +32 -0
- package/dist/src/table/tr.js +56 -0
- package/dist/src/table/trBase.d.ts +54 -0
- package/dist/src/table/trBase.js +159 -0
- package/dist/src/tagPair/ext.d.ts +56 -0
- package/dist/src/tagPair/ext.js +142 -0
- package/dist/src/tagPair/include.d.ts +53 -0
- package/dist/src/tagPair/include.js +63 -0
- package/dist/src/tagPair/index.d.ts +48 -0
- package/dist/src/tagPair/index.js +93 -0
- package/dist/src/transclude.d.ts +167 -0
- package/dist/src/transclude.js +690 -0
- package/dist/util/constants.js +108 -0
- package/dist/util/debug.js +64 -0
- package/dist/util/diff.js +72 -0
- package/dist/util/lint.js +46 -0
- package/dist/util/string.js +61 -0
- package/errors/README +1 -0
- package/package.json +12 -32
- package/printed/README +1 -0
- package/bundle/bundle.min.js +0 -36
- package/extensions/dist/base.js +0 -68
- package/extensions/dist/editor.js +0 -159
- package/extensions/dist/highlight.js +0 -30
- package/extensions/dist/lint.js +0 -48
- package/extensions/editor.css +0 -63
- package/extensions/ui.css +0 -114
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint @stylistic/operator-linebreak: [2, "before", {overrides: {"=": "after"}}] */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Layout = void 0;
|
|
5
|
+
const assert = require("assert/strict");
|
|
6
|
+
const debug_1 = require("../util/debug");
|
|
7
|
+
const constants_1 = require("../util/constants");
|
|
8
|
+
const src_1 = require("../src");
|
|
9
|
+
const tr_1 = require("../src/table/tr");
|
|
10
|
+
const table_1 = require("../src/table");
|
|
11
|
+
const td_1 = require("../src/table/td");
|
|
12
|
+
const trBase_1 = require("../src/table/trBase");
|
|
13
|
+
/**
|
|
14
|
+
* 检查坐标形式
|
|
15
|
+
* @param coords 坐标
|
|
16
|
+
*/
|
|
17
|
+
const isTableCoords = (coords) => coords.x === undefined;
|
|
18
|
+
/**
|
|
19
|
+
* 比较两个表格坐标
|
|
20
|
+
* @param coords1 坐标1
|
|
21
|
+
* @param coords2 坐标2
|
|
22
|
+
*/
|
|
23
|
+
const cmpCoords = (coords1, coords2) => {
|
|
24
|
+
const diff = coords1.row - coords2.row;
|
|
25
|
+
return diff === 0 ? coords1.column - coords2.column : diff;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 是否是行尾
|
|
29
|
+
* @param {Token} cell 表格单元格
|
|
30
|
+
*/
|
|
31
|
+
const isRowEnd = ({ type }) => type === 'tr' || type === 'table-syntax';
|
|
32
|
+
/**
|
|
33
|
+
* 是否是合并单元格的第一列
|
|
34
|
+
* @param rowLayout 行布局
|
|
35
|
+
* @param i 单元格序号
|
|
36
|
+
* @param oneCol 是否仅有一列
|
|
37
|
+
*/
|
|
38
|
+
const isStartCol = (rowLayout, i, oneCol = false) => {
|
|
39
|
+
const coords = rowLayout[i];
|
|
40
|
+
return rowLayout[i - 1] !== coords && (!oneCol || rowLayout[i + 1] !== coords);
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* 设置表格格式
|
|
44
|
+
* @param cells 单元格
|
|
45
|
+
* @param attr 属性
|
|
46
|
+
* @param multi 是否对所有单元格设置,或是仅对行首单元格设置
|
|
47
|
+
*/
|
|
48
|
+
const format = (cells, attr = {}, multi = false) => {
|
|
49
|
+
for (const [token, start] of cells) {
|
|
50
|
+
if (multi || start) {
|
|
51
|
+
if (typeof attr === 'string') {
|
|
52
|
+
token.setSyntax(attr);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
for (const [k, v] of Object.entries(attr)) {
|
|
56
|
+
token.setAttr(k, v);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* 填补缺失单元格
|
|
64
|
+
* @param y 行号
|
|
65
|
+
* @param rowToken 表格行
|
|
66
|
+
* @param layout 表格布局
|
|
67
|
+
* @param maxCol 最大列数
|
|
68
|
+
* @param token 待填充的单元格
|
|
69
|
+
*/
|
|
70
|
+
const fill = (y, rowToken, layout, maxCol, token) => {
|
|
71
|
+
const rowLayout = layout[y], lastIndex = rowToken.childNodes.findLastIndex(child => child instanceof td_1.TdToken && child.subtype !== 'caption'), pos = lastIndex + 1 || undefined;
|
|
72
|
+
debug_1.Shadow.run(() => {
|
|
73
|
+
for (let i = 0; i < maxCol; i++) {
|
|
74
|
+
if (!rowLayout[i]) {
|
|
75
|
+
rowToken.insertAt(token.cloneNode(), pos);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
/** @extends {Array<TableCoords[]>} */
|
|
81
|
+
class Layout extends Array {
|
|
82
|
+
/** 打印表格布局 */
|
|
83
|
+
print() {
|
|
84
|
+
const hBorders = new Array(this.length + 1).fill(undefined).map((_, i) => {
|
|
85
|
+
const prev = this[i - 1] ?? [], next = this[i] ?? [];
|
|
86
|
+
return new Array(Math.max(prev.length, next.length)).fill(undefined)
|
|
87
|
+
.map((__, j) => prev[j] !== next[j]);
|
|
88
|
+
}), vBorders = this.map(cur => new Array(cur.length + 1).fill(undefined).map((_, j) => cur[j - 1] !== cur[j]));
|
|
89
|
+
let out = '';
|
|
90
|
+
for (let i = 0; i <= this.length; i++) {
|
|
91
|
+
const hBorder = hBorders[i].map(Number), vBorderTop = (vBorders[i - 1] ?? []).map(Number), vBorderBottom = (vBorders[i] ?? []).map(Number),
|
|
92
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
93
|
+
border = [' ', , , '┌', , '┐', '─', '┬', , '│', '└', '├', '┘', '┤', '┴', '┼'];
|
|
94
|
+
for (let j = 0; j <= hBorder.length; j++) {
|
|
95
|
+
const bit = (vBorderTop[j] << 3) + (vBorderBottom[j] << 0)
|
|
96
|
+
+ (hBorder[j - 1] << 2) + (hBorder[j] << 1);
|
|
97
|
+
out += `${border[bit]}${hBorder[j] ? '─' : ' '}`;
|
|
98
|
+
}
|
|
99
|
+
out += '\n';
|
|
100
|
+
}
|
|
101
|
+
console.log(out.slice(0, -1));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.Layout = Layout;
|
|
105
|
+
table_1.TableToken.prototype.getNthCell =
|
|
106
|
+
/** @implements */
|
|
107
|
+
function (coords) {
|
|
108
|
+
const rawCoords = coords.row === undefined ? this.toRawCoords(coords) : coords;
|
|
109
|
+
return rawCoords && this.getNthRow(rawCoords.row, false, false)?.getNthCol(rawCoords.column);
|
|
110
|
+
};
|
|
111
|
+
table_1.TableToken.prototype.getLayout =
|
|
112
|
+
/** @implements */
|
|
113
|
+
function (stop) {
|
|
114
|
+
const rows = this.getAllRows(), { length } = rows, layout = new Layout(...new Array(length).fill(undefined).map(() => []));
|
|
115
|
+
for (let i = 0; i < length; i++) {
|
|
116
|
+
if (i > (stop?.row ?? stop?.y ?? NaN)) {
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
const rowLayout = layout[i];
|
|
120
|
+
let j = 0, k = 0, last;
|
|
121
|
+
for (const cell of rows[i].childNodes.slice(2)) {
|
|
122
|
+
if (cell.type === 'td') {
|
|
123
|
+
if (cell.isIndependent()) {
|
|
124
|
+
last = cell.subtype !== 'caption';
|
|
125
|
+
}
|
|
126
|
+
if (last) {
|
|
127
|
+
const coords = { row: i, column: j }, { rowspan, colspan } = cell;
|
|
128
|
+
j++;
|
|
129
|
+
while (rowLayout[k]) {
|
|
130
|
+
k++;
|
|
131
|
+
}
|
|
132
|
+
if (i === stop?.row && j > (stop.column ?? NaN)) {
|
|
133
|
+
layout[i][k] = coords;
|
|
134
|
+
return layout;
|
|
135
|
+
}
|
|
136
|
+
for (let y = i; y < Math.min(i + rowspan, length); y++) {
|
|
137
|
+
for (let x = k; x < k + colspan; x++) {
|
|
138
|
+
layout[y][x] = coords;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
k += colspan;
|
|
142
|
+
if (i === stop?.y && k > (stop.x ?? NaN)) {
|
|
143
|
+
return layout;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else if (isRowEnd(cell)) {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return layout;
|
|
153
|
+
};
|
|
154
|
+
table_1.TableToken.prototype.printLayout =
|
|
155
|
+
/** @implements */
|
|
156
|
+
function () {
|
|
157
|
+
this.getLayout().print();
|
|
158
|
+
};
|
|
159
|
+
table_1.TableToken.prototype.toRenderedCoords =
|
|
160
|
+
/** @implements */
|
|
161
|
+
function ({ row, column }) {
|
|
162
|
+
const rowLayout = this.getLayout({ row, column })[row], x = rowLayout?.findIndex(coords => cmpCoords(coords, { row, column }) === 0);
|
|
163
|
+
return rowLayout && (x === -1 ? undefined : { y: row, x: x });
|
|
164
|
+
};
|
|
165
|
+
table_1.TableToken.prototype.toRawCoords =
|
|
166
|
+
/** @implements */
|
|
167
|
+
function ({ x, y }) {
|
|
168
|
+
const rowLayout = this.getLayout({ x, y })[y], coords = rowLayout?.[x];
|
|
169
|
+
if (coords) {
|
|
170
|
+
return { ...coords, start: coords.row === y && rowLayout[x - 1] !== coords };
|
|
171
|
+
}
|
|
172
|
+
else if (rowLayout || y > 0) {
|
|
173
|
+
return x === rowLayout?.length
|
|
174
|
+
? { row: y, column: (rowLayout.findLast(({ row }) => row === y)?.column ?? -1) + 1, start: true }
|
|
175
|
+
: undefined;
|
|
176
|
+
}
|
|
177
|
+
return { row: 0, column: 0, start: true };
|
|
178
|
+
};
|
|
179
|
+
table_1.TableToken.prototype.getFullRow =
|
|
180
|
+
/** @implements */
|
|
181
|
+
function (y) {
|
|
182
|
+
const rows = this.getAllRows();
|
|
183
|
+
return new Map(this.getLayout({ y })[y]?.map(({ row, column }) => [rows[row].getNthCol(column), row === y]));
|
|
184
|
+
};
|
|
185
|
+
table_1.TableToken.prototype.getFullCol =
|
|
186
|
+
/** @implements */
|
|
187
|
+
function (x) {
|
|
188
|
+
const layout = this.getLayout(), colLayout = layout.map(row => row[x]).filter(Boolean), rows = this.getAllRows();
|
|
189
|
+
return new Map(colLayout.map(coords => [rows[coords.row].getNthCol(coords.column), layout[coords.row][x - 1] !== coords]));
|
|
190
|
+
};
|
|
191
|
+
table_1.TableToken.prototype.formatTableRow =
|
|
192
|
+
/** @implements */
|
|
193
|
+
function (y, attr = {}, multiRow = false) {
|
|
194
|
+
format(this.getFullRow(y), attr, multiRow);
|
|
195
|
+
};
|
|
196
|
+
table_1.TableToken.prototype.formatTableCol =
|
|
197
|
+
/** @implements */
|
|
198
|
+
function (x, attr = {}, multiCol = false) {
|
|
199
|
+
format(this.getFullCol(x), attr, multiCol);
|
|
200
|
+
};
|
|
201
|
+
table_1.TableToken.prototype.fillTableRow =
|
|
202
|
+
/** @implements */
|
|
203
|
+
function (y, inner, subtype = 'td', attr = {}) {
|
|
204
|
+
const rowToken = this.getNthRow(y), layout = this.getLayout({ y }), maxCol = Math.max(...layout.map(({ length }) => length)), token = (0, td_1.createTd)(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
|
|
205
|
+
fill(y, rowToken, layout, maxCol, token);
|
|
206
|
+
};
|
|
207
|
+
table_1.TableToken.prototype.fillTable =
|
|
208
|
+
/** @implements */
|
|
209
|
+
function (inner, subtype = 'td', attr = {}) {
|
|
210
|
+
const rowTokens = this.getAllRows(), layout = this.getLayout(), maxCol = Math.max(...layout.map(({ length }) => length)), token = (0, td_1.createTd)(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
|
|
211
|
+
for (let y = 0; y < rowTokens.length; y++) {
|
|
212
|
+
fill(y, rowTokens[y], layout, maxCol, token);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
table_1.TableToken.prototype.insertTableCell =
|
|
216
|
+
/** @implements */
|
|
217
|
+
function (inner, coords, subtype = 'td', attr = {}) {
|
|
218
|
+
let rawCoords;
|
|
219
|
+
if (coords.column === undefined) {
|
|
220
|
+
const { x, y } = coords;
|
|
221
|
+
rawCoords = this.toRawCoords(coords);
|
|
222
|
+
if (!rawCoords?.start) {
|
|
223
|
+
throw new RangeError(`指定的坐标不是单元格起始点:(${x}, ${y})`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
rawCoords = coords;
|
|
228
|
+
}
|
|
229
|
+
const rowToken = this.getNthRow(rawCoords.row, true);
|
|
230
|
+
return rowToken === this
|
|
231
|
+
? trBase_1.TrBaseToken.prototype.insertTableCell.call(this, inner, rawCoords, subtype, attr)
|
|
232
|
+
: rowToken.insertTableCell(inner, rawCoords, subtype, attr);
|
|
233
|
+
};
|
|
234
|
+
table_1.TableToken.prototype.prependTableRow =
|
|
235
|
+
/** @implements */
|
|
236
|
+
function () {
|
|
237
|
+
const row = debug_1.Shadow.run(() => new tr_1.TrToken('\n|-', undefined, this.getAttribute('config'))), { childNodes } = this, [, , plain] = childNodes, start = plain?.constructor === src_1.Token ? 3 : 2, tdChildren = childNodes.slice(start), index = tdChildren.findIndex(({ type }) => type !== 'td');
|
|
238
|
+
this.insertAt(row, index === -1 ? -1 : index + start);
|
|
239
|
+
debug_1.Shadow.run(() => {
|
|
240
|
+
for (const cell of tdChildren.slice(0, index === -1 ? undefined : index)) {
|
|
241
|
+
if (cell.subtype !== 'caption') {
|
|
242
|
+
row.insertAt(cell);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
return row;
|
|
247
|
+
};
|
|
248
|
+
table_1.TableToken.prototype.insertTableRow =
|
|
249
|
+
/** @implements */
|
|
250
|
+
function (y, attr = {}, inner, subtype = 'td', innerAttr = {}) {
|
|
251
|
+
let reference = this.getNthRow(y, false, true);
|
|
252
|
+
const token = debug_1.Shadow.run(() => new tr_1.TrToken('\n|-', undefined, this.getAttribute('config')));
|
|
253
|
+
for (const [k, v] of Object.entries(attr)) {
|
|
254
|
+
token.setAttr(k, v);
|
|
255
|
+
}
|
|
256
|
+
if (reference?.type === 'table') { // `row === 0`且表格自身是有效行
|
|
257
|
+
reference = this.prependTableRow();
|
|
258
|
+
}
|
|
259
|
+
this.insertBefore(token, reference);
|
|
260
|
+
if (inner !== undefined) {
|
|
261
|
+
const td = token.insertTableCell(inner, { row: 0, column: 0 }, subtype, innerAttr), set = new WeakSet(), layout = this.getLayout({ y }), maxCol = Math.max(...layout.map(({ length }) => length)), rowLayout = layout[y];
|
|
262
|
+
debug_1.Shadow.run(() => {
|
|
263
|
+
for (let i = 0; i < maxCol; i++) {
|
|
264
|
+
const coords = rowLayout[i];
|
|
265
|
+
if (!coords) {
|
|
266
|
+
token.insertAt(td.cloneNode());
|
|
267
|
+
}
|
|
268
|
+
else if (!set.has(coords)) {
|
|
269
|
+
set.add(coords);
|
|
270
|
+
if (coords.row < y) {
|
|
271
|
+
this.getNthCell(coords).rowspan++;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return token;
|
|
278
|
+
};
|
|
279
|
+
table_1.TableToken.prototype.insertTableCol =
|
|
280
|
+
/** @implements */
|
|
281
|
+
function (x, inner, subtype = 'td', attr = {}) {
|
|
282
|
+
const layout = this.getLayout(), rowLength = layout.map(({ length }) => length), minCol = Math.min(...rowLength);
|
|
283
|
+
if (x > minCol) {
|
|
284
|
+
throw new RangeError(`表格第 ${rowLength.indexOf(minCol)} 行仅有 ${minCol} 列!`);
|
|
285
|
+
}
|
|
286
|
+
const token = (0, td_1.createTd)(inner, subtype, attr, this.getAttribute('include'), this.getAttribute('config'));
|
|
287
|
+
for (let i = 0; i < layout.length; i++) {
|
|
288
|
+
const coords = layout[i][x], prevCoords = x === 0 ? true : layout[i][x - 1];
|
|
289
|
+
if (!prevCoords) {
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
else if (prevCoords !== coords) {
|
|
293
|
+
const rowToken = this.getNthRow(i);
|
|
294
|
+
rowToken.insertBefore(token.cloneNode(), rowToken.getNthCol(coords.column, true));
|
|
295
|
+
}
|
|
296
|
+
else if (coords.row === i) {
|
|
297
|
+
this.getNthCell(coords).colspan++;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
table_1.TableToken.prototype.removeTableRow =
|
|
302
|
+
/** @implements */
|
|
303
|
+
function (y) {
|
|
304
|
+
const rows = this.getAllRows(), layout = this.getLayout(), rowLayout = layout[y], set = new WeakSet();
|
|
305
|
+
for (let x = rowLayout.length - 1; x >= 0; x--) {
|
|
306
|
+
const coords = rowLayout[x];
|
|
307
|
+
if (set.has(coords)) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
set.add(coords);
|
|
311
|
+
const token = rows[coords.row].getNthCol(coords.column);
|
|
312
|
+
let { rowspan } = token;
|
|
313
|
+
if (rowspan > 1) {
|
|
314
|
+
token.rowspan = --rowspan;
|
|
315
|
+
if (coords.row === y) {
|
|
316
|
+
const { colspan, subtype } = token, attr = token.getAttrs();
|
|
317
|
+
for (let i = y + 1; rowspan && i < rows.length; i++, rowspan--) {
|
|
318
|
+
const { column } = layout[i].slice(x + colspan).find(({ row }) => row === i) ?? {};
|
|
319
|
+
if (column !== undefined) {
|
|
320
|
+
rows[i].insertTableCell('', { row: 0, column }, subtype, { ...attr, rowspan });
|
|
321
|
+
break;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const row = rows[y], rowToken = row.type === 'tr' ? row : this.prependTableRow();
|
|
328
|
+
rowToken.remove();
|
|
329
|
+
return rowToken;
|
|
330
|
+
};
|
|
331
|
+
table_1.TableToken.prototype.removeTableCol =
|
|
332
|
+
/** @implements */
|
|
333
|
+
function (x) {
|
|
334
|
+
for (const [token, start] of this.getFullCol(x)) {
|
|
335
|
+
const { colspan, lastChild } = token;
|
|
336
|
+
if (colspan > 1) {
|
|
337
|
+
token.colspan = colspan - 1;
|
|
338
|
+
if (start) {
|
|
339
|
+
lastChild.replaceChildren();
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
token.remove();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
table_1.TableToken.prototype.mergeCells =
|
|
348
|
+
/** @implements */
|
|
349
|
+
function (xlim, ylim) {
|
|
350
|
+
const layout = this.getLayout(), maxCol = Math.max(...layout.map(({ length }) => length)), [xmin, xmax] = xlim.map(x => x < 0 ? x + maxCol : x).sort(), [ymin, ymax] = ylim.map(y => y < 0 ? y + layout.length : y).sort(), set = new Set(layout.slice(ymin, ymax).flatMap(rowLayout => rowLayout.slice(xmin, xmax)));
|
|
351
|
+
if ([...layout[ymin - 1] ?? [], ...layout[ymax] ?? []].some(coords => set.has(coords))
|
|
352
|
+
|| layout.some(rowLayout => set.has(rowLayout[xmin - 1]) || set.has(rowLayout[xmax]))) {
|
|
353
|
+
throw new RangeError('待合并区域与外侧区域有重叠!');
|
|
354
|
+
}
|
|
355
|
+
const corner = layout[ymin][xmin], rows = this.getAllRows(), cornerCell = rows[corner.row].getNthCol(corner.column);
|
|
356
|
+
cornerCell.rowspan = ymax - ymin;
|
|
357
|
+
cornerCell.colspan = xmax - xmin;
|
|
358
|
+
set.delete(corner);
|
|
359
|
+
for (const token of [...set].map(({ row, column }) => rows[row].getNthCol(column))) {
|
|
360
|
+
token.remove();
|
|
361
|
+
}
|
|
362
|
+
return cornerCell;
|
|
363
|
+
};
|
|
364
|
+
table_1.TableToken.prototype.split =
|
|
365
|
+
/** @implements */
|
|
366
|
+
function (coords, dirs) {
|
|
367
|
+
const cell = this.getNthCell(coords), attr = cell.getAttrs(), { subtype } = cell;
|
|
368
|
+
attr.rowspan ||= 1;
|
|
369
|
+
attr.colspan ||= 1;
|
|
370
|
+
for (const dir of dirs) {
|
|
371
|
+
if (attr[dir] === 1) {
|
|
372
|
+
dirs.delete(dir);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (dirs.size === 0) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
let { x, y } = coords;
|
|
379
|
+
const rawCoords = isTableCoords(coords) ? coords : this.toRawCoords(coords);
|
|
380
|
+
if (rawCoords.start === false || x === undefined) {
|
|
381
|
+
({ x, y } = this.toRenderedCoords(rawCoords));
|
|
382
|
+
}
|
|
383
|
+
const splitting = { rowspan: 1, colspan: 1 };
|
|
384
|
+
for (const dir of dirs) {
|
|
385
|
+
cell.setAttr(dir, 1);
|
|
386
|
+
splitting[dir] = attr[dir];
|
|
387
|
+
delete attr[dir];
|
|
388
|
+
}
|
|
389
|
+
for (let j = y; j < y + splitting.rowspan; j++) {
|
|
390
|
+
for (let i = x; i < x + splitting.colspan; i++) {
|
|
391
|
+
if (i > x || j > y) {
|
|
392
|
+
try {
|
|
393
|
+
this.insertTableCell('', { x: i, y: j }, subtype, attr);
|
|
394
|
+
}
|
|
395
|
+
catch (e) {
|
|
396
|
+
if (e instanceof RangeError && e.message.startsWith('指定的坐标不是单元格起始点:')) {
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
throw e;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
table_1.TableToken.prototype.splitIntoRows =
|
|
406
|
+
/** @implements */
|
|
407
|
+
function (coords) {
|
|
408
|
+
this.split(coords, new Set(['rowspan']));
|
|
409
|
+
};
|
|
410
|
+
table_1.TableToken.prototype.splitIntoCols =
|
|
411
|
+
/** @implements */
|
|
412
|
+
function (coords) {
|
|
413
|
+
this.split(coords, new Set(['colspan']));
|
|
414
|
+
};
|
|
415
|
+
table_1.TableToken.prototype.splitIntoCells =
|
|
416
|
+
/** @implements */
|
|
417
|
+
function (coords) {
|
|
418
|
+
this.split(coords, new Set(['rowspan', 'colspan']));
|
|
419
|
+
};
|
|
420
|
+
table_1.TableToken.prototype.replicateTableRow =
|
|
421
|
+
/** @implements */
|
|
422
|
+
function (row) {
|
|
423
|
+
let rowToken = this.getNthRow(row);
|
|
424
|
+
if (rowToken.type === 'table') {
|
|
425
|
+
rowToken = this.prependTableRow();
|
|
426
|
+
}
|
|
427
|
+
const replicated = this.insertBefore(rowToken.cloneNode(), rowToken);
|
|
428
|
+
for (const [token, start] of this.getFullRow(row)) {
|
|
429
|
+
if (start) {
|
|
430
|
+
token.rowspan = 1;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
token.rowspan++;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
return replicated;
|
|
437
|
+
};
|
|
438
|
+
table_1.TableToken.prototype.replicateTableCol =
|
|
439
|
+
/** @implements */
|
|
440
|
+
function (x) {
|
|
441
|
+
const replicated = [];
|
|
442
|
+
for (const [token, start] of this.getFullCol(x)) {
|
|
443
|
+
if (start) {
|
|
444
|
+
const newToken = token.cloneNode();
|
|
445
|
+
newToken.colspan = 1;
|
|
446
|
+
token.before(newToken);
|
|
447
|
+
replicated.push(newToken);
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
token.colspan++;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return replicated;
|
|
454
|
+
};
|
|
455
|
+
table_1.TableToken.prototype.moveTableRowBefore =
|
|
456
|
+
/** @implements */
|
|
457
|
+
function (y, before) {
|
|
458
|
+
const layout = this.getLayout(),
|
|
459
|
+
/** @ignore */
|
|
460
|
+
occupied = (i) => layout[i].map(({ row }, j) => row === i ? j : undefined).filter((j) => j !== undefined);
|
|
461
|
+
try {
|
|
462
|
+
assert.deepEqual(occupied(y), occupied(before));
|
|
463
|
+
}
|
|
464
|
+
catch (e) {
|
|
465
|
+
if (e instanceof assert.AssertionError) {
|
|
466
|
+
throw new RangeError(`第 ${y} 行与第 ${before} 行的构造不同,无法移动!`);
|
|
467
|
+
}
|
|
468
|
+
throw e;
|
|
469
|
+
}
|
|
470
|
+
const rowToken = this.removeTableRow(y);
|
|
471
|
+
for (const coords of layout[before]) {
|
|
472
|
+
if (coords.row < before) {
|
|
473
|
+
this.getNthCell(coords).rowspan++;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
let beforeToken = this.getNthRow(before);
|
|
477
|
+
if (beforeToken.type === 'table') {
|
|
478
|
+
beforeToken = this.prependTableRow();
|
|
479
|
+
}
|
|
480
|
+
this.insertBefore(rowToken, beforeToken);
|
|
481
|
+
return rowToken;
|
|
482
|
+
};
|
|
483
|
+
table_1.TableToken.prototype.moveTableRowAfter =
|
|
484
|
+
/** @implements */
|
|
485
|
+
function (y, after) {
|
|
486
|
+
const layout = this.getLayout(), afterToken = this.getNthRow(after), cells = afterToken.childNodes.filter(child => child.type === 'td' && child.subtype !== 'caption'),
|
|
487
|
+
/** @ignore */
|
|
488
|
+
occupied = (i, oneRow = false) => layout[i].map(({ row, column }, j) => row === i && (!oneRow || cells[column].rowspan === 1) ? j : undefined).filter((j) => j !== undefined);
|
|
489
|
+
try {
|
|
490
|
+
assert.deepEqual(occupied(y), occupied(after, true));
|
|
491
|
+
}
|
|
492
|
+
catch (e) {
|
|
493
|
+
if (e instanceof assert.AssertionError) {
|
|
494
|
+
throw new RangeError(`第 ${y} 行与第 ${after} 行的构造不同,无法移动!`);
|
|
495
|
+
}
|
|
496
|
+
throw e;
|
|
497
|
+
}
|
|
498
|
+
const rowToken = this.removeTableRow(y);
|
|
499
|
+
for (const coords of layout[after]) {
|
|
500
|
+
if (coords.row < after) {
|
|
501
|
+
this.getNthCell(coords).rowspan++;
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
const cell = cells[coords.column], { rowspan } = cell;
|
|
505
|
+
if (rowspan > 1) {
|
|
506
|
+
cell.rowspan = rowspan + 1;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
if (afterToken === this) {
|
|
511
|
+
const index = this.childNodes.slice(2).findIndex(isRowEnd);
|
|
512
|
+
this.insertAt(rowToken, index + 2);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
this.insertBefore(rowToken, afterToken);
|
|
516
|
+
}
|
|
517
|
+
return rowToken;
|
|
518
|
+
};
|
|
519
|
+
table_1.TableToken.prototype.moveCol =
|
|
520
|
+
/** @implements */
|
|
521
|
+
function (x, reference, after = false) {
|
|
522
|
+
const layout = this.getLayout();
|
|
523
|
+
if (layout.some(rowLayout => isStartCol(rowLayout, x) !== isStartCol(rowLayout, reference, after))) {
|
|
524
|
+
throw new RangeError(`第 ${x} 列与第 ${reference} 列的构造不同,无法移动!`);
|
|
525
|
+
}
|
|
526
|
+
const setX = new WeakSet(), setRef = new WeakSet(), rows = this.getAllRows();
|
|
527
|
+
for (let i = 0; i < layout.length; i++) {
|
|
528
|
+
const rowLayout = layout[i], coords = rowLayout[x], refCoords = rowLayout[reference], start = isStartCol(rowLayout, x);
|
|
529
|
+
if (refCoords && !start && !setRef.has(refCoords)) {
|
|
530
|
+
setRef.add(refCoords);
|
|
531
|
+
rows[refCoords.row].getNthCol(refCoords.column).colspan++;
|
|
532
|
+
}
|
|
533
|
+
if (coords && !setX.has(coords)) {
|
|
534
|
+
setX.add(coords);
|
|
535
|
+
const rowToken = rows[i];
|
|
536
|
+
let token = rowToken.getNthCol(coords.column);
|
|
537
|
+
const { colspan } = token;
|
|
538
|
+
if (colspan > 1) {
|
|
539
|
+
token.colspan = colspan - 1;
|
|
540
|
+
if (start) {
|
|
541
|
+
const original = token;
|
|
542
|
+
token = token.cloneNode();
|
|
543
|
+
original.lastChild.replaceChildren();
|
|
544
|
+
token.colspan = 1;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
if (start) {
|
|
548
|
+
const col = rowLayout.slice(reference + Number(after)).find(({ row }) => row === i)?.column;
|
|
549
|
+
rowToken.insertBefore(token, col === undefined ? rowToken.childNodes.slice(2).find(isRowEnd) : rowToken.getNthCol(col));
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
table_1.TableToken.prototype.moveTableColBefore =
|
|
555
|
+
/** @implements */
|
|
556
|
+
function (x, before) {
|
|
557
|
+
this.moveCol(x, before);
|
|
558
|
+
};
|
|
559
|
+
table_1.TableToken.prototype.moveTableColAfter =
|
|
560
|
+
/** @implements */
|
|
561
|
+
function (x, after) {
|
|
562
|
+
this.moveCol(x, after, true);
|
|
563
|
+
};
|
|
564
|
+
constants_1.classes['ExtendTableToken'] = __filename;
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface Config {
|
|
2
|
+
ext: string[];
|
|
3
|
+
readonly html: [string[], string[], string[]];
|
|
4
|
+
readonly namespaces: Record<string, string>;
|
|
5
|
+
readonly nsid: Record<string, number>;
|
|
6
|
+
readonly parserFunction: [Record<string, string>, string[], string[], string[]];
|
|
7
|
+
readonly doubleUnderscore: [string[], string[]];
|
|
8
|
+
readonly protocol: string;
|
|
9
|
+
readonly img: Record<string, string>;
|
|
10
|
+
readonly variants: string[];
|
|
11
|
+
readonly interwiki: string[];
|
|
12
|
+
readonly excludes?: string[];
|
|
13
|
+
readonly conversionTable?: [string, string][];
|
|
14
|
+
readonly redirects?: [string, string][];
|
|
15
|
+
}
|
|
16
|
+
export type Severity = 'error' | 'warning';
|
|
17
|
+
export interface LintError {
|
|
18
|
+
readonly message: string;
|
|
19
|
+
readonly severity: Severity;
|
|
20
|
+
readonly startIndex: number;
|
|
21
|
+
readonly endIndex: number;
|
|
22
|
+
readonly startLine: number;
|
|
23
|
+
readonly startCol: number;
|
|
24
|
+
readonly endLine: number;
|
|
25
|
+
readonly endCol: number;
|
|
26
|
+
}
|
|
27
|
+
/** 类似Node */
|
|
28
|
+
export interface AstNode {
|
|
29
|
+
type: string;
|
|
30
|
+
readonly childNodes: AstNode[];
|
|
31
|
+
/** Linter */
|
|
32
|
+
lint(): LintError[];
|
|
33
|
+
/** 以HTML格式打印 */
|
|
34
|
+
print(): string;
|
|
35
|
+
}
|
|
36
|
+
export interface Parser {
|
|
37
|
+
config: string | Config;
|
|
38
|
+
i18n: string | Record<string, string> | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* 解析wikitext
|
|
41
|
+
* @param include 是否嵌入
|
|
42
|
+
* @param maxStage 最大解析层级
|
|
43
|
+
*/
|
|
44
|
+
parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): AstNode;
|
|
45
|
+
}
|
package/dist/bin/toc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { argv: [, , filename] } = process;
|
|
6
|
+
if (!filename) {
|
|
7
|
+
throw new RangeError('请指定文档文件!');
|
|
8
|
+
}
|
|
9
|
+
const fullpath = path.join(__dirname, '..', '..', 'wiki', `${filename}.md`), isEnglish = filename.endsWith('-(EN)');
|
|
10
|
+
if (!fs.existsSync(fullpath)) {
|
|
11
|
+
throw new RangeError(`文档 ${filename}.md 不存在!`);
|
|
12
|
+
}
|
|
13
|
+
const content = fs.readFileSync(fullpath, 'utf8');
|
|
14
|
+
if (/^- \[[^\]]+\]\(#[^)]+\)$/mu.test(content)) {
|
|
15
|
+
throw new Error(`文档 ${filename}.md 中已包含目录!`);
|
|
16
|
+
}
|
|
17
|
+
const toc = content.split('\n').filter(line => line.startsWith('#')).map(line => line.replace(/^(#+)\s+(\S.*)$/u, (_, { length }, title) => `${'\t'.repeat(length - 1)}- [${title}](#${title.toLowerCase().replaceAll(' ', '-').replaceAll('.', '')})`)).join('\n');
|
|
18
|
+
fs.writeFileSync(fullpath, `<details>\n\t<summary>${isEnglish ? 'Table of Contents' : '目录'}</summary>\n\n${toc}\n\n</details>\n\n# Other Languages\n\n- [${isEnglish ? '简体中文' : 'English'}](./${isEnglish ? filename.slice(0, -5) : `${filename}-%28EN%29`})\n\n${content}`);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Shadow } from './util/debug';
|
|
2
|
+
import type { Config, LintError, Parser as ParserBase } from './base';
|
|
3
|
+
import type { Title } from './lib/title';
|
|
4
|
+
import type { Token } from './internal';
|
|
5
|
+
declare interface Parser extends ParserBase {
|
|
6
|
+
readonly Shadow: typeof Shadow;
|
|
7
|
+
conversionTable: Map<string, string>;
|
|
8
|
+
redirects: Map<string, string>;
|
|
9
|
+
warning: boolean;
|
|
10
|
+
debugging: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* 规范化页面标题
|
|
13
|
+
* @param title 标题(含或不含命名空间前缀)
|
|
14
|
+
* @param defaultNs 命名空间
|
|
15
|
+
* @param include 是否嵌入
|
|
16
|
+
* @param halfParsed 是否是半解析状态
|
|
17
|
+
* @param decode 是否需要解码
|
|
18
|
+
* @param selfLink 是否允许selfLink
|
|
19
|
+
*/
|
|
20
|
+
normalizeTitle(title: string, defaultNs?: number, include?: boolean, config?: Config, halfParsed?: boolean, decode?: boolean, selfLink?: boolean): Title;
|
|
21
|
+
parse(wikitext: string, include?: boolean, maxStage?: number, config?: Config): Token;
|
|
22
|
+
/**
|
|
23
|
+
* 是否是跨维基链接
|
|
24
|
+
* @param title 链接标题
|
|
25
|
+
*/
|
|
26
|
+
isInterwiki(title: string, config?: Config): RegExpExecArray | null;
|
|
27
|
+
}
|
|
28
|
+
declare const Parser: Parser;
|
|
29
|
+
export = Parser;
|
|
30
|
+
export type { Config, LintError };
|
|
31
|
+
export type * from './internal';
|