@heroiclands/package-build 23.6.1 → 23.7.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/CHANGELOG.md +39 -0
- package/docs/authoring/links-and-markup.md +418 -24
- package/docs/content-format.md +1 -1
- package/docs/reference/format-details.md +680 -73
- package/docs/reference/note-types.md +208 -39
- package/engine/content-format.yaml +13 -0
- package/engine/content-index.mjs +21 -14
- package/engine/dictionary-layout.mjs +7 -3
- package/engine/dictionary-rows.mjs +6 -13
- package/engine/index-records.mjs +1 -0
- package/engine/language-data.mjs +613 -0
- package/engine/language-rows.mjs +993 -0
- package/engine/language-terms.mjs +1718 -0
- package/engine/language-words.mjs +115 -6
- package/engine/note-addresses.mjs +38 -4
- package/engine/note-vocabulary.mjs +9 -2
- package/engine/sql-tables.mjs +45 -12
- package/engine/word-citations.mjs +206 -0
- package/engine/word-rows.mjs +66 -12
- package/engine/word-terms.mjs +131 -13
- package/package.json +1 -1
- package/types/engine/dictionary-layout.d.mts +5 -3
- package/types/engine/language-data.d.mts +22 -0
- package/types/engine/language-rows.d.mts +313 -0
- package/types/engine/language-terms.d.mts +367 -0
- package/types/engine/note-addresses.d.mts +8 -10
- package/types/engine/sql-tables.d.mts +2 -2
- package/types/engine/word-citations.d.mts +43 -0
- package/types/engine/word-rows.d.mts +18 -1
- package/types/engine/word-terms.d.mts +27 -0
|
@@ -26,8 +26,15 @@
|
|
|
26
26
|
* - a word's `field` is one of `data.fields`;
|
|
27
27
|
* - a `borrowed` source names its `language`;
|
|
28
28
|
* - no form stands twice in one language;
|
|
29
|
-
* -
|
|
30
|
-
*
|
|
29
|
+
* - a word's `gloss` states at least one sense;
|
|
30
|
+
* - an affix that `replaces` the stem's close does not also `lengthen` it;
|
|
31
|
+
* - an affix states `given` only where its `kind` is `name`;
|
|
32
|
+
* - an affix states a `form` or `zero: true`, never both;
|
|
33
|
+
* - an element's `word` and the particle of `data.negation` are forms of the
|
|
34
|
+
* words of the language or an ancestor;
|
|
35
|
+
* - a word's `frame` names a frame of `data.sounds.frames`, its language's or
|
|
36
|
+
* an ancestor's;
|
|
37
|
+
* - every source language resolves, and names a language skill;
|
|
31
38
|
* - a pronunciation is a respelling;
|
|
32
39
|
* - a `built` piece no word or affix of the language glosses is a warning.
|
|
33
40
|
*
|
|
@@ -48,7 +55,9 @@ import { addressKeyOf, ownKey } from "./culture-reach.mjs";
|
|
|
48
55
|
import {
|
|
49
56
|
BORROWED_SOURCE_KIND,
|
|
50
57
|
LANGUAGE_SUBTYPE,
|
|
58
|
+
NAME_AFFIX_KIND,
|
|
51
59
|
WORD_DATA_KEYS,
|
|
60
|
+
glossSenses,
|
|
52
61
|
pronunciationProblem,
|
|
53
62
|
} from "./word-terms.mjs";
|
|
54
63
|
import { WORD_CLASS_MODIFIERS, parseWordClass } from "./word-classes.mjs";
|
|
@@ -174,6 +183,13 @@ export function checkLanguageWords(note, { index } = {}) {
|
|
|
174
183
|
}).map((key) => dataAt(key))
|
|
175
184
|
: [];
|
|
176
185
|
const glossOf = pieceGlosses(data, ...ancestors);
|
|
186
|
+
// The frames a word may be set in: its language's, then each ancestor's.
|
|
187
|
+
const frames = new Set();
|
|
188
|
+
for (const one of [data, ...ancestors]) {
|
|
189
|
+
const sounds = isMap(one?.sounds) ? one.sounds : {};
|
|
190
|
+
for (const frame of listOf(sounds.frames))
|
|
191
|
+
if (isMap(frame) && typeof frame.frame === "string") frames.add(frame.frame);
|
|
192
|
+
}
|
|
177
193
|
const used = new Set();
|
|
178
194
|
const forms = new Map();
|
|
179
195
|
listOf(data.words).forEach((word, i) => {
|
|
@@ -236,10 +252,20 @@ export function checkLanguageWords(note, { index } = {}) {
|
|
|
236
252
|
findings.push(...languageFindings(word.source.language, [...at("source"), "language"]));
|
|
237
253
|
}
|
|
238
254
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
255
|
+
if (typeof word.frame === "string" && !frames.has(word.frame))
|
|
256
|
+
findings.push(
|
|
257
|
+
finding(
|
|
258
|
+
at("frame"),
|
|
259
|
+
"error",
|
|
260
|
+
`the frame \`${word.frame}\` of \`${word.form}\` is not one of this language's ` +
|
|
261
|
+
"`data.sounds.frames`",
|
|
262
|
+
),
|
|
263
|
+
);
|
|
264
|
+
|
|
265
|
+
if (Array.isArray(word.gloss) && !glossSenses(word.gloss).length)
|
|
266
|
+
findings.push(
|
|
267
|
+
finding(at("gloss"), "error", "a `gloss` list states at least one sense"),
|
|
268
|
+
);
|
|
243
269
|
|
|
244
270
|
if (typeof word.pronunciation === "string") {
|
|
245
271
|
const problem = pronunciationProblem(word.pronunciation);
|
|
@@ -266,6 +292,89 @@ export function checkLanguageWords(note, { index } = {}) {
|
|
|
266
292
|
});
|
|
267
293
|
});
|
|
268
294
|
|
|
295
|
+
listOf(data.affixes).forEach((affix, i) => {
|
|
296
|
+
if (!isMap(affix)) return;
|
|
297
|
+
const form = typeof affix.form === "string" && affix.form.trim() !== "";
|
|
298
|
+
const zero = affix.zero === true;
|
|
299
|
+
if (form && zero)
|
|
300
|
+
findings.push(
|
|
301
|
+
finding(
|
|
302
|
+
["data", "affixes", i, "zero"],
|
|
303
|
+
"error",
|
|
304
|
+
`\`data.affixes[${i}]\` states a \`form\` and \`zero: true\`; a zero affix states no form`,
|
|
305
|
+
true,
|
|
306
|
+
),
|
|
307
|
+
);
|
|
308
|
+
else if (!form && !zero && affix.form == null)
|
|
309
|
+
findings.push(
|
|
310
|
+
finding(
|
|
311
|
+
["data", "affixes", i],
|
|
312
|
+
"error",
|
|
313
|
+
`\`data.affixes[${i}]\` states neither a \`form\` nor \`zero: true\``,
|
|
314
|
+
),
|
|
315
|
+
);
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
listOf(data.affixes).forEach((affix, i) => {
|
|
319
|
+
if (!isMap(affix) || affix.given == null || affix.kind === NAME_AFFIX_KIND) return;
|
|
320
|
+
findings.push(
|
|
321
|
+
finding(
|
|
322
|
+
["data", "affixes", i, "given"],
|
|
323
|
+
"error",
|
|
324
|
+
"`given` says who a given name belongs to, and " +
|
|
325
|
+
(typeof affix.kind === "string" ?
|
|
326
|
+
`\`data.affixes[${i}]\` is of kind \`${affix.kind}\``
|
|
327
|
+
: `\`data.affixes[${i}]\` is of no kind`) +
|
|
328
|
+
`; it is stated on an affix of kind \`${NAME_AFFIX_KIND}\` only`,
|
|
329
|
+
true,
|
|
330
|
+
),
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
// The forms an element's `word` and the negative particle may name.
|
|
335
|
+
const wordForms = new Set();
|
|
336
|
+
for (const one of [data, ...ancestors])
|
|
337
|
+
for (const word of listOf(one?.words))
|
|
338
|
+
if (isMap(word) && typeof word.form === "string") wordForms.add(word.form);
|
|
339
|
+
listOf(data.elements).forEach((element, i) => {
|
|
340
|
+
if (!isMap(element) || typeof element.word !== "string" || wordForms.has(element.word))
|
|
341
|
+
return;
|
|
342
|
+
findings.push(
|
|
343
|
+
finding(
|
|
344
|
+
["data", "elements", i, "word"],
|
|
345
|
+
"error",
|
|
346
|
+
`the word \`${element.word}\` of the element \`${element.form}\` is not a form of ` +
|
|
347
|
+
"this language's words or its ancestors'",
|
|
348
|
+
),
|
|
349
|
+
);
|
|
350
|
+
});
|
|
351
|
+
if (
|
|
352
|
+
isMap(data.negation) &&
|
|
353
|
+
typeof data.negation.particle === "string" &&
|
|
354
|
+
!wordForms.has(data.negation.particle)
|
|
355
|
+
)
|
|
356
|
+
findings.push(
|
|
357
|
+
finding(
|
|
358
|
+
["data", "negation", "particle"],
|
|
359
|
+
"error",
|
|
360
|
+
`the negative particle \`${data.negation.particle}\` is not a form of this ` +
|
|
361
|
+
"language's words or its ancestors'",
|
|
362
|
+
),
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
listOf(data.affixes).forEach((affix, i) => {
|
|
366
|
+
if (!isMap(affix) || affix.lengthens !== true || affix.replaces == null) return;
|
|
367
|
+
findings.push(
|
|
368
|
+
finding(
|
|
369
|
+
["data", "affixes", i, "lengthens"],
|
|
370
|
+
"error",
|
|
371
|
+
`\`${affix.form}\`: an affix that replaces the stem's close does not also lengthen ` +
|
|
372
|
+
"its last vowel; state one",
|
|
373
|
+
true,
|
|
374
|
+
),
|
|
375
|
+
);
|
|
376
|
+
});
|
|
377
|
+
|
|
269
378
|
for (const [token, i] of declared)
|
|
270
379
|
if (!used.has(token))
|
|
271
380
|
findings.push(
|
|
@@ -69,15 +69,49 @@ export const STRUCTURED_ADDRESSES = Object.freeze([
|
|
|
69
69
|
type: "place",
|
|
70
70
|
accepts: ["place"],
|
|
71
71
|
},
|
|
72
|
-
// A language's
|
|
73
|
-
//
|
|
72
|
+
// A language's Addresses and a glossary entry's language, read leniently:
|
|
73
|
+
// a value that is no Address stays as written, for the language or
|
|
74
74
|
// glossary check to refuse at its own key.
|
|
75
75
|
{
|
|
76
76
|
noteType: "skill",
|
|
77
|
-
path: ["data", "
|
|
78
|
-
shape: "
|
|
77
|
+
path: ["data", "tongues", "*", "language"],
|
|
78
|
+
shape: "value",
|
|
79
|
+
type: "skill",
|
|
80
|
+
accepts: ["skill"],
|
|
81
|
+
lenient: true,
|
|
82
|
+
},
|
|
83
|
+
...["from", "to"].map((key) => ({
|
|
84
|
+
noteType: "skill",
|
|
85
|
+
path: ["data", "soundChanges", "*", key],
|
|
86
|
+
shape: "value",
|
|
87
|
+
type: "skill",
|
|
88
|
+
accepts: ["skill"],
|
|
89
|
+
lenient: true,
|
|
90
|
+
})),
|
|
91
|
+
{
|
|
92
|
+
noteType: "skill",
|
|
93
|
+
path: ["data", "cognates", "*", "forms"],
|
|
94
|
+
shape: "keys",
|
|
95
|
+
type: "skill",
|
|
96
|
+
accepts: ["skill"],
|
|
79
97
|
lenient: true,
|
|
80
98
|
},
|
|
99
|
+
...[
|
|
100
|
+
["folders", "folder", ["folder"], "none"],
|
|
101
|
+
["cultures", "lore", ["lore"]],
|
|
102
|
+
["lore", "lore", ["lore"]],
|
|
103
|
+
["parents", "place", undefined],
|
|
104
|
+
["addresses", undefined, undefined],
|
|
105
|
+
["skip", undefined, undefined],
|
|
106
|
+
].map(([kind, type, accepts, system]) => ({
|
|
107
|
+
noteType: "skill",
|
|
108
|
+
path: ["data", "scope", kind],
|
|
109
|
+
shape: "list",
|
|
110
|
+
...(type ? { type } : {}),
|
|
111
|
+
...(accepts ? { accepts } : {}),
|
|
112
|
+
...(system ? { system } : {}),
|
|
113
|
+
lenient: true,
|
|
114
|
+
})),
|
|
81
115
|
{
|
|
82
116
|
noteType: "skill",
|
|
83
117
|
path: ["data", "words", "*", "source", "language"],
|
|
@@ -132,6 +132,8 @@ import {
|
|
|
132
132
|
checkLanguageWords,
|
|
133
133
|
} from "./language-words.mjs";
|
|
134
134
|
import { checkGlossary } from "./glossary-entries.mjs";
|
|
135
|
+
import { LANGUAGE_DATA_FIELDS } from "./language-terms.mjs";
|
|
136
|
+
import { checkLanguageData } from "./language-data.mjs";
|
|
135
137
|
import { checkAffiliationReach } from "./culture-reach.mjs";
|
|
136
138
|
import { checkLanguageNames, requiredGivenGenders } from "./language-names.mjs";
|
|
137
139
|
import { SOCIAL_TIES, SOCIAL_TIE_TARGET_TYPES } from "./social-tie-terms.mjs";
|
|
@@ -1989,8 +1991,12 @@ export const NOTE_VOCABULARY = Object.freeze({
|
|
|
1989
1991
|
skill: Object.freeze({
|
|
1990
1992
|
stubbable: true,
|
|
1991
1993
|
// A language note's words, held to the master word classes and to what
|
|
1992
|
-
// the note declares
|
|
1993
|
-
|
|
1994
|
+
// the note declares, and its register, sounds and cognates held to
|
|
1995
|
+
// what they name; no other skill states them.
|
|
1996
|
+
check: (note, context) => [
|
|
1997
|
+
...checkLanguageWords(note, context),
|
|
1998
|
+
...checkLanguageData(note, { ...context, types: Object.keys(NOTE_VOCABULARY) }),
|
|
1999
|
+
],
|
|
1994
2000
|
subTypes: Object.freeze([
|
|
1995
2001
|
"social",
|
|
1996
2002
|
"nature",
|
|
@@ -2017,6 +2023,7 @@ export const NOTE_VOCABULARY = Object.freeze({
|
|
|
2017
2023
|
FAMILY_FIELD,
|
|
2018
2024
|
ABBREVIATION_FIELD,
|
|
2019
2025
|
...WORD_DATA_FIELDS,
|
|
2026
|
+
...LANGUAGE_DATA_FIELDS,
|
|
2020
2027
|
]),
|
|
2021
2028
|
}),
|
|
2022
2029
|
|
package/engine/sql-tables.mjs
CHANGED
|
@@ -44,6 +44,7 @@ import {
|
|
|
44
44
|
wordRows,
|
|
45
45
|
} from "./word-rows.mjs";
|
|
46
46
|
import { DICTIONARY_COLUMNS, TERM_COLUMNS, dictionaryAndTermRows } from "./dictionary-rows.mjs";
|
|
47
|
+
import { LANGUAGE_COLUMN_TYPES, LANGUAGE_RELATIONS } from "./language-rows.mjs";
|
|
47
48
|
import { renderDictionary, renderGlossary } from "./dictionary-layout.mjs";
|
|
48
49
|
import { glossaryLanguage } from "./glossary-language.mjs";
|
|
49
50
|
import { eventViewSections } from "./event-views.mjs";
|
|
@@ -99,12 +100,13 @@ export const SQL_FENCE_ATTRIBUTES = Object.freeze({
|
|
|
99
100
|
"letter of a dictionary or a glossary.",
|
|
100
101
|
}),
|
|
101
102
|
key: Object.freeze({
|
|
102
|
-
value: "`true` or `
|
|
103
|
+
value: "`true`, `false` or `only`",
|
|
103
104
|
default: "`true`",
|
|
104
105
|
summary:
|
|
105
106
|
"Whether a dictionary or a glossary prints its key, the line of abbreviations it " +
|
|
106
107
|
"uses. `false` leaves the key out, for a page that states it once above several " +
|
|
107
|
-
"fences
|
|
108
|
+
"fences; `only` prints the key for the fence's rows and nothing else, which is how " +
|
|
109
|
+
"that page states it. Refused on a fence set as a table.",
|
|
108
110
|
}),
|
|
109
111
|
layout: Object.freeze({
|
|
110
112
|
value: "`table`, `dictionary` or `glossary`",
|
|
@@ -135,7 +137,7 @@ const escapeCell = (text) =>
|
|
|
135
137
|
*
|
|
136
138
|
* @param {string} markdown - The note body, frontmatter already stripped.
|
|
137
139
|
* @returns {Array<{line: number, close: number, indent: string, query: string,
|
|
138
|
-
* allowEmpty: boolean, sectionLevel: number, showKey: boolean, layout: string, block: string}>} One entry per
|
|
140
|
+
* allowEmpty: boolean, sectionLevel: number, showKey: boolean|"only", layout: string, block: string}>} One entry per
|
|
139
141
|
* fence, in document order.
|
|
140
142
|
*/
|
|
141
143
|
export function findSqlBlocks(markdown) {
|
|
@@ -170,19 +172,22 @@ export function findSqlBlocks(markdown) {
|
|
|
170
172
|
errors.push(error.message);
|
|
171
173
|
}
|
|
172
174
|
}
|
|
175
|
+
/** @type {boolean|"only"} */
|
|
173
176
|
let showKey = true;
|
|
174
177
|
if (Object.hasOwn(args, "key")) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
errors.push(error.message);
|
|
179
|
-
}
|
|
178
|
+
if (args.key === "only") showKey = "only";
|
|
179
|
+
else if (args.key === "true" || args.key === "false") showKey = args.key === "true";
|
|
180
|
+
else errors.push("key needs the literal value true, false or only");
|
|
180
181
|
}
|
|
181
182
|
if (!Number.isInteger(level) || level < 1 || level > 6)
|
|
182
183
|
errors.push("section-level needs an integer from 1 through 6");
|
|
183
184
|
const layout = String(args.layout ?? "table");
|
|
184
185
|
if (!SQL_LAYOUTS.includes(layout))
|
|
185
186
|
errors.push(`layout is \`table\`, \`dictionary\` or \`glossary\`, not \`${layout}\``);
|
|
187
|
+
else if (Object.hasOwn(args, "key") && layout === "table")
|
|
188
|
+
errors.push(
|
|
189
|
+
"key sets a dictionary's or a glossary's key, and this fence's layout is `table`",
|
|
190
|
+
);
|
|
186
191
|
blocks.push({
|
|
187
192
|
line: i,
|
|
188
193
|
close,
|
|
@@ -337,6 +342,7 @@ export async function openNotesDatabase(
|
|
|
337
342
|
"language_lineage",
|
|
338
343
|
"dictionary",
|
|
339
344
|
"terms",
|
|
345
|
+
...LANGUAGE_RELATIONS.map(([name]) => name),
|
|
340
346
|
])
|
|
341
347
|
await connection.run(
|
|
342
348
|
`CREATE VIEW ${WITH_STUBS_SCHEMA}.${relation} AS SELECT * FROM main.${relation}`,
|
|
@@ -556,7 +562,9 @@ async function createTableFromFile(connection, name, select) {
|
|
|
556
562
|
* {@link module:engine/word-rows.GLOSSARY_COLUMNS},
|
|
557
563
|
* {@link module:engine/word-rows.LINEAGE_COLUMNS},
|
|
558
564
|
* {@link module:engine/dictionary-rows.DICTIONARY_COLUMNS} and
|
|
559
|
-
* {@link module:engine/dictionary-rows.TERM_COLUMNS} name
|
|
565
|
+
* {@link module:engine/dictionary-rows.TERM_COLUMNS} name; then each relation
|
|
566
|
+
* of a language's register, sounds, history and names, as
|
|
567
|
+
* {@link module:engine/language-rows} derives them.
|
|
560
568
|
*
|
|
561
569
|
* Derived from the same records `entries` reads, stubs included, and a GM
|
|
562
570
|
* note's rows are left out on a public surface exactly as the note is, while a
|
|
@@ -623,6 +631,15 @@ async function createWordRelations(connection, stem, records, prefix, audience)
|
|
|
623
631
|
`${prefix}terms`,
|
|
624
632
|
TERM_COLUMNS,
|
|
625
633
|
);
|
|
634
|
+
for (const [name, rowsOf, columns] of LANGUAGE_RELATIONS)
|
|
635
|
+
await createRowRelation(
|
|
636
|
+
connection,
|
|
637
|
+
`${stem}-${name}.jsonl`,
|
|
638
|
+
rowsOf(visible, records),
|
|
639
|
+
`${prefix}${name}`,
|
|
640
|
+
columns,
|
|
641
|
+
LANGUAGE_COLUMN_TYPES[name],
|
|
642
|
+
);
|
|
626
643
|
}
|
|
627
644
|
|
|
628
645
|
/**
|
|
@@ -635,19 +652,35 @@ async function createWordRelations(connection, stem, records, prefix, audience)
|
|
|
635
652
|
* @param {object[]} rows - The rows, each carrying every column.
|
|
636
653
|
* @param {string} view - The qualified view name.
|
|
637
654
|
* @param {readonly string[]} columns - Its columns, for the empty case.
|
|
655
|
+
* @param {Readonly<Record<string, string>>} [types] - Each column's type, for
|
|
656
|
+
* the empty case; a column it leaves out is text. Without it, a whole-number
|
|
657
|
+
* column is a number and every other column text. A `DOUBLE` column is cast
|
|
658
|
+
* whatever the rows hold, because a fraction written whole reads as an
|
|
659
|
+
* integer.
|
|
638
660
|
* @returns {Promise<void>}
|
|
639
661
|
*/
|
|
640
|
-
async function createRowRelation(connection, file, rows, view, columns) {
|
|
662
|
+
async function createRowRelation(connection, file, rows, view, columns, types) {
|
|
641
663
|
if (!rows.length) {
|
|
642
664
|
const typed = columns.map((column) => {
|
|
643
|
-
const type =
|
|
665
|
+
const type =
|
|
666
|
+
types ? (types[column] ?? "VARCHAR")
|
|
667
|
+
: ROW_INTEGER_COLUMNS.includes(column) ? "BIGINT"
|
|
668
|
+
: "VARCHAR";
|
|
644
669
|
return `NULL::${type} AS "${column}"`;
|
|
645
670
|
});
|
|
646
671
|
await connection.run(`CREATE VIEW ${view} AS SELECT ${typed.join(", ")} WHERE false`);
|
|
647
672
|
return;
|
|
648
673
|
}
|
|
649
674
|
fs.writeFileSync(file, rows.map((row) => JSON.stringify(row)).join("\n"));
|
|
650
|
-
|
|
675
|
+
const fractions = Object.entries(types ?? {})
|
|
676
|
+
.filter(([, type]) => type === "DOUBLE")
|
|
677
|
+
.map(([column]) => `CAST("${column}" AS DOUBLE) AS "${column}"`);
|
|
678
|
+
const source = readJsonAuto(file);
|
|
679
|
+
await createTableFromFile(
|
|
680
|
+
connection,
|
|
681
|
+
view,
|
|
682
|
+
fractions.length ? `SELECT * REPLACE (${fractions.join(", ")}) FROM (${source})` : source,
|
|
683
|
+
);
|
|
651
684
|
}
|
|
652
685
|
|
|
653
686
|
/**
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This file is part of the Song of Heroic Lands (SoHL) system for Foundry VTT.
|
|
3
|
+
* Copyright (c) 2024-2026 Tom Rodriguez ("Toasty") — <toasty@heroiclands.org>
|
|
4
|
+
*
|
|
5
|
+
* This work is licensed under the GNU General Public License v3.0 (GPLv3).
|
|
6
|
+
* You may copy, modify, and distribute it under the terms of that license.
|
|
7
|
+
*
|
|
8
|
+
* For full terms, see the LICENSE.md file in the project root or visit:
|
|
9
|
+
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
10
|
+
*
|
|
11
|
+
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The notes that cite a language's words, read from the notes' prose and from
|
|
16
|
+
* the names their frontmatter carries.
|
|
17
|
+
*
|
|
18
|
+
* **A marked use is an italic span of a note's body**: `_vazhmadêr_` or
|
|
19
|
+
* `*vazhmadêr*`, holding text alone — a span holding a link, a code span or
|
|
20
|
+
* other markup is no use, and neither is anything in a code fence. A use cites
|
|
21
|
+
* every word, of every language note of the package, whose form or one of
|
|
22
|
+
* whose `variants` it spells, compared with diacritics taken off and case
|
|
23
|
+
* folded, so `_Vazhmadêr_` and `_vazhmader_` both cite `vazhmadêr`. An
|
|
24
|
+
* italic span that spells no word cites nothing.
|
|
25
|
+
*
|
|
26
|
+
* **A note also cites a word its frontmatter names whole**: a full name or an
|
|
27
|
+
* alias, or an affiliation's rank or office title, that spells the word's form
|
|
28
|
+
* or a variant under the same folding. A word found only inside a longer name
|
|
29
|
+
* is no citation.
|
|
30
|
+
*
|
|
31
|
+
* A language note never cites its own words, and its own body cites nothing:
|
|
32
|
+
* its italics are the words it describes. The index carries what this derives as each language record's
|
|
33
|
+
* `citations` — the citing notes' Addresses, never their text — and the
|
|
34
|
+
* `attested` column of the `words` relation and the `usage` of a `dictionary`
|
|
35
|
+
* entry read it.
|
|
36
|
+
*
|
|
37
|
+
* @module
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import MarkdownIt from "markdown-it";
|
|
41
|
+
|
|
42
|
+
import { isLanguageRecord, noteAddressOf } from "./word-rows.mjs";
|
|
43
|
+
|
|
44
|
+
/** The parser marked uses are read with: CommonMark's emphasis and nothing added. */
|
|
45
|
+
const PARSER = new MarkdownIt("commonmark");
|
|
46
|
+
|
|
47
|
+
/** Code-point order, so a list is the same on every machine. */
|
|
48
|
+
const compare = (a, b) =>
|
|
49
|
+
a < b ? -1
|
|
50
|
+
: a > b ? 1
|
|
51
|
+
: 0;
|
|
52
|
+
|
|
53
|
+
/** Whether a value is a plain map. */
|
|
54
|
+
const isMap = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A form as uses are matched against it: diacritics taken off, case folded.
|
|
58
|
+
*
|
|
59
|
+
* @param {unknown} text - A form or a use.
|
|
60
|
+
* @returns {string} The folded form.
|
|
61
|
+
*/
|
|
62
|
+
export function foldForm(text) {
|
|
63
|
+
return String(text ?? "")
|
|
64
|
+
.normalize("NFD")
|
|
65
|
+
.replace(/\p{M}/gu, "")
|
|
66
|
+
.toLowerCase()
|
|
67
|
+
.normalize("NFC")
|
|
68
|
+
.trim();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The marked uses in a note's body: the text of every italic span that holds
|
|
73
|
+
* text alone, in document order.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} body - The note's Markdown body.
|
|
76
|
+
* @returns {string[]} Each span's text.
|
|
77
|
+
*/
|
|
78
|
+
export function markedUses(body) {
|
|
79
|
+
const text = String(body ?? "");
|
|
80
|
+
if (!/[_*]/.test(text)) return [];
|
|
81
|
+
const uses = [];
|
|
82
|
+
for (const token of PARSER.parse(text, {})) {
|
|
83
|
+
if (token.type !== "inline" || !token.children) continue;
|
|
84
|
+
let span;
|
|
85
|
+
for (const child of token.children) {
|
|
86
|
+
if (child.type === "em_open") span = [];
|
|
87
|
+
else if (child.type === "em_close") {
|
|
88
|
+
if (span) {
|
|
89
|
+
const use = span.join("").trim();
|
|
90
|
+
if (use && !use.includes("[[")) uses.push(use);
|
|
91
|
+
}
|
|
92
|
+
span = undefined;
|
|
93
|
+
} else if (span) {
|
|
94
|
+
if (child.type === "text") span.push(child.content);
|
|
95
|
+
else span = undefined;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return uses;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The names a note carries whole, which cite a word they spell exactly: its
|
|
104
|
+
* full name and aliases, and an affiliation's rank and office titles.
|
|
105
|
+
*
|
|
106
|
+
* @param {Record<string, any>} record - An index record.
|
|
107
|
+
* @returns {string[]}
|
|
108
|
+
*/
|
|
109
|
+
function namesCarried(record) {
|
|
110
|
+
const out = [];
|
|
111
|
+
const name = isMap(record.name) ? record.name : {};
|
|
112
|
+
if (typeof name.full === "string") out.push(name.full);
|
|
113
|
+
for (const alias of Array.isArray(name.aliases) ? name.aliases : [])
|
|
114
|
+
if (typeof alias === "string") out.push(alias);
|
|
115
|
+
const governance = isMap(record.data?.governance) ? record.data.governance : {};
|
|
116
|
+
for (const rank of Array.isArray(governance.ranks) ? governance.ranks : [])
|
|
117
|
+
if (isMap(rank) && typeof rank.title === "string") out.push(rank.title);
|
|
118
|
+
if (isMap(governance.offices)) out.push(...Object.keys(governance.offices));
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Each language note's cited words: for every word some note's marked use
|
|
124
|
+
* spells, the Addresses of the notes citing it.
|
|
125
|
+
*
|
|
126
|
+
* @param {Iterable<Record<string, any>>} records - Index records.
|
|
127
|
+
* @param {Iterable<{record: Record<string, any>, body: string}>} notes - The
|
|
128
|
+
* notes whose bodies are read, each with its record.
|
|
129
|
+
* @returns {Map<string, Map<string, string[]>>} By language Address, by the
|
|
130
|
+
* word's form, the citing notes in code-point order.
|
|
131
|
+
*/
|
|
132
|
+
export function wordCitations(records, notes) {
|
|
133
|
+
const byFold = new Map();
|
|
134
|
+
for (const record of records) {
|
|
135
|
+
if (!isLanguageRecord(record)) continue;
|
|
136
|
+
const language = noteAddressOf(record);
|
|
137
|
+
if (!language) continue;
|
|
138
|
+
for (const word of Array.isArray(record.data?.words) ? record.data.words : []) {
|
|
139
|
+
if (!isMap(word) || typeof word.form !== "string") continue;
|
|
140
|
+
const spellings = [
|
|
141
|
+
word.form,
|
|
142
|
+
...(Array.isArray(word.variants) ? word.variants : []).filter(
|
|
143
|
+
(variant) => typeof variant === "string",
|
|
144
|
+
),
|
|
145
|
+
];
|
|
146
|
+
for (const spelling of spellings) {
|
|
147
|
+
const fold = foldForm(spelling);
|
|
148
|
+
if (!fold) continue;
|
|
149
|
+
if (!byFold.has(fold)) byFold.set(fold, []);
|
|
150
|
+
byFold.get(fold).push({ language, form: word.form });
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const cited = new Map();
|
|
155
|
+
if (!byFold.size) return cited;
|
|
156
|
+
/** Cite every word a folded spelling matches from `note`, never from its own language. */
|
|
157
|
+
const cite = (note, fold) => {
|
|
158
|
+
for (const { language, form } of byFold.get(fold) ?? []) {
|
|
159
|
+
if (language === note) continue;
|
|
160
|
+
if (!cited.has(language)) cited.set(language, new Map());
|
|
161
|
+
const words = cited.get(language);
|
|
162
|
+
if (!words.has(form)) words.set(form, new Set());
|
|
163
|
+
words.get(form).add(note);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
for (const { record, body } of notes) {
|
|
167
|
+
if (isLanguageRecord(record)) continue;
|
|
168
|
+
const note = noteAddressOf(record);
|
|
169
|
+
if (!note) continue;
|
|
170
|
+
for (const fold of new Set(markedUses(body).map(foldForm))) cite(note, fold);
|
|
171
|
+
}
|
|
172
|
+
for (const record of records) {
|
|
173
|
+
if (record?.documents || record?.type === "folder") continue;
|
|
174
|
+
const note = noteAddressOf(record);
|
|
175
|
+
if (!note) continue;
|
|
176
|
+
for (const name of namesCarried(record)) cite(note, foldForm(name));
|
|
177
|
+
}
|
|
178
|
+
const out = new Map();
|
|
179
|
+
for (const [language, words] of cited)
|
|
180
|
+
out.set(language, new Map([...words].map(([form, set]) => [form, [...set].sort(compare)])));
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Set each language record's `citations` from {@link wordCitations}: one entry
|
|
186
|
+
* per cited word, in the order the words stand, `{ form, notes }`. A language
|
|
187
|
+
* with no cited word carries no `citations`.
|
|
188
|
+
*
|
|
189
|
+
* @param {Record<string, any>[]} records - Index records, written in place.
|
|
190
|
+
* @param {Iterable<{record: Record<string, any>, body: string}>} notes - The
|
|
191
|
+
* notes whose bodies are read.
|
|
192
|
+
* @returns {void}
|
|
193
|
+
*/
|
|
194
|
+
export function attachCitations(records, notes) {
|
|
195
|
+
const cited = wordCitations(records, notes);
|
|
196
|
+
for (const record of records) {
|
|
197
|
+
if (!isLanguageRecord(record)) continue;
|
|
198
|
+
const words = cited.get(noteAddressOf(record) ?? "");
|
|
199
|
+
if (!words) continue;
|
|
200
|
+
const citations = [];
|
|
201
|
+
for (const word of record.data.words)
|
|
202
|
+
if (isMap(word) && words.has(word.form))
|
|
203
|
+
citations.push({ form: word.form, notes: words.get(word.form) });
|
|
204
|
+
record.citations = citations;
|
|
205
|
+
}
|
|
206
|
+
}
|