@dialecte/cli 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +212 -19
- package/dist/cli.js +54477 -48362
- package/dist/lookups.d.ts +357 -13
- package/dist/lookups.js +2246 -26
- package/package.json +20 -1
package/dist/lookups.js
CHANGED
|
@@ -324,40 +324,45 @@ var SHORT_SUMMARY_MAX_LENGTH = 120;
|
|
|
324
324
|
|
|
325
325
|
// src/lib/api/read-api-indexes.ts
|
|
326
326
|
function readApiIndexes(params) {
|
|
327
|
+
return apiIndexesOf({ files: apiIndexFilesOf(params) });
|
|
328
|
+
}
|
|
329
|
+
function apiIndexFilesOf(params) {
|
|
327
330
|
const { cwd } = params;
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
331
|
+
const installed = existsSync2(join3(cwd, "package.json")) ? discoverSkillSources({ cwd }).sources.map((source) => source.path) : [];
|
|
332
|
+
return [...ownSkillFolders(cwd), ...installed].map((folder) => join3(folder, API_JSON_FOLDER, API_JSON_FILE)).filter((path) => existsSync2(path));
|
|
333
|
+
}
|
|
334
|
+
function apiIndexesOf(params) {
|
|
335
|
+
return firstPerPackage(params.files.map(readApiJson));
|
|
336
|
+
}
|
|
337
|
+
function firstPerPackage(indexes) {
|
|
332
338
|
const byPackage = /* @__PURE__ */ new Map();
|
|
333
|
-
for (const
|
|
334
|
-
const path = join3(folder, API_JSON_FOLDER, API_JSON_FILE);
|
|
335
|
-
if (!existsSync2(path)) continue;
|
|
336
|
-
const index = readApiJson(path);
|
|
339
|
+
for (const index of indexes)
|
|
337
340
|
if (!byPackage.has(index.package)) byPackage.set(index.package, index);
|
|
338
|
-
}
|
|
339
341
|
return [...byPackage.values()].sort(
|
|
340
342
|
(left, right) => TEXT_COLLATOR.compare(left.package, right.package)
|
|
341
343
|
);
|
|
342
344
|
}
|
|
345
|
+
function parseApiJson(path) {
|
|
346
|
+
let value;
|
|
347
|
+
try {
|
|
348
|
+
value = JSON.parse(readFileSync2(path, "utf8"));
|
|
349
|
+
} catch (error) {
|
|
350
|
+
return { problem: error instanceof Error ? error.message : String(error) };
|
|
351
|
+
}
|
|
352
|
+
if (!isApiJson(value)) return { problem: "no package name or no method list" };
|
|
353
|
+
return { index: value };
|
|
354
|
+
}
|
|
343
355
|
function ownSkillFolders(cwd) {
|
|
344
356
|
const skillsRoot = join3(cwd, SKILLS_FOLDER);
|
|
345
357
|
if (!existsSync2(skillsRoot)) return [];
|
|
346
358
|
return readdirSync2(skillsRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => join3(skillsRoot, entry.name)).sort();
|
|
347
359
|
}
|
|
348
360
|
function readApiJson(path) {
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
throw new Error(
|
|
355
|
-
`${path} is not a valid api index (${error instanceof Error ? error.message : String(error)}): ${fix}`
|
|
356
|
-
);
|
|
357
|
-
}
|
|
358
|
-
if (!isApiJson(value))
|
|
359
|
-
throw new Error(`${path} is not a valid api index (no package name or no method list): ${fix}`);
|
|
360
|
-
return value;
|
|
361
|
+
const read = parseApiJson(path);
|
|
362
|
+
if ("index" in read) return read.index;
|
|
363
|
+
throw new Error(
|
|
364
|
+
`${path} is not a valid api index (${read.problem}): reinstall the package, or run \`dialecte skills generate\` in its repository`
|
|
365
|
+
);
|
|
361
366
|
}
|
|
362
367
|
function isApiJson(value) {
|
|
363
368
|
return typeof value === "object" && value !== null && "package" in value && typeof value.package === "string" && "methods" in value && Array.isArray(value.methods);
|
|
@@ -558,12 +563,111 @@ function isInWorkTree(root) {
|
|
|
558
563
|
return tryReadGit({ root, args: ["rev-parse", "--is-inside-work-tree"] })?.trim() === "true";
|
|
559
564
|
}
|
|
560
565
|
|
|
561
|
-
// src/lib/
|
|
566
|
+
// src/lib/gate/step-source.ts
|
|
562
567
|
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
563
568
|
import { join as join5 } from "path";
|
|
569
|
+
|
|
570
|
+
// src/lib/gate/step-source.constants.ts
|
|
571
|
+
var DIALECTE_JSON_FILE = "dialecte.json";
|
|
572
|
+
var DIALECTE_JSON_SCHEMA = 1;
|
|
573
|
+
var DIALECTE_JSON_KEYS = ["schema", "gate"];
|
|
574
|
+
var INFERRED_TOOLS = [
|
|
575
|
+
{
|
|
576
|
+
label: "pyproject.toml + uv.lock",
|
|
577
|
+
files: ["pyproject.toml", "uv.lock"],
|
|
578
|
+
steps: {
|
|
579
|
+
fast: [{ name: "tests", run: "uv run pytest", counts: "tests" }],
|
|
580
|
+
full: [{ name: "tests", run: "uv run pytest", counts: "tests" }]
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
];
|
|
584
|
+
var NO_STEP_SOURCE = 'no package.json and nothing to run: write a dialecte.json at the root ({ "gate": { "fast": [...], "full": [...] } }), or hold the files the gate infers its steps from: {tools}';
|
|
585
|
+
var INVALID_DIALECTE_JSON = "dialecte.json: {problem}";
|
|
586
|
+
var DIALECTE_JSON_PROBLEMS = {
|
|
587
|
+
notJson: "not valid JSON ({error})",
|
|
588
|
+
notAnObject: 'an object such as { "gate": { "fast": [...], "full": [...] } }',
|
|
589
|
+
unknownKey: 'unknown key "{key}" (only "schema" and "gate" are read)',
|
|
590
|
+
schema: '"schema" is 1',
|
|
591
|
+
gateNotAnObject: '"gate" is an object of step lists',
|
|
592
|
+
skip: '"gate.skip" opts out of a default npm step, and a repository without package.json has none: declare the lists instead',
|
|
593
|
+
missingList: 'no "{list}" list, and nothing to infer it from: declare it, or hold the files the gate infers its steps from: {tools}'
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// src/lib/gate/step-source.ts
|
|
597
|
+
function readStepSource(root) {
|
|
598
|
+
const packagePath = join5(root, "package.json");
|
|
599
|
+
if (existsSync3(packagePath))
|
|
600
|
+
return {
|
|
601
|
+
origin: "package.json",
|
|
602
|
+
packageJson: JSON.parse(readFileSync3(packagePath, "utf8"))
|
|
603
|
+
};
|
|
604
|
+
const inferred = INFERRED_TOOLS.find(function holdsEveryFile(tool) {
|
|
605
|
+
return tool.files.every((file) => existsSync3(join5(root, file)));
|
|
606
|
+
});
|
|
607
|
+
const jsonPath = join5(root, DIALECTE_JSON_FILE);
|
|
608
|
+
if (!existsSync3(jsonPath))
|
|
609
|
+
return inferred ? {
|
|
610
|
+
origin: "inferred",
|
|
611
|
+
packageJson: gatePackageJson({
|
|
612
|
+
fast: [...inferred.steps.fast],
|
|
613
|
+
full: [...inferred.steps.full]
|
|
614
|
+
})
|
|
615
|
+
} : { refusal: NO_STEP_SOURCE.replace("{tools}", inferredToolsLabel()) };
|
|
616
|
+
const read = readDialecteJson(readFileSync3(jsonPath, "utf8"));
|
|
617
|
+
if ("refusal" in read) return read;
|
|
618
|
+
const gate = { ...read.gate };
|
|
619
|
+
for (const list of ["fast", "full"]) {
|
|
620
|
+
if (gate[list] !== void 0) continue;
|
|
621
|
+
if (inferred === void 0)
|
|
622
|
+
return {
|
|
623
|
+
refusal: INVALID_DIALECTE_JSON.replace(
|
|
624
|
+
"{problem}",
|
|
625
|
+
() => DIALECTE_JSON_PROBLEMS.missingList.replace("{list}", list).replace("{tools}", inferredToolsLabel())
|
|
626
|
+
)
|
|
627
|
+
};
|
|
628
|
+
gate[list] = [...inferred.steps[list]];
|
|
629
|
+
}
|
|
630
|
+
return { origin: "dialecte.json", packageJson: gatePackageJson(gate) };
|
|
631
|
+
}
|
|
632
|
+
function readDialecteJson(text) {
|
|
633
|
+
const problem = findDialecteJsonProblem(text);
|
|
634
|
+
return typeof problem === "string" ? { refusal: INVALID_DIALECTE_JSON.replace("{problem}", () => problem) } : problem;
|
|
635
|
+
}
|
|
636
|
+
function findDialecteJsonProblem(text) {
|
|
637
|
+
let value;
|
|
638
|
+
try {
|
|
639
|
+
value = JSON.parse(text);
|
|
640
|
+
} catch (error) {
|
|
641
|
+
return DIALECTE_JSON_PROBLEMS.notJson.replace("{error}", () => error.message);
|
|
642
|
+
}
|
|
643
|
+
if (!isObject(value)) return DIALECTE_JSON_PROBLEMS.notAnObject;
|
|
644
|
+
const unknownKey = Object.keys(value).find((key) => !DIALECTE_JSON_KEYS.includes(key));
|
|
645
|
+
if (unknownKey !== void 0)
|
|
646
|
+
return DIALECTE_JSON_PROBLEMS.unknownKey.replace("{key}", unknownKey);
|
|
647
|
+
if (value.schema !== void 0 && value.schema !== DIALECTE_JSON_SCHEMA)
|
|
648
|
+
return DIALECTE_JSON_PROBLEMS.schema;
|
|
649
|
+
if (value.gate !== void 0 && !isObject(value.gate))
|
|
650
|
+
return DIALECTE_JSON_PROBLEMS.gateNotAnObject;
|
|
651
|
+
if (isObject(value.gate) && value.gate.skip !== void 0) return DIALECTE_JSON_PROBLEMS.skip;
|
|
652
|
+
return value;
|
|
653
|
+
}
|
|
654
|
+
function isObject(value) {
|
|
655
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
656
|
+
}
|
|
657
|
+
function gatePackageJson(gate) {
|
|
658
|
+
return { dialecte: { gate } };
|
|
659
|
+
}
|
|
660
|
+
function inferredToolsLabel() {
|
|
661
|
+
return INFERRED_TOOLS.map(function toolLabel(tool) {
|
|
662
|
+
const commands = [...new Set(tool.steps.full.map((step) => step.run))];
|
|
663
|
+
return `${tool.label} (${commands.join(", ")})`;
|
|
664
|
+
}).join("; ");
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
// src/lib/package-json.ts
|
|
564
668
|
function readPackageJson(root) {
|
|
565
|
-
const
|
|
566
|
-
return
|
|
669
|
+
const source = readStepSource(root);
|
|
670
|
+
return "refusal" in source ? {} : source.packageJson;
|
|
567
671
|
}
|
|
568
672
|
|
|
569
673
|
// src/lib/process-alive.ts
|
|
@@ -1273,6 +1377,11 @@ import { existsSync as existsSync12, readFileSync as readFileSync9 } from "fs";
|
|
|
1273
1377
|
import { dirname as dirname10, join as join16 } from "path";
|
|
1274
1378
|
import { fileURLToPath } from "url";
|
|
1275
1379
|
function readCliVersion(params = {}) {
|
|
1380
|
+
const folder = findPackageFolder(params);
|
|
1381
|
+
const { version } = JSON.parse(readFileSync9(join16(folder, "package.json"), "utf8"));
|
|
1382
|
+
return version;
|
|
1383
|
+
}
|
|
1384
|
+
function findPackageFolder(params = {}) {
|
|
1276
1385
|
const startFolder = params.startFolder ?? dirname10(fileURLToPath(import.meta.url));
|
|
1277
1386
|
let folder = startFolder;
|
|
1278
1387
|
while (!existsSync12(join16(folder, "package.json"))) {
|
|
@@ -1281,8 +1390,7 @@ function readCliVersion(params = {}) {
|
|
|
1281
1390
|
throw new Error(`no package.json above ${startFolder}: cannot read the cli version`);
|
|
1282
1391
|
folder = parent;
|
|
1283
1392
|
}
|
|
1284
|
-
|
|
1285
|
-
return version;
|
|
1393
|
+
return folder;
|
|
1286
1394
|
}
|
|
1287
1395
|
|
|
1288
1396
|
// src/lib/api/source-index.ts
|
|
@@ -2939,10 +3047,2122 @@ async function readSchemaDialectes(params) {
|
|
|
2939
3047
|
}
|
|
2940
3048
|
};
|
|
2941
3049
|
}
|
|
3050
|
+
|
|
3051
|
+
// src/lib/knowledge/standard/ensure-standard-index.ts
|
|
3052
|
+
import { createHash as createHash2 } from "crypto";
|
|
3053
|
+
import { existsSync as existsSync22, readdirSync as readdirSync11, readFileSync as readFileSync16, renameSync as renameSync5, rmSync as rmSync5 } from "fs";
|
|
3054
|
+
import { join as join27 } from "path";
|
|
3055
|
+
|
|
3056
|
+
// src/lib/progress/format-duration.ts
|
|
3057
|
+
function formatDuration(milliseconds) {
|
|
3058
|
+
const seconds = milliseconds / 1e3;
|
|
3059
|
+
if (seconds < 10) return `${seconds.toFixed(1)} s`;
|
|
3060
|
+
if (seconds < 60) return `${Math.floor(seconds)} s`;
|
|
3061
|
+
const minutes = Math.floor(seconds / 60);
|
|
3062
|
+
if (minutes < 60) return `${minutes} m ${Math.floor(seconds % 60)} s`;
|
|
3063
|
+
return `${Math.floor(minutes / 60)} h ${minutes % 60} m`;
|
|
3064
|
+
}
|
|
3065
|
+
|
|
3066
|
+
// src/lib/knowledge/standard/fill-message.constants.ts
|
|
3067
|
+
var PLACEHOLDER_PATTERN = /\{([^{}]+)\}/g;
|
|
3068
|
+
|
|
3069
|
+
// src/lib/knowledge/standard/fill-message.ts
|
|
3070
|
+
function fillMessage(params) {
|
|
3071
|
+
const { template, values } = params;
|
|
3072
|
+
return template.replace(
|
|
3073
|
+
PLACEHOLDER_PATTERN,
|
|
3074
|
+
function valueOf(placeholder, name) {
|
|
3075
|
+
return Object.hasOwn(values, name) ? values[name] : placeholder;
|
|
3076
|
+
}
|
|
3077
|
+
);
|
|
3078
|
+
}
|
|
3079
|
+
|
|
3080
|
+
// src/lib/knowledge/standard/standard-index.constants.ts
|
|
3081
|
+
var CAPTION_WORDS = {
|
|
3082
|
+
table: ["Ta", "ble"].join(""),
|
|
3083
|
+
figure: ["Fig", "ure"].join("")
|
|
3084
|
+
};
|
|
3085
|
+
var ANNEX_WORD = ["An", "nex"].join("");
|
|
3086
|
+
var CLAUSE_WORD = ["cla", "use"].join("");
|
|
3087
|
+
var SUBCLAUSE_PREFIX = "sub";
|
|
3088
|
+
function eitherCaseFirst(word) {
|
|
3089
|
+
return `[${word.charAt(0).toUpperCase()}${word.charAt(0).toLowerCase()}]${word.slice(1)}`;
|
|
3090
|
+
}
|
|
3091
|
+
var NUMBERED_HEADING_PATTERN = /^(\d+(?:\.\d+)*)\.?\s+(\S.*)$/u;
|
|
3092
|
+
var ANNEX_SUB_HEADING_PATTERN = /^([A-Z](?:\.\d+)+)\.?\s+(\S.*)$/u;
|
|
3093
|
+
var ANNEX_HEADING_PATTERN = new RegExp(
|
|
3094
|
+
`^${ANNEX_WORD}\\s+([A-Z])(?![\\p{L}\\p{N}_.])\\s*(.*)$`,
|
|
3095
|
+
"u"
|
|
3096
|
+
);
|
|
3097
|
+
var QUALIFIER_BEFORE_PATTERN = new RegExp("^\\(\\s*\\p{Ll}+\\s*\\)\\s*", "u");
|
|
3098
|
+
var QUALIFIER_AFTER_PATTERN = new RegExp("\\s*\\(\\s*\\p{Ll}+\\s*\\)$", "u");
|
|
3099
|
+
var CAPITAL_START_PATTERN = new RegExp("^\\p{Lu}", "u");
|
|
3100
|
+
var TITLE_LENGTH_LIMIT = 120;
|
|
3101
|
+
var TOP_LEVEL_NUMBER_LIMIT = 50;
|
|
3102
|
+
var TOP_LEVEL_JUMP_LIMIT = 3;
|
|
3103
|
+
var DIGITS_PATTERN = /^\d+$/;
|
|
3104
|
+
var LETTER_ORDER_BASE = 1e9;
|
|
3105
|
+
var DOT_LEADER_PATTERN = /\.{4,}|(?:\. ){3,}/;
|
|
3106
|
+
var DEFINITION_PATTERN = /^(\d+)((?:\.\d+)+)\.?(?:\s+(.*))?$/u;
|
|
3107
|
+
var TERMS_TITLE_PATTERN = /^terms(?![\p{L}\p{N}_])/iu;
|
|
3108
|
+
var CAPTION_PATTERN = new RegExp(
|
|
3109
|
+
`^(${CAPTION_WORDS.table}|${CAPTION_WORDS.figure})\\s+([A-Z]\\.\\d+|\\d+)(?:\\s*[-\\u2013\\u2014:.].*)?$`,
|
|
3110
|
+
"u"
|
|
3111
|
+
);
|
|
3112
|
+
var TABLE_SEPARATOR_ROW_PATTERN = /^\|?\s*:?-+:?\s*(?:\|\s*:?-+:?\s*)*\|?$/;
|
|
3113
|
+
var LEADING_MARKERS_PATTERN = /^(?:#+(?=\s|$)\s*|>\s*)+/;
|
|
3114
|
+
var HEADING_MARKER_PATTERN = /^#+(?=\s|$)/u;
|
|
3115
|
+
var LIST_ITEM_PATTERN = /^\d+\.\s/u;
|
|
3116
|
+
var WRAPPING_MARKERS = ["**", "__"];
|
|
3117
|
+
var FENCE_MARKER = "```";
|
|
3118
|
+
var TABLE_LINE_START = "|";
|
|
3119
|
+
var TABLE_ROW_START_PIPE_PATTERN = /^\|/;
|
|
3120
|
+
var TABLE_ROW_END_PIPE_PATTERN = /\|$/;
|
|
3121
|
+
var CODE_LINE_START = "<";
|
|
3122
|
+
var LETTER_SPACED_MINIMUM_TOKENS = 4;
|
|
3123
|
+
var LETTER_SPACED_MINIMUM_SHARE = 0.8;
|
|
3124
|
+
var WIDE_GAP_PATTERN = /\s{2,}/;
|
|
3125
|
+
var WHITESPACE_PATTERN = /\s+/;
|
|
3126
|
+
var NOISE_RUN_MINIMUM = 20;
|
|
3127
|
+
var REPEATED_MINIMUM_COUNT = 20;
|
|
3128
|
+
var REPEATED_MINIMUM_MEDIAN_GAP = 20;
|
|
3129
|
+
var REPEATED_MINIMUM_LENGTH = 2;
|
|
3130
|
+
var QUALITY_BLOCK_LINES = 1e3;
|
|
3131
|
+
var GOOD_NOISE_RATIO_LIMIT = 0.2;
|
|
3132
|
+
var POOR_NOISE_RATIO_LIMIT = 0.7;
|
|
3133
|
+
var CAPTION_LOOKBACK_LINES = 5;
|
|
3134
|
+
var SPLIT_WORD_END_PATTERN = new RegExp("\\p{L}-$", "u");
|
|
3135
|
+
var LOWER_CASE_START_PATTERN = new RegExp("^\\p{Ll}", "u");
|
|
3136
|
+
var CROSS_REFERENCE_PATTERN = new RegExp(
|
|
3137
|
+
[
|
|
3138
|
+
"(?<![\\p{L}\\p{N}_])(?:",
|
|
3139
|
+
`(?:${eitherCaseFirst(SUBCLAUSE_PREFIX)})?${eitherCaseFirst(CLAUSE_WORD)}\\s+(?<clause>[A-Z](?:\\.\\d+)+|\\d+(?:\\.\\d+)*)`,
|
|
3140
|
+
"|[Ss]ee\\s+(?<see>\\d+(?:\\.\\d+)+)",
|
|
3141
|
+
"|\\((?<parenthesised>\\d+\\.\\d+\\.\\d+(?:\\.\\d+)*)\\)",
|
|
3142
|
+
`|${eitherCaseFirst(CAPTION_WORDS.table)}\\s+(?<table>[A-Z]\\.\\d+|\\d+)(?![\\p{L}\\p{N}_])`,
|
|
3143
|
+
`|${eitherCaseFirst(ANNEX_WORD)}\\s+(?<annex>[A-Z])(?![\\p{L}\\p{N}_])`,
|
|
3144
|
+
")"
|
|
3145
|
+
].join(""),
|
|
3146
|
+
"gu"
|
|
3147
|
+
);
|
|
3148
|
+
var TABLE_TARGET_PREFIX = "table-";
|
|
3149
|
+
var DASH_PATTERN = /[‒–—−]/g;
|
|
3150
|
+
var SINGLE_QUOTE_PATTERN = /[‘’‚‛]/g;
|
|
3151
|
+
var DOUBLE_QUOTE_PATTERN = /[“”„‟]/g;
|
|
3152
|
+
var SOFT_HYPHEN_PATTERN = //g;
|
|
3153
|
+
var WHITESPACE_RUN_PATTERN = /\s+/g;
|
|
3154
|
+
var LINE_BREAK_PATTERN = /\r?\n/;
|
|
3155
|
+
|
|
3156
|
+
// src/lib/knowledge/standard/assess-quality.ts
|
|
3157
|
+
function assessQuality(params) {
|
|
3158
|
+
const { lines, noise, candidates, kept, repeatedLines } = params;
|
|
3159
|
+
const blocks = [];
|
|
3160
|
+
for (let start = 0; start < lines.length; start += QUALITY_BLOCK_LINES)
|
|
3161
|
+
blocks.push(blockOf({ noise, start, end: Math.min(start + QUALITY_BLOCK_LINES, lines.length) }));
|
|
3162
|
+
const droppedLines = noise.filter(Boolean).length;
|
|
3163
|
+
const blankLines = lines.filter((line) => line.kind === "blank").length;
|
|
3164
|
+
return {
|
|
3165
|
+
blocks,
|
|
3166
|
+
verdict: fileVerdictOf(blocks),
|
|
3167
|
+
readableLines: lines.length - blankLines - droppedLines,
|
|
3168
|
+
droppedLines,
|
|
3169
|
+
headingCandidatesKept: kept,
|
|
3170
|
+
headingCandidatesTurnedProse: candidates - kept,
|
|
3171
|
+
repeatedLines
|
|
3172
|
+
};
|
|
3173
|
+
}
|
|
3174
|
+
function blockOf(params) {
|
|
3175
|
+
const { noise, start, end } = params;
|
|
3176
|
+
const lines = end - start;
|
|
3177
|
+
const dropped = noise.slice(start, end).filter(Boolean).length;
|
|
3178
|
+
const noiseRatio = dropped / lines;
|
|
3179
|
+
return {
|
|
3180
|
+
from: start + 1,
|
|
3181
|
+
to: end,
|
|
3182
|
+
lines,
|
|
3183
|
+
dropped,
|
|
3184
|
+
noiseRatio,
|
|
3185
|
+
verdict: blockVerdictOf(noiseRatio)
|
|
3186
|
+
};
|
|
3187
|
+
}
|
|
3188
|
+
function blockVerdictOf(noiseRatio) {
|
|
3189
|
+
if (noiseRatio < GOOD_NOISE_RATIO_LIMIT) return "good";
|
|
3190
|
+
if (noiseRatio < POOR_NOISE_RATIO_LIMIT) return "poor";
|
|
3191
|
+
return "unreadable";
|
|
3192
|
+
}
|
|
3193
|
+
function fileVerdictOf(blocks) {
|
|
3194
|
+
if (blocks.every((block) => block.verdict === "good")) return "good";
|
|
3195
|
+
if (blocks.every((block) => block.verdict === "unreadable")) return "unreadable";
|
|
3196
|
+
return "partial";
|
|
3197
|
+
}
|
|
3198
|
+
|
|
3199
|
+
// src/lib/knowledge/standard/parse-caption.ts
|
|
3200
|
+
function parseCaption(content) {
|
|
3201
|
+
const match = CAPTION_PATTERN.exec(content);
|
|
3202
|
+
if (!match) return null;
|
|
3203
|
+
return { word: match[1] === CAPTION_WORDS.table ? "table" : "figure", number: match[2] };
|
|
3204
|
+
}
|
|
3205
|
+
|
|
3206
|
+
// src/lib/knowledge/standard/normalise-text.ts
|
|
3207
|
+
function normaliseText(text) {
|
|
3208
|
+
return text.normalize("NFKC").replace(SOFT_HYPHEN_PATTERN, "").replace(DASH_PATTERN, "-").replace(SINGLE_QUOTE_PATTERN, "'").replace(DOUBLE_QUOTE_PATTERN, '"').replace(WHITESPACE_RUN_PATTERN, " ").trim();
|
|
3209
|
+
}
|
|
3210
|
+
|
|
3211
|
+
// src/lib/knowledge/standard/parse-heading-candidate.ts
|
|
3212
|
+
function parseHeadingCandidate(params) {
|
|
3213
|
+
const { content, line } = params;
|
|
3214
|
+
const annex = ANNEX_HEADING_PATTERN.exec(content);
|
|
3215
|
+
if (annex) {
|
|
3216
|
+
const title = annex[2].replace(QUALIFIER_BEFORE_PATTERN, "").replace(QUALIFIER_AFTER_PATTERN, "");
|
|
3217
|
+
return candidateOf({ line, number: annex[1], title });
|
|
3218
|
+
}
|
|
3219
|
+
const numbered = NUMBERED_HEADING_PATTERN.exec(content) ?? ANNEX_SUB_HEADING_PATTERN.exec(content);
|
|
3220
|
+
if (!numbered) return null;
|
|
3221
|
+
return candidateOf({ line, number: numbered[1], title: numbered[2] });
|
|
3222
|
+
}
|
|
3223
|
+
function candidateOf(params) {
|
|
3224
|
+
const { line, number } = params;
|
|
3225
|
+
const title = params.title.trim();
|
|
3226
|
+
if (title.length === 0 || title.length >= TITLE_LENGTH_LIMIT) return null;
|
|
3227
|
+
if (!CAPITAL_START_PATTERN.test(title)) return null;
|
|
3228
|
+
const parts = number.split(".");
|
|
3229
|
+
const top = DIGITS_PATTERN.test(parts[0]) ? Number(parts[0]) : null;
|
|
3230
|
+
if (top !== null && top > TOP_LEVEL_NUMBER_LIMIT) return null;
|
|
3231
|
+
return {
|
|
3232
|
+
line,
|
|
3233
|
+
number,
|
|
3234
|
+
title: normaliseText(title),
|
|
3235
|
+
depth: parts.length,
|
|
3236
|
+
order: parts.map(orderOfPart),
|
|
3237
|
+
top
|
|
3238
|
+
};
|
|
3239
|
+
}
|
|
3240
|
+
function orderOfPart(part) {
|
|
3241
|
+
return DIGITS_PATTERN.test(part) ? Number(part) : LETTER_ORDER_BASE + part.charCodeAt(0);
|
|
3242
|
+
}
|
|
3243
|
+
|
|
3244
|
+
// src/lib/knowledge/standard/classify-standard-lines.ts
|
|
3245
|
+
function classifyStandardLines(params) {
|
|
3246
|
+
const asCandidates = classifyAll({ lines: params.lines, listItems: false });
|
|
3247
|
+
const otherStyle = asCandidates.some(
|
|
3248
|
+
(classified, index) => classified.candidate?.depth === 1 && classified.candidate.top !== null && (HEADING_MARKER_PATTERN.test(params.lines[index].trim()) || !LIST_ITEM_PATTERN.test(classified.content))
|
|
3249
|
+
);
|
|
3250
|
+
return otherStyle ? classifyAll({ lines: params.lines, listItems: true }) : asCandidates;
|
|
3251
|
+
}
|
|
3252
|
+
function classifyAll(params) {
|
|
3253
|
+
const classified = [];
|
|
3254
|
+
let state = {
|
|
3255
|
+
inFence: false,
|
|
3256
|
+
termsClause: null,
|
|
3257
|
+
listItems: params.listItems
|
|
3258
|
+
};
|
|
3259
|
+
for (const [index, original] of params.lines.entries()) {
|
|
3260
|
+
const next = classifyLine({ original, line: index + 1, state });
|
|
3261
|
+
classified.push(next.classified);
|
|
3262
|
+
state = next.state;
|
|
3263
|
+
}
|
|
3264
|
+
return classified;
|
|
3265
|
+
}
|
|
3266
|
+
function classifyLine(params) {
|
|
3267
|
+
const { line, state } = params;
|
|
3268
|
+
const trimmed = params.original.trim();
|
|
3269
|
+
if (state.inFence || trimmed.startsWith(FENCE_MARKER)) {
|
|
3270
|
+
const inFence = trimmed.startsWith(FENCE_MARKER) ? !state.inFence : state.inFence;
|
|
3271
|
+
return {
|
|
3272
|
+
classified: lineOf({ line, content: trimmed, kind: "code" }),
|
|
3273
|
+
state: { ...state, inFence }
|
|
3274
|
+
};
|
|
3275
|
+
}
|
|
3276
|
+
if (trimmed.startsWith(TABLE_LINE_START))
|
|
3277
|
+
return { classified: lineOf({ line, content: trimmed, kind: "table" }), state };
|
|
3278
|
+
if (trimmed.startsWith(CODE_LINE_START))
|
|
3279
|
+
return { classified: lineOf({ line, content: trimmed, kind: "code" }), state };
|
|
3280
|
+
const content = stripMarkdownMarkers(trimmed);
|
|
3281
|
+
if (content.length === 0) return { classified: lineOf({ line, content, kind: "blank" }), state };
|
|
3282
|
+
const listItems = state.listItems && !HEADING_MARKER_PATTERN.test(trimmed);
|
|
3283
|
+
const classified = classifyContent({ content, line, termsClause: state.termsClause, listItems });
|
|
3284
|
+
const settled = classified.kind === "prose" && !classified.listItem && isLetterSpaced(content) ? classifyRebuilt({ content: rebuildLetterSpaced(content), line, state, listItems }) : classified;
|
|
3285
|
+
if (settled.candidate?.depth !== 1) return { classified: settled, state };
|
|
3286
|
+
const termsClause = TERMS_TITLE_PATTERN.test(settled.candidate.title) ? settled.candidate.number : null;
|
|
3287
|
+
return { classified: settled, state: { ...state, termsClause } };
|
|
3288
|
+
}
|
|
3289
|
+
function classifyRebuilt(params) {
|
|
3290
|
+
const { content, line, state, listItems } = params;
|
|
3291
|
+
if (content.startsWith(CODE_LINE_START)) return lineOf({ line, content, kind: "code" });
|
|
3292
|
+
return classifyContent({ content, line, termsClause: state.termsClause, listItems });
|
|
3293
|
+
}
|
|
3294
|
+
function classifyContent(params) {
|
|
3295
|
+
const { content, line, termsClause } = params;
|
|
3296
|
+
if (DOT_LEADER_PATTERN.test(content) && isHeadingShaped(content))
|
|
3297
|
+
return lineOf({ line, content, kind: "toc" });
|
|
3298
|
+
if (parseCaption(content)) return lineOf({ line, content, kind: "caption" });
|
|
3299
|
+
if (termsClause !== null) {
|
|
3300
|
+
const definition = DEFINITION_PATTERN.exec(content);
|
|
3301
|
+
if (definition && definition[1] === termsClause)
|
|
3302
|
+
return {
|
|
3303
|
+
...lineOf({ line, content, kind: "definition" }),
|
|
3304
|
+
definition: { number: `${definition[1]}${definition[2]}`, term: definition[3] ?? "" }
|
|
3305
|
+
};
|
|
3306
|
+
}
|
|
3307
|
+
const candidate = parseHeadingCandidate({ content, line });
|
|
3308
|
+
if (params.listItems && LIST_ITEM_PATTERN.test(content))
|
|
3309
|
+
return { ...lineOf({ line, content, kind: "prose" }), listItem: candidate !== null };
|
|
3310
|
+
if (candidate) return { ...lineOf({ line, content, kind: "heading" }), candidate };
|
|
3311
|
+
return lineOf({ line, content, kind: "prose" });
|
|
3312
|
+
}
|
|
3313
|
+
function lineOf(params) {
|
|
3314
|
+
return { ...params, candidate: null, definition: null, listItem: false };
|
|
3315
|
+
}
|
|
3316
|
+
function stripMarkdownMarkers(trimmed) {
|
|
3317
|
+
const unmarked = trimmed.replace(LEADING_MARKERS_PATTERN, "");
|
|
3318
|
+
for (const marker of WRAPPING_MARKERS)
|
|
3319
|
+
if (unmarked.length > marker.length * 2 && unmarked.startsWith(marker) && unmarked.endsWith(marker))
|
|
3320
|
+
return unmarked.slice(marker.length, -marker.length).trim();
|
|
3321
|
+
return unmarked;
|
|
3322
|
+
}
|
|
3323
|
+
function isHeadingShaped(content) {
|
|
3324
|
+
return ANNEX_HEADING_PATTERN.test(content) || NUMBERED_HEADING_PATTERN.test(content) || ANNEX_SUB_HEADING_PATTERN.test(content);
|
|
3325
|
+
}
|
|
3326
|
+
function isLetterSpaced(content) {
|
|
3327
|
+
const tokens = content.split(WHITESPACE_PATTERN);
|
|
3328
|
+
if (tokens.length < LETTER_SPACED_MINIMUM_TOKENS) return false;
|
|
3329
|
+
const single = tokens.filter((token) => token.length === 1).length;
|
|
3330
|
+
return single >= tokens.length * LETTER_SPACED_MINIMUM_SHARE;
|
|
3331
|
+
}
|
|
3332
|
+
function rebuildLetterSpaced(content) {
|
|
3333
|
+
return content.split(WIDE_GAP_PATTERN).map((word) => word.split(WHITESPACE_PATTERN).join("")).join(" ");
|
|
3334
|
+
}
|
|
3335
|
+
|
|
3336
|
+
// src/lib/knowledge/standard/find-repeated-lines.ts
|
|
3337
|
+
function findRepeatedLines(params) {
|
|
3338
|
+
const occurrences = /* @__PURE__ */ new Map();
|
|
3339
|
+
for (const [index, original] of params.lines.entries()) {
|
|
3340
|
+
const text = original.trim();
|
|
3341
|
+
if (text.length < REPEATED_MINIMUM_LENGTH) continue;
|
|
3342
|
+
const seen = occurrences.get(text);
|
|
3343
|
+
if (seen) seen.push(index + 1);
|
|
3344
|
+
else occurrences.set(text, [index + 1]);
|
|
3345
|
+
}
|
|
3346
|
+
const repeated = [];
|
|
3347
|
+
for (const [text, lineNumbers] of occurrences) {
|
|
3348
|
+
if (lineNumbers.length < REPEATED_MINIMUM_COUNT) continue;
|
|
3349
|
+
if (TABLE_SEPARATOR_ROW_PATTERN.test(text)) continue;
|
|
3350
|
+
const medianGap = lowerMedianGap(lineNumbers);
|
|
3351
|
+
if (medianGap < REPEATED_MINIMUM_MEDIAN_GAP) continue;
|
|
3352
|
+
repeated.push({ text, count: lineNumbers.length, length: text.length, medianGap });
|
|
3353
|
+
}
|
|
3354
|
+
return repeated.sort(
|
|
3355
|
+
(first, second) => second.count - first.count || (first.text < second.text ? -1 : first.text > second.text ? 1 : 0)
|
|
3356
|
+
);
|
|
3357
|
+
}
|
|
3358
|
+
function lowerMedianGap(lineNumbers) {
|
|
3359
|
+
const gaps = lineNumbers.slice(1).map((lineNumber, index) => lineNumber - lineNumbers[index]).sort((first, second) => first - second);
|
|
3360
|
+
return gaps[Math.floor((gaps.length - 1) / 2)];
|
|
3361
|
+
}
|
|
3362
|
+
|
|
3363
|
+
// src/lib/knowledge/standard/join-logical-lines.ts
|
|
3364
|
+
function joinLogicalLines(params) {
|
|
3365
|
+
const logicalLines = [];
|
|
3366
|
+
let paragraph = [];
|
|
3367
|
+
for (const [index, line] of params.lines.entries()) {
|
|
3368
|
+
if (params.noise[index]) continue;
|
|
3369
|
+
if (line.kind === "prose") {
|
|
3370
|
+
paragraph.push(line);
|
|
3371
|
+
continue;
|
|
3372
|
+
}
|
|
3373
|
+
if (paragraph.length > 0) logicalLines.push(paragraphOf(paragraph));
|
|
3374
|
+
paragraph = [];
|
|
3375
|
+
if (line.kind !== "blank")
|
|
3376
|
+
logicalLines.push({
|
|
3377
|
+
from: line.line,
|
|
3378
|
+
to: line.line,
|
|
3379
|
+
kind: line.kind,
|
|
3380
|
+
text: normaliseText(line.content)
|
|
3381
|
+
});
|
|
3382
|
+
}
|
|
3383
|
+
if (paragraph.length > 0) logicalLines.push(paragraphOf(paragraph));
|
|
3384
|
+
return logicalLines;
|
|
3385
|
+
}
|
|
3386
|
+
function paragraphOf(lines) {
|
|
3387
|
+
const pieces = [lines[0].content];
|
|
3388
|
+
for (const [index, line] of lines.entries()) {
|
|
3389
|
+
if (index === 0) continue;
|
|
3390
|
+
const previous = pieces[pieces.length - 1];
|
|
3391
|
+
if (SPLIT_WORD_END_PATTERN.test(previous) && LOWER_CASE_START_PATTERN.test(line.content))
|
|
3392
|
+
pieces[pieces.length - 1] = `${previous.slice(0, -1)}${line.content}`;
|
|
3393
|
+
else pieces.push(line.content);
|
|
3394
|
+
}
|
|
3395
|
+
const text = pieces.join(" ");
|
|
3396
|
+
return {
|
|
3397
|
+
from: lines[0].line,
|
|
3398
|
+
to: lines[lines.length - 1].line,
|
|
3399
|
+
kind: "prose",
|
|
3400
|
+
text: normaliseText(text)
|
|
3401
|
+
};
|
|
3402
|
+
}
|
|
3403
|
+
|
|
3404
|
+
// src/lib/knowledge/standard/mark-noise-lines.ts
|
|
3405
|
+
function markNoiseLines(params) {
|
|
3406
|
+
const runs = droppedRunsOf(params.lines);
|
|
3407
|
+
const dropped = params.lines.map(() => false);
|
|
3408
|
+
for (const run of runs) for (const index of run) dropped[index] = true;
|
|
3409
|
+
return {
|
|
3410
|
+
dropped,
|
|
3411
|
+
ranges: runs.map((run) => ({ from: run[0] + 1, to: run[run.length - 1] + 1 }))
|
|
3412
|
+
};
|
|
3413
|
+
}
|
|
3414
|
+
function droppedRunsOf(lines) {
|
|
3415
|
+
const runs = [];
|
|
3416
|
+
let run = [];
|
|
3417
|
+
for (const [index, line] of lines.entries()) {
|
|
3418
|
+
if (line.kind === "blank") continue;
|
|
3419
|
+
if (line.kind === "prose" && line.content.length === 1) {
|
|
3420
|
+
run.push(index);
|
|
3421
|
+
continue;
|
|
3422
|
+
}
|
|
3423
|
+
if (run.length >= NOISE_RUN_MINIMUM) runs.push(run);
|
|
3424
|
+
run = [];
|
|
3425
|
+
}
|
|
3426
|
+
if (run.length >= NOISE_RUN_MINIMUM) runs.push(run);
|
|
3427
|
+
return runs;
|
|
3428
|
+
}
|
|
3429
|
+
|
|
3430
|
+
// src/lib/knowledge/standard/holding-clause.ts
|
|
3431
|
+
function holdingClause(params) {
|
|
3432
|
+
const { headings, line } = params;
|
|
3433
|
+
let low = 0;
|
|
3434
|
+
let high = headings.length - 1;
|
|
3435
|
+
let found = null;
|
|
3436
|
+
while (low <= high) {
|
|
3437
|
+
const middle = low + high >> 1;
|
|
3438
|
+
if (headings[middle].from <= line) {
|
|
3439
|
+
found = headings[middle];
|
|
3440
|
+
low = middle + 1;
|
|
3441
|
+
} else high = middle - 1;
|
|
3442
|
+
}
|
|
3443
|
+
return found?.number ?? null;
|
|
3444
|
+
}
|
|
3445
|
+
|
|
3446
|
+
// src/lib/knowledge/standard/read-cross-references.ts
|
|
3447
|
+
function readCrossReferences(params) {
|
|
3448
|
+
const { lines, noise, headings, tables } = params;
|
|
3449
|
+
const headingNumbers = new Set(headings.map((heading) => heading.number));
|
|
3450
|
+
const tableTargets = new Set(
|
|
3451
|
+
tables.flatMap((table) => table.number === null ? [] : [TABLE_TARGET_PREFIX + table.number])
|
|
3452
|
+
);
|
|
3453
|
+
const references = [];
|
|
3454
|
+
for (const [index, line] of lines.entries()) {
|
|
3455
|
+
if (noise[index] || !isScanned(line)) continue;
|
|
3456
|
+
for (const target of targetsOf(normaliseText(line.content)))
|
|
3457
|
+
references.push({
|
|
3458
|
+
from: line.line,
|
|
3459
|
+
inClause: holdingClause({ headings, line: line.line }),
|
|
3460
|
+
target,
|
|
3461
|
+
resolved: headingNumbers.has(target) || tableTargets.has(target)
|
|
3462
|
+
});
|
|
3463
|
+
}
|
|
3464
|
+
return references;
|
|
3465
|
+
}
|
|
3466
|
+
function isScanned(line) {
|
|
3467
|
+
return line.kind === "prose" || line.kind === "definition" || line.kind === "table";
|
|
3468
|
+
}
|
|
3469
|
+
function targetsOf(text) {
|
|
3470
|
+
const targets = [];
|
|
3471
|
+
for (const match of text.matchAll(CROSS_REFERENCE_PATTERN)) {
|
|
3472
|
+
const { clause, see, parenthesised, table, annex } = match.groups ?? {};
|
|
3473
|
+
if (table !== void 0) targets.push(TABLE_TARGET_PREFIX + table);
|
|
3474
|
+
else {
|
|
3475
|
+
const target = clause ?? see ?? parenthesised ?? annex;
|
|
3476
|
+
if (target !== void 0) targets.push(target);
|
|
3477
|
+
}
|
|
3478
|
+
}
|
|
3479
|
+
return targets;
|
|
3480
|
+
}
|
|
3481
|
+
|
|
3482
|
+
// src/lib/knowledge/standard/read-definitions.ts
|
|
3483
|
+
function readDefinitions(params) {
|
|
3484
|
+
const { lines, noise } = params;
|
|
3485
|
+
const definitions = [];
|
|
3486
|
+
for (const [index, line] of lines.entries()) {
|
|
3487
|
+
if (!line.definition || noise[index]) continue;
|
|
3488
|
+
definitions.push({
|
|
3489
|
+
term: normaliseText(line.definition.term) || termBelow({ lines, noise, index }),
|
|
3490
|
+
number: line.definition.number,
|
|
3491
|
+
from: line.line,
|
|
3492
|
+
to: endOf({ lines, index })
|
|
3493
|
+
});
|
|
3494
|
+
}
|
|
3495
|
+
return definitions;
|
|
3496
|
+
}
|
|
3497
|
+
function termBelow(params) {
|
|
3498
|
+
const { lines, noise, index } = params;
|
|
3499
|
+
for (let next = index + 1; next < lines.length; next++)
|
|
3500
|
+
if (lines[next].kind !== "blank" && !noise[next]) return normaliseText(lines[next].content);
|
|
3501
|
+
return "";
|
|
3502
|
+
}
|
|
3503
|
+
function endOf(params) {
|
|
3504
|
+
const { lines, index } = params;
|
|
3505
|
+
for (let next = index + 1; next < lines.length; next++)
|
|
3506
|
+
if (lines[next].definition || lines[next].candidate?.depth === 1) return lines[next].line - 1;
|
|
3507
|
+
return lines.length;
|
|
3508
|
+
}
|
|
3509
|
+
|
|
3510
|
+
// src/lib/knowledge/standard/read-headings.ts
|
|
3511
|
+
function readHeadings(params) {
|
|
3512
|
+
const { fileId, kept, lineCount } = params;
|
|
3513
|
+
const ids = kept.map((candidate) => idOf({ fileId, candidate }));
|
|
3514
|
+
const lastIdAtDepth = /* @__PURE__ */ new Map();
|
|
3515
|
+
return kept.map((candidate, index) => {
|
|
3516
|
+
const parent = lastIdAtDepth.get(`${candidate.pass}:${candidate.depth - 1}`) ?? null;
|
|
3517
|
+
lastIdAtDepth.set(`${candidate.pass}:${candidate.depth}`, ids[index]);
|
|
3518
|
+
return {
|
|
3519
|
+
id: ids[index],
|
|
3520
|
+
number: candidate.number,
|
|
3521
|
+
title: candidate.title,
|
|
3522
|
+
from: candidate.line,
|
|
3523
|
+
to: endOf2({ kept, index, lineCount }),
|
|
3524
|
+
depth: candidate.depth,
|
|
3525
|
+
parent,
|
|
3526
|
+
pass: candidate.pass
|
|
3527
|
+
};
|
|
3528
|
+
});
|
|
3529
|
+
}
|
|
3530
|
+
function idOf(params) {
|
|
3531
|
+
const { fileId, candidate } = params;
|
|
3532
|
+
const suffix = candidate.pass === 1 ? "" : `~${candidate.pass}`;
|
|
3533
|
+
return `${fileId}#${candidate.number}${suffix}`;
|
|
3534
|
+
}
|
|
3535
|
+
function endOf2(params) {
|
|
3536
|
+
const { kept, index, lineCount } = params;
|
|
3537
|
+
const depth = kept[index].depth;
|
|
3538
|
+
for (let next = index + 1; next < kept.length; next++)
|
|
3539
|
+
if (kept[next].depth <= depth) return kept[next].line - 1;
|
|
3540
|
+
return lineCount;
|
|
3541
|
+
}
|
|
3542
|
+
|
|
3543
|
+
// src/lib/knowledge/standard/cells-of-row.ts
|
|
3544
|
+
function cellsOfRow(row) {
|
|
3545
|
+
const inner = row.trim().replace(TABLE_ROW_START_PIPE_PATTERN, "").replace(TABLE_ROW_END_PIPE_PATTERN, "");
|
|
3546
|
+
return inner.split(TABLE_LINE_START).map((cell) => cell.trim());
|
|
3547
|
+
}
|
|
3548
|
+
|
|
3549
|
+
// src/lib/knowledge/standard/find-caption-above.ts
|
|
3550
|
+
function findCaptionAbove(params) {
|
|
3551
|
+
const { logicalLines, start } = params;
|
|
3552
|
+
const first = Math.max(0, start - CAPTION_LOOKBACK_LINES);
|
|
3553
|
+
for (let index = start - 1; index >= first; index--) {
|
|
3554
|
+
const line = logicalLines[index];
|
|
3555
|
+
if (line.kind === "table") return null;
|
|
3556
|
+
if (line.kind !== "caption") continue;
|
|
3557
|
+
if (parseCaption(line.text)?.word === "table") return index;
|
|
3558
|
+
}
|
|
3559
|
+
return null;
|
|
3560
|
+
}
|
|
3561
|
+
|
|
3562
|
+
// src/lib/knowledge/standard/read-tables.ts
|
|
3563
|
+
function readTables(params) {
|
|
3564
|
+
const { fileId, logicalLines, headings } = params;
|
|
3565
|
+
const tables = [];
|
|
3566
|
+
let start = -1;
|
|
3567
|
+
for (const [index, line] of logicalLines.entries()) {
|
|
3568
|
+
if (line.kind !== "table") continue;
|
|
3569
|
+
if (start === -1) start = index;
|
|
3570
|
+
const next = logicalLines[index + 1];
|
|
3571
|
+
if (next?.kind === "table" && next.from === line.to + 1) continue;
|
|
3572
|
+
tables.push(
|
|
3573
|
+
tableOf({
|
|
3574
|
+
fileId,
|
|
3575
|
+
logicalLines,
|
|
3576
|
+
headings,
|
|
3577
|
+
start,
|
|
3578
|
+
end: index,
|
|
3579
|
+
position: tables.length + 1
|
|
3580
|
+
})
|
|
3581
|
+
);
|
|
3582
|
+
start = -1;
|
|
3583
|
+
}
|
|
3584
|
+
return tables;
|
|
3585
|
+
}
|
|
3586
|
+
function tableOf(params) {
|
|
3587
|
+
const { fileId, logicalLines, headings, start, end, position } = params;
|
|
3588
|
+
const rows = logicalLines.slice(start, end + 1);
|
|
3589
|
+
const captionIndex = findCaptionAbove({ logicalLines, start });
|
|
3590
|
+
const caption = captionIndex === null ? null : logicalLines[captionIndex].text;
|
|
3591
|
+
const header = rows.find((row) => !TABLE_SEPARATOR_ROW_PATTERN.test(row.text));
|
|
3592
|
+
return {
|
|
3593
|
+
id: `${fileId}#table-${position}`,
|
|
3594
|
+
caption,
|
|
3595
|
+
number: caption === null ? null : parseCaption(caption)?.number ?? null,
|
|
3596
|
+
from: rows[0].from,
|
|
3597
|
+
to: rows[rows.length - 1].to,
|
|
3598
|
+
clause: holdingClause({ headings, line: rows[0].from }),
|
|
3599
|
+
columns: header ? cellsOfRow(header.text) : []
|
|
3600
|
+
};
|
|
3601
|
+
}
|
|
3602
|
+
|
|
3603
|
+
// src/lib/knowledge/standard/select-headings.ts
|
|
3604
|
+
function selectHeadings(params) {
|
|
3605
|
+
return passesOf(params.candidates).flatMap(
|
|
3606
|
+
(pass, index) => longestSequence(pass).map((candidate) => ({ ...candidate, pass: index + 1 }))
|
|
3607
|
+
);
|
|
3608
|
+
}
|
|
3609
|
+
function passesOf(candidates) {
|
|
3610
|
+
const passes = [[]];
|
|
3611
|
+
let holdsOne = false;
|
|
3612
|
+
for (const candidate of candidates) {
|
|
3613
|
+
if (candidate.number === "1") {
|
|
3614
|
+
if (holdsOne) passes.push([]);
|
|
3615
|
+
holdsOne = true;
|
|
3616
|
+
}
|
|
3617
|
+
passes[passes.length - 1].push(candidate);
|
|
3618
|
+
}
|
|
3619
|
+
return passes;
|
|
3620
|
+
}
|
|
3621
|
+
function longestSequence(candidates) {
|
|
3622
|
+
if (candidates.length === 0) return [];
|
|
3623
|
+
const lengths = candidates.map(() => 1);
|
|
3624
|
+
for (let from = candidates.length - 1; from >= 0; from--)
|
|
3625
|
+
for (let to = from + 1; to < candidates.length; to++)
|
|
3626
|
+
if (lengths[to] + 1 > lengths[from] && isValidStep({ from: candidates[from], to: candidates[to] }))
|
|
3627
|
+
lengths[from] = lengths[to] + 1;
|
|
3628
|
+
const longest = lengths.reduce((maximum, length) => Math.max(maximum, length), 0);
|
|
3629
|
+
let current = lengths.indexOf(longest);
|
|
3630
|
+
const sequence = [candidates[current]];
|
|
3631
|
+
while (lengths[current] > 1) {
|
|
3632
|
+
const remaining = lengths[current] - 1;
|
|
3633
|
+
let next = current + 1;
|
|
3634
|
+
while (!(lengths[next] === remaining && isValidStep({ from: candidates[current], to: candidates[next] })))
|
|
3635
|
+
next++;
|
|
3636
|
+
sequence.push(candidates[next]);
|
|
3637
|
+
current = next;
|
|
3638
|
+
}
|
|
3639
|
+
return sequence;
|
|
3640
|
+
}
|
|
3641
|
+
function isValidStep(params) {
|
|
3642
|
+
const { from, to } = params;
|
|
3643
|
+
if (compareTreeOrder({ first: from.order, second: to.order }) >= 0) return false;
|
|
3644
|
+
if (to.depth > from.depth + 1) return false;
|
|
3645
|
+
if (from.top !== null && to.top !== null) return to.top - from.top <= TOP_LEVEL_JUMP_LIMIT;
|
|
3646
|
+
return true;
|
|
3647
|
+
}
|
|
3648
|
+
function compareTreeOrder(params) {
|
|
3649
|
+
const { first, second } = params;
|
|
3650
|
+
const shared = Math.min(first.length, second.length);
|
|
3651
|
+
for (let part = 0; part < shared; part++)
|
|
3652
|
+
if (first[part] !== second[part]) return first[part] - second[part];
|
|
3653
|
+
return first.length - second.length;
|
|
3654
|
+
}
|
|
3655
|
+
|
|
3656
|
+
// src/lib/knowledge/standard/index-standard-text.ts
|
|
3657
|
+
function indexStandardText(params) {
|
|
3658
|
+
const { fileId } = params;
|
|
3659
|
+
const originalLines = linesOf(params.text);
|
|
3660
|
+
const classified = classifyStandardLines({ lines: originalLines });
|
|
3661
|
+
const { dropped: noise, ranges: noiseRanges } = markNoiseLines({ lines: classified });
|
|
3662
|
+
const candidates = classified.flatMap(
|
|
3663
|
+
(line) => line.kind === "heading" && line.candidate ? [line.candidate] : []
|
|
3664
|
+
);
|
|
3665
|
+
const kept = selectHeadings({ candidates });
|
|
3666
|
+
const lines = settleLeftOutCandidates({
|
|
3667
|
+
lines: classified,
|
|
3668
|
+
keptLines: new Set(kept.map((candidate) => candidate.line))
|
|
3669
|
+
});
|
|
3670
|
+
const logicalLines = joinLogicalLines({ lines, noise });
|
|
3671
|
+
const headings = readHeadings({ fileId, kept, lineCount: lines.length });
|
|
3672
|
+
const tables = readTables({ fileId, logicalLines, headings });
|
|
3673
|
+
return {
|
|
3674
|
+
fileId,
|
|
3675
|
+
lineCount: lines.length,
|
|
3676
|
+
logicalLines,
|
|
3677
|
+
noiseRanges,
|
|
3678
|
+
headings,
|
|
3679
|
+
definitions: readDefinitions({ lines, noise }),
|
|
3680
|
+
tables,
|
|
3681
|
+
crossReferences: readCrossReferences({ lines, noise, headings, tables }),
|
|
3682
|
+
quality: assessQuality({
|
|
3683
|
+
lines,
|
|
3684
|
+
noise,
|
|
3685
|
+
// the list items a heading style turned prose are counted as candidates turned prose
|
|
3686
|
+
candidates: candidates.length + classified.filter((line) => line.listItem).length,
|
|
3687
|
+
kept: kept.length,
|
|
3688
|
+
repeatedLines: findRepeatedLines({ lines: originalLines })
|
|
3689
|
+
})
|
|
3690
|
+
};
|
|
3691
|
+
}
|
|
3692
|
+
function linesOf(text) {
|
|
3693
|
+
if (text.length === 0) return [];
|
|
3694
|
+
const lines = text.split(LINE_BREAK_PATTERN);
|
|
3695
|
+
return lines[lines.length - 1] === "" ? lines.slice(0, -1) : lines;
|
|
3696
|
+
}
|
|
3697
|
+
function settleLeftOutCandidates(params) {
|
|
3698
|
+
return params.lines.map(
|
|
3699
|
+
(line) => line.kind === "heading" && !params.keptLines.has(line.line) ? { ...line, kind: "prose" } : line
|
|
3700
|
+
);
|
|
3701
|
+
}
|
|
3702
|
+
|
|
3703
|
+
// src/lib/knowledge/standard/standard-search.constants.ts
|
|
3704
|
+
var WORD_TOKEN_PATTERN = /[A-Za-z0-9_]+/g;
|
|
3705
|
+
var WORD_PART_SEPARATOR = "_";
|
|
3706
|
+
var CAMEL_CASE_BOUNDARY_PATTERN = /(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/;
|
|
3707
|
+
var MINIMUM_WORD_LENGTH = 3;
|
|
3708
|
+
var LOWERCASE_WORD_PATTERN = /^[a-z]+$/u;
|
|
3709
|
+
var ACRONYM_PATTERN = /^(?=(?:\d*[A-Z]){2})[A-Z\d]+$/u;
|
|
3710
|
+
var ACRONYM_PLURAL_PATTERN = /^[A-Z\d]+s$/u;
|
|
3711
|
+
var STOP_WORDS2 = /* @__PURE__ */ new Set([
|
|
3712
|
+
"about",
|
|
3713
|
+
"above",
|
|
3714
|
+
"after",
|
|
3715
|
+
"again",
|
|
3716
|
+
"against",
|
|
3717
|
+
"all",
|
|
3718
|
+
"and",
|
|
3719
|
+
"any",
|
|
3720
|
+
"are",
|
|
3721
|
+
"because",
|
|
3722
|
+
"been",
|
|
3723
|
+
"before",
|
|
3724
|
+
"being",
|
|
3725
|
+
"below",
|
|
3726
|
+
"between",
|
|
3727
|
+
"both",
|
|
3728
|
+
"but",
|
|
3729
|
+
"can",
|
|
3730
|
+
"could",
|
|
3731
|
+
"did",
|
|
3732
|
+
"do",
|
|
3733
|
+
"does",
|
|
3734
|
+
"doing",
|
|
3735
|
+
"down",
|
|
3736
|
+
"during",
|
|
3737
|
+
"each",
|
|
3738
|
+
"few",
|
|
3739
|
+
"for",
|
|
3740
|
+
"from",
|
|
3741
|
+
"further",
|
|
3742
|
+
"had",
|
|
3743
|
+
"has",
|
|
3744
|
+
"have",
|
|
3745
|
+
"having",
|
|
3746
|
+
"her",
|
|
3747
|
+
"here",
|
|
3748
|
+
"hers",
|
|
3749
|
+
"him",
|
|
3750
|
+
"his",
|
|
3751
|
+
"how",
|
|
3752
|
+
"into",
|
|
3753
|
+
"its",
|
|
3754
|
+
"itself",
|
|
3755
|
+
"may",
|
|
3756
|
+
"might",
|
|
3757
|
+
"more",
|
|
3758
|
+
"most",
|
|
3759
|
+
"must",
|
|
3760
|
+
"nor",
|
|
3761
|
+
"not",
|
|
3762
|
+
"off",
|
|
3763
|
+
"once",
|
|
3764
|
+
"only",
|
|
3765
|
+
"onto",
|
|
3766
|
+
"other",
|
|
3767
|
+
"our",
|
|
3768
|
+
"ours",
|
|
3769
|
+
"out",
|
|
3770
|
+
"over",
|
|
3771
|
+
"own",
|
|
3772
|
+
"same",
|
|
3773
|
+
"shall",
|
|
3774
|
+
"she",
|
|
3775
|
+
"should",
|
|
3776
|
+
"some",
|
|
3777
|
+
"such",
|
|
3778
|
+
"than",
|
|
3779
|
+
"that",
|
|
3780
|
+
"the",
|
|
3781
|
+
"their",
|
|
3782
|
+
"theirs",
|
|
3783
|
+
"them",
|
|
3784
|
+
"then",
|
|
3785
|
+
"there",
|
|
3786
|
+
"these",
|
|
3787
|
+
"they",
|
|
3788
|
+
"this",
|
|
3789
|
+
"those",
|
|
3790
|
+
"through",
|
|
3791
|
+
"too",
|
|
3792
|
+
"under",
|
|
3793
|
+
"until",
|
|
3794
|
+
"upon",
|
|
3795
|
+
"very",
|
|
3796
|
+
"was",
|
|
3797
|
+
"were",
|
|
3798
|
+
"what",
|
|
3799
|
+
"when",
|
|
3800
|
+
"where",
|
|
3801
|
+
"which",
|
|
3802
|
+
"while",
|
|
3803
|
+
"who",
|
|
3804
|
+
"whom",
|
|
3805
|
+
"whose",
|
|
3806
|
+
"why",
|
|
3807
|
+
"will",
|
|
3808
|
+
"with",
|
|
3809
|
+
"within",
|
|
3810
|
+
"without",
|
|
3811
|
+
"would",
|
|
3812
|
+
"you",
|
|
3813
|
+
"your",
|
|
3814
|
+
"yours"
|
|
3815
|
+
]);
|
|
3816
|
+
var PLURAL_IES_PATTERN = /ies$/;
|
|
3817
|
+
var PLURAL_IES_REPLACEMENT = "y";
|
|
3818
|
+
var PLURAL_ES_PATTERN = /(s|x|z|ch|sh)es$/;
|
|
3819
|
+
var PLURAL_ES_REPLACEMENT = "$1";
|
|
3820
|
+
var PLURAL_S_PATTERN = /([^sui])s$/;
|
|
3821
|
+
var PLURAL_S_REPLACEMENT = "$1";
|
|
3822
|
+
var PAST_IED_PATTERN = /ied$/u;
|
|
3823
|
+
var PAST_IED_REPLACEMENT = "y";
|
|
3824
|
+
var PROGRESSIVE_YING_PATTERN = /(?<=y)ing$/u;
|
|
3825
|
+
var INFLECTION_PATTERN = /(?:ing|ed)$/u;
|
|
3826
|
+
var STEM_MINIMUM_LENGTH = 3;
|
|
3827
|
+
var STEM_VOWEL_PATTERN = /[aeiouy]/u;
|
|
3828
|
+
var DOUBLED_CONSONANT_PATTERN = /([bcdfghjkmnpqrtvwx])\1$/u;
|
|
3829
|
+
var DOUBLED_CONSONANT_REPLACEMENT = "$1";
|
|
3830
|
+
var FINAL_E_PATTERN = /e$/u;
|
|
3831
|
+
var COMMON_WORD_SHARE = 0.05;
|
|
3832
|
+
var COMMON_WORD_WEIGHT_FACTOR = 10;
|
|
3833
|
+
var LINK_STRENGTHS = {
|
|
3834
|
+
heading: 3,
|
|
3835
|
+
caption: 3,
|
|
3836
|
+
definition: 2,
|
|
3837
|
+
cell: 2,
|
|
3838
|
+
text: 1
|
|
3839
|
+
};
|
|
3840
|
+
var SHORT_ATTRIBUTE_LENGTH = 3;
|
|
3841
|
+
var ATTRIBUTE_AT_SIGN = "@";
|
|
3842
|
+
var ATTRIBUTE_EQUAL_SIGN = "=";
|
|
3843
|
+
var TITLE_HIT_FACTOR = 2;
|
|
3844
|
+
var ABOUT_FACTOR = 2;
|
|
3845
|
+
var SCORE_DECIMALS = 2;
|
|
3846
|
+
var SLICE_LINE_CAP = 60;
|
|
3847
|
+
var DEFAULT_FIND_LIMIT2 = 10;
|
|
3848
|
+
|
|
3849
|
+
// src/lib/knowledge/standard/tag-links-of.ts
|
|
3850
|
+
function tagLinksOf(params) {
|
|
3851
|
+
const { files, units, lineUnits, vocabulary } = params;
|
|
3852
|
+
const rules = rulesOf(vocabulary);
|
|
3853
|
+
if (rules.size === 0) return {};
|
|
3854
|
+
const otherNames = [...rules.keys()].filter((name) => !isWholeWord(name));
|
|
3855
|
+
const found = /* @__PURE__ */ new Map();
|
|
3856
|
+
const definitionTerms = definitionTermLinesOf({ files });
|
|
3857
|
+
const lines = files.flatMap((file) => file.logicalLines);
|
|
3858
|
+
for (const [standardLine, line] of lines.entries()) {
|
|
3859
|
+
const unit = lineUnits[standardLine];
|
|
3860
|
+
const strength = strengthOf({
|
|
3861
|
+
line,
|
|
3862
|
+
unit: units[unit],
|
|
3863
|
+
standardLine,
|
|
3864
|
+
definitionTerms
|
|
3865
|
+
});
|
|
3866
|
+
for (const name of namesOfLine({ line, rules, otherNames })) {
|
|
3867
|
+
const byUnit = found.get(name) ?? /* @__PURE__ */ new Map();
|
|
3868
|
+
found.set(name, byUnit);
|
|
3869
|
+
const link = byUnit.get(unit);
|
|
3870
|
+
if (!link) byUnit.set(unit, { unit, strength, lines: [standardLine] });
|
|
3871
|
+
else {
|
|
3872
|
+
link.strength = Math.max(link.strength, strength);
|
|
3873
|
+
link.lines.push(standardLine);
|
|
3874
|
+
}
|
|
3875
|
+
}
|
|
3876
|
+
}
|
|
3877
|
+
const links = /* @__PURE__ */ Object.create(null);
|
|
3878
|
+
for (const [name, byUnit] of found) links[name] = [...byUnit.values()];
|
|
3879
|
+
return links;
|
|
3880
|
+
}
|
|
3881
|
+
function rulesOf(vocabulary) {
|
|
3882
|
+
const rules = /* @__PURE__ */ new Map();
|
|
3883
|
+
for (const value of vocabulary.values)
|
|
3884
|
+
rules.set(value, value.length < SHORT_ATTRIBUTE_LENGTH ? "cellOnly" : "anywhere");
|
|
3885
|
+
for (const attribute of vocabulary.attributes)
|
|
3886
|
+
rules.set(attribute, attribute.length < SHORT_ATTRIBUTE_LENGTH ? "attributeForms" : "anywhere");
|
|
3887
|
+
for (const tag of vocabulary.tags) rules.set(tag, "anywhere");
|
|
3888
|
+
return rules;
|
|
3889
|
+
}
|
|
3890
|
+
function definitionTermLinesOf(params) {
|
|
3891
|
+
const termLines = /* @__PURE__ */ new Set();
|
|
3892
|
+
let offset = 0;
|
|
3893
|
+
for (const file of params.files) {
|
|
3894
|
+
const indexOfFrom = new Map(file.logicalLines.map((line, index) => [line.from, index]));
|
|
3895
|
+
for (const definition of file.definitions) {
|
|
3896
|
+
const index = indexOfFrom.get(definition.from);
|
|
3897
|
+
if (index === void 0) continue;
|
|
3898
|
+
const onItsLine = file.logicalLines[index].text.includes(definition.term);
|
|
3899
|
+
termLines.add(offset + (onItsLine ? index : index + 1));
|
|
3900
|
+
}
|
|
3901
|
+
offset += file.logicalLines.length;
|
|
3902
|
+
}
|
|
3903
|
+
return termLines;
|
|
3904
|
+
}
|
|
3905
|
+
function strengthOf(params) {
|
|
3906
|
+
const { line, unit, standardLine, definitionTerms } = params;
|
|
3907
|
+
if (unit.titleLine === standardLine)
|
|
3908
|
+
return unit.kind === "table" ? LINK_STRENGTHS.caption : LINK_STRENGTHS.heading;
|
|
3909
|
+
if (definitionTerms.has(standardLine)) return LINK_STRENGTHS.definition;
|
|
3910
|
+
if (line.kind === "table") return LINK_STRENGTHS.cell;
|
|
3911
|
+
return LINK_STRENGTHS.text;
|
|
3912
|
+
}
|
|
3913
|
+
function namesOfLine(params) {
|
|
3914
|
+
const { line, rules, otherNames } = params;
|
|
3915
|
+
const names = /* @__PURE__ */ new Set();
|
|
3916
|
+
const text = line.text;
|
|
3917
|
+
for (const match of text.matchAll(WORD_TOKEN_PATTERN)) {
|
|
3918
|
+
const name = match[0];
|
|
3919
|
+
const rule = rules.get(name);
|
|
3920
|
+
if (rule === void 0 || rule === "cellOnly") continue;
|
|
3921
|
+
if (rule === "anywhere" || inAttributeForm({ text, start: match.index, name })) names.add(name);
|
|
3922
|
+
}
|
|
3923
|
+
for (const name of otherNames)
|
|
3924
|
+
if (rules.get(name) === "anywhere" && containsBounded({ text, name })) names.add(name);
|
|
3925
|
+
if (line.kind === "table" && !TABLE_SEPARATOR_ROW_PATTERN.test(text)) {
|
|
3926
|
+
for (const cell of cellsOfRow(text)) if (rules.has(cell)) names.add(cell);
|
|
3927
|
+
}
|
|
3928
|
+
return names;
|
|
3929
|
+
}
|
|
3930
|
+
function inAttributeForm(params) {
|
|
3931
|
+
const { text, start, name } = params;
|
|
3932
|
+
return text.charAt(start - 1) === ATTRIBUTE_AT_SIGN || text.charAt(start + name.length) === ATTRIBUTE_EQUAL_SIGN;
|
|
3933
|
+
}
|
|
3934
|
+
function isWholeWord(name) {
|
|
3935
|
+
const match = name.match(WORD_TOKEN_PATTERN);
|
|
3936
|
+
return match?.length === 1 && match[0] === name;
|
|
3937
|
+
}
|
|
3938
|
+
function containsBounded(params) {
|
|
3939
|
+
const { text, name } = params;
|
|
3940
|
+
let start = text.indexOf(name);
|
|
3941
|
+
while (start !== -1) {
|
|
3942
|
+
const before = text.charAt(start - 1);
|
|
3943
|
+
const after = text.charAt(start + name.length);
|
|
3944
|
+
if (!isWordCharacter(before) && !isWordCharacter(after)) return true;
|
|
3945
|
+
start = text.indexOf(name, start + 1);
|
|
3946
|
+
}
|
|
3947
|
+
return false;
|
|
3948
|
+
}
|
|
3949
|
+
function isWordCharacter(character) {
|
|
3950
|
+
return character !== "" && isWholeWord(character);
|
|
3951
|
+
}
|
|
3952
|
+
|
|
3953
|
+
// src/lib/knowledge/standard/units-of.ts
|
|
3954
|
+
function unitsOf(params) {
|
|
3955
|
+
const units = [];
|
|
3956
|
+
const lineUnits = [];
|
|
3957
|
+
let offset = 0;
|
|
3958
|
+
for (const file of params.files) {
|
|
3959
|
+
const fileUnits = fileUnitsOf({ file, offset });
|
|
3960
|
+
const base = units.length;
|
|
3961
|
+
for (const unit of fileUnits.units) units.push(unit);
|
|
3962
|
+
for (const unit of fileUnits.lineUnits) lineUnits.push(unit === -1 ? -1 : base + unit);
|
|
3963
|
+
offset += file.logicalLines.length;
|
|
3964
|
+
}
|
|
3965
|
+
return { units, lineUnits };
|
|
3966
|
+
}
|
|
3967
|
+
function fileUnitsOf(params) {
|
|
3968
|
+
const { file, offset } = params;
|
|
3969
|
+
const tables = tableUnitsOf({ file, offset });
|
|
3970
|
+
const units = [...tables.units, ...headingUnitsOf(file)];
|
|
3971
|
+
const headingUnits = new Map(
|
|
3972
|
+
file.headings.map((heading, index) => [heading.from, tables.units.length + index])
|
|
3973
|
+
);
|
|
3974
|
+
const lineUnits = [];
|
|
3975
|
+
let frontUnit = -1;
|
|
3976
|
+
let currentUnit = -1;
|
|
3977
|
+
for (const [index, line] of file.logicalLines.entries()) {
|
|
3978
|
+
const standardLine = offset + index;
|
|
3979
|
+
const headingUnit = line.kind === "heading" ? headingUnits.get(line.from) : void 0;
|
|
3980
|
+
if (headingUnit !== void 0) {
|
|
3981
|
+
currentUnit = headingUnit;
|
|
3982
|
+
units[headingUnit].titleLine = standardLine;
|
|
3983
|
+
}
|
|
3984
|
+
const tableUnit = tables.unitOfLine.get(index);
|
|
3985
|
+
if (tableUnit !== void 0)
|
|
3986
|
+
units[tableUnit] = extendUnit({ unit: units[tableUnit], standardLine });
|
|
3987
|
+
else if (currentUnit === -1) {
|
|
3988
|
+
units.push(frontUnitOf({ file, line: line.from }));
|
|
3989
|
+
frontUnit = units.length - 1;
|
|
3990
|
+
currentUnit = frontUnit;
|
|
3991
|
+
}
|
|
3992
|
+
if (currentUnit !== -1)
|
|
3993
|
+
units[currentUnit] = extendUnit({ unit: units[currentUnit], standardLine });
|
|
3994
|
+
if (currentUnit !== -1 && currentUnit === frontUnit) units[frontUnit].to = line.to;
|
|
3995
|
+
lineUnits.push(tableUnit ?? currentUnit);
|
|
3996
|
+
}
|
|
3997
|
+
return { units, lineUnits };
|
|
3998
|
+
}
|
|
3999
|
+
function headingUnitsOf(file) {
|
|
4000
|
+
return file.headings.map((heading, index) => {
|
|
4001
|
+
const next = file.headings[index + 1];
|
|
4002
|
+
return {
|
|
4003
|
+
fileId: file.fileId,
|
|
4004
|
+
kind: "clause",
|
|
4005
|
+
clause: heading.number,
|
|
4006
|
+
title: heading.title,
|
|
4007
|
+
target: heading.number,
|
|
4008
|
+
from: heading.from,
|
|
4009
|
+
to: next ? next.from - 1 : file.lineCount,
|
|
4010
|
+
firstLine: -1,
|
|
4011
|
+
lastLine: -1,
|
|
4012
|
+
titleLine: null
|
|
4013
|
+
};
|
|
4014
|
+
});
|
|
4015
|
+
}
|
|
4016
|
+
function tableUnitsOf(params) {
|
|
4017
|
+
const { file } = params;
|
|
4018
|
+
const indexOfFrom = new Map(file.logicalLines.map((line, index) => [line.from, index]));
|
|
4019
|
+
const units = [];
|
|
4020
|
+
const unitOfLine = /* @__PURE__ */ new Map();
|
|
4021
|
+
for (const table of file.tables) {
|
|
4022
|
+
const start = indexOfFrom.get(table.from);
|
|
4023
|
+
if (start === void 0) continue;
|
|
4024
|
+
const caption = findCaptionAbove({ logicalLines: file.logicalLines, start });
|
|
4025
|
+
const unit = units.length;
|
|
4026
|
+
units.push({
|
|
4027
|
+
fileId: file.fileId,
|
|
4028
|
+
kind: "table",
|
|
4029
|
+
clause: table.clause,
|
|
4030
|
+
title: table.caption,
|
|
4031
|
+
target: table.number === null ? null : TABLE_TARGET_PREFIX + table.number,
|
|
4032
|
+
from: file.logicalLines[caption ?? start].from,
|
|
4033
|
+
to: table.to,
|
|
4034
|
+
firstLine: -1,
|
|
4035
|
+
lastLine: -1,
|
|
4036
|
+
titleLine: caption === null ? null : params.offset + caption
|
|
4037
|
+
});
|
|
4038
|
+
if (caption !== null) unitOfLine.set(caption, unit);
|
|
4039
|
+
for (let index = start; index < file.logicalLines.length; index++) {
|
|
4040
|
+
if (file.logicalLines[index].from > table.to) break;
|
|
4041
|
+
unitOfLine.set(index, unit);
|
|
4042
|
+
}
|
|
4043
|
+
}
|
|
4044
|
+
return { units, unitOfLine };
|
|
4045
|
+
}
|
|
4046
|
+
function frontUnitOf(params) {
|
|
4047
|
+
return {
|
|
4048
|
+
fileId: params.file.fileId,
|
|
4049
|
+
kind: "front",
|
|
4050
|
+
clause: null,
|
|
4051
|
+
title: null,
|
|
4052
|
+
target: null,
|
|
4053
|
+
from: params.line,
|
|
4054
|
+
to: params.line,
|
|
4055
|
+
firstLine: -1,
|
|
4056
|
+
lastLine: -1,
|
|
4057
|
+
titleLine: null
|
|
4058
|
+
};
|
|
4059
|
+
}
|
|
4060
|
+
function extendUnit(params) {
|
|
4061
|
+
const { unit, standardLine } = params;
|
|
4062
|
+
return {
|
|
4063
|
+
...unit,
|
|
4064
|
+
firstLine: unit.firstLine === -1 ? standardLine : unit.firstLine,
|
|
4065
|
+
lastLine: standardLine
|
|
4066
|
+
};
|
|
4067
|
+
}
|
|
4068
|
+
|
|
4069
|
+
// src/lib/knowledge/standard/word-keys-of.ts
|
|
4070
|
+
function wordKeysOf(params) {
|
|
4071
|
+
const { token, split } = params;
|
|
4072
|
+
const acronym = acronymOf(token);
|
|
4073
|
+
if (acronym !== void 0) return [acronym];
|
|
4074
|
+
const words = split ? [token, ...partsOf(token)] : [token];
|
|
4075
|
+
const keys = /* @__PURE__ */ new Set();
|
|
4076
|
+
for (const word of words) {
|
|
4077
|
+
const lower = word.toLowerCase();
|
|
4078
|
+
const partAcronym = acronymOf(word);
|
|
4079
|
+
if (partAcronym !== void 0) {
|
|
4080
|
+
keys.add(partAcronym);
|
|
4081
|
+
continue;
|
|
4082
|
+
}
|
|
4083
|
+
if (lower.length < MINIMUM_WORD_LENGTH || STOP_WORDS2.has(lower)) continue;
|
|
4084
|
+
keys.add(stemOf(lower));
|
|
4085
|
+
}
|
|
4086
|
+
return [...keys];
|
|
4087
|
+
}
|
|
4088
|
+
function acronymOf(word) {
|
|
4089
|
+
const acronym = ACRONYM_PLURAL_PATTERN.test(word) ? word.slice(0, -1) : word;
|
|
4090
|
+
return ACRONYM_PATTERN.test(acronym) ? acronym.toLowerCase() : void 0;
|
|
4091
|
+
}
|
|
4092
|
+
function partsOf(token) {
|
|
4093
|
+
const parts = token.split(WORD_PART_SEPARATOR).flatMap((part) => part.split(CAMEL_CASE_BOUNDARY_PATTERN));
|
|
4094
|
+
return parts.length > 1 ? parts : [];
|
|
4095
|
+
}
|
|
4096
|
+
function stemOf(word) {
|
|
4097
|
+
const singular = word.replace(PLURAL_IES_PATTERN, PLURAL_IES_REPLACEMENT).replace(PLURAL_ES_PATTERN, PLURAL_ES_REPLACEMENT).replace(PLURAL_S_PATTERN, PLURAL_S_REPLACEMENT);
|
|
4098
|
+
const uninflected = withoutInflection(singular);
|
|
4099
|
+
return FINAL_E_PATTERN.test(uninflected) && uninflected.length > STEM_MINIMUM_LENGTH ? uninflected.replace(FINAL_E_PATTERN, "") : uninflected;
|
|
4100
|
+
}
|
|
4101
|
+
function withoutInflection(word) {
|
|
4102
|
+
if (PAST_IED_PATTERN.test(word)) return word.replace(PAST_IED_PATTERN, PAST_IED_REPLACEMENT);
|
|
4103
|
+
if (PROGRESSIVE_YING_PATTERN.test(word)) return word.replace(PROGRESSIVE_YING_PATTERN, "");
|
|
4104
|
+
const rest = word.replace(INFLECTION_PATTERN, "");
|
|
4105
|
+
if (rest === word || rest.length < STEM_MINIMUM_LENGTH || !STEM_VOWEL_PATTERN.test(rest))
|
|
4106
|
+
return word;
|
|
4107
|
+
return rest.replace(DOUBLED_CONSONANT_PATTERN, DOUBLED_CONSONANT_REPLACEMENT);
|
|
4108
|
+
}
|
|
4109
|
+
|
|
4110
|
+
// src/lib/knowledge/standard/word-index-of.ts
|
|
4111
|
+
function wordIndexOf(params) {
|
|
4112
|
+
const stems = /* @__PURE__ */ Object.create(null);
|
|
4113
|
+
const keysOfToken = /* @__PURE__ */ new Map();
|
|
4114
|
+
let standardLine = 0;
|
|
4115
|
+
for (const file of params.files)
|
|
4116
|
+
for (const line of file.logicalLines) {
|
|
4117
|
+
const lineStems = /* @__PURE__ */ new Set();
|
|
4118
|
+
for (const match of line.text.matchAll(WORD_TOKEN_PATTERN)) {
|
|
4119
|
+
const token = match[0];
|
|
4120
|
+
const keys = keysOfToken.get(token) ?? wordKeysOf({ token, split: true });
|
|
4121
|
+
keysOfToken.set(token, keys);
|
|
4122
|
+
for (const key of keys) lineStems.add(key);
|
|
4123
|
+
}
|
|
4124
|
+
for (const stem2 of lineStems) {
|
|
4125
|
+
const lines = stems[stem2];
|
|
4126
|
+
if (lines) lines.push(standardLine);
|
|
4127
|
+
else stems[stem2] = [standardLine];
|
|
4128
|
+
}
|
|
4129
|
+
standardLine++;
|
|
4130
|
+
}
|
|
4131
|
+
return { logicalLineCount: standardLine, stems };
|
|
4132
|
+
}
|
|
4133
|
+
|
|
4134
|
+
// src/lib/knowledge/standard/search-index-of.ts
|
|
4135
|
+
function searchIndexOf(params) {
|
|
4136
|
+
const { files, vocabulary } = params;
|
|
4137
|
+
const { units, lineUnits } = unitsOf({ files });
|
|
4138
|
+
return {
|
|
4139
|
+
files,
|
|
4140
|
+
vocabulary,
|
|
4141
|
+
words: wordIndexOf({ files }),
|
|
4142
|
+
units,
|
|
4143
|
+
lineUnits,
|
|
4144
|
+
tagLinks: tagLinksOf({ files, units, lineUnits, vocabulary })
|
|
4145
|
+
};
|
|
4146
|
+
}
|
|
4147
|
+
|
|
4148
|
+
// src/lib/knowledge/standard/standard-cache.constants.ts
|
|
4149
|
+
var PACKAGE_MANIFEST = "package.json";
|
|
4150
|
+
var INSTALLED_HASH_LENGTH = 16;
|
|
4151
|
+
var INSTALLED_LIST_SEPARATOR = "\n";
|
|
4152
|
+
var INDEX_FORMAT = 2;
|
|
4153
|
+
var CACHE_LOCATION = {
|
|
4154
|
+
environmentVariable: "XDG_CACHE_HOME",
|
|
4155
|
+
homeVariable: "HOME",
|
|
4156
|
+
homeFolder: ".cache",
|
|
4157
|
+
toolFolder: "dialecte",
|
|
4158
|
+
standardFolder: "standard"
|
|
4159
|
+
};
|
|
4160
|
+
var INDEX_FILES = {
|
|
4161
|
+
manifest: "manifest.json",
|
|
4162
|
+
quality: "quality.json",
|
|
4163
|
+
headings: "headings.json",
|
|
4164
|
+
definitions: "definitions.json",
|
|
4165
|
+
tables: "tables.json",
|
|
4166
|
+
crossReferences: "crossrefs.json",
|
|
4167
|
+
words: "words.json"
|
|
4168
|
+
};
|
|
4169
|
+
var PER_FILE_PARTS = [
|
|
4170
|
+
"quality",
|
|
4171
|
+
"headings",
|
|
4172
|
+
"definitions",
|
|
4173
|
+
"tables",
|
|
4174
|
+
"crossReferences"
|
|
4175
|
+
];
|
|
4176
|
+
var LINES_FOLDER = "lines";
|
|
4177
|
+
var LINES_EXTENSION = ".jsonl";
|
|
4178
|
+
var FILE_ID_SEPARATOR = "/";
|
|
4179
|
+
var TAGS_FILE_PREFIX = "tags-";
|
|
4180
|
+
var JSON_EXTENSION = ".json";
|
|
4181
|
+
var TEMPORARY_INFIX2 = ".tmp-";
|
|
4182
|
+
var ASIDE_INFIX = ".old-";
|
|
4183
|
+
var LEFT_FOLDER_PATTERN = /\.(?:tmp|old)-(\d+)$/u;
|
|
4184
|
+
var TARGET_TAKEN_CODES = ["ENOTEMPTY", "EEXIST"];
|
|
4185
|
+
var MISSING_ENTRY_CODE = "ENOENT";
|
|
4186
|
+
var PLACE_FAILED_MESSAGE = "cannot put the index in place: {code} renaming {from} to {to}; nothing kept";
|
|
4187
|
+
var BUILD_OUTCOMES = {
|
|
4188
|
+
built: "built",
|
|
4189
|
+
reused: "reused",
|
|
4190
|
+
aborted: "aborted",
|
|
4191
|
+
failed: "failed"
|
|
4192
|
+
};
|
|
4193
|
+
var FILE_CHANGED_MESSAGE = "{name} ({id}) changed while the index was built: nothing kept, run it again once the file is stable";
|
|
4194
|
+
var BUILD_STEP_NAME = "standard index";
|
|
4195
|
+
|
|
4196
|
+
// src/lib/knowledge/standard/standard-index-folder.ts
|
|
4197
|
+
import { existsSync as existsSync21, mkdirSync as mkdirSync6, readFileSync as readFileSync15, renameSync as renameSync4, writeFileSync as writeFileSync5 } from "fs";
|
|
4198
|
+
import { join as join26 } from "path";
|
|
4199
|
+
function writeIndexFolder(params) {
|
|
4200
|
+
const { folder, manifest, search } = params;
|
|
4201
|
+
mkdirSync6(join26(folder, LINES_FOLDER), { recursive: true });
|
|
4202
|
+
for (const file of search.files)
|
|
4203
|
+
writeFileSync5(
|
|
4204
|
+
linesPathOf({ folder, fileId: file.fileId }),
|
|
4205
|
+
file.logicalLines.map((line) => `${JSON.stringify(line)}
|
|
4206
|
+
`).join("")
|
|
4207
|
+
);
|
|
4208
|
+
const parts = {
|
|
4209
|
+
quality: perFile({
|
|
4210
|
+
files: search.files,
|
|
4211
|
+
value: (file) => ({ ...file.quality, noiseRanges: file.noiseRanges })
|
|
4212
|
+
}),
|
|
4213
|
+
headings: perFile({ files: search.files, value: (file) => file.headings }),
|
|
4214
|
+
definitions: perFile({ files: search.files, value: (file) => file.definitions }),
|
|
4215
|
+
tables: perFile({ files: search.files, value: (file) => file.tables }),
|
|
4216
|
+
crossReferences: perFile({ files: search.files, value: (file) => file.crossReferences })
|
|
4217
|
+
};
|
|
4218
|
+
for (const part of PER_FILE_PARTS)
|
|
4219
|
+
writeJson({ path: join26(folder, INDEX_FILES[part]), content: parts[part] });
|
|
4220
|
+
const words = {
|
|
4221
|
+
words: search.words,
|
|
4222
|
+
units: search.units,
|
|
4223
|
+
lineUnits: search.lineUnits
|
|
4224
|
+
};
|
|
4225
|
+
writeJson({ path: join26(folder, INDEX_FILES.words), content: words });
|
|
4226
|
+
writeJson({ path: join26(folder, INDEX_FILES.manifest), content: manifest });
|
|
4227
|
+
}
|
|
4228
|
+
function isCompleteIndexFolder(params) {
|
|
4229
|
+
return existsSync21(join26(params.folder, INDEX_FILES.manifest));
|
|
4230
|
+
}
|
|
4231
|
+
function readIndexFolder(params) {
|
|
4232
|
+
const { folder } = params;
|
|
4233
|
+
const manifest = readJson({ path: join26(folder, INDEX_FILES.manifest) });
|
|
4234
|
+
const parts = {
|
|
4235
|
+
quality: readJson({ path: join26(folder, INDEX_FILES.quality) }),
|
|
4236
|
+
headings: readJson({ path: join26(folder, INDEX_FILES.headings) }),
|
|
4237
|
+
definitions: readJson({ path: join26(folder, INDEX_FILES.definitions) }),
|
|
4238
|
+
tables: readJson({ path: join26(folder, INDEX_FILES.tables) }),
|
|
4239
|
+
crossReferences: readJson({ path: join26(folder, INDEX_FILES.crossReferences) })
|
|
4240
|
+
};
|
|
4241
|
+
const files = manifest.files.map((entry) => {
|
|
4242
|
+
const { noiseRanges, ...quality } = parts.quality[entry.id];
|
|
4243
|
+
return {
|
|
4244
|
+
fileId: entry.id,
|
|
4245
|
+
lineCount: entry.lines,
|
|
4246
|
+
logicalLines: readLines({ folder, fileId: entry.id }),
|
|
4247
|
+
noiseRanges,
|
|
4248
|
+
headings: parts.headings[entry.id],
|
|
4249
|
+
definitions: parts.definitions[entry.id],
|
|
4250
|
+
tables: parts.tables[entry.id],
|
|
4251
|
+
crossReferences: parts.crossReferences[entry.id],
|
|
4252
|
+
quality
|
|
4253
|
+
};
|
|
4254
|
+
});
|
|
4255
|
+
return { manifest, files, words: readJson({ path: join26(folder, INDEX_FILES.words) }) };
|
|
4256
|
+
}
|
|
4257
|
+
function tagsPathOf(params) {
|
|
4258
|
+
return join26(params.folder, `${TAGS_FILE_PREFIX}${params.installedHash}${JSON_EXTENSION}`);
|
|
4259
|
+
}
|
|
4260
|
+
function writeTagsFile(params) {
|
|
4261
|
+
const temporary = `${params.path}${TEMPORARY_INFIX2}${process.pid}`;
|
|
4262
|
+
writeJson({ path: temporary, content: params.content });
|
|
4263
|
+
renameSync4(temporary, params.path);
|
|
4264
|
+
}
|
|
4265
|
+
function readTagsFile(params) {
|
|
4266
|
+
return readJson({ path: params.path });
|
|
4267
|
+
}
|
|
4268
|
+
function linesPathOf(params) {
|
|
4269
|
+
const name = params.fileId.slice(params.fileId.indexOf(FILE_ID_SEPARATOR) + 1);
|
|
4270
|
+
return join26(params.folder, LINES_FOLDER, `${name}${LINES_EXTENSION}`);
|
|
4271
|
+
}
|
|
4272
|
+
function readLines(params) {
|
|
4273
|
+
return readFileSync15(linesPathOf(params), "utf8").split("\n").filter((line) => line !== "").map((line) => JSON.parse(line));
|
|
4274
|
+
}
|
|
4275
|
+
function perFile(params) {
|
|
4276
|
+
return Object.fromEntries(params.files.map((file) => [file.fileId, params.value(file)]));
|
|
4277
|
+
}
|
|
4278
|
+
function writeJson(params) {
|
|
4279
|
+
writeFileSync5(params.path, `${JSON.stringify(params.content)}
|
|
4280
|
+
`);
|
|
4281
|
+
}
|
|
4282
|
+
function readJson(params) {
|
|
4283
|
+
return JSON.parse(readFileSync15(params.path, "utf8"));
|
|
4284
|
+
}
|
|
4285
|
+
|
|
4286
|
+
// src/lib/knowledge/standard/ensure-standard-index.ts
|
|
4287
|
+
async function ensureStandardIndex(params) {
|
|
4288
|
+
const startedAt = Date.now();
|
|
4289
|
+
const { standard, installed } = params;
|
|
4290
|
+
const folder = join27(params.cacheRoot, CACHE_LOCATION.standardFolder, standard.id);
|
|
4291
|
+
const hashed = standard.files.map(hashedFileOf);
|
|
4292
|
+
const manifestHash = manifestHashOf({ hashed });
|
|
4293
|
+
const target = join27(folder, manifestHash);
|
|
4294
|
+
const ready = { standardId: standard.id, manifestHash, startedAt, target, installed };
|
|
4295
|
+
if (!params.rebuild && isCompleteIndexFolder({ folder: target }))
|
|
4296
|
+
return readyAnswer({ ...ready, state: "reused" });
|
|
4297
|
+
removeStaleTemporaryFolders({ folder });
|
|
4298
|
+
const temporary = `${target}${TEMPORARY_INFIX2}${process.pid}`;
|
|
4299
|
+
const step = params.progress?.start({ name: BUILD_STEP_NAME, detail: standard.id });
|
|
4300
|
+
let outcome = BUILD_OUTCOMES.failed;
|
|
4301
|
+
try {
|
|
4302
|
+
const built = await buildInto({ ...params, hashed, folder: temporary });
|
|
4303
|
+
if (built.kind === "aborted") {
|
|
4304
|
+
rmSync5(temporary, { recursive: true, force: true });
|
|
4305
|
+
outcome = BUILD_OUTCOMES.aborted;
|
|
4306
|
+
return built;
|
|
4307
|
+
}
|
|
4308
|
+
if (isCompleteIndexFolder({ folder: target }) && !params.rebuild) {
|
|
4309
|
+
rmSync5(temporary, { recursive: true, force: true });
|
|
4310
|
+
outcome = BUILD_OUTCOMES.reused;
|
|
4311
|
+
return readyAnswer({ ...ready, state: "reused" });
|
|
4312
|
+
}
|
|
4313
|
+
const placed = placeFolder({ temporary, target, replace: params.rebuild });
|
|
4314
|
+
if (placed.kind === "failed") return { kind: "aborted", message: placed.message };
|
|
4315
|
+
outcome = placed.kind === "taken" ? BUILD_OUTCOMES.reused : BUILD_OUTCOMES.built;
|
|
4316
|
+
if (placed.kind === "taken") return readyAnswer({ ...ready, state: "reused" });
|
|
4317
|
+
return readyAnswer({ ...ready, state: "built", search: built.search });
|
|
4318
|
+
} catch (error) {
|
|
4319
|
+
rmSync5(temporary, { recursive: true, force: true });
|
|
4320
|
+
throw error;
|
|
4321
|
+
} finally {
|
|
4322
|
+
step?.finish(
|
|
4323
|
+
`${BUILD_STEP_NAME} ${standard.id} ${outcome} ${formatDuration(Date.now() - startedAt)}`
|
|
4324
|
+
);
|
|
4325
|
+
}
|
|
4326
|
+
}
|
|
4327
|
+
function readyAnswer(params) {
|
|
4328
|
+
return {
|
|
4329
|
+
kind: "ready",
|
|
4330
|
+
index: {
|
|
4331
|
+
standardId: params.standardId,
|
|
4332
|
+
manifestHash: params.manifestHash,
|
|
4333
|
+
state: params.state,
|
|
4334
|
+
elapsed: Date.now() - params.startedAt,
|
|
4335
|
+
search: params.search ?? readSearchIndex({ folder: params.target, installed: params.installed })
|
|
4336
|
+
}
|
|
4337
|
+
};
|
|
4338
|
+
}
|
|
4339
|
+
function hashedFileOf(file) {
|
|
4340
|
+
const bytes = readFileSync16(file.path);
|
|
4341
|
+
return {
|
|
4342
|
+
id: file.id,
|
|
4343
|
+
name: file.name,
|
|
4344
|
+
path: file.path,
|
|
4345
|
+
size: bytes.length,
|
|
4346
|
+
sha256: sha256Of(bytes)
|
|
4347
|
+
};
|
|
4348
|
+
}
|
|
4349
|
+
function manifestHashOf(params) {
|
|
4350
|
+
const pairs = params.hashed.map((file) => [file.id, file.sha256]).sort(([left], [right]) => left < right ? -1 : 1);
|
|
4351
|
+
return sha256Of(JSON.stringify({ indexFormat: INDEX_FORMAT, files: pairs }));
|
|
4352
|
+
}
|
|
4353
|
+
async function buildInto(params) {
|
|
4354
|
+
const { hashed, installed } = params;
|
|
4355
|
+
const files = [];
|
|
4356
|
+
for (const file of hashed) {
|
|
4357
|
+
await nextTurn();
|
|
4358
|
+
const bytes = readFileSync16(file.path);
|
|
4359
|
+
params.afterFileRead?.({ path: file.path });
|
|
4360
|
+
if (sha256Of(bytes) !== file.sha256) return abortedBy(file);
|
|
4361
|
+
files.push(indexStandardText({ fileId: file.id, text: bytes.toString("utf8") }));
|
|
4362
|
+
}
|
|
4363
|
+
const search = searchIndexOf({ files, vocabulary: installed.vocabulary });
|
|
4364
|
+
const manifest = {
|
|
4365
|
+
files: hashed.map((file, index) => ({
|
|
4366
|
+
id: file.id,
|
|
4367
|
+
name: file.name,
|
|
4368
|
+
size: file.size,
|
|
4369
|
+
sha256: file.sha256,
|
|
4370
|
+
lines: files[index].lineCount
|
|
4371
|
+
})),
|
|
4372
|
+
built: (/* @__PURE__ */ new Date()).toISOString(),
|
|
4373
|
+
cliVersion: params.cliVersion,
|
|
4374
|
+
indexFormat: INDEX_FORMAT
|
|
4375
|
+
};
|
|
4376
|
+
writeIndexFolder({ folder: params.folder, manifest, search });
|
|
4377
|
+
if (installed.installedHash !== void 0)
|
|
4378
|
+
writeTagsFile({
|
|
4379
|
+
path: tagsPathOf({ folder: params.folder, installedHash: installed.installedHash }),
|
|
4380
|
+
content: { vocabulary: installed.vocabulary, tagLinks: search.tagLinks }
|
|
4381
|
+
});
|
|
4382
|
+
const moved = hashed.find((file) => sha256Of(readFileSync16(file.path)) !== file.sha256);
|
|
4383
|
+
return moved ? abortedBy(moved) : { kind: "built", search };
|
|
4384
|
+
}
|
|
4385
|
+
function placeFolder(params) {
|
|
4386
|
+
const { temporary, target } = params;
|
|
4387
|
+
const aside = params.replace ? moveAside({ temporary, target, aside: `${target}${ASIDE_INFIX}${process.pid}` }) : { kind: "moved", aside: void 0 };
|
|
4388
|
+
if (aside.kind === "failed") return aside;
|
|
4389
|
+
return placeInto({ temporary, target, aside: aside.aside });
|
|
4390
|
+
}
|
|
4391
|
+
function moveAside(params) {
|
|
4392
|
+
const { temporary, target, aside } = params;
|
|
4393
|
+
try {
|
|
4394
|
+
renameSync5(target, aside);
|
|
4395
|
+
return { kind: "moved", aside };
|
|
4396
|
+
} catch (error) {
|
|
4397
|
+
const code = error.code ?? String(error);
|
|
4398
|
+
if (code === MISSING_ENTRY_CODE) return { kind: "moved", aside: void 0 };
|
|
4399
|
+
rmSync5(temporary, { recursive: true, force: true });
|
|
4400
|
+
return {
|
|
4401
|
+
kind: "failed",
|
|
4402
|
+
message: fillMessage({
|
|
4403
|
+
template: PLACE_FAILED_MESSAGE,
|
|
4404
|
+
values: { code, from: target, to: aside }
|
|
4405
|
+
})
|
|
4406
|
+
};
|
|
4407
|
+
}
|
|
4408
|
+
}
|
|
4409
|
+
function placeInto(params) {
|
|
4410
|
+
const { temporary, target, aside } = params;
|
|
4411
|
+
try {
|
|
4412
|
+
renameSync5(temporary, target);
|
|
4413
|
+
} catch (error) {
|
|
4414
|
+
const code = error.code ?? String(error);
|
|
4415
|
+
rmSync5(temporary, { recursive: true, force: true });
|
|
4416
|
+
if (TARGET_TAKEN_CODES.includes(code)) {
|
|
4417
|
+
if (aside !== void 0) rmSync5(aside, { recursive: true, force: true });
|
|
4418
|
+
return { kind: "taken" };
|
|
4419
|
+
}
|
|
4420
|
+
if (aside !== void 0) renameSync5(aside, target);
|
|
4421
|
+
return {
|
|
4422
|
+
kind: "failed",
|
|
4423
|
+
message: fillMessage({
|
|
4424
|
+
template: PLACE_FAILED_MESSAGE,
|
|
4425
|
+
values: { code, from: temporary, to: target }
|
|
4426
|
+
})
|
|
4427
|
+
};
|
|
4428
|
+
}
|
|
4429
|
+
if (aside !== void 0) rmSync5(aside, { recursive: true, force: true });
|
|
4430
|
+
return { kind: "placed" };
|
|
4431
|
+
}
|
|
4432
|
+
function readSearchIndex(params) {
|
|
4433
|
+
const { folder, installed } = params;
|
|
4434
|
+
const { files, words } = readIndexFolder({ folder });
|
|
4435
|
+
const search = {
|
|
4436
|
+
files,
|
|
4437
|
+
vocabulary: installed.vocabulary,
|
|
4438
|
+
words: words.words,
|
|
4439
|
+
units: words.units,
|
|
4440
|
+
lineUnits: words.lineUnits,
|
|
4441
|
+
tagLinks: {}
|
|
4442
|
+
};
|
|
4443
|
+
if (installed.installedHash === void 0) return search;
|
|
4444
|
+
const path = tagsPathOf({ folder, installedHash: installed.installedHash });
|
|
4445
|
+
if (existsSync22(path)) return { ...search, tagLinks: readTagsFile({ path }).tagLinks };
|
|
4446
|
+
const tagLinks = tagLinksOf({
|
|
4447
|
+
files,
|
|
4448
|
+
units: words.units,
|
|
4449
|
+
lineUnits: words.lineUnits,
|
|
4450
|
+
vocabulary: installed.vocabulary
|
|
4451
|
+
});
|
|
4452
|
+
writeTagsFile({ path, content: { vocabulary: installed.vocabulary, tagLinks } });
|
|
4453
|
+
return { ...search, tagLinks };
|
|
4454
|
+
}
|
|
4455
|
+
function removeStaleTemporaryFolders(params) {
|
|
4456
|
+
if (!existsSync22(params.folder)) return;
|
|
4457
|
+
for (const name of readdirSync11(params.folder)) {
|
|
4458
|
+
const pid = LEFT_FOLDER_PATTERN.exec(name)?.[1];
|
|
4459
|
+
if (pid !== void 0 && !isProcessAlive(Number(pid)))
|
|
4460
|
+
rmSync5(join27(params.folder, name), { recursive: true, force: true });
|
|
4461
|
+
}
|
|
4462
|
+
}
|
|
4463
|
+
function abortedBy(file) {
|
|
4464
|
+
return {
|
|
4465
|
+
kind: "aborted",
|
|
4466
|
+
message: fillMessage({
|
|
4467
|
+
template: FILE_CHANGED_MESSAGE,
|
|
4468
|
+
values: { name: file.name, id: file.id }
|
|
4469
|
+
})
|
|
4470
|
+
};
|
|
4471
|
+
}
|
|
4472
|
+
function nextTurn() {
|
|
4473
|
+
return new Promise(function resolveOnNextTurn(resolve7) {
|
|
4474
|
+
setImmediate(resolve7);
|
|
4475
|
+
});
|
|
4476
|
+
}
|
|
4477
|
+
function sha256Of(content) {
|
|
4478
|
+
return createHash2("sha256").update(content).digest("hex");
|
|
4479
|
+
}
|
|
4480
|
+
|
|
4481
|
+
// src/lib/knowledge/standard/read-installed-vocabulary.ts
|
|
4482
|
+
import { createHash as createHash3 } from "crypto";
|
|
4483
|
+
import { existsSync as existsSync23 } from "fs";
|
|
4484
|
+
import { join as join28 } from "path";
|
|
4485
|
+
async function readInstalledVocabulary(params) {
|
|
4486
|
+
const { cwd, packageName: packageName2 } = params;
|
|
4487
|
+
const installed = existsSync23(join28(cwd, PACKAGE_MANIFEST)) ? (await loadDialectes({ cwd })).dialectes : [];
|
|
4488
|
+
const selected = packageName2 === void 0 ? installed : installed.filter((dialecte) => dialecte.packageName === packageName2);
|
|
4489
|
+
if (packageName2 !== void 0 && selected.length === 0)
|
|
4490
|
+
return {
|
|
4491
|
+
kind: "unknownPackage",
|
|
4492
|
+
name: packageName2,
|
|
4493
|
+
installed: installed.map((dialecte) => dialecte.packageName).sort()
|
|
4494
|
+
};
|
|
4495
|
+
return { kind: "read", installed: vocabularyOf({ cwd, dialectes: selected }) };
|
|
4496
|
+
}
|
|
4497
|
+
function vocabularyOf(params) {
|
|
4498
|
+
const { cwd, dialectes } = params;
|
|
4499
|
+
if (dialectes.length === 0) return emptyInstalledVocabulary();
|
|
4500
|
+
const tags = /* @__PURE__ */ new Set();
|
|
4501
|
+
const attributes = /* @__PURE__ */ new Set();
|
|
4502
|
+
const values = /* @__PURE__ */ new Set();
|
|
4503
|
+
for (const { config, readers } of dialectes) {
|
|
4504
|
+
for (const tag of config.elements) tags.add(tag);
|
|
4505
|
+
for (const definition of Object.values(config.definition))
|
|
4506
|
+
for (const attribute of definition.attributes.sequence) {
|
|
4507
|
+
attributes.add(attribute);
|
|
4508
|
+
const datatype = definition.attributes.details[attribute]?.type;
|
|
4509
|
+
for (const value of readers.enumeratedValues(datatype)) values.add(value);
|
|
4510
|
+
}
|
|
4511
|
+
}
|
|
4512
|
+
const packages = dialectes.map((dialecte) => `${dialecte.packageName}@${versionOf({ cwd, dialecte })}`).sort();
|
|
4513
|
+
return {
|
|
4514
|
+
vocabulary: {
|
|
4515
|
+
tags: [...tags].sort(),
|
|
4516
|
+
attributes: [...attributes].sort(),
|
|
4517
|
+
values: [...values].sort()
|
|
4518
|
+
},
|
|
4519
|
+
packages,
|
|
4520
|
+
installedHash: createHash3("sha256").update(packages.join(INSTALLED_LIST_SEPARATOR)).digest("hex").slice(0, INSTALLED_HASH_LENGTH)
|
|
4521
|
+
};
|
|
4522
|
+
}
|
|
4523
|
+
function versionOf(params) {
|
|
4524
|
+
const own = readManifest(params.cwd);
|
|
4525
|
+
const folder = own?.name === params.dialecte.packageName ? params.cwd : findInstalledPackage({ cwd: params.cwd, name: params.dialecte.packageName });
|
|
4526
|
+
return (folder === void 0 ? void 0 : readManifest(folder)?.version) ?? "";
|
|
4527
|
+
}
|
|
4528
|
+
function emptyInstalledVocabulary() {
|
|
4529
|
+
return {
|
|
4530
|
+
vocabulary: { tags: [], attributes: [], values: [] },
|
|
4531
|
+
packages: [],
|
|
4532
|
+
installedHash: void 0
|
|
4533
|
+
};
|
|
4534
|
+
}
|
|
4535
|
+
|
|
4536
|
+
// src/lib/knowledge/standard/read-standard-input.ts
|
|
4537
|
+
import { existsSync as existsSync25, readdirSync as readdirSync12, realpathSync as realpathSync5, statSync as statSync7 } from "fs";
|
|
4538
|
+
import { join as join30 } from "path";
|
|
4539
|
+
|
|
4540
|
+
// src/lib/standard/read-standard-root.ts
|
|
4541
|
+
import { resolve as resolve6 } from "path";
|
|
4542
|
+
|
|
4543
|
+
// src/lib/standard/standard.constants.ts
|
|
4544
|
+
var STANDARD_ENVIRONMENT_VARIABLE = "DIALECTE_STANDARD";
|
|
4545
|
+
|
|
4546
|
+
// src/lib/standard/read-standard-root.ts
|
|
4547
|
+
function readStandardRoot(params = {}) {
|
|
4548
|
+
const value = (params.environment ?? process.env)[STANDARD_ENVIRONMENT_VARIABLE];
|
|
4549
|
+
return value ? resolve6(value) : void 0;
|
|
4550
|
+
}
|
|
4551
|
+
|
|
4552
|
+
// src/lib/knowledge/standard/standard-input.constants.ts
|
|
4553
|
+
var HIDDEN_NAME_START = ".";
|
|
4554
|
+
var STANDARD_FILE_EXTENSION = ".md";
|
|
4555
|
+
var GIT_ENTRY2 = ".git";
|
|
4556
|
+
var STANDARD_LAYOUT = "set DIALECTE_STANDARD to one root folder holding one folder per standard, each holding its .md files: <root>/<standard id>/<file>.md (the folder name is the standard id); keep it outside every repository";
|
|
4557
|
+
var INPUT_MESSAGES = {
|
|
4558
|
+
notSet: "no standard configured: DIALECTE_STANDARD is not set",
|
|
4559
|
+
missing: "DIALECTE_STANDARD names {path}, which does not exist",
|
|
4560
|
+
notFolder: "DIALECTE_STANDARD names {path}, a file, not a root folder",
|
|
4561
|
+
noStandard: "the root {path} holds no standard folder",
|
|
4562
|
+
topLevelFiles: "the root {path} holds .md files directly: {names}",
|
|
4563
|
+
workTree: "{path} is inside a git work tree ({top}): the standard and its index never live in a repository",
|
|
4564
|
+
folderCollision: "two standard folders have the same id: {names}",
|
|
4565
|
+
fileCollision: "two files of the standard {id} have the same id: {names}",
|
|
4566
|
+
unknownStandard: "no standard folder {id} in {path} - known: {known}"
|
|
4567
|
+
};
|
|
4568
|
+
var NAME_LIST_SEPARATOR = ", ";
|
|
4569
|
+
|
|
4570
|
+
// src/lib/knowledge/standard/standard-names-answer-of.ts
|
|
4571
|
+
function namesAnswerOf(params) {
|
|
4572
|
+
const byId2 = /* @__PURE__ */ new Map();
|
|
4573
|
+
for (const named of [...params.named].sort(compareNames)) {
|
|
4574
|
+
const known = byId2.get(named.id);
|
|
4575
|
+
if (known !== void 0) return { kind: "collision", names: [known, named.name] };
|
|
4576
|
+
byId2.set(named.id, named.name);
|
|
4577
|
+
}
|
|
4578
|
+
return {
|
|
4579
|
+
kind: "kept",
|
|
4580
|
+
kept: [...params.named].sort((left, right) => compareText(left.id, right.id))
|
|
4581
|
+
};
|
|
4582
|
+
}
|
|
4583
|
+
function compareNames(left, right) {
|
|
4584
|
+
return compareText(left.name, right.name);
|
|
4585
|
+
}
|
|
4586
|
+
function compareText(left, right) {
|
|
4587
|
+
if (left === right) return 0;
|
|
4588
|
+
return left < right ? -1 : 1;
|
|
4589
|
+
}
|
|
4590
|
+
|
|
4591
|
+
// src/lib/knowledge/standard/standard-files-of.ts
|
|
4592
|
+
function standardFilesOf(params) {
|
|
4593
|
+
return namesAnswerOf({
|
|
4594
|
+
named: params.entries.filter(
|
|
4595
|
+
(entry) => entry.kind === "file" && !entry.name.startsWith(HIDDEN_NAME_START) && entry.name.endsWith(STANDARD_FILE_EXTENSION)
|
|
4596
|
+
).map((entry) => ({
|
|
4597
|
+
id: `${params.standardId}/${entry.name.slice(0, -STANDARD_FILE_EXTENSION.length).toLowerCase()}`,
|
|
4598
|
+
name: entry.name
|
|
4599
|
+
}))
|
|
4600
|
+
});
|
|
4601
|
+
}
|
|
4602
|
+
|
|
4603
|
+
// src/lib/knowledge/standard/standard-folders-of.ts
|
|
4604
|
+
function standardFoldersOf(params) {
|
|
4605
|
+
return namesAnswerOf({
|
|
4606
|
+
named: params.entries.filter((entry) => entry.kind === "folder" && !entry.name.startsWith(HIDDEN_NAME_START)).map((entry) => ({ id: entry.name.toLowerCase(), name: entry.name }))
|
|
4607
|
+
});
|
|
4608
|
+
}
|
|
4609
|
+
|
|
4610
|
+
// src/lib/knowledge/standard/work-tree-holding.ts
|
|
4611
|
+
import { existsSync as existsSync24, realpathSync as realpathSync4 } from "fs";
|
|
4612
|
+
import { dirname as dirname12, join as join29 } from "path";
|
|
4613
|
+
function workTreeHolding(params) {
|
|
4614
|
+
let folder = realpathSync4(nearestExisting(params.path));
|
|
4615
|
+
for (; ; ) {
|
|
4616
|
+
if (existsSync24(join29(folder, GIT_ENTRY2))) return folder;
|
|
4617
|
+
const parent = dirname12(folder);
|
|
4618
|
+
if (parent === folder) return void 0;
|
|
4619
|
+
folder = parent;
|
|
4620
|
+
}
|
|
4621
|
+
}
|
|
4622
|
+
function nearestExisting(path) {
|
|
4623
|
+
let existing = path;
|
|
4624
|
+
while (!existsSync24(existing)) existing = dirname12(existing);
|
|
4625
|
+
return existing;
|
|
4626
|
+
}
|
|
4627
|
+
|
|
4628
|
+
// src/lib/knowledge/standard/read-standard-input.ts
|
|
4629
|
+
function readStandardInput(params) {
|
|
4630
|
+
const root = readStandardRoot({ environment: params.environment });
|
|
4631
|
+
const rootRefusal = refusalOfRoot({ root });
|
|
4632
|
+
if (rootRefusal !== void 0 || root === void 0)
|
|
4633
|
+
return refused(rootRefusal ?? INPUT_MESSAGES.notSet, STANDARD_LAYOUT);
|
|
4634
|
+
const workTree = workTreeRefusal({ path: root });
|
|
4635
|
+
if (workTree) return refused(workTree);
|
|
4636
|
+
const entries = entriesOf({ folder: root });
|
|
4637
|
+
const topLevelFiles = entries.filter((entry) => entry.kind === "file" && entry.name.endsWith(STANDARD_FILE_EXTENSION)).filter((entry) => !entry.name.startsWith(HIDDEN_NAME_START)).map((entry) => entry.name).sort();
|
|
4638
|
+
if (topLevelFiles.length > 0)
|
|
4639
|
+
return refused(
|
|
4640
|
+
fillMessage({
|
|
4641
|
+
template: INPUT_MESSAGES.topLevelFiles,
|
|
4642
|
+
values: { path: root, names: topLevelFiles.join(NAME_LIST_SEPARATOR) }
|
|
4643
|
+
}),
|
|
4644
|
+
STANDARD_LAYOUT
|
|
4645
|
+
);
|
|
4646
|
+
const folders = standardFoldersOf({ entries });
|
|
4647
|
+
if (folders.kind === "collision")
|
|
4648
|
+
return refused(
|
|
4649
|
+
fillMessage({
|
|
4650
|
+
template: INPUT_MESSAGES.folderCollision,
|
|
4651
|
+
values: { names: folders.names.join(NAME_LIST_SEPARATOR) }
|
|
4652
|
+
})
|
|
4653
|
+
);
|
|
4654
|
+
if (folders.kept.length === 0)
|
|
4655
|
+
return refused(
|
|
4656
|
+
fillMessage({ template: INPUT_MESSAGES.noStandard, values: { path: root } }),
|
|
4657
|
+
STANDARD_LAYOUT
|
|
4658
|
+
);
|
|
4659
|
+
const known = folders.kept.map((folder) => folder.id);
|
|
4660
|
+
const wanted = params.standardId?.toLowerCase();
|
|
4661
|
+
const chosen = folders.kept.filter((folder) => wanted === void 0 || folder.id === wanted);
|
|
4662
|
+
if (chosen.length === 0 && wanted !== void 0)
|
|
4663
|
+
return refused(
|
|
4664
|
+
fillMessage({
|
|
4665
|
+
template: INPUT_MESSAGES.unknownStandard,
|
|
4666
|
+
values: { id: wanted, path: root, known: known.join(NAME_LIST_SEPARATOR) || "none" }
|
|
4667
|
+
})
|
|
4668
|
+
);
|
|
4669
|
+
const selected = [];
|
|
4670
|
+
for (const named of chosen) {
|
|
4671
|
+
const folder = join30(root, named.name);
|
|
4672
|
+
const folderRefusal = workTreeRefusal({ path: folder });
|
|
4673
|
+
if (folderRefusal) return refused(folderRefusal);
|
|
4674
|
+
const files = standardFilesOf({ standardId: named.id, entries: entriesOf({ folder }) });
|
|
4675
|
+
if (files.kind === "collision")
|
|
4676
|
+
return refused(
|
|
4677
|
+
fillMessage({
|
|
4678
|
+
template: INPUT_MESSAGES.fileCollision,
|
|
4679
|
+
values: { id: named.id, names: files.names.join(NAME_LIST_SEPARATOR) }
|
|
4680
|
+
})
|
|
4681
|
+
);
|
|
4682
|
+
selected.push({
|
|
4683
|
+
...named,
|
|
4684
|
+
folder,
|
|
4685
|
+
files: files.kept.map((file) => ({ ...file, path: join30(folder, file.name) }))
|
|
4686
|
+
});
|
|
4687
|
+
}
|
|
4688
|
+
return { kind: "input", input: { root, known, selected } };
|
|
4689
|
+
}
|
|
4690
|
+
function workTreeRefusal(params) {
|
|
4691
|
+
const top = workTreeHolding({ path: params.path });
|
|
4692
|
+
return top === void 0 ? void 0 : fillMessage({ template: INPUT_MESSAGES.workTree, values: { path: params.path, top } });
|
|
4693
|
+
}
|
|
4694
|
+
function refusalOfRoot(params) {
|
|
4695
|
+
const { root } = params;
|
|
4696
|
+
if (root === void 0) return INPUT_MESSAGES.notSet;
|
|
4697
|
+
if (!existsSync25(root))
|
|
4698
|
+
return fillMessage({ template: INPUT_MESSAGES.missing, values: { path: root } });
|
|
4699
|
+
if (!statSync7(root).isDirectory())
|
|
4700
|
+
return fillMessage({ template: INPUT_MESSAGES.notFolder, values: { path: root } });
|
|
4701
|
+
return void 0;
|
|
4702
|
+
}
|
|
4703
|
+
function entriesOf(params) {
|
|
4704
|
+
return readdirSync12(params.folder).flatMap((name) => {
|
|
4705
|
+
const path = join30(params.folder, name);
|
|
4706
|
+
if (!existsSync25(path)) return [];
|
|
4707
|
+
const stats = statSync7(realpathSync5(path));
|
|
4708
|
+
if (stats.isDirectory()) return [{ name, kind: "folder" }];
|
|
4709
|
+
return stats.isFile() ? [{ name, kind: "file" }] : [];
|
|
4710
|
+
});
|
|
4711
|
+
}
|
|
4712
|
+
function refused(message, layout) {
|
|
4713
|
+
return layout === void 0 ? { kind: "refused", message } : { kind: "refused", message, layout };
|
|
4714
|
+
}
|
|
4715
|
+
|
|
4716
|
+
// src/lib/knowledge/standard/standard-cache-root.ts
|
|
4717
|
+
import { homedir } from "os";
|
|
4718
|
+
import { join as join31 } from "path";
|
|
4719
|
+
function standardCacheRoot(params) {
|
|
4720
|
+
const { environment } = params;
|
|
4721
|
+
const cacheHome = environment[CACHE_LOCATION.environmentVariable];
|
|
4722
|
+
const base = cacheHome ? cacheHome : join31(environment[CACHE_LOCATION.homeVariable] || homedir(), CACHE_LOCATION.homeFolder);
|
|
4723
|
+
return join31(base, CACHE_LOCATION.toolFolder);
|
|
4724
|
+
}
|
|
4725
|
+
|
|
4726
|
+
// src/lib/knowledge/standard/standard-files-key.ts
|
|
4727
|
+
import { createHash as createHash4 } from "crypto";
|
|
4728
|
+
import { readFileSync as readFileSync17 } from "fs";
|
|
4729
|
+
|
|
4730
|
+
// src/lib/stable-json.ts
|
|
4731
|
+
function stableJson(value) {
|
|
4732
|
+
if (Array.isArray(value)) return `[${arrayItemsJson(value).join(",")}]`;
|
|
4733
|
+
if (typeof value === "object" && value !== null) {
|
|
4734
|
+
if (!isPlainObject(value)) throw notPlainJson({ found: describeObject(value) });
|
|
4735
|
+
const members = Object.entries(value).filter(([, member]) => member !== void 0).sort(byKeyCodeUnits).map(([key, member]) => `${JSON.stringify(key)}:${stableJson(member)}`);
|
|
4736
|
+
return `{${members.join(",")}}`;
|
|
4737
|
+
}
|
|
4738
|
+
if (typeof value === "function" || typeof value === "symbol" || typeof value === "bigint")
|
|
4739
|
+
throw notPlainJson({ found: `a ${typeof value}` });
|
|
4740
|
+
return JSON.stringify(value) ?? "null";
|
|
4741
|
+
}
|
|
4742
|
+
function arrayItemsJson(items) {
|
|
4743
|
+
const written = [];
|
|
4744
|
+
for (let index = 0; index < items.length; index++) {
|
|
4745
|
+
if (!(index in items)) throw notPlainJson({ found: `a sparse array (no item at ${index})` });
|
|
4746
|
+
const item = items[index];
|
|
4747
|
+
written.push(item === void 0 ? "null" : stableJson(item));
|
|
4748
|
+
}
|
|
4749
|
+
return written;
|
|
4750
|
+
}
|
|
4751
|
+
function isPlainObject(value) {
|
|
4752
|
+
const prototype = Object.getPrototypeOf(value);
|
|
4753
|
+
return prototype === Object.prototype || prototype === null;
|
|
4754
|
+
}
|
|
4755
|
+
function describeObject(value) {
|
|
4756
|
+
const name = value.constructor?.name;
|
|
4757
|
+
return typeof name === "string" && name !== "" ? `a ${name}` : "an object with a prototype";
|
|
4758
|
+
}
|
|
4759
|
+
function notPlainJson(params) {
|
|
4760
|
+
return new TypeError(`stableJson: not plain JSON data: ${params.found}`);
|
|
4761
|
+
}
|
|
4762
|
+
function byKeyCodeUnits([first], [second]) {
|
|
4763
|
+
return first < second ? -1 : first > second ? 1 : 0;
|
|
4764
|
+
}
|
|
4765
|
+
|
|
4766
|
+
// src/lib/knowledge/standard/standard-files-key.constants.ts
|
|
4767
|
+
var STANDARD_FILES_KEY_PREFIX = "sha256:";
|
|
4768
|
+
|
|
4769
|
+
// src/lib/knowledge/standard/standard-files-key.ts
|
|
4770
|
+
function standardFilesKey(params) {
|
|
4771
|
+
const pairs = params.files.map((file) => [file.id, file.sha256]).sort(([first], [second]) => first < second ? -1 : first > second ? 1 : 0);
|
|
4772
|
+
return `${STANDARD_FILES_KEY_PREFIX}${sha256Of2(stableJson(pairs))}`;
|
|
4773
|
+
}
|
|
4774
|
+
function standardFolderFilesKey(params) {
|
|
4775
|
+
return standardFilesKey({
|
|
4776
|
+
files: params.standard.files.map((file) => ({
|
|
4777
|
+
id: file.id,
|
|
4778
|
+
sha256: sha256Of2(readFileSync17(file.path))
|
|
4779
|
+
}))
|
|
4780
|
+
});
|
|
4781
|
+
}
|
|
4782
|
+
function sha256Of2(content) {
|
|
4783
|
+
return createHash4("sha256").update(content).digest("hex");
|
|
4784
|
+
}
|
|
4785
|
+
|
|
4786
|
+
// src/lib/knowledge/ask/selected-folders-of.ts
|
|
4787
|
+
function selectedFoldersOf(params) {
|
|
4788
|
+
const { environment, standardIds } = params;
|
|
4789
|
+
if (standardIds === void 0) {
|
|
4790
|
+
const input = readStandardInput({ environment });
|
|
4791
|
+
return input.kind === "refused" ? input : { kind: "selected", folders: input.input.selected };
|
|
4792
|
+
}
|
|
4793
|
+
const folders = /* @__PURE__ */ new Map();
|
|
4794
|
+
for (const standardId of standardIds) {
|
|
4795
|
+
const input = readStandardInput({ environment, standardId });
|
|
4796
|
+
if (input.kind === "refused") return input;
|
|
4797
|
+
for (const folder of input.input.selected) folders.set(folder.id, folder);
|
|
4798
|
+
}
|
|
4799
|
+
return { kind: "selected", folders: [...folders.values()].sort(byId) };
|
|
4800
|
+
}
|
|
4801
|
+
function isReadableFolder(params) {
|
|
4802
|
+
return params.folder.files.length > 0;
|
|
4803
|
+
}
|
|
4804
|
+
function byId(first, second) {
|
|
4805
|
+
return first.id < second.id ? -1 : first.id > second.id ? 1 : 0;
|
|
4806
|
+
}
|
|
4807
|
+
|
|
4808
|
+
// src/lib/knowledge/ask/load-standard-indexes.ts
|
|
4809
|
+
async function loadStandardIndexes(params) {
|
|
4810
|
+
const { environment } = params;
|
|
4811
|
+
const selected = selectedFoldersOf({ environment, standardIds: params.standardIds });
|
|
4812
|
+
if (selected.kind === "refused") return selected;
|
|
4813
|
+
const cacheRoot = standardCacheRoot({ environment });
|
|
4814
|
+
const cacheRefusal = workTreeRefusal({ path: cacheRoot });
|
|
4815
|
+
if (cacheRefusal) return { kind: "refused", message: cacheRefusal };
|
|
4816
|
+
const vocabulary = await vocabularyOf2(params);
|
|
4817
|
+
if (vocabulary.kind === "unknownPackage") return vocabulary;
|
|
4818
|
+
const { installed } = vocabulary;
|
|
4819
|
+
const vocabularyRefusal = params.checkVocabulary?.({ installed });
|
|
4820
|
+
if (vocabularyRefusal) return { kind: "refused", message: vocabularyRefusal };
|
|
4821
|
+
const cliVersion = readCliVersion();
|
|
4822
|
+
const loaded = {
|
|
4823
|
+
kind: "loaded",
|
|
4824
|
+
standard: { kind: "documents", indexes: [], folders: [] },
|
|
4825
|
+
ready: [],
|
|
4826
|
+
skipped: [],
|
|
4827
|
+
installed
|
|
4828
|
+
};
|
|
4829
|
+
for (const standard of selected.folders) {
|
|
4830
|
+
if (!isReadableFolder({ folder: standard })) {
|
|
4831
|
+
loaded.skipped.push(standard);
|
|
4832
|
+
continue;
|
|
4833
|
+
}
|
|
4834
|
+
const answer = await ensureStandardIndex({
|
|
4835
|
+
standard,
|
|
4836
|
+
cacheRoot,
|
|
4837
|
+
installed,
|
|
4838
|
+
rebuild: params.rebuild ?? false,
|
|
4839
|
+
cliVersion,
|
|
4840
|
+
progress: params.progress,
|
|
4841
|
+
afterFileRead: params.afterFileRead
|
|
4842
|
+
});
|
|
4843
|
+
if (answer.kind === "aborted") return answer;
|
|
4844
|
+
loaded.ready.push(answer.index);
|
|
4845
|
+
loaded.standard.indexes.push(answer.index.search);
|
|
4846
|
+
loaded.standard.folders.push({
|
|
4847
|
+
id: standard.id,
|
|
4848
|
+
files: standardFolderFilesKey({ standard })
|
|
4849
|
+
});
|
|
4850
|
+
}
|
|
4851
|
+
return loaded;
|
|
4852
|
+
}
|
|
4853
|
+
async function vocabularyOf2(params) {
|
|
4854
|
+
if (params.readVocabulary === false)
|
|
4855
|
+
return { kind: "read", installed: emptyInstalledVocabulary() };
|
|
4856
|
+
return readInstalledVocabulary({ cwd: params.cwd, packageName: params.packageName });
|
|
4857
|
+
}
|
|
4858
|
+
|
|
4859
|
+
// src/lib/knowledge/standard/cap-slice-range.ts
|
|
4860
|
+
function capSliceRange(params) {
|
|
4861
|
+
const { lines, noiseRanges, first, last, windowFirst } = params;
|
|
4862
|
+
if (countedLines({ from: lines[first].from, to: lines[last].to, noiseRanges }) <= SLICE_LINE_CAP)
|
|
4863
|
+
return { first, last };
|
|
4864
|
+
let end = windowFirst;
|
|
4865
|
+
while (end < last && countedLines({ from: lines[windowFirst].from, to: lines[end + 1].to, noiseRanges }) <= SLICE_LINE_CAP)
|
|
4866
|
+
end++;
|
|
4867
|
+
return { first: windowFirst, last: end };
|
|
4868
|
+
}
|
|
4869
|
+
function countedLines(params) {
|
|
4870
|
+
const { from, to, noiseRanges } = params;
|
|
4871
|
+
let count = to - from + 1;
|
|
4872
|
+
for (const range of noiseRanges) {
|
|
4873
|
+
const overlap = Math.min(to, range.to) - Math.max(from, range.from) + 1;
|
|
4874
|
+
if (overlap > 0) count -= overlap;
|
|
4875
|
+
}
|
|
4876
|
+
return count;
|
|
4877
|
+
}
|
|
4878
|
+
|
|
4879
|
+
// src/lib/knowledge/standard/round-score.ts
|
|
4880
|
+
function roundScore(score) {
|
|
4881
|
+
const scale = 10 ** SCORE_DECIMALS;
|
|
4882
|
+
return Math.round(score * scale) / scale;
|
|
4883
|
+
}
|
|
4884
|
+
|
|
4885
|
+
// src/lib/knowledge/standard/best-window-of.ts
|
|
4886
|
+
function bestWindowOf(params) {
|
|
4887
|
+
const { lines, noiseRanges, contributions, titleLine } = params;
|
|
4888
|
+
const hits = bodyHitsOf({ contributions, titleLine });
|
|
4889
|
+
if (hits.length === 0) return null;
|
|
4890
|
+
const lastHit = hits[hits.length - 1].line;
|
|
4891
|
+
const counts = contributions.map(() => 0);
|
|
4892
|
+
let held = 0;
|
|
4893
|
+
let sum = 0;
|
|
4894
|
+
let next = 0;
|
|
4895
|
+
let best = null;
|
|
4896
|
+
let bestSum = -1;
|
|
4897
|
+
for (let start = 0; start < hits.length; ) {
|
|
4898
|
+
const first = hits[start].line;
|
|
4899
|
+
const end = capSliceRange({ lines, noiseRanges, first, last: lastHit, windowFirst: first }).last;
|
|
4900
|
+
for (; next < hits.length && hits[next].line <= end; next++) {
|
|
4901
|
+
const { term } = hits[next];
|
|
4902
|
+
if (counts[term]++ === 0) {
|
|
4903
|
+
held++;
|
|
4904
|
+
sum += contributions[term].contribution;
|
|
4905
|
+
}
|
|
4906
|
+
}
|
|
4907
|
+
const rounded = roundScore(sum);
|
|
4908
|
+
if (rounded > bestSum) {
|
|
4909
|
+
best = { first, last: hits[next - 1].line };
|
|
4910
|
+
bestSum = rounded;
|
|
4911
|
+
}
|
|
4912
|
+
if (held === contributions.length || end >= lastHit) break;
|
|
4913
|
+
for (; start < next && hits[start].line === first; start++) {
|
|
4914
|
+
const { term } = hits[start];
|
|
4915
|
+
if (--counts[term] === 0) {
|
|
4916
|
+
held--;
|
|
4917
|
+
sum -= contributions[term].contribution;
|
|
4918
|
+
}
|
|
4919
|
+
}
|
|
4920
|
+
}
|
|
4921
|
+
return best;
|
|
4922
|
+
}
|
|
4923
|
+
function bodyHitsOf(params) {
|
|
4924
|
+
return params.contributions.flatMap(
|
|
4925
|
+
(contribution, term) => contribution.lines.filter((line) => line !== params.titleLine).map((line) => ({ line, term }))
|
|
4926
|
+
).sort((left, right) => left.line - right.line);
|
|
4927
|
+
}
|
|
4928
|
+
|
|
4929
|
+
// src/lib/knowledge/standard/query-terms-of.ts
|
|
4930
|
+
function queryTermsOf(params) {
|
|
4931
|
+
const { query, about, names } = params;
|
|
4932
|
+
const terms = [];
|
|
4933
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4934
|
+
const aboutKey = about === void 0 ? null : keyOf({ kind: "name", key: about });
|
|
4935
|
+
for (const match of normaliseText(query).matchAll(WORD_TOKEN_PATTERN)) {
|
|
4936
|
+
const typed = match[0];
|
|
4937
|
+
const term = termOf({ typed, names });
|
|
4938
|
+
if (!term) continue;
|
|
4939
|
+
const key = keyOf(term);
|
|
4940
|
+
if (seen.has(key) || key === aboutKey) continue;
|
|
4941
|
+
seen.add(key);
|
|
4942
|
+
terms.push(term);
|
|
4943
|
+
}
|
|
4944
|
+
if (about !== void 0)
|
|
4945
|
+
terms.push({ typed: about, kind: "name", key: about, factor: ABOUT_FACTOR });
|
|
4946
|
+
return terms;
|
|
4947
|
+
}
|
|
4948
|
+
function termOf(params) {
|
|
4949
|
+
const { typed, names } = params;
|
|
4950
|
+
if (names.has(typed) && !isOrdinaryWord(typed))
|
|
4951
|
+
return { typed, kind: "name", key: typed, factor: 1 };
|
|
4952
|
+
const [stem2] = wordKeysOf({ token: typed, split: false });
|
|
4953
|
+
return stem2 === void 0 ? null : { typed, kind: "word", key: stem2, factor: 1 };
|
|
4954
|
+
}
|
|
4955
|
+
function isOrdinaryWord(name) {
|
|
4956
|
+
return name.length >= MINIMUM_WORD_LENGTH && LOWERCASE_WORD_PATTERN.test(name);
|
|
4957
|
+
}
|
|
4958
|
+
function keyOf(term) {
|
|
4959
|
+
return `${term.kind}:${term.key}`;
|
|
4960
|
+
}
|
|
4961
|
+
|
|
4962
|
+
// src/lib/knowledge/standard/score-units.ts
|
|
4963
|
+
function scoreUnits(params) {
|
|
4964
|
+
const { index, terms } = params;
|
|
4965
|
+
const scores = /* @__PURE__ */ new Map();
|
|
4966
|
+
for (const [termIndex, term] of terms.entries()) {
|
|
4967
|
+
const { hits, weight } = term.kind === "word" ? wordHits({ index, stem: term.key }) : nameHits({ index, name: term.key });
|
|
4968
|
+
for (const hit of hits) {
|
|
4969
|
+
const titleLine = index.units[hit.unit].titleLine;
|
|
4970
|
+
const titleFactor = titleLine !== null && hit.lines.includes(titleLine) ? TITLE_HIT_FACTOR : 1;
|
|
4971
|
+
const score = scores.get(hit.unit) ?? emptyScore({ unit: hit.unit, termCount: terms.length });
|
|
4972
|
+
scores.set(hit.unit, score);
|
|
4973
|
+
const contribution = titleFactor * term.factor / weight;
|
|
4974
|
+
score.score += contribution;
|
|
4975
|
+
score.termsHit[termIndex] = true;
|
|
4976
|
+
score.contributions.push({ term: termIndex, contribution, lines: hit.lines });
|
|
4977
|
+
for (const line of hit.lines) score.hitLines.push(line);
|
|
4978
|
+
}
|
|
4979
|
+
}
|
|
4980
|
+
return [...scores.values()];
|
|
4981
|
+
}
|
|
4982
|
+
function wordHits(params) {
|
|
4983
|
+
const { words, lineUnits } = params.index;
|
|
4984
|
+
const lines = Object.hasOwn(words.stems, params.stem) ? words.stems[params.stem] : [];
|
|
4985
|
+
const byUnit = /* @__PURE__ */ new Map();
|
|
4986
|
+
for (const line of lines) {
|
|
4987
|
+
const unit = lineUnits[line];
|
|
4988
|
+
const hit = byUnit.get(unit);
|
|
4989
|
+
if (hit) hit.lines.push(line);
|
|
4990
|
+
else byUnit.set(unit, { unit, lines: [line] });
|
|
4991
|
+
}
|
|
4992
|
+
return {
|
|
4993
|
+
hits: [...byUnit.values()],
|
|
4994
|
+
weight: frequencyWeight({ lineCount: lines.length, logicalLineCount: words.logicalLineCount })
|
|
4995
|
+
};
|
|
4996
|
+
}
|
|
4997
|
+
function nameHits(params) {
|
|
4998
|
+
const { tagLinks, words } = params.index;
|
|
4999
|
+
const links = Object.hasOwn(tagLinks, params.name) ? tagLinks[params.name] : [];
|
|
5000
|
+
const lineCount = links.reduce((count, link) => count + link.lines.length, 0);
|
|
5001
|
+
return {
|
|
5002
|
+
hits: links.map((link) => ({ unit: link.unit, lines: link.lines })),
|
|
5003
|
+
weight: frequencyWeight({ lineCount, logicalLineCount: words.logicalLineCount })
|
|
5004
|
+
};
|
|
5005
|
+
}
|
|
5006
|
+
function frequencyWeight(params) {
|
|
5007
|
+
const { lineCount, logicalLineCount } = params;
|
|
5008
|
+
const common = lineCount > COMMON_WORD_SHARE * logicalLineCount;
|
|
5009
|
+
return (1 + Math.log(1 + lineCount)) * (common ? COMMON_WORD_WEIGHT_FACTOR : 1);
|
|
5010
|
+
}
|
|
5011
|
+
function emptyScore(params) {
|
|
5012
|
+
return {
|
|
5013
|
+
unit: params.unit,
|
|
5014
|
+
score: 0,
|
|
5015
|
+
termsHit: Array.from({ length: params.termCount }, () => false),
|
|
5016
|
+
hitLines: [],
|
|
5017
|
+
contributions: []
|
|
5018
|
+
};
|
|
5019
|
+
}
|
|
5020
|
+
|
|
5021
|
+
// src/lib/knowledge/standard/find-in-standard.ts
|
|
5022
|
+
function findInStandard(params) {
|
|
5023
|
+
const { indexes, query, about } = params;
|
|
5024
|
+
const limit = params.limit ?? DEFAULT_FIND_LIMIT2;
|
|
5025
|
+
const names = new Set(
|
|
5026
|
+
indexes.flatMap((index) => [
|
|
5027
|
+
...index.vocabulary.tags,
|
|
5028
|
+
...index.vocabulary.attributes,
|
|
5029
|
+
...index.vocabulary.values
|
|
5030
|
+
])
|
|
5031
|
+
);
|
|
5032
|
+
const terms = queryTermsOf({ query, about, names });
|
|
5033
|
+
if (terms.length === 0) return [];
|
|
5034
|
+
const standards = indexes.map((index) => ({
|
|
5035
|
+
index,
|
|
5036
|
+
lines: index.files.flatMap((file) => file.logicalLines),
|
|
5037
|
+
noiseRanges: new Map(index.files.map((file) => [file.fileId, file.noiseRanges]))
|
|
5038
|
+
}));
|
|
5039
|
+
const direct = standards.flatMap(
|
|
5040
|
+
(searched, standard) => scoreUnits({ index: searched.index, terms }).map((score) => ({
|
|
5041
|
+
slice: directSliceOf({ searched, score, terms }),
|
|
5042
|
+
standard,
|
|
5043
|
+
unit: score.unit
|
|
5044
|
+
}))
|
|
5045
|
+
).sort(compareRanked).slice(0, limit);
|
|
5046
|
+
const linked = linkedSlicesOf({ standards, direct, limit });
|
|
5047
|
+
return [...direct, ...linked].map((ranked) => ranked.slice);
|
|
5048
|
+
}
|
|
5049
|
+
function directSliceOf(params) {
|
|
5050
|
+
const { searched, score, terms } = params;
|
|
5051
|
+
const unit = searched.index.units[score.unit];
|
|
5052
|
+
const noiseRanges = searched.noiseRanges.get(unit.fileId) ?? [];
|
|
5053
|
+
const window = bestWindowOf({
|
|
5054
|
+
lines: searched.lines,
|
|
5055
|
+
noiseRanges,
|
|
5056
|
+
contributions: score.contributions,
|
|
5057
|
+
titleLine: unit.titleLine
|
|
5058
|
+
});
|
|
5059
|
+
const range = window ? capSliceRange({
|
|
5060
|
+
lines: searched.lines,
|
|
5061
|
+
noiseRanges,
|
|
5062
|
+
...hitSliceBounds({ searched, noiseRanges, score, unit, window })
|
|
5063
|
+
}) : (
|
|
5064
|
+
// a unit hit only in its heading title or caption: the slice runs through its body, so it cites text
|
|
5065
|
+
capSliceRange({
|
|
5066
|
+
lines: searched.lines,
|
|
5067
|
+
noiseRanges,
|
|
5068
|
+
first: unit.firstLine,
|
|
5069
|
+
last: unit.lastLine,
|
|
5070
|
+
windowFirst: unit.firstLine
|
|
5071
|
+
})
|
|
5072
|
+
);
|
|
5073
|
+
return {
|
|
5074
|
+
file: unit.fileId,
|
|
5075
|
+
from: searched.lines[range.first].from,
|
|
5076
|
+
to: searched.lines[range.last].to,
|
|
5077
|
+
clause: unit.clause,
|
|
5078
|
+
title: unit.title,
|
|
5079
|
+
score: roundScore(score.score),
|
|
5080
|
+
terms: terms.filter((_term, index) => score.termsHit[index]).map((term) => term.typed),
|
|
5081
|
+
linked: false
|
|
5082
|
+
};
|
|
5083
|
+
}
|
|
5084
|
+
function hitSliceBounds(params) {
|
|
5085
|
+
const { searched, noiseRanges, score, unit, window } = params;
|
|
5086
|
+
const last = score.hitLines.reduce((latest, line) => Math.max(latest, line), window.last);
|
|
5087
|
+
const fromCaption = unit.kind === "table" && capSliceRange({
|
|
5088
|
+
lines: searched.lines,
|
|
5089
|
+
noiseRanges,
|
|
5090
|
+
first: unit.firstLine,
|
|
5091
|
+
last: window.last,
|
|
5092
|
+
windowFirst: unit.firstLine
|
|
5093
|
+
}).last === window.last;
|
|
5094
|
+
const first = fromCaption ? unit.firstLine : window.first;
|
|
5095
|
+
return { first, last, windowFirst: first };
|
|
5096
|
+
}
|
|
5097
|
+
function compareRanked(left, right) {
|
|
5098
|
+
if (left.slice.score !== right.slice.score) return right.slice.score - left.slice.score;
|
|
5099
|
+
if (left.slice.file !== right.slice.file) return left.slice.file < right.slice.file ? -1 : 1;
|
|
5100
|
+
return left.slice.from - right.slice.from;
|
|
5101
|
+
}
|
|
5102
|
+
function linkedSlicesOf(params) {
|
|
5103
|
+
const { standards, direct, limit } = params;
|
|
5104
|
+
const given = new Set(direct.map((ranked) => `${ranked.standard}:${ranked.unit}`));
|
|
5105
|
+
const targets = /* @__PURE__ */ new Map();
|
|
5106
|
+
const files = /* @__PURE__ */ new Map();
|
|
5107
|
+
const linked = [];
|
|
5108
|
+
for (const ranked of direct) {
|
|
5109
|
+
const searched = standards[ranked.standard];
|
|
5110
|
+
const filesById = files.get(ranked.standard) ?? filesOf(searched);
|
|
5111
|
+
const unitOfTarget = targets.get(ranked.standard) ?? targetsOf2(searched);
|
|
5112
|
+
files.set(ranked.standard, filesById);
|
|
5113
|
+
targets.set(ranked.standard, unitOfTarget);
|
|
5114
|
+
const file = filesById.get(ranked.slice.file);
|
|
5115
|
+
for (const reference of file?.crossReferences ?? []) {
|
|
5116
|
+
if (linked.length === limit) return linked;
|
|
5117
|
+
if (!reference.resolved) continue;
|
|
5118
|
+
if (reference.from < ranked.slice.from || reference.from > ranked.slice.to) continue;
|
|
5119
|
+
const unit = unitOfTarget.get(`${ranked.slice.file}#${reference.target}`);
|
|
5120
|
+
if (unit === void 0 || given.has(`${ranked.standard}:${unit}`)) continue;
|
|
5121
|
+
given.add(`${ranked.standard}:${unit}`);
|
|
5122
|
+
linked.push({ slice: linkedSliceOf({ searched, unit }), standard: ranked.standard, unit });
|
|
5123
|
+
}
|
|
5124
|
+
}
|
|
5125
|
+
return linked;
|
|
5126
|
+
}
|
|
5127
|
+
function linkedSliceOf(params) {
|
|
5128
|
+
const { searched } = params;
|
|
5129
|
+
const unit = searched.index.units[params.unit];
|
|
5130
|
+
const range = capSliceRange({
|
|
5131
|
+
lines: searched.lines,
|
|
5132
|
+
noiseRanges: searched.noiseRanges.get(unit.fileId) ?? [],
|
|
5133
|
+
first: unit.firstLine,
|
|
5134
|
+
last: unit.lastLine,
|
|
5135
|
+
windowFirst: unit.firstLine
|
|
5136
|
+
});
|
|
5137
|
+
return {
|
|
5138
|
+
file: unit.fileId,
|
|
5139
|
+
from: searched.lines[range.first].from,
|
|
5140
|
+
to: searched.lines[range.last].to,
|
|
5141
|
+
clause: unit.clause,
|
|
5142
|
+
title: unit.title,
|
|
5143
|
+
score: 0,
|
|
5144
|
+
terms: [],
|
|
5145
|
+
linked: true
|
|
5146
|
+
};
|
|
5147
|
+
}
|
|
5148
|
+
function targetsOf2(searched) {
|
|
5149
|
+
const unitOfTarget = /* @__PURE__ */ new Map();
|
|
5150
|
+
for (const [index, unit] of searched.index.units.entries()) {
|
|
5151
|
+
if (unit.target === null) continue;
|
|
5152
|
+
const key = `${unit.fileId}#${unit.target}`;
|
|
5153
|
+
if (!unitOfTarget.has(key)) unitOfTarget.set(key, index);
|
|
5154
|
+
}
|
|
5155
|
+
return unitOfTarget;
|
|
5156
|
+
}
|
|
5157
|
+
function filesOf(searched) {
|
|
5158
|
+
return new Map(searched.index.files.map((file) => [file.fileId, file]));
|
|
5159
|
+
}
|
|
2942
5160
|
export {
|
|
2943
5161
|
answerMembers,
|
|
2944
5162
|
answerSchema,
|
|
2945
5163
|
describeElement,
|
|
5164
|
+
findInStandard,
|
|
5165
|
+
loadStandardIndexes,
|
|
2946
5166
|
packagesText,
|
|
2947
5167
|
readApiIndexes,
|
|
2948
5168
|
readCliVersion,
|