pf2e-sage-stats 0.2.10 → 0.3.1
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/app.d.ts +79 -80
- package/dist/app.js +144 -75
- package/dist/index.js +62 -101
- package/dist/lines.d.ts +13 -0
- package/dist/lines.js +237 -0
- package/package.json +6 -6
package/dist/lines.js
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normaliseStatblockLines = normaliseStatblockLines;
|
|
4
|
+
exports.classifyStatblockLines = classifyStatblockLines;
|
|
5
|
+
exports.getLine = getLine;
|
|
6
|
+
exports.listLines = listLines;
|
|
7
|
+
function isCapitalised(token) {
|
|
8
|
+
const first = token.charAt(0);
|
|
9
|
+
return /^\p{L}/u.test(first) && first.toUpperCase() === first;
|
|
10
|
+
}
|
|
11
|
+
function isUnbalanced(str) {
|
|
12
|
+
let count = 0;
|
|
13
|
+
for (const char of str) {
|
|
14
|
+
if (char === '(')
|
|
15
|
+
count++;
|
|
16
|
+
if (char === ')')
|
|
17
|
+
count--;
|
|
18
|
+
if (count < 0)
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return count > 0;
|
|
22
|
+
}
|
|
23
|
+
function normaliseLine(line) {
|
|
24
|
+
return line
|
|
25
|
+
.replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, " ")
|
|
26
|
+
.replace(/[\u00AD\b]/g, "")
|
|
27
|
+
.replace(/[\u2010\u2011\u2012\u2013\u2014\u2212]/g, "-")
|
|
28
|
+
.replace(/[\u2018\u2019\u2032]/g, "'")
|
|
29
|
+
.replace(/[\u201C\u201D]/g, '"')
|
|
30
|
+
.replace(/[ \t\f\v]+/g, " ")
|
|
31
|
+
.trim();
|
|
32
|
+
}
|
|
33
|
+
const CONTINUATIONS = [
|
|
34
|
+
"plus", "and", "but", "or", 'a', "•",
|
|
35
|
+
];
|
|
36
|
+
const POINTS = [
|
|
37
|
+
"•",
|
|
38
|
+
];
|
|
39
|
+
function normaliseStatblockLines(source) {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
const pieces = source.replace(/^\uFEFF/, "").split(/\r\n|\n|\r|\u2028|\u2029/u);
|
|
42
|
+
const lines = [];
|
|
43
|
+
for (const piece of pieces) {
|
|
44
|
+
const text = normaliseLine(piece);
|
|
45
|
+
if (text.length === 0) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const lastLine = lines[lines.length - 1];
|
|
49
|
+
// Pathbuilder exception
|
|
50
|
+
if (/^Languages\s+/.test(text) && /^Perception\s+.*;$/.test((_a = lastLine === null || lastLine === void 0 ? void 0 : lastLine.text) !== null && _a !== void 0 ? _a : '')) {
|
|
51
|
+
lastLine.text = lastLine.text.slice(0, -1);
|
|
52
|
+
}
|
|
53
|
+
const lastText = lastLine === null || lastLine === void 0 ? void 0 : lastLine.text.split(' ');
|
|
54
|
+
const lastWord = lastText === null || lastText === void 0 ? void 0 : lastText[lastText.length - 1];
|
|
55
|
+
if ((isCapitalised(text) || POINTS.includes(text.charAt(0)))
|
|
56
|
+
&&
|
|
57
|
+
!(lastWord && (CONTINUATIONS.includes(lastWord) || /[,:;]$/.test(lastWord)))
|
|
58
|
+
&&
|
|
59
|
+
!isUnbalanced((_b = lastLine === null || lastLine === void 0 ? void 0 : lastLine.text) !== null && _b !== void 0 ? _b : '')) {
|
|
60
|
+
lines.push({ text });
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
lines[lines.length - 1].text = [lines[lines.length - 1].text, text].join(' ');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return lines;
|
|
67
|
+
}
|
|
68
|
+
function findIndexFrom(array, predicate, index) {
|
|
69
|
+
return array.slice(index).findIndex(predicate) + index;
|
|
70
|
+
}
|
|
71
|
+
function classifyStatblockLines(source) {
|
|
72
|
+
const normalised = normaliseStatblockLines(source);
|
|
73
|
+
const lines = [];
|
|
74
|
+
let lastLine = -1;
|
|
75
|
+
const titleLine = findIndexFrom(normalised, ({ text }) => /\S+\s+-?\d+$/.test(text), lastLine + 1);
|
|
76
|
+
const title = { text: normalised[titleLine].text, kind: 'title', lineFrom: titleLine, lineTo: titleLine };
|
|
77
|
+
lastLine = titleLine;
|
|
78
|
+
lines.push(title);
|
|
79
|
+
const perceptionLine = findIndexFrom(normalised, ({ text }) => /^Perception\s+[+-]\d+/.test(text), lastLine + 1);
|
|
80
|
+
if (perceptionLine > lastLine) {
|
|
81
|
+
const perception = { text: normalised[perceptionLine].text, kind: 'perception', lineFrom: perceptionLine, lineTo: perceptionLine };
|
|
82
|
+
lastLine = perceptionLine;
|
|
83
|
+
lines.push(perception);
|
|
84
|
+
}
|
|
85
|
+
const stealthLine = findIndexFrom(normalised, ({ text }) => /^Stealth\s+(?:DC)\s?[+-]?\d+/.test(text), lastLine + 1);
|
|
86
|
+
if (stealthLine > lastLine) {
|
|
87
|
+
const stealth = { text: normalised[stealthLine].text, kind: 'stealth', lineFrom: stealthLine, lineTo: stealthLine };
|
|
88
|
+
lastLine = stealthLine;
|
|
89
|
+
lines.push(stealth);
|
|
90
|
+
}
|
|
91
|
+
const languagesLine = findIndexFrom(normalised, ({ text }) => /^Languages\s+/.test(text), lastLine + 1);
|
|
92
|
+
if (languagesLine > lastLine) {
|
|
93
|
+
const languages = { text: normalised[languagesLine].text, kind: 'languages', lineFrom: languagesLine, lineTo: languagesLine };
|
|
94
|
+
lastLine = languagesLine;
|
|
95
|
+
lines.push(languages);
|
|
96
|
+
}
|
|
97
|
+
const skillsLine = findIndexFrom(normalised, ({ text }) => /^Skills\s+/.test(text), lastLine + 1);
|
|
98
|
+
if (skillsLine > lastLine) {
|
|
99
|
+
const skills = { text: normalised[skillsLine].text, kind: 'skills', lineFrom: skillsLine, lineTo: skillsLine };
|
|
100
|
+
lastLine = skillsLine;
|
|
101
|
+
lines.push(skills);
|
|
102
|
+
}
|
|
103
|
+
const descriptionLine = findIndexFrom(normalised, ({ text }) => /^Description\s+/.test(text), lastLine + 1);
|
|
104
|
+
if (descriptionLine > lastLine) {
|
|
105
|
+
const description = { text: normalised[descriptionLine].text, kind: 'description', lineFrom: descriptionLine, lineTo: descriptionLine };
|
|
106
|
+
lastLine = descriptionLine;
|
|
107
|
+
lines.push(description);
|
|
108
|
+
}
|
|
109
|
+
const disableLine = findIndexFrom(normalised, ({ text }) => /^Disable\s+/.test(text), lastLine + 1);
|
|
110
|
+
if (disableLine > lastLine) {
|
|
111
|
+
const disable = { text: normalised[disableLine].text, kind: 'disable', lineFrom: disableLine, lineTo: disableLine };
|
|
112
|
+
lastLine = disableLine;
|
|
113
|
+
lines.push(disable);
|
|
114
|
+
}
|
|
115
|
+
const attributeLine = findIndexFrom(normalised, ({ text }) => /^Str\s+[+-]\d+,/.test(text), lastLine + 1);
|
|
116
|
+
if (attributeLine > lastLine) {
|
|
117
|
+
const attributes = { text: normalised[attributeLine].text, kind: 'attributes', lineFrom: attributeLine, lineTo: attributeLine };
|
|
118
|
+
lastLine = attributeLine;
|
|
119
|
+
lines.push(attributes);
|
|
120
|
+
}
|
|
121
|
+
const itemsLine = findIndexFrom(normalised, ({ text }) => /^Items\s+/.test(text), lastLine + 1);
|
|
122
|
+
if (itemsLine > lastLine) {
|
|
123
|
+
const items = { text: normalised[itemsLine].text, kind: 'items', lineFrom: itemsLine, lineTo: itemsLine };
|
|
124
|
+
lastLine = itemsLine;
|
|
125
|
+
lines.push(items);
|
|
126
|
+
}
|
|
127
|
+
const defenceLine = findIndexFrom(normalised, ({ text }) => /^AC\s+\d+/.test(text), lastLine + 1);
|
|
128
|
+
if (defenceLine > lastLine) {
|
|
129
|
+
const defence = { text: normalised[defenceLine].text, kind: 'defence', lineFrom: defenceLine, lineTo: defenceLine };
|
|
130
|
+
lastLine = defenceLine;
|
|
131
|
+
lines.push(defence);
|
|
132
|
+
}
|
|
133
|
+
const hpLine = findIndexFrom(normalised, ({ text }) => /^(?:Hardness\s+\d+;\s+)?HP\s+\d+/.test(text), lastLine + 1);
|
|
134
|
+
if (hpLine > lastLine) {
|
|
135
|
+
const hp = { text: normalised[hpLine].text, kind: 'hp', lineFrom: hpLine, lineTo: hpLine };
|
|
136
|
+
lastLine = hpLine;
|
|
137
|
+
lines.push(hp);
|
|
138
|
+
}
|
|
139
|
+
const speedLine = findIndexFrom(normalised, ({ text }) => /^Speed\s+(?:(?:fly|climb|swim|burrow)\s+)?\d+/.test(text), lastLine + 1);
|
|
140
|
+
if (speedLine > lastLine) {
|
|
141
|
+
const speed = { text: normalised[speedLine].text, kind: 'speed', lineFrom: speedLine, lineTo: speedLine };
|
|
142
|
+
lastLine = speedLine;
|
|
143
|
+
lines.push(speed);
|
|
144
|
+
}
|
|
145
|
+
let strikeLine = findIndexFrom(normalised, ({ text }) => /^(?:Melee|Ranged)\s+/.test(text), lastLine + 1);
|
|
146
|
+
while (strikeLine > lastLine) {
|
|
147
|
+
const strike = { text: normalised[strikeLine].text, kind: 'strike', lineFrom: strikeLine, lineTo: strikeLine };
|
|
148
|
+
lastLine = strikeLine;
|
|
149
|
+
lines.push(strike);
|
|
150
|
+
strikeLine = findIndexFrom(normalised, ({ text }) => /^(?:Melee|Ranged)\s+/.test(text), lastLine + 1);
|
|
151
|
+
}
|
|
152
|
+
let spellsLine = findIndexFrom(normalised, ({ text }) => /^(?:Arcane|Divine|Occult|Primal)\s+(?:(?:Prepared|Spontaneous|Innate)\s+)?Spells/.test(text), lastLine + 1);
|
|
153
|
+
while (spellsLine > lastLine) {
|
|
154
|
+
const spells = { text: normalised[spellsLine].text, kind: 'spells', lineFrom: spellsLine, lineTo: spellsLine };
|
|
155
|
+
lastLine = spellsLine;
|
|
156
|
+
lines.push(spells);
|
|
157
|
+
spellsLine = findIndexFrom(normalised, ({ text }) => /^(?:Arcane|Divine|Occult|Primal)\s+(?:(?:Prepared|Spontaneous|Innate)\s+)?Spells/.test(text), lastLine + 1);
|
|
158
|
+
}
|
|
159
|
+
const ritualsLine = findIndexFrom(normalised, ({ text }) => /^Rituals\s+/.test(text), lastLine + 1);
|
|
160
|
+
if (ritualsLine > lastLine) {
|
|
161
|
+
const rituals = { text: normalised[ritualsLine].text, kind: 'rituals', lineFrom: ritualsLine, lineTo: ritualsLine };
|
|
162
|
+
lastLine = ritualsLine;
|
|
163
|
+
lines.push(rituals);
|
|
164
|
+
}
|
|
165
|
+
const featsLine = findIndexFrom(normalised, ({ text }) => /^Additional\s+Feats\s+/.test(text), lastLine + 1);
|
|
166
|
+
if (featsLine > lastLine) {
|
|
167
|
+
const feats = { text: normalised[featsLine].text, kind: 'feats', lineFrom: featsLine, lineTo: featsLine };
|
|
168
|
+
lastLine = featsLine;
|
|
169
|
+
lines.push(feats);
|
|
170
|
+
}
|
|
171
|
+
const specialsLine = findIndexFrom(normalised, ({ text }) => /^Additional\s+Specials\s+/.test(text), lastLine + 1);
|
|
172
|
+
if (specialsLine > lastLine) {
|
|
173
|
+
const specials = { text: normalised[specialsLine].text, kind: 'specials', lineFrom: specialsLine, lineTo: specialsLine };
|
|
174
|
+
lastLine = specialsLine;
|
|
175
|
+
lines.push(specials);
|
|
176
|
+
}
|
|
177
|
+
// Recovery
|
|
178
|
+
const skills = lines.find(({ kind }) => kind === 'skills');
|
|
179
|
+
while (skills && skills.lineTo + 1 < attributeLine) { // There's more skill lines
|
|
180
|
+
if (/\S+\s+[+-]\d+/.test(normalised[skills.lineTo + 1].text)) {
|
|
181
|
+
skills.text = [skills.text, normalised[skills.lineTo + 1].text].join(' ');
|
|
182
|
+
skills.lineTo = skills.lineTo + 1;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (title.lineTo + 1 < lines[1].lineFrom) {
|
|
189
|
+
const traits = { text: normalised[title.lineTo + 1].text, kind: 'traits', lineFrom: title.lineTo + 1, lineTo: title.lineTo + 1 };
|
|
190
|
+
lines.splice(1, 0, traits);
|
|
191
|
+
}
|
|
192
|
+
const traitsIndex = lines.findIndex(({ kind }) => kind === 'traits');
|
|
193
|
+
if (traitsIndex > 0 && lines[traitsIndex].lineTo + 1 < lines[traitsIndex + 1].lineFrom) {
|
|
194
|
+
const source = { text: normalised[lines[traitsIndex].lineTo + 1].text, kind: 'source', lineFrom: lines[traitsIndex].lineTo + 1, lineTo: lines[traitsIndex].lineTo + 1 };
|
|
195
|
+
lines.splice(traitsIndex + 1, 0, source);
|
|
196
|
+
}
|
|
197
|
+
const abilityBlocks = [...lines, { text: 'stub', kind: 'stub', lineFrom: normalised.length, lineTo: normalised.length }].flatMap((item, i) => {
|
|
198
|
+
const prevTo = i === 0 ? 0 : lines[i - 1].lineTo;
|
|
199
|
+
const start = prevTo + 1;
|
|
200
|
+
return start <= item.lineFrom - 1 ? [
|
|
201
|
+
{ lineFrom: start, lineTo: item.lineFrom - 1 }
|
|
202
|
+
] : [];
|
|
203
|
+
}).map((s) => (normalised.slice(s.lineFrom, s.lineTo + 1).map((l, i) => ({ text: l.text, kind: 'ability', lineFrom: s.lineFrom + i, lineTo: s.lineFrom + i }))));
|
|
204
|
+
for (const abilityBlock of abilityBlocks) {
|
|
205
|
+
let ability = abilityBlock[0];
|
|
206
|
+
let line = 1;
|
|
207
|
+
while (line < abilityBlock.length) {
|
|
208
|
+
const text = abilityBlock[line].text.replace(/(\[.+\]|\(.+\))\s+/g, '');
|
|
209
|
+
if (!/^\p{Lu}[\p{L}\p{P}]*(?:\s+[\p{Ll}\p{N}][\p{L}\p{N}\p{P}]*){2}/u.test(text)
|
|
210
|
+
&&
|
|
211
|
+
!/^\p{Lu}\p{L}*\./u.test(text)
|
|
212
|
+
&&
|
|
213
|
+
!/^\p{Lu}[\p{L}\p{P}]*\s+[\p{L}\p{N}\p{P}]+\./u.test(text)
|
|
214
|
+
&&
|
|
215
|
+
!/^(Success|Critical Success|Failure|Critical Failure|Maximum Duration|Recall Knowledge|Hit Points|Saving Throw|Effect|Requirements)\p{P}?\s+/u.test(abilityBlock[line].text)
|
|
216
|
+
&&
|
|
217
|
+
!/^\p{L}+;\s+/u.test(text)) {
|
|
218
|
+
lines.push(ability);
|
|
219
|
+
ability = abilityBlock[line];
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
ability.text = [ability.text, abilityBlock[line].text].join(' ');
|
|
223
|
+
ability.lineTo = abilityBlock[line].lineTo;
|
|
224
|
+
}
|
|
225
|
+
line++;
|
|
226
|
+
}
|
|
227
|
+
lines.push(ability);
|
|
228
|
+
}
|
|
229
|
+
return lines.sort((a, b) => a.lineFrom - b.lineFrom);
|
|
230
|
+
}
|
|
231
|
+
function getLine(lines, kind) {
|
|
232
|
+
var _a, _b;
|
|
233
|
+
return (_b = (_a = lines.find((l) => kind === l.kind)) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : '';
|
|
234
|
+
}
|
|
235
|
+
function listLines(lines, kind) {
|
|
236
|
+
return lines.filter((l) => kind === l.kind).map(({ text }) => text).join('\n');
|
|
237
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pf2e-sage-stats",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "An RPG Sage's .tsv stat generation tool",
|
|
5
5
|
"author": "ikariott@gmail.com",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,12 +21,14 @@
|
|
|
21
21
|
"default": "./dist/app.js"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"files": [
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
25
27
|
"devDependencies": {
|
|
26
28
|
"@eslint/js": "^9.17.0",
|
|
27
29
|
"@types/jest": "^29.5.14",
|
|
28
|
-
"@types/jsdom": "^27.0.0",
|
|
29
30
|
"@types/lodash": "^4.17.13",
|
|
31
|
+
"@types/node": "^26.6.6",
|
|
30
32
|
"@types/pluralize": "^0.0.33",
|
|
31
33
|
"@types/tsv": "^0.2.4",
|
|
32
34
|
"eslint": "^9.17.0",
|
|
@@ -42,11 +44,9 @@
|
|
|
42
44
|
"clipboardy": "^5.1.0",
|
|
43
45
|
"commander": "^12.1.0",
|
|
44
46
|
"dedent-js": "^1.0.1",
|
|
45
|
-
"jsdom": "^28.0.0",
|
|
46
47
|
"lodash": "^4.17.21",
|
|
47
48
|
"pluralize": "^8.0.0",
|
|
48
49
|
"tsv": "^0.2.0",
|
|
49
50
|
"zod": "^3.24.1"
|
|
50
|
-
}
|
|
51
|
-
"packageManager": "yarn@1.22.21+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
|
|
51
|
+
}
|
|
52
52
|
}
|