paperlint 18.5.0 → 18.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/CLAUDE.md +3 -3
- package/CONTRIBUTING.md +5 -2
- package/README.md +6 -6
- package/dist/adapters/clack/index.d.ts +3 -0
- package/dist/adapters/clack/index.d.ts.map +1 -0
- package/dist/adapters/clack/index.js +3 -0
- package/dist/adapters/clack/index.js.map +1 -0
- package/dist/adapters/clack/prompts.io.d.ts +8 -0
- package/dist/adapters/clack/prompts.io.d.ts.map +1 -0
- package/dist/adapters/clack/prompts.io.js +20 -0
- package/dist/adapters/clack/prompts.io.js.map +1 -0
- package/dist/adapters/hotcrp/portal.io.d.ts.map +1 -1
- package/dist/adapters/hotcrp/portal.io.js +1 -0
- package/dist/adapters/hotcrp/portal.io.js.map +1 -1
- package/dist/adapters/hotcrp/response.d.ts.map +1 -1
- package/dist/adapters/hotcrp/response.js +15 -0
- package/dist/adapters/hotcrp/response.js.map +1 -1
- package/dist/adapters/package-manager/index.d.ts +4 -0
- package/dist/adapters/package-manager/index.d.ts.map +1 -0
- package/dist/adapters/package-manager/index.js +4 -0
- package/dist/adapters/package-manager/index.js.map +1 -0
- package/dist/adapters/package-manager/install.io.d.ts +3 -0
- package/dist/adapters/package-manager/install.io.d.ts.map +1 -0
- package/dist/adapters/package-manager/install.io.js +23 -0
- package/dist/adapters/package-manager/install.io.js.map +1 -0
- package/dist/adapters/package-manager/manager.d.ts +13 -0
- package/dist/adapters/package-manager/manager.d.ts.map +1 -0
- package/dist/adapters/package-manager/manager.js +22 -0
- package/dist/adapters/package-manager/manager.js.map +1 -0
- package/dist/adapters/study-registry/index.d.ts +2 -2
- package/dist/cli.d.ts +4 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +47 -9
- package/dist/cli.js.map +1 -1
- package/dist/domain/submission.d.ts +4 -0
- package/dist/domain/submission.d.ts.map +1 -1
- package/dist/domain/submission.js.map +1 -1
- package/dist/init.d.ts +38 -15
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +99 -39
- package/dist/init.js.map +1 -1
- package/dist/ports/project-install.d.ts +19 -0
- package/dist/ports/project-install.d.ts.map +1 -0
- package/dist/ports/project-install.js +2 -0
- package/dist/ports/project-install.js.map +1 -0
- package/dist/ports/prompts.d.ts +31 -0
- package/dist/ports/prompts.d.ts.map +1 -0
- package/dist/ports/prompts.js +2 -0
- package/dist/ports/prompts.js.map +1 -0
- package/dist/self-install.d.ts +65 -0
- package/dist/self-install.d.ts.map +1 -0
- package/dist/self-install.js +145 -0
- package/dist/self-install.js.map +1 -0
- package/dist/skill-chunks/chunk-65X5HVIJ.js +26462 -0
- package/dist/skill-chunks/chunk-6NU6CXUS.js +258 -0
- package/dist/skill-chunks/chunk-GRSXIWYF.js +352 -0
- package/dist/skill-chunks/chunk-IREAROCV.js +84 -0
- package/dist/skill-chunks/chunk-KWK6RT4T.js +24267 -0
- package/dist/skill-chunks/chunk-NPQVDEE4.js +19681 -0
- package/dist/skill-chunks/chunk-PJ4KNV4J.js +45 -0
- package/dist/skill-chunks/dist-UPCJMLBM.js +385 -0
- package/dist/skill-chunks/pdfjs-LUMEFOH2.js +39518 -0
- package/dist/submission.d.ts +19 -2
- package/dist/submission.d.ts.map +1 -1
- package/dist/submission.js +121 -17
- package/dist/submission.js.map +1 -1
- package/docs/e2e.md +10 -0
- package/docs/install.md +27 -16
- package/docs/prior-art/README.md +2 -0
- package/docs/prior-art/one-command-install.md +33 -0
- package/docs/prior-art/prompt-libraries.md +32 -0
- package/docs/submission.md +19 -4
- package/eslint.config.mjs +1 -0
- package/lib/skill-checks.mjs +17 -9
- package/lib/skill-checks.test.mjs +28 -15
- package/lib/skill-corpus.mjs +5 -2
- package/package.json +5 -3
- package/scripts/bundle-skills.test.ts +226 -0
- package/scripts/bundle-skills.ts +304 -0
- package/skills/argument-arc/SKILL.md +2 -2
- package/skills/argument-arc/SKILL.md.spec.ts +1 -1
- package/skills/argument-arc/scripts/headings.js +57 -13
- package/skills/build-label-page/scripts/label-page.js +2005 -93
- package/skills/cold-read-diff/scripts/cold-read.js +221 -14
- package/skills/label-agent-runs/scripts/labels.js +272 -348
- package/skills/paper-pipeline/scripts/extract-ref-facts.js +304 -0
- package/skills/pc-panel-review/scripts/prior-reviews.js +7244 -12
- package/skills/run-agent-grid/scripts/grid-guard.js +562 -627
- package/skills/verify-citations/SKILL.md +9 -9
- package/skills/verify-citations/SKILL.md.spec.ts +8 -8
- package/skills/verify-citations/scripts/bib-authors.js +228 -0
- package/skills/verify-citations/scripts/verify-cites.js +816 -0
- package/src/adapters/clack/index.ts +2 -0
- package/src/adapters/clack/prompts.io.ts +35 -0
- package/src/adapters/clack/prompts.test.ts +58 -0
- package/src/adapters/hotcrp/portal.io.ts +1 -0
- package/src/adapters/hotcrp/portal.test.ts +43 -2
- package/src/adapters/hotcrp/response.ts +21 -0
- package/src/adapters/package-manager/index.test.ts +67 -0
- package/src/adapters/package-manager/index.ts +3 -0
- package/src/adapters/package-manager/install.io.ts +27 -0
- package/src/adapters/package-manager/manager.ts +33 -0
- package/src/cli.harness.mjs +41 -26
- package/src/cli.test.ts +93 -2
- package/src/cli.ts +80 -7
- package/src/domain/submission.ts +4 -0
- package/src/init.test.ts +138 -27
- package/src/init.ts +128 -72
- package/src/ports/project-install.ts +18 -0
- package/src/ports/prompts.ts +27 -0
- package/src/self-install.test.ts +267 -0
- package/src/self-install.ts +224 -0
- package/src/submission-fields-run.test.ts +1 -0
- package/src/submission-prior-reviews.test.ts +9 -1
- package/src/submission-show.test.ts +81 -0
- package/src/submission-update.test.ts +164 -6
- package/src/submission.ts +208 -24
|
@@ -1,97 +1,2009 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
2
|
+
import { createRequire as __plCreateRequire } from "node:module";
|
|
3
|
+
import { fileURLToPath as __plFileURLToPath } from "node:url";
|
|
4
|
+
import { dirname as __plDirname } from "node:path";
|
|
5
|
+
const require = __plCreateRequire(import.meta.url);
|
|
6
|
+
const __filename = __plFileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = __plDirname(__filename);
|
|
8
|
+
import {
|
|
9
|
+
e,
|
|
10
|
+
e2,
|
|
11
|
+
n,
|
|
12
|
+
n2,
|
|
13
|
+
t11 as t7,
|
|
14
|
+
t12 as t8,
|
|
15
|
+
t15 as t9,
|
|
16
|
+
t16 as t10,
|
|
17
|
+
t17 as t11,
|
|
18
|
+
t18 as t12,
|
|
19
|
+
t3 as t,
|
|
20
|
+
t5 as t2,
|
|
21
|
+
t6 as t3,
|
|
22
|
+
t7 as t4,
|
|
23
|
+
t8 as t5,
|
|
24
|
+
t9 as t6
|
|
25
|
+
} from "../../../dist/skill-chunks/chunk-GRSXIWYF.js";
|
|
26
|
+
import {
|
|
27
|
+
external_exports
|
|
28
|
+
} from "../../../dist/skill-chunks/chunk-NPQVDEE4.js";
|
|
29
|
+
import "../../../dist/skill-chunks/chunk-PJ4KNV4J.js";
|
|
30
|
+
|
|
31
|
+
// skills/build-label-page/scripts/label-page-cli.ts
|
|
32
|
+
import { parseArgs } from "node:util";
|
|
33
|
+
|
|
34
|
+
// skills/build-label-page/scripts/label-page-commands.ts
|
|
35
|
+
var COMMANDS = {
|
|
36
|
+
build: "build",
|
|
37
|
+
export: "export",
|
|
38
|
+
schema: "schema"
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// skills/build-label-page/scripts/label-page-core.ts
|
|
42
|
+
import { createHash } from "node:crypto";
|
|
43
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
44
|
+
import { dirname, join } from "node:path";
|
|
45
|
+
|
|
46
|
+
// skills/build-label-page/scripts/rule-descriptions.ts
|
|
47
|
+
import { spawnSync } from "node:child_process";
|
|
48
|
+
import { createRequire } from "node:module";
|
|
49
|
+
import { resolve } from "node:path";
|
|
50
|
+
var runProcess = (cmd, args) => {
|
|
51
|
+
const r = spawnSync(cmd, args, { encoding: "utf8" });
|
|
52
|
+
return { status: r.status, stdout: r.stdout };
|
|
53
|
+
};
|
|
54
|
+
var RuffRule = external_exports.object({ explanation: external_exports.string(), linter: external_exports.string() });
|
|
55
|
+
function ruffDescription(id, run2) {
|
|
56
|
+
const r = run2("ruff", ["rule", id, "--output-format", "json"]);
|
|
57
|
+
if (r.status !== 0) return void 0;
|
|
58
|
+
const { explanation, linter } = RuffRule.parse(JSON.parse(r.stdout));
|
|
59
|
+
const what = /## What it does\n([\s\S]*?)(?:\n\n|$)/.exec(explanation)?.[1];
|
|
60
|
+
return what === void 0 ? void 0 : { description: what.split("\n").join(" ").trim(), family: linter };
|
|
61
|
+
}
|
|
62
|
+
var RuleModule = external_exports.object({
|
|
63
|
+
meta: external_exports.object({ docs: external_exports.object({ description: external_exports.string() }) })
|
|
64
|
+
});
|
|
65
|
+
var Plugin = external_exports.object({ rules: external_exports.record(external_exports.string(), external_exports.unknown()) });
|
|
66
|
+
var Builtins = external_exports.object({
|
|
67
|
+
builtinRules: external_exports.map(external_exports.string(), external_exports.unknown())
|
|
68
|
+
});
|
|
69
|
+
function pluginsOf(id) {
|
|
70
|
+
const [first = "", second = "", ...rest] = id.split("/");
|
|
71
|
+
const after = (n3) => id.split("/").slice(n3).join("/");
|
|
72
|
+
return id.startsWith("@") ? [
|
|
73
|
+
...rest.length ? [[`${first}/eslint-plugin-${second}`, after(2)]] : [],
|
|
74
|
+
[`${first}/eslint-plugin`, after(1)]
|
|
75
|
+
] : [[`eslint-plugin-${first}`, after(1)]];
|
|
76
|
+
}
|
|
77
|
+
var EsmPlugin = external_exports.object({ default: Plugin });
|
|
78
|
+
var pluginRules = (mod) => {
|
|
79
|
+
const esm = EsmPlugin.safeParse(mod);
|
|
80
|
+
return esm.success ? esm.data.default.rules : Plugin.parse(mod).rules;
|
|
81
|
+
};
|
|
82
|
+
var description = (rule) => {
|
|
83
|
+
const r = RuleModule.safeParse(rule);
|
|
84
|
+
return r.success ? { description: r.data.meta.docs.description } : void 0;
|
|
85
|
+
};
|
|
86
|
+
var tryLoad = (load, pkg) => {
|
|
87
|
+
try {
|
|
88
|
+
return load(pkg);
|
|
89
|
+
} catch {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
function eslintDescription(id, fromDir) {
|
|
94
|
+
const load = createRequire(resolve(fromDir, "items.json"));
|
|
95
|
+
if (!id.includes("/")) {
|
|
96
|
+
const core = tryLoad(load, "eslint/use-at-your-own-risk");
|
|
97
|
+
return core === void 0 ? void 0 : description(Builtins.parse(core).builtinRules.get(id));
|
|
98
|
+
}
|
|
99
|
+
return pluginsOf(id).map(([pkg, name]) => {
|
|
100
|
+
const mod = tryLoad(load, pkg);
|
|
101
|
+
return mod === void 0 ? void 0 : description(pluginRules(mod)[name]);
|
|
102
|
+
}).find((d) => d !== void 0);
|
|
103
|
+
}
|
|
104
|
+
function describeRule(ref, fromDir, run2 = runProcess) {
|
|
105
|
+
return ref.linter === "ruff" ? ruffDescription(ref.id, run2) : eslintDescription(ref.id, fromDir);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// skills/build-label-page/scripts/label-page-presets.ts
|
|
109
|
+
var LINE = external_exports.strictObject({
|
|
110
|
+
n: external_exports.int(),
|
|
111
|
+
text: external_exports.string(),
|
|
112
|
+
mark: external_exports.boolean()
|
|
113
|
+
});
|
|
114
|
+
var EXAMPLE = external_exports.strictObject({
|
|
115
|
+
file: external_exports.string().min(1),
|
|
116
|
+
url: external_exports.url().optional(),
|
|
117
|
+
lines: external_exports.array(LINE).min(1),
|
|
118
|
+
message: external_exports.string().min(1),
|
|
119
|
+
caught_by: external_exports.enum(["a", "b"])
|
|
120
|
+
});
|
|
121
|
+
var EXPLANATION = external_exports.strictObject({
|
|
122
|
+
source: external_exports.enum([
|
|
123
|
+
"ruff rule",
|
|
124
|
+
"eslint docs",
|
|
125
|
+
"plugin docs",
|
|
126
|
+
"config message",
|
|
127
|
+
"none"
|
|
128
|
+
]),
|
|
129
|
+
what: external_exports.string().min(1).optional(),
|
|
130
|
+
why: external_exports.string().min(1).optional(),
|
|
131
|
+
example_bad: external_exports.string().min(1).optional(),
|
|
132
|
+
example_good: external_exports.string().min(1).optional(),
|
|
133
|
+
url: external_exports.url().optional()
|
|
134
|
+
}).refine((x) => x.source === "none" || x.what !== void 0, {
|
|
135
|
+
message: "`what` is required unless `source` is 'none'",
|
|
136
|
+
path: ["what"]
|
|
137
|
+
});
|
|
138
|
+
var RULE = external_exports.strictObject({
|
|
139
|
+
linter: external_exports.string().min(1),
|
|
140
|
+
id: external_exports.string().min(1),
|
|
141
|
+
explanation: EXPLANATION
|
|
142
|
+
});
|
|
143
|
+
var SETTING = external_exports.strictObject({
|
|
144
|
+
key: external_exports.string().regex(/^(files|ignores|rule|option:\S+)$/u),
|
|
145
|
+
raw: external_exports.string().min(1),
|
|
146
|
+
plain: external_exports.string().min(1),
|
|
147
|
+
matches_example: external_exports.boolean().nullable().optional()
|
|
148
|
+
});
|
|
149
|
+
var LINTER_CARD = external_exports.looseObject({
|
|
150
|
+
example: EXAMPLE,
|
|
151
|
+
rules: external_exports.array(RULE).min(1),
|
|
152
|
+
more_like_this: external_exports.strictObject({ count: external_exports.int(), files: external_exports.array(external_exports.string()) }).optional(),
|
|
153
|
+
details: external_exports.strictObject({
|
|
154
|
+
a: external_exports.array(SETTING).min(1),
|
|
155
|
+
b: external_exports.array(SETTING).min(1)
|
|
156
|
+
})
|
|
157
|
+
});
|
|
158
|
+
var LINTER_LABELS = {
|
|
159
|
+
place: "Card {n} of {total}",
|
|
160
|
+
body: "Requirement",
|
|
161
|
+
line: "Where this requirement comes from (the project's rules file)",
|
|
162
|
+
line_part: "Where this requirement comes from (the project's rules file)",
|
|
163
|
+
repeat: "Card {index} of {of} for this requirement",
|
|
164
|
+
practice_example: "Example of this answer, about other code: "
|
|
165
|
+
};
|
|
166
|
+
var KEY_LABEL = {
|
|
167
|
+
files: "Checks files",
|
|
168
|
+
ignores: "Leaves out",
|
|
169
|
+
rule: "Rule"
|
|
170
|
+
};
|
|
171
|
+
var DOCS_NOTE = "The flagged/allowed examples are the docs' own, with the docs' own settings, not this project's.";
|
|
172
|
+
var ruleEntry = (r) => {
|
|
173
|
+
const x = r.explanation;
|
|
174
|
+
return {
|
|
175
|
+
label: "Rule",
|
|
176
|
+
title: `${r.linter} \`${r.id}\``,
|
|
177
|
+
fields: [
|
|
178
|
+
x.source === "none" ? { style: "note", text: "No official explanation for this rule." } : {
|
|
179
|
+
...x.source === "config message" ? { label: "The config's own message" } : {},
|
|
180
|
+
text: String(x.what)
|
|
181
|
+
},
|
|
182
|
+
...x.why === void 0 ? [] : [{ label: "Why", text: x.why }],
|
|
183
|
+
...x.example_bad === void 0 && x.example_good === void 0 ? [] : [{ style: "note", text: DOCS_NOTE }],
|
|
184
|
+
...x.example_bad === void 0 ? [] : [{ label: "Flagged", style: "code", text: x.example_bad }],
|
|
185
|
+
...x.example_good === void 0 ? [] : [{ label: "Allowed", style: "code", text: x.example_good }]
|
|
186
|
+
],
|
|
187
|
+
...x.url === void 0 ? {} : { url: x.url, url_label: "Rule docs" }
|
|
188
|
+
};
|
|
189
|
+
};
|
|
190
|
+
var settingRows = (d) => {
|
|
191
|
+
const same = (r, s) => r.key === s.key && r.raw === s.raw;
|
|
192
|
+
const onlyIn = (xs, ys) => xs.filter((r) => !ys.some((s) => same(r, s)));
|
|
193
|
+
const rows = [
|
|
194
|
+
...onlyIn(d.a, d.b).map((r) => [r, "A only"]),
|
|
195
|
+
...onlyIn(d.b, d.a).map((r) => [r, "B only"]),
|
|
196
|
+
...d.a.filter((r) => d.b.some((s) => same(r, s))).map((r) => [r, "A and B"])
|
|
197
|
+
];
|
|
198
|
+
return rows.map(([r, side]) => ({
|
|
199
|
+
label: `${side} \xB7 ${KEY_LABEL[r.key] ?? `Option ${r.key.replace(/^option:/u, "")}`}`,
|
|
200
|
+
value: r.raw,
|
|
201
|
+
plain: r.plain,
|
|
202
|
+
...r.matches_example === true ? { mark: { tone: "yes", text: "\u2713 matches this file" } } : r.matches_example === false ? { mark: { tone: "no", text: "\u2717 does not match this file" } } : {}
|
|
203
|
+
}));
|
|
204
|
+
};
|
|
205
|
+
var moreList = (more) => more === void 0 || more.count === 0 ? {} : {
|
|
206
|
+
more: {
|
|
207
|
+
summary: `${String(more.count)} more file(s) with the same situation`,
|
|
208
|
+
items: more.files.map((f) => `\`${f}\``),
|
|
209
|
+
...more.count > more.files.length ? { rest: `and ${String(more.count - more.files.length)} more` } : {}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
function linterCard(card) {
|
|
213
|
+
const { example: ex, rules, more_like_this: more, details, ...rest } = card;
|
|
214
|
+
const by = ex.caught_by === "a" ? ["A", "B"] : ["B", "A"];
|
|
215
|
+
return {
|
|
216
|
+
...rest,
|
|
217
|
+
excerpt: {
|
|
218
|
+
file: ex.file,
|
|
219
|
+
...ex.url === void 0 ? {} : { url: ex.url },
|
|
220
|
+
lines: ex.lines,
|
|
221
|
+
caption: `The linter says: ${ex.message}`
|
|
222
|
+
},
|
|
223
|
+
references: {
|
|
224
|
+
summary: rules.length > 1 ? "What the linter rules do (official docs)" : "What the linter rule does (official docs)",
|
|
225
|
+
entries: rules.map(ruleEntry)
|
|
226
|
+
},
|
|
227
|
+
...moreList(more),
|
|
228
|
+
table: {
|
|
229
|
+
summary: "Settings behind this card",
|
|
230
|
+
note: `Setting ${String(by[0])} flags the example line; setting ${String(by[1])} passes it.`,
|
|
231
|
+
rows: settingRows(details)
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
var CONFIG = external_exports.looseObject({
|
|
236
|
+
preset: external_exports.string(),
|
|
237
|
+
labels: external_exports.record(external_exports.string(), external_exports.string()).optional(),
|
|
238
|
+
practice: external_exports.array(external_exports.unknown()),
|
|
239
|
+
items: external_exports.array(external_exports.unknown())
|
|
240
|
+
});
|
|
241
|
+
var PRESETS = {
|
|
242
|
+
"linter-config-witness": {
|
|
243
|
+
labels: LINTER_LABELS,
|
|
244
|
+
card: linterCard,
|
|
245
|
+
schema: LINTER_CARD
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
var issuesOf = (where, e3) => e3.issues.map(
|
|
249
|
+
(issue) => `${where}${issue.path.map(
|
|
250
|
+
(p) => typeof p === "number" ? `[${String(p)}]` : `.${String(p)}`
|
|
251
|
+
).join("")}: ${issue.message}`
|
|
252
|
+
).join("; ");
|
|
253
|
+
var mapCards = (preset, practice, items) => [
|
|
254
|
+
...practice.map((c, n3) => [`practice[${String(n3)}]`, c]),
|
|
255
|
+
...items.map((c, n3) => [`items[${String(n3)}]`, c])
|
|
256
|
+
].map(([at2, c]) => {
|
|
257
|
+
const r = preset.schema.safeParse(c);
|
|
258
|
+
return r.success ? { ok: true, value: preset.card(r.data) } : { ok: false, message: issuesOf(at2, r.error) };
|
|
259
|
+
});
|
|
260
|
+
function applyPreset(raw) {
|
|
261
|
+
const cfg = CONFIG.safeParse(raw);
|
|
262
|
+
if (!cfg.success) return { ok: true, value: raw };
|
|
263
|
+
const { preset: name, labels, practice, items, ...rest } = cfg.data;
|
|
264
|
+
const preset = PRESETS[name];
|
|
265
|
+
if (preset === void 0)
|
|
266
|
+
return {
|
|
267
|
+
ok: false,
|
|
268
|
+
message: `preset: '${name}' is not a preset (${Object.keys(PRESETS).join(", ")})`
|
|
269
|
+
};
|
|
270
|
+
const cards = mapCards(preset, practice, items);
|
|
271
|
+
const bad = cards.find((c) => !c.ok);
|
|
272
|
+
if (bad !== void 0) return bad;
|
|
273
|
+
const made = cards.filter((c) => c.ok).map((c) => c.value);
|
|
274
|
+
return {
|
|
275
|
+
ok: true,
|
|
276
|
+
value: {
|
|
277
|
+
...rest,
|
|
278
|
+
labels: { ...preset.labels, ...labels },
|
|
279
|
+
practice: made.slice(0, practice.length),
|
|
280
|
+
items: made.slice(practice.length)
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// skills/build-label-page/scripts/label-page-core.ts
|
|
286
|
+
var Refusal = class extends Error {
|
|
287
|
+
exitCode;
|
|
288
|
+
constructor(exitCode, message) {
|
|
289
|
+
super(message);
|
|
290
|
+
this.exitCode = exitCode;
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
var fail = (message) => {
|
|
294
|
+
throw new Refusal(1, `REFUSED: ${message}`);
|
|
295
|
+
};
|
|
296
|
+
var printed = [];
|
|
297
|
+
var takePrinted = () => printed.splice(0).map((l) => `${l}
|
|
298
|
+
`).join("");
|
|
299
|
+
var say = (line) => {
|
|
300
|
+
printed.push(line);
|
|
301
|
+
};
|
|
302
|
+
var isList = (v) => Array.isArray(v);
|
|
303
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
304
|
+
var has = (o, key) => Object.hasOwn(o, key);
|
|
305
|
+
var quoted = (s) => {
|
|
306
|
+
const q = s.includes("'") && !s.includes('"') ? '"' : "'";
|
|
307
|
+
const body = s.replaceAll("\\", "\\\\").replaceAll("\n", "\\n").replaceAll(" ", "\\t").replaceAll("\r", "\\r");
|
|
308
|
+
return `${q}${q === "'" ? body.replaceAll("'", "\\'") : body}${q}`;
|
|
309
|
+
};
|
|
310
|
+
var repr = (v) => {
|
|
311
|
+
if (v === null || v === void 0) return "None";
|
|
312
|
+
if (typeof v === "boolean") return v ? "True" : "False";
|
|
313
|
+
if (typeof v === "string") return quoted(v);
|
|
314
|
+
if (typeof v === "number") return String(v);
|
|
315
|
+
if (Array.isArray(v)) return `[${v.map(repr).join(", ")}]`;
|
|
316
|
+
return `{${Object.entries(v).map(([k, x]) => `${quoted(k)}: ${repr(x)}`).join(", ")}}`;
|
|
317
|
+
};
|
|
318
|
+
var typeName = (v) => {
|
|
319
|
+
if (v === null) return "NoneType";
|
|
320
|
+
if (Array.isArray(v)) return "list";
|
|
321
|
+
if (typeof v === "number") return Number.isInteger(v) ? "int" : "float";
|
|
322
|
+
return typeof v === "boolean" ? "bool" : "str";
|
|
323
|
+
};
|
|
324
|
+
var byText = (a, b) => Number(a > b) - Number(a < b);
|
|
325
|
+
var writeJson = (path, value) => {
|
|
326
|
+
writeFileSync(path, `${JSON.stringify(value, null, 2)}
|
|
327
|
+
`, "utf8");
|
|
328
|
+
};
|
|
329
|
+
var JSON_VALUE = external_exports.json();
|
|
330
|
+
var withPreset = (raw) => {
|
|
331
|
+
const r = applyPreset(raw);
|
|
332
|
+
return r.ok ? JSON_VALUE.parse(r.value) : fail(r.message);
|
|
333
|
+
};
|
|
334
|
+
var readJson = (path) => {
|
|
335
|
+
try {
|
|
336
|
+
return JSON_VALUE.parse(JSON.parse(readFileSync(path, "utf8")));
|
|
337
|
+
} catch (e3) {
|
|
338
|
+
return fail(`cannot read ${path} as JSON: ${String(e3)}`);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
var PAGE_FIELDS = ["group", "title", "body", "code", "context", "file"];
|
|
342
|
+
var PRIVATE_FIELDS = ["source_id"];
|
|
343
|
+
var ANSWERERS_FIELD = "answerers";
|
|
344
|
+
var RULES_FIELD = "rules";
|
|
345
|
+
var UNIT_FIELD = "unit";
|
|
346
|
+
var READINGS_FIELD = "readings";
|
|
347
|
+
var BLOCK_FIELDS = [
|
|
348
|
+
"situation",
|
|
349
|
+
"excerpt",
|
|
350
|
+
"references",
|
|
351
|
+
"more",
|
|
352
|
+
"table",
|
|
353
|
+
"repeat",
|
|
354
|
+
"quote",
|
|
355
|
+
"terms",
|
|
356
|
+
"answer_notes"
|
|
357
|
+
];
|
|
358
|
+
var ALLOWED_FIELDS = [
|
|
359
|
+
...PAGE_FIELDS,
|
|
360
|
+
RULES_FIELD,
|
|
361
|
+
UNIT_FIELD,
|
|
362
|
+
READINGS_FIELD,
|
|
363
|
+
...BLOCK_FIELDS,
|
|
364
|
+
ANSWERERS_FIELD,
|
|
365
|
+
...PRIVATE_FIELDS
|
|
366
|
+
];
|
|
367
|
+
var FAMILIARITY = [
|
|
368
|
+
{ value: "well", label: "Well" },
|
|
369
|
+
{ value: "somewhat", label: "Somewhat" },
|
|
370
|
+
{ value: "not_really", label: "Not really" }
|
|
371
|
+
];
|
|
372
|
+
var RESERVED_NAMES = [
|
|
373
|
+
"__proto__",
|
|
374
|
+
"constructor",
|
|
375
|
+
"prototype",
|
|
376
|
+
"hasOwnProperty",
|
|
377
|
+
"isPrototypeOf",
|
|
378
|
+
"propertyIsEnumerable",
|
|
379
|
+
"toLocaleString",
|
|
380
|
+
"toString",
|
|
381
|
+
"valueOf",
|
|
382
|
+
"__defineGetter__",
|
|
383
|
+
"__defineSetter__",
|
|
384
|
+
"__lookupGetter__",
|
|
385
|
+
"__lookupSetter__"
|
|
386
|
+
];
|
|
387
|
+
var NAME_PATTERN = `(?!(?:${RESERVED_NAMES.join("|")})$)[A-Za-z0-9_-]+`;
|
|
388
|
+
var SEP = "~";
|
|
389
|
+
var SOURCES = ["page", "chat"];
|
|
390
|
+
var SOURCES_SHOWN = `(${SOURCES.map(quoted).join(", ")})`;
|
|
391
|
+
var string = (required = true, pattern, why) => pattern === void 0 ? { kind: "string", required } : {
|
|
392
|
+
kind: "string",
|
|
393
|
+
required,
|
|
394
|
+
pattern,
|
|
395
|
+
...why === void 0 ? {} : { why }
|
|
396
|
+
};
|
|
397
|
+
var text = () => ({ kind: "text", required: true });
|
|
398
|
+
var integer = (required = true) => ({
|
|
399
|
+
kind: "integer",
|
|
400
|
+
required
|
|
401
|
+
});
|
|
402
|
+
var boolean = (required = true) => ({
|
|
403
|
+
kind: "boolean",
|
|
404
|
+
required
|
|
405
|
+
});
|
|
406
|
+
var array = (items, required = true, min = 0) => ({
|
|
407
|
+
kind: "array",
|
|
408
|
+
required,
|
|
409
|
+
min,
|
|
410
|
+
items
|
|
411
|
+
});
|
|
412
|
+
var mapping = (values, keyPattern, min = 0, required = true) => ({
|
|
413
|
+
kind: "map",
|
|
414
|
+
required,
|
|
415
|
+
min,
|
|
416
|
+
key_pattern: keyPattern,
|
|
417
|
+
values
|
|
418
|
+
});
|
|
419
|
+
var obj = (fields, closed = true, required = true) => ({ kind: "object", required, closed, fields });
|
|
420
|
+
var LINK = string(false, "https?://\\S+", "an http(s) link");
|
|
421
|
+
var optional = (fields) => obj(fields, true, false);
|
|
422
|
+
var TEXT_FIELD = obj({
|
|
423
|
+
label: string(false),
|
|
424
|
+
text: string(),
|
|
425
|
+
style: string(false, "code|note", "code or note")
|
|
426
|
+
});
|
|
427
|
+
var CARD_FIELDS = {
|
|
428
|
+
[RULES_FIELD]: array(
|
|
429
|
+
obj({
|
|
430
|
+
linter: string(true, "eslint|ruff", "eslint or ruff"),
|
|
431
|
+
id: string()
|
|
432
|
+
}),
|
|
433
|
+
false,
|
|
434
|
+
1
|
|
435
|
+
),
|
|
436
|
+
[UNIT_FIELD]: obj(
|
|
437
|
+
// `id` names one occurrence of the line (two files may hold the same words): never shown.
|
|
438
|
+
{
|
|
439
|
+
id: string(),
|
|
440
|
+
text: string(),
|
|
441
|
+
mark: string(),
|
|
442
|
+
part: integer(),
|
|
443
|
+
of: integer()
|
|
444
|
+
},
|
|
445
|
+
true,
|
|
446
|
+
false
|
|
447
|
+
),
|
|
448
|
+
[READINGS_FIELD]: obj({ a: string(), b: string() }, true, false),
|
|
449
|
+
file: string(false),
|
|
450
|
+
situation: optional({
|
|
451
|
+
summary: string(false),
|
|
452
|
+
question: string(),
|
|
453
|
+
// the source tag over the summary (#400); the question is the page's own and gets none
|
|
454
|
+
from: string(false)
|
|
455
|
+
}),
|
|
456
|
+
excerpt: optional({
|
|
457
|
+
file: string(false),
|
|
458
|
+
// the file's language, after its path: "path · Python"
|
|
459
|
+
lang: string(false),
|
|
460
|
+
url: LINK,
|
|
461
|
+
// the header of an extra gutter column; each line's `gutter` is its value there ("depth")
|
|
462
|
+
gutter_header: string(false),
|
|
463
|
+
lines: array(
|
|
464
|
+
obj({
|
|
465
|
+
n: integer(),
|
|
466
|
+
text: text(),
|
|
467
|
+
mark: boolean(),
|
|
468
|
+
gutter: string(false),
|
|
469
|
+
// a line shown only to place the code (an enclosing block's opener): drawn faint
|
|
470
|
+
context: boolean(false)
|
|
471
|
+
}),
|
|
472
|
+
true,
|
|
473
|
+
1
|
|
474
|
+
),
|
|
475
|
+
// one short fact, drawn in a box right under the marked line, pointing at it
|
|
476
|
+
annotation: string(false),
|
|
477
|
+
caption: string(false),
|
|
478
|
+
mark_label: string(false),
|
|
479
|
+
// source tags (#400): over the code box, over the caption under it, and inside the annotation's box
|
|
480
|
+
from: string(false),
|
|
481
|
+
caption_from: string(false),
|
|
482
|
+
annotation_from: string(false)
|
|
483
|
+
}),
|
|
484
|
+
quote: optional({
|
|
485
|
+
text: string(),
|
|
486
|
+
source: string(false),
|
|
487
|
+
from: string(false)
|
|
488
|
+
}),
|
|
489
|
+
terms: array(
|
|
490
|
+
obj({
|
|
491
|
+
term: string(),
|
|
492
|
+
note: array(
|
|
493
|
+
obj({
|
|
494
|
+
title: string(false),
|
|
495
|
+
says: string(false),
|
|
496
|
+
quote: string(false),
|
|
497
|
+
code: string(false),
|
|
498
|
+
example: string(false),
|
|
499
|
+
url: string(true, "https?://\\S+", "an http(s) link"),
|
|
500
|
+
url_label: string()
|
|
501
|
+
}),
|
|
502
|
+
true,
|
|
503
|
+
1
|
|
504
|
+
)
|
|
505
|
+
}),
|
|
506
|
+
false,
|
|
507
|
+
1
|
|
508
|
+
),
|
|
509
|
+
// question id → option value → the sub-line under that answer's button on this card
|
|
510
|
+
answer_notes: mapping(
|
|
511
|
+
mapping(string(), NAME_PATTERN, 1),
|
|
512
|
+
NAME_PATTERN,
|
|
513
|
+
1,
|
|
514
|
+
false
|
|
515
|
+
),
|
|
516
|
+
references: optional({
|
|
517
|
+
summary: string(),
|
|
518
|
+
entries: array(
|
|
519
|
+
obj({
|
|
520
|
+
label: string(false),
|
|
521
|
+
title: string(),
|
|
522
|
+
fields: array(TEXT_FIELD, true, 1),
|
|
523
|
+
url: LINK,
|
|
524
|
+
url_label: string(false)
|
|
525
|
+
}),
|
|
526
|
+
true,
|
|
527
|
+
1
|
|
528
|
+
)
|
|
529
|
+
}),
|
|
530
|
+
more: optional({
|
|
531
|
+
summary: string(),
|
|
532
|
+
items: array(string(), true, 1),
|
|
533
|
+
rest: string(false)
|
|
534
|
+
}),
|
|
535
|
+
table: optional({
|
|
536
|
+
summary: string(),
|
|
537
|
+
note: string(false),
|
|
538
|
+
rows: array(
|
|
539
|
+
obj({
|
|
540
|
+
label: string(),
|
|
541
|
+
value: string(),
|
|
542
|
+
plain: string(false),
|
|
543
|
+
mark: optional({
|
|
544
|
+
tone: string(true, "yes|no", "yes or no"),
|
|
545
|
+
text: string()
|
|
546
|
+
})
|
|
547
|
+
}),
|
|
548
|
+
true,
|
|
549
|
+
1
|
|
550
|
+
)
|
|
551
|
+
}),
|
|
552
|
+
repeat: optional({ group: string(), index: integer(), of: integer() })
|
|
553
|
+
};
|
|
554
|
+
var KINDS = {
|
|
555
|
+
// keep or drop a drawn requirement: the line it came from, marked, and the rule read for it
|
|
556
|
+
approval: {
|
|
557
|
+
[UNIT_FIELD]: "an approval card shows the line its requirement was drawn from, the requirement marked",
|
|
558
|
+
[RULES_FIELD]: "an approval card shows the rule read for it, with the linter's own description"
|
|
559
|
+
},
|
|
560
|
+
// why two readings of one requirement disagree on a file
|
|
561
|
+
witness: {
|
|
562
|
+
[RULES_FIELD]: "a witness card shows the rule both readings configure, with the linter's description",
|
|
563
|
+
[READINGS_FIELD]: "a witness card shows the two readings, one row each",
|
|
564
|
+
file: "a witness card shows the file the readings disagree on, on its own row"
|
|
565
|
+
},
|
|
566
|
+
// gold labels, a blind review: the card is the item, nothing more is required
|
|
567
|
+
labels: {}
|
|
568
|
+
};
|
|
569
|
+
var KIND_NAMES = Object.keys(KINDS).join(", ");
|
|
570
|
+
var cardFields = (kind) => Object.fromEntries(
|
|
571
|
+
Object.entries(CARD_FIELDS).map(([name, node]) => {
|
|
572
|
+
const why = KINDS[kind][name];
|
|
573
|
+
return [
|
|
574
|
+
name,
|
|
575
|
+
why === void 0 ? node : { ...node, required: true, why }
|
|
576
|
+
];
|
|
577
|
+
})
|
|
578
|
+
);
|
|
579
|
+
var itemSchema = (kind) => obj(
|
|
580
|
+
{
|
|
581
|
+
source_id: string(),
|
|
582
|
+
...Object.fromEntries(
|
|
583
|
+
PAGE_FIELDS.filter((f) => f !== "file").map((f) => [
|
|
584
|
+
f,
|
|
585
|
+
string(f === "group")
|
|
586
|
+
])
|
|
587
|
+
),
|
|
588
|
+
...cardFields(kind),
|
|
589
|
+
[ANSWERERS_FIELD]: array(string(true, NAME_PATTERN), false, 1)
|
|
590
|
+
},
|
|
591
|
+
false
|
|
592
|
+
// an unlisted item field is the leak guard's refusal, with its own message
|
|
593
|
+
);
|
|
594
|
+
var OPTION_SCHEMA = obj({
|
|
595
|
+
value: string(),
|
|
596
|
+
label: string(),
|
|
597
|
+
example: string(),
|
|
598
|
+
icon: string(false),
|
|
599
|
+
ask_why: string(false)
|
|
600
|
+
});
|
|
601
|
+
var practiceSchema = (kind) => obj({
|
|
602
|
+
...Object.fromEntries(
|
|
603
|
+
PAGE_FIELDS.filter((f) => f !== "group" && f !== "file").map((f) => [
|
|
604
|
+
f,
|
|
605
|
+
string(false)
|
|
606
|
+
])
|
|
607
|
+
),
|
|
608
|
+
...cardFields(kind),
|
|
609
|
+
expected: mapping(string(), NAME_PATTERN, 1),
|
|
610
|
+
why: string()
|
|
611
|
+
});
|
|
612
|
+
var MIN_PRACTICE = 3;
|
|
613
|
+
var QUESTION_SCHEMA = obj({
|
|
614
|
+
id: string(true, NAME_PATTERN),
|
|
615
|
+
text: string(),
|
|
616
|
+
short: string(false),
|
|
617
|
+
options: array(OPTION_SCHEMA, true, 2)
|
|
618
|
+
});
|
|
619
|
+
var LABELS = optional({
|
|
620
|
+
place: string(false),
|
|
621
|
+
body: string(false),
|
|
622
|
+
line: string(false),
|
|
623
|
+
line_part: string(false),
|
|
624
|
+
repeat: string(false),
|
|
625
|
+
practice_example: string(false),
|
|
626
|
+
hidden: string(false),
|
|
627
|
+
hidden_one: string(false),
|
|
628
|
+
wait: string(false),
|
|
629
|
+
how_to: string(false),
|
|
630
|
+
for_example: string(false)
|
|
631
|
+
});
|
|
632
|
+
var TRANSLATION = obj({
|
|
633
|
+
title: string(false),
|
|
634
|
+
intro: string(false),
|
|
635
|
+
labels: LABELS,
|
|
636
|
+
// the first question as the cards with a `situation` ask it; `{n}` is the marked line's number
|
|
637
|
+
situation_question: string(false),
|
|
638
|
+
// the labels of the page's `sources`, by id; one left out reads as in the page's own language
|
|
639
|
+
sources: mapping(string(), NAME_PATTERN, 1, false),
|
|
640
|
+
questions: mapping(
|
|
641
|
+
obj({
|
|
642
|
+
text: string(),
|
|
643
|
+
short: string(false),
|
|
644
|
+
options: mapping(
|
|
645
|
+
obj({ label: string(), example: string(), ask_why: string(false) }),
|
|
646
|
+
NAME_PATTERN,
|
|
647
|
+
1
|
|
648
|
+
)
|
|
649
|
+
}),
|
|
650
|
+
NAME_PATTERN,
|
|
651
|
+
1
|
|
652
|
+
)
|
|
653
|
+
});
|
|
654
|
+
var configSchema = (kind) => obj({
|
|
655
|
+
study: string(true, NAME_PATTERN),
|
|
656
|
+
// becomes a file name
|
|
657
|
+
kind: string(true, Object.keys(KINDS).join("|"), `one of ${KIND_NAMES}`),
|
|
658
|
+
title: string(),
|
|
659
|
+
intro: string(false),
|
|
660
|
+
seed: integer(),
|
|
661
|
+
groups: mapping(string(), NAME_PATTERN, 1),
|
|
662
|
+
// ids become store document ids
|
|
663
|
+
labeller_knows: array(string(true, NAME_PATTERN), false),
|
|
664
|
+
// Two or more people answer the same page, each under their own name (doc ids `<name>~<id>`).
|
|
665
|
+
answerers: array(string(true, NAME_PATTERN), false, 2),
|
|
666
|
+
// An item's answer saves only once the item has been on screen this many seconds.
|
|
667
|
+
min_seconds: integer(false),
|
|
668
|
+
// true: a "how well do you know {group}" question per group, for a study that filters on it
|
|
669
|
+
// (rule 5). Left out, none is asked: an approval page's author has never seen the repositories.
|
|
670
|
+
familiarity: boolean(false),
|
|
671
|
+
familiarity_question: string(false),
|
|
672
|
+
questions: array(QUESTION_SCHEMA, true, 1),
|
|
673
|
+
practice: array(practiceSchema(kind), true, MIN_PRACTICE),
|
|
674
|
+
practice_round: integer(false),
|
|
675
|
+
// "matched": the real items stay closed until every practice item of the round is answered as
|
|
676
|
+
// expected. Left out, answering every practice item opens them, a miss included.
|
|
677
|
+
practice_gate: string(false, "matched", "'matched' or left out"),
|
|
678
|
+
labels: LABELS,
|
|
679
|
+
// the source tags a card's blocks carry (#400): id → the tag's label, in colour order
|
|
680
|
+
sources: mapping(string(), NAME_PATTERN, 1, false),
|
|
681
|
+
// the language of the page's own words, and the same words in more languages, with a switch
|
|
682
|
+
lang: string(false, NAME_PATTERN),
|
|
683
|
+
translations: mapping(TRANSLATION, NAME_PATTERN, 1, false),
|
|
684
|
+
items: array(itemSchema(kind), true, 1)
|
|
685
|
+
});
|
|
686
|
+
var CONFIG_SCHEMA = configSchema("labels");
|
|
687
|
+
var at = (path, name) => path ? `${path}.${name}` : name;
|
|
688
|
+
var fullMatch = (pattern, s) => new RegExp(`^(?:${pattern})$`, "u").test(s);
|
|
689
|
+
var ID_RULE = "letters, digits, - and _ only";
|
|
690
|
+
function checkString(value, node, where) {
|
|
691
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
692
|
+
fail(`${where}: must be a non-empty string, got ${repr(value)}`);
|
|
693
|
+
} else if (node.pattern !== void 0 && !fullMatch(node.pattern, value)) {
|
|
694
|
+
fail(`${where}: ${repr(value)} must be ${node.why ?? ID_RULE}`);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
var checkCount = (n3, min, where) => {
|
|
698
|
+
if (n3 < min)
|
|
699
|
+
fail(`${where}: needs ${String(min)} or more entries, has ${String(n3)}`);
|
|
700
|
+
};
|
|
701
|
+
function checkArray(value, node, path, where) {
|
|
702
|
+
if (!isList(value))
|
|
703
|
+
return fail(`${where}: must be a list, got ${repr(value)}`);
|
|
704
|
+
checkCount(value.length, node.min, where);
|
|
705
|
+
value.forEach((entry, n3) => {
|
|
706
|
+
checkValue(entry, node.items, `${path}[${String(n3)}]`);
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
function checkMap(value, node, path, where) {
|
|
710
|
+
if (!isObject(value))
|
|
711
|
+
return fail(`${where}: must be an object, got ${repr(value)}`);
|
|
712
|
+
checkCount(Object.keys(value).length, node.min, where);
|
|
713
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
714
|
+
if (!fullMatch(node.key_pattern, key))
|
|
715
|
+
fail(`${at(path, key)}: key must be ${ID_RULE}`);
|
|
716
|
+
checkValue(entry, node.values, at(path, key));
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
function checkObject(value, node, path, where) {
|
|
720
|
+
if (!isObject(value))
|
|
721
|
+
return fail(`${where}: must be an object, got ${repr(value)}`);
|
|
722
|
+
for (const [name, sub] of Object.entries(node.fields)) {
|
|
723
|
+
if (has(value, name)) checkValue(value[name] ?? null, sub, at(path, name));
|
|
724
|
+
else if (sub.required)
|
|
725
|
+
fail(
|
|
726
|
+
`${at(path, name)}: is required and missing${sub.why === void 0 ? "" : ` \u2014 ${sub.why}`}`
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const unknown = Object.keys(value).find((name) => !has(node.fields, name));
|
|
730
|
+
if (node.closed && unknown !== void 0) {
|
|
731
|
+
fail(
|
|
732
|
+
`${at(path, unknown)}: not a known field (known: ${Object.keys(node.fields).join(", ")})`
|
|
733
|
+
);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
function checkBoolean(value, where) {
|
|
737
|
+
if (typeof value !== "boolean")
|
|
738
|
+
fail(`${where}: must be true or false, got ${repr(value)}`);
|
|
739
|
+
}
|
|
740
|
+
function checkScalar(value, kind, where) {
|
|
741
|
+
if (kind === "text" && typeof value !== "string")
|
|
742
|
+
fail(`${where}: must be a string, got ${repr(value)}`);
|
|
743
|
+
if (kind === "integer" && (typeof value !== "number" || !Number.isInteger(value)))
|
|
744
|
+
fail(`${where}: must be an integer, got ${repr(value)}`);
|
|
745
|
+
}
|
|
746
|
+
function checkValue(value, node, path) {
|
|
747
|
+
const where = path || "items.json";
|
|
748
|
+
switch (node.kind) {
|
|
749
|
+
case "string":
|
|
750
|
+
checkString(value, node, where);
|
|
751
|
+
break;
|
|
752
|
+
case "text":
|
|
753
|
+
case "integer":
|
|
754
|
+
checkScalar(value, node.kind, where);
|
|
755
|
+
break;
|
|
756
|
+
case "boolean":
|
|
757
|
+
checkBoolean(value, where);
|
|
758
|
+
break;
|
|
759
|
+
case "array":
|
|
760
|
+
checkArray(value, node, path, where);
|
|
761
|
+
break;
|
|
762
|
+
case "map":
|
|
763
|
+
checkMap(value, node, path, where);
|
|
764
|
+
break;
|
|
765
|
+
case "object":
|
|
766
|
+
checkObject(value, node, path, where);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
function assertConfig(value) {
|
|
770
|
+
checkValue(value, CONFIG_SCHEMA, "");
|
|
771
|
+
}
|
|
772
|
+
var allowedValues = (questions, asksFamiliarity2) => ({
|
|
773
|
+
questions: Object.fromEntries(
|
|
774
|
+
questions.map((q) => [q.id, q.options.map((o) => o.value)])
|
|
775
|
+
),
|
|
776
|
+
// The answers that need a reason, per question: the page and the export read the same list.
|
|
777
|
+
needs_why: Object.fromEntries(
|
|
778
|
+
questions.map((q) => [
|
|
779
|
+
q.id,
|
|
780
|
+
q.options.filter((o) => o.ask_why !== void 0).map((o) => o.value)
|
|
781
|
+
])
|
|
782
|
+
),
|
|
783
|
+
// Empty when the page asks no familiarity: the export then expects no group document at all.
|
|
784
|
+
familiarity: asksFamiliarity2 ? FAMILIARITY.map((o) => o.value) : []
|
|
785
|
+
});
|
|
786
|
+
var asksFamiliarity = (cfg) => cfg.familiarity === true;
|
|
787
|
+
var readsAs = (s) => s.trim().toLowerCase();
|
|
788
|
+
var firstRepeat = (labels) => {
|
|
789
|
+
const seen = labels.map(readsAs);
|
|
790
|
+
const at2 = seen.findIndex((x, i) => seen.indexOf(x) !== i);
|
|
791
|
+
return at2 === -1 ? void 0 : { at: at2, earlier: seen.findIndex((x) => x === seen[at2]) };
|
|
792
|
+
};
|
|
793
|
+
function checkGroups(cfg) {
|
|
794
|
+
const ids = Object.keys(cfg.groups);
|
|
795
|
+
const labels = Object.values(cfg.groups);
|
|
796
|
+
const twice = firstRepeat(labels);
|
|
797
|
+
if (twice !== void 0) {
|
|
798
|
+
fail(
|
|
799
|
+
`groups.${String(ids[twice.at])}: ${repr(labels[twice.at])} reads the same as groups.${String(ids[twice.earlier])} \u2014 each group needs its own label`
|
|
800
|
+
);
|
|
801
|
+
}
|
|
802
|
+
(cfg.labeller_knows ?? []).forEach((gid, n3) => {
|
|
803
|
+
if (!has(cfg.groups, gid))
|
|
804
|
+
fail(`labeller_knows[${String(n3)}]: ${repr(gid)} is not in \`groups\``);
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
function checkQuestion(q, qn, earlierIds) {
|
|
808
|
+
if (earlierIds.includes(q.id))
|
|
809
|
+
fail(`questions[${String(qn)}].id: ${repr(q.id)} is repeated`);
|
|
810
|
+
const values = q.options.map((o) => o.value);
|
|
811
|
+
if (new Set(values).size !== values.length) {
|
|
812
|
+
fail(
|
|
813
|
+
`questions[${String(qn)}].options: values must be distinct, got ${repr(values)}`
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
const twice = firstRepeat(q.options.map((o) => o.label));
|
|
817
|
+
if (twice !== void 0) {
|
|
818
|
+
fail(
|
|
819
|
+
`questions[${String(qn)}].options[${String(twice.at)}].label: ${repr(q.options[twice.at]?.label)} reads the same as options[${String(twice.earlier)}].label \u2014 each button needs its own label`
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
function checkPractice(cfg) {
|
|
824
|
+
const offered = Object.fromEntries(
|
|
825
|
+
cfg.questions.map((q) => [q.id, q.options.map((o) => o.value)])
|
|
826
|
+
);
|
|
827
|
+
if ((cfg.practice_round ?? 1) < 1) {
|
|
828
|
+
fail(`practice_round: ${repr(cfg.practice_round)} \u2014 must be 1 or more`);
|
|
829
|
+
}
|
|
830
|
+
cfg.practice.forEach((item, n3) => {
|
|
831
|
+
const where = `practice[${String(n3)}].expected`;
|
|
832
|
+
for (const [q, v] of Object.entries(item.expected)) {
|
|
833
|
+
const values = offered[q];
|
|
834
|
+
if (values === void 0)
|
|
835
|
+
fail(
|
|
836
|
+
`${where}.${q}: not a question of this page (${Object.keys(offered).join(", ")})`
|
|
837
|
+
);
|
|
838
|
+
else if (!values.includes(v))
|
|
839
|
+
fail(`${where}.${q}: ${repr(v)} is not one of ${values.join(", ")}`);
|
|
840
|
+
}
|
|
841
|
+
const missing = Object.keys(offered).find((q) => !has(item.expected, q));
|
|
842
|
+
if (missing !== void 0)
|
|
843
|
+
fail(`${where}.${missing}: every question needs its expected answer`);
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
function checkDistinct(names, where) {
|
|
847
|
+
const at2 = names.findIndex((x, i) => names.indexOf(x) !== i);
|
|
848
|
+
if (at2 !== -1)
|
|
849
|
+
fail(`${where}[${String(at2)}]: ${repr(names[at2])} is repeated`);
|
|
850
|
+
}
|
|
851
|
+
function checkAnswerers(cfg) {
|
|
852
|
+
if ((cfg.min_seconds ?? 1) < 1)
|
|
853
|
+
fail(`min_seconds: ${repr(cfg.min_seconds)} \u2014 must be 1 or more`);
|
|
854
|
+
const all = cfg.answerers;
|
|
855
|
+
if (all !== void 0) checkDistinct(all, "answerers");
|
|
856
|
+
cfg.items.forEach((item, n3) => {
|
|
857
|
+
const where = `items[${String(n3)}].answerers`;
|
|
858
|
+
const own = item.answerers;
|
|
859
|
+
if (own === void 0) return;
|
|
860
|
+
if (all === void 0)
|
|
861
|
+
return fail(
|
|
862
|
+
`${where}: the page has no \`answerers\` \u2014 add the page's list, or take this out`
|
|
863
|
+
);
|
|
864
|
+
const k = own.findIndex((who) => !e2(who, all));
|
|
865
|
+
if (k !== -1)
|
|
866
|
+
fail(
|
|
867
|
+
`${where}[${String(k)}]: ${repr(own[k])} is not in \`answerers\` (${all.join(", ")})`
|
|
868
|
+
);
|
|
869
|
+
checkDistinct(own, where);
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
function checkConfig(cfg) {
|
|
873
|
+
checkGroups(cfg);
|
|
874
|
+
checkAnswerers(cfg);
|
|
875
|
+
if (!asksFamiliarity(cfg) && cfg.familiarity_question !== void 0) {
|
|
876
|
+
fail(
|
|
877
|
+
`familiarity_question: ${repr(cfg.familiarity_question)} is never asked, because \`familiarity\` is not true \u2014 remove it, or set "familiarity": true`
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
cfg.questions.forEach((q, qn) => {
|
|
881
|
+
checkQuestion(
|
|
882
|
+
q,
|
|
883
|
+
qn,
|
|
884
|
+
cfg.questions.slice(0, qn).map((x) => x.id)
|
|
885
|
+
);
|
|
886
|
+
});
|
|
887
|
+
checkPractice(cfg);
|
|
888
|
+
}
|
|
889
|
+
var practiceOf = (cfg) => cfg.practice.map((item, n3) => ({ id: `P${String(n3 + 1)}`, ...item }));
|
|
890
|
+
var linesOf = (cfg) => cfg.items.reduce((byLine, item, n3) => {
|
|
891
|
+
if (item.unit === void 0) return byLine;
|
|
892
|
+
const part = { item, unit: item.unit, n: n3 };
|
|
893
|
+
const seen = byLine.get(item.unit.id);
|
|
894
|
+
return byLine.set(
|
|
895
|
+
item.unit.id,
|
|
896
|
+
seen ? [seen[0], ...seen.slice(1), part] : [part]
|
|
897
|
+
);
|
|
898
|
+
}, /* @__PURE__ */ new Map());
|
|
899
|
+
function checkPart({ item, unit, n: n3 }, parts) {
|
|
900
|
+
const [first] = parts;
|
|
901
|
+
const at2 = `items[${String(n3)}]`;
|
|
902
|
+
if (unit.text !== first.unit.text)
|
|
903
|
+
fail(
|
|
904
|
+
`${at2}.unit.text: ${repr(unit.text)}, but items[${String(first.n)}] of line ${repr(unit.id)} says ${repr(first.unit.text)}`
|
|
905
|
+
);
|
|
906
|
+
if (unit.of !== first.unit.of)
|
|
907
|
+
fail(
|
|
908
|
+
`${at2}.unit.of: ${String(unit.of)}, but items[${String(first.n)}].unit.of says ${String(first.unit.of)} for the same line`
|
|
909
|
+
);
|
|
910
|
+
const earlier = parts.find((p) => p.n < n3 && p.unit.part === unit.part);
|
|
911
|
+
if (earlier !== void 0)
|
|
912
|
+
fail(
|
|
913
|
+
`${at2}.unit.part: ${String(unit.part)} repeats items[${String(earlier.n)}].unit.part`
|
|
914
|
+
);
|
|
915
|
+
if (item.group !== first.item.group)
|
|
916
|
+
fail(
|
|
917
|
+
`${at2}.group: ${repr(item.group)}, but items[${String(first.n)}] of the same line is in ${repr(first.item.group)}`
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
function checkUnit(unit, at2) {
|
|
921
|
+
if (unit === void 0) return;
|
|
922
|
+
if (unit.part < 1 || unit.part > unit.of)
|
|
923
|
+
fail(
|
|
924
|
+
`${at2}.unit.part: ${String(unit.part)} is not between 1 and ${String(unit.of)}`
|
|
925
|
+
);
|
|
926
|
+
if (!unit.text.includes(unit.mark))
|
|
927
|
+
fail(
|
|
928
|
+
`${at2}.unit.mark: ${repr(unit.mark)} is not in the line ${repr(unit.text)} \u2014 mark the requirement with the line's own words`
|
|
929
|
+
);
|
|
930
|
+
}
|
|
931
|
+
function checkUnits(cfg) {
|
|
932
|
+
cardsOf(cfg).forEach(([at2, card]) => {
|
|
933
|
+
checkUnit(card.unit, at2);
|
|
934
|
+
});
|
|
935
|
+
for (const parts of linesOf(cfg).values()) {
|
|
936
|
+
parts.forEach((p) => {
|
|
937
|
+
checkPart(p, parts);
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
var LINE_TOKEN = "{line}";
|
|
942
|
+
var SITUATION_TEXTS = ["summary", "question"];
|
|
943
|
+
var lineOf = (card) => card.excerpt?.lines.filter((l) => l.mark).map((l) => ({ n: l.n, text: l.text.trim() }))[0];
|
|
944
|
+
var usesLine = (card) => SITUATION_TEXTS.some((f) => card.situation?.[f]?.includes(LINE_TOKEN));
|
|
945
|
+
function checkBlocks(cfg) {
|
|
946
|
+
cardsOf(cfg).forEach(([at2, card]) => {
|
|
947
|
+
if (card.excerpt !== void 0 && !card.excerpt.lines.some((l) => l.mark))
|
|
948
|
+
fail(
|
|
949
|
+
`${at2}.excerpt.lines: no line is marked \u2014 mark the line the card is about (\`mark: true\`)`
|
|
950
|
+
);
|
|
951
|
+
const token = SITUATION_TEXTS.find(
|
|
952
|
+
(f) => card.situation?.[f]?.includes(LINE_TOKEN)
|
|
953
|
+
);
|
|
954
|
+
if (token !== void 0 && card.excerpt === void 0)
|
|
955
|
+
fail(
|
|
956
|
+
`${at2}.situation.${token}: \`${LINE_TOKEN}\` names the excerpt's marked line, and the card has no \`excerpt\` \u2014 add it, or write the ${token} without \`${LINE_TOKEN}\``
|
|
957
|
+
);
|
|
958
|
+
const r = card.repeat;
|
|
959
|
+
if (r !== void 0 && (r.index < 1 || r.index > r.of))
|
|
960
|
+
fail(
|
|
961
|
+
`${at2}.repeat.index: ${String(r.index)} is not between 1 and ${String(r.of)}`
|
|
962
|
+
);
|
|
963
|
+
});
|
|
964
|
+
}
|
|
965
|
+
var SOURCE_COLOURS = 5;
|
|
966
|
+
var tagsOf = (at2, card) => t(
|
|
967
|
+
[
|
|
968
|
+
{ field: "quote.from", id: card.quote?.from, over: "text" },
|
|
969
|
+
{
|
|
970
|
+
field: "situation.from",
|
|
971
|
+
id: card.situation?.from,
|
|
972
|
+
over: card.situation?.summary
|
|
973
|
+
},
|
|
974
|
+
{ field: "excerpt.from", id: card.excerpt?.from, over: "box" },
|
|
975
|
+
{
|
|
976
|
+
field: "excerpt.caption_from",
|
|
977
|
+
id: card.excerpt?.caption_from,
|
|
978
|
+
over: card.excerpt?.caption
|
|
979
|
+
},
|
|
980
|
+
{
|
|
981
|
+
field: "excerpt.annotation_from",
|
|
982
|
+
id: card.excerpt?.annotation_from,
|
|
983
|
+
over: card.excerpt?.annotation
|
|
984
|
+
}
|
|
985
|
+
],
|
|
986
|
+
t3(
|
|
987
|
+
({ field, id, over }) => id === void 0 ? [] : [{ at: `${at2}.${field}`, id, over }]
|
|
988
|
+
)
|
|
989
|
+
);
|
|
990
|
+
function checkSources(cfg) {
|
|
991
|
+
const tags = cardsOf(cfg).flatMap(([at2, card]) => tagsOf(at2, card));
|
|
992
|
+
const ids = Object.keys(cfg.sources ?? {});
|
|
993
|
+
if (ids.length > SOURCE_COLOURS)
|
|
994
|
+
fail(
|
|
995
|
+
`sources: ${String(ids.length)} ids, and the page has ${String(SOURCE_COLOURS)} colours for tags \u2014 merge ids, or drop one`
|
|
996
|
+
);
|
|
997
|
+
tags.forEach((t13) => {
|
|
998
|
+
if (cfg.sources === void 0)
|
|
999
|
+
fail(
|
|
1000
|
+
`${t13.at}: names the source ${repr(t13.id)}, and the page has no \`sources\` \u2014 add them, or take the tag out`
|
|
1001
|
+
);
|
|
1002
|
+
else if (!has(cfg.sources, t13.id))
|
|
1003
|
+
fail(`${t13.at}: ${repr(t13.id)} is not in \`sources\` (${ids.join(", ")})`);
|
|
1004
|
+
if (t13.over === void 0)
|
|
1005
|
+
fail(
|
|
1006
|
+
`${t13.at}: tags a text the card does not have \u2014 add it, or take the tag out`
|
|
1007
|
+
);
|
|
1008
|
+
});
|
|
1009
|
+
const dead = ids.find((id) => !tags.some((t13) => t13.id === id));
|
|
1010
|
+
if (dead !== void 0)
|
|
1011
|
+
fail(
|
|
1012
|
+
`sources.${dead}: no item or practice item names it \u2014 a tag nothing carries is a dead entry`
|
|
1013
|
+
);
|
|
1014
|
+
}
|
|
1015
|
+
var termTexts = (card) => [card.quote?.text, card.quote?.source, card.excerpt?.annotation].filter(
|
|
1016
|
+
e
|
|
1017
|
+
);
|
|
1018
|
+
var findsTerm = (card, term) => termTexts(card).some((t13) => t13.split("`").some((part) => part.includes(term)));
|
|
1019
|
+
function checkCardText(cfg) {
|
|
1020
|
+
const options = new Map(
|
|
1021
|
+
cfg.questions.map((q) => [q.id, q.options.map((o) => o.value)])
|
|
1022
|
+
);
|
|
1023
|
+
cardsOf(cfg).forEach(([at2, card]) => {
|
|
1024
|
+
if (card.body === void 0 && card.quote === void 0)
|
|
1025
|
+
fail(
|
|
1026
|
+
`${at2}.body: is required and missing \u2014 a card shows its \`body\`, or its words as a \`quote\``
|
|
1027
|
+
);
|
|
1028
|
+
checkTerms(at2, card);
|
|
1029
|
+
checkAnswerNotes(at2, card, options);
|
|
1030
|
+
});
|
|
1031
|
+
}
|
|
1032
|
+
function checkTermsDoNotOverlap(at2, card) {
|
|
1033
|
+
const texts = (card.terms ?? []).map((t13) => t13.term);
|
|
1034
|
+
const named = (k) => `${at2}.terms[${String(k)}]`;
|
|
1035
|
+
texts.forEach((inner, k) => {
|
|
1036
|
+
const j = texts.findIndex(
|
|
1037
|
+
(outer, m) => m !== k && outer.includes(inner) && (outer !== inner || m > k)
|
|
1038
|
+
);
|
|
1039
|
+
if (j !== -1)
|
|
1040
|
+
fail(
|
|
1041
|
+
`${named(k)}.term: ${repr(inner)} is inside ${named(j)}.term ${repr(texts[j])} \u2014 the page draws one of two overlapping terms, so the other note never opens`
|
|
1042
|
+
);
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
function checkTerms(at2, card) {
|
|
1046
|
+
checkTermsDoNotOverlap(at2, card);
|
|
1047
|
+
(card.terms ?? []).forEach((t13, k) => {
|
|
1048
|
+
const where = `${at2}.terms[${String(k)}]`;
|
|
1049
|
+
if (!findsTerm(card, t13.term))
|
|
1050
|
+
fail(
|
|
1051
|
+
`${where}.term: ${repr(t13.term)} is not in the card's annotation, quote or quote source \u2014 a note opens from words the card shows`
|
|
1052
|
+
);
|
|
1053
|
+
const bare = t13.note.findIndex(
|
|
1054
|
+
(e3) => e3.quote === void 0 && e3.code === void 0
|
|
1055
|
+
);
|
|
1056
|
+
if (bare !== -1)
|
|
1057
|
+
fail(
|
|
1058
|
+
`${where}.note[${String(bare)}]: has neither \`quote\` nor \`code\` \u2014 a note shows its source's own words`
|
|
1059
|
+
);
|
|
1060
|
+
});
|
|
1061
|
+
}
|
|
1062
|
+
function checkAnswerNotes(at2, card, options) {
|
|
1063
|
+
Object.entries(card.answer_notes ?? {}).forEach(([qid, notes]) => {
|
|
1064
|
+
const values = options.get(qid);
|
|
1065
|
+
if (values === void 0)
|
|
1066
|
+
return fail(
|
|
1067
|
+
`${at2}.answer_notes.${qid}: not a question of this page (${[...options.keys()].join(", ")})`
|
|
1068
|
+
);
|
|
1069
|
+
const stray = Object.keys(notes).find((v) => !values.includes(v));
|
|
1070
|
+
if (stray !== void 0)
|
|
1071
|
+
fail(
|
|
1072
|
+
`${at2}.answer_notes.${qid}.${stray}: not one of ${values.join(", ")}`
|
|
1073
|
+
);
|
|
1074
|
+
});
|
|
1075
|
+
}
|
|
1076
|
+
var cardsOf = (cfg) => [
|
|
1077
|
+
...cfg.items.map((i, n3) => [`items[${String(n3)}]`, i]),
|
|
1078
|
+
...cfg.practice.map((p, n3) => [`practice[${String(n3)}]`, p])
|
|
1079
|
+
];
|
|
1080
|
+
function checkItemFields(cfg) {
|
|
1081
|
+
const sids = [];
|
|
1082
|
+
cfg.items.forEach((item, n3) => {
|
|
1083
|
+
const extra = Object.keys(item).filter((f) => !ALLOWED_FIELDS.includes(f)).sort();
|
|
1084
|
+
if (extra.length) {
|
|
1085
|
+
fail(
|
|
1086
|
+
`item #${String(n3)} (${item.source_id}) carries ${extra.join(", ")}, which is not in the allow-list ${repr([...ALLOWED_FIELDS].sort())}. A blind page shows the labeller the item and nothing about the answer or the source. Remove the field from items.json; add it to PAGE_FIELDS only if it is safe for the labeller to see.`
|
|
1087
|
+
);
|
|
1088
|
+
}
|
|
1089
|
+
if (sids.includes(item.source_id))
|
|
1090
|
+
fail(
|
|
1091
|
+
`items[${String(n3)}].source_id: ${repr(item.source_id)} is repeated`
|
|
1092
|
+
);
|
|
1093
|
+
sids.push(item.source_id);
|
|
1094
|
+
if (!has(cfg.groups, item.group))
|
|
1095
|
+
fail(
|
|
1096
|
+
`items[${String(n3)}].group: ${repr(item.group)} is not in \`groups\``
|
|
1097
|
+
);
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1100
|
+
var stringsIn = (v) => typeof v === "string" ? [v] : Array.isArray(v) ? v.flatMap(stringsIn) : typeof v === "object" && v !== null ? Object.values(v).flatMap(stringsIn) : [];
|
|
1101
|
+
var visibleTexts = (item) => Object.entries({
|
|
1102
|
+
...shownFields(item),
|
|
1103
|
+
[RULES_FIELD]: item.rules ?? []
|
|
1104
|
+
}).flatMap(([f, v]) => stringsIn(v).map((t13) => [f, t13]));
|
|
1105
|
+
var escapeRegExp = (s) => s.replace(/[\\^$.*+?()[\]{}|/]/g, "\\$&");
|
|
1106
|
+
function checkNoSourceShown(cfg) {
|
|
1107
|
+
const long = cfg.items.map((i) => i.source_id).filter((sid) => sid.length >= 4);
|
|
1108
|
+
for (const item of cfg.items) {
|
|
1109
|
+
for (const sid of long) {
|
|
1110
|
+
const word = new RegExp(
|
|
1111
|
+
`(?<![\\p{L}\\p{N}_-])${escapeRegExp(sid)}(?![\\p{L}\\p{N}_-])`,
|
|
1112
|
+
"u"
|
|
1113
|
+
);
|
|
1114
|
+
const field = visibleTexts(item).find(([, text2]) => word.test(text2))?.[0];
|
|
1115
|
+
if (field !== void 0) {
|
|
1116
|
+
fail(
|
|
1117
|
+
`the source id ${repr(sid)} appears in the visible \`${field}\` of item ${item.source_id}. Take it out of the text (or use source ids that are not natural words).`
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
var BASE_LANG = "en";
|
|
1124
|
+
var NUMBER_TOKEN = "{n}";
|
|
1125
|
+
function checkPresence(where, shown, given) {
|
|
1126
|
+
if (shown !== void 0 && given === void 0)
|
|
1127
|
+
fail(`${where}: is required and missing \u2014 ${shown}`);
|
|
1128
|
+
if (shown === void 0 && given !== void 0)
|
|
1129
|
+
fail(`${where}: is never shown \u2014 the page has no text it translates`);
|
|
1130
|
+
}
|
|
1131
|
+
function checkSituationWords(cfg, at2, t13) {
|
|
1132
|
+
const cards = cardsOf(cfg).filter(([, c]) => c.situation !== void 0);
|
|
1133
|
+
const first = cards[0];
|
|
1134
|
+
checkPresence(
|
|
1135
|
+
`${at2}.situation_question`,
|
|
1136
|
+
first && `${first[0]}.situation asks the first question in the card's own words`,
|
|
1137
|
+
t13.situation_question
|
|
1138
|
+
);
|
|
1139
|
+
const q = t13.situation_question ?? "";
|
|
1140
|
+
const bare = cards.find(([, c]) => lineOf(c) === void 0);
|
|
1141
|
+
if (bare !== void 0 && [NUMBER_TOKEN, LINE_TOKEN].some((token) => q.includes(token)))
|
|
1142
|
+
fail(
|
|
1143
|
+
`${at2}.situation_question: names the marked line, and ${bare[0]} has no \`excerpt\` \u2014 add it, or write the question without \`${NUMBER_TOKEN}\` and \`${LINE_TOKEN}\``
|
|
1144
|
+
);
|
|
1145
|
+
}
|
|
1146
|
+
var optionIn = (o, words, where) => {
|
|
1147
|
+
const w = words.options[o.value] ?? fail(
|
|
1148
|
+
`${where}.options.${o.value}: is required and missing \u2014 every answer is translated`
|
|
1149
|
+
);
|
|
1150
|
+
checkPresence(
|
|
1151
|
+
`${where}.options.${o.value}.ask_why`,
|
|
1152
|
+
o.ask_why && "this answer asks why",
|
|
1153
|
+
w.ask_why
|
|
1154
|
+
);
|
|
1155
|
+
return {
|
|
1156
|
+
...n(o, ["ask_why"]),
|
|
1157
|
+
label: w.label,
|
|
1158
|
+
example: w.example,
|
|
1159
|
+
...t10({ ask_why: w.ask_why }, e)
|
|
1160
|
+
};
|
|
1161
|
+
};
|
|
1162
|
+
function questionIn(q, t13, at2) {
|
|
1163
|
+
const where = `${at2}.questions.${q.id}`;
|
|
1164
|
+
const words = t13.questions[q.id] ?? fail(
|
|
1165
|
+
`${where}: is required and missing \u2014 every question of the page is translated`
|
|
1166
|
+
);
|
|
1167
|
+
const values = q.options.map((o) => o.value);
|
|
1168
|
+
const stray = Object.keys(words.options).find((v) => !values.includes(v));
|
|
1169
|
+
if (stray !== void 0)
|
|
1170
|
+
fail(`${where}.options.${stray}: not one of ${values.join(", ")}`);
|
|
1171
|
+
const options = q.options.map((o) => optionIn(o, words, where));
|
|
1172
|
+
const twice = firstRepeat(options.map((o) => o.label));
|
|
1173
|
+
if (twice !== void 0)
|
|
1174
|
+
fail(
|
|
1175
|
+
`${where}.options.${String(values[twice.at])}.label: ${repr(options[twice.at]?.label)} reads the same as options.${String(values[twice.earlier])}.label \u2014 each button needs its own label`
|
|
1176
|
+
);
|
|
1177
|
+
return {
|
|
1178
|
+
id: q.id,
|
|
1179
|
+
text: words.text,
|
|
1180
|
+
...t10({ short: words.short }, e),
|
|
1181
|
+
options
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
function checkTranslatedSources(cfg, at2, t13) {
|
|
1185
|
+
const known = Object.keys(cfg.sources ?? {});
|
|
1186
|
+
const stray = Object.keys(t13.sources ?? {}).find((id) => !known.includes(id));
|
|
1187
|
+
if (stray !== void 0)
|
|
1188
|
+
fail(
|
|
1189
|
+
`${at2}.sources.${stray}: not one of the page's \`sources\` (${known.join(", ") || "none"})`
|
|
1190
|
+
);
|
|
1191
|
+
}
|
|
1192
|
+
var optionalWords = (t13) => t10(
|
|
1193
|
+
{ situation_question: t13.situation_question, sources: t13.sources },
|
|
1194
|
+
e
|
|
1195
|
+
);
|
|
1196
|
+
function translationsOf(cfg) {
|
|
1197
|
+
const base = cfg.lang ?? BASE_LANG;
|
|
1198
|
+
return t8(cfg.translations ?? {}, (t13, lang) => {
|
|
1199
|
+
const at2 = `translations.${lang}`;
|
|
1200
|
+
if (lang === base)
|
|
1201
|
+
fail(
|
|
1202
|
+
`${at2}: is the page's own language (\`lang\` is ${repr(base)}) \u2014 translate into another one`
|
|
1203
|
+
);
|
|
1204
|
+
checkPresence(
|
|
1205
|
+
`${at2}.intro`,
|
|
1206
|
+
cfg.intro && "the page has an `intro`",
|
|
1207
|
+
t13.intro
|
|
1208
|
+
);
|
|
1209
|
+
checkSituationWords(cfg, at2, t13);
|
|
1210
|
+
checkTranslatedSources(cfg, at2, t13);
|
|
1211
|
+
const ids = cfg.questions.map((q) => q.id);
|
|
1212
|
+
const stray = Object.keys(t13.questions).find((id) => !ids.includes(id));
|
|
1213
|
+
if (stray !== void 0)
|
|
1214
|
+
fail(
|
|
1215
|
+
`${at2}.questions.${stray}: not a question of this page (${ids.join(", ")})`
|
|
1216
|
+
);
|
|
1217
|
+
return {
|
|
1218
|
+
title: t13.title ?? cfg.title,
|
|
1219
|
+
intro: t13.intro ?? "",
|
|
1220
|
+
labels: { ...DEFAULT_LABELS, ...cfg.labels, ...t13.labels },
|
|
1221
|
+
...optionalWords(t13),
|
|
1222
|
+
questions: cfg.questions.map((q) => questionIn(q, t13, at2))
|
|
1223
|
+
};
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
var languagesOf = (cfg, translated) => cfg.translations === void 0 ? t10({ lang: cfg.lang }, e) : { lang: cfg.lang ?? BASE_LANG, translations: translated };
|
|
1227
|
+
function assertOwnKey(_value) {
|
|
1228
|
+
}
|
|
1229
|
+
var readOldKey = (keyPath, seed) => {
|
|
1230
|
+
if (!existsSync(keyPath)) return void 0;
|
|
1231
|
+
const old = readJson(keyPath);
|
|
1232
|
+
assertOwnKey(old);
|
|
1233
|
+
if (old.seed !== seed) {
|
|
1234
|
+
fail(
|
|
1235
|
+
`${keyPath} was made with seed ${String(old.seed)}; this config says ${String(seed)}. Answers are keyed by the old ids: restore the seed, or start a new study folder.`
|
|
1236
|
+
);
|
|
1237
|
+
}
|
|
1238
|
+
return old;
|
|
1239
|
+
};
|
|
1240
|
+
function assignIds(items, seed, keyPath) {
|
|
1241
|
+
const old = readOldKey(keyPath, seed);
|
|
1242
|
+
const entries = old?.ids ?? {};
|
|
1243
|
+
const known = new Map(
|
|
1244
|
+
Object.entries(entries).map(([nid, v]) => [v.source_id, nid])
|
|
1245
|
+
);
|
|
1246
|
+
const rank = (s) => createHash("sha256").update(`${String(seed)}:${s}`).digest("hex");
|
|
1247
|
+
const fresh = items.map((i) => i.source_id).filter((sid) => !known.has(sid)).sort((a, b) => byText(rank(a), rank(b)));
|
|
1248
|
+
const ids = new Map([
|
|
1249
|
+
...known,
|
|
1250
|
+
...fresh.map((sid, k) => [
|
|
1251
|
+
sid,
|
|
1252
|
+
`G${String(known.size + k + 1).padStart(3, "0")}`
|
|
1253
|
+
])
|
|
1254
|
+
]);
|
|
1255
|
+
return { ids, entries };
|
|
1256
|
+
}
|
|
1257
|
+
var situationShown = (item) => item.situation === void 0 ? {} : {
|
|
1258
|
+
situation: {
|
|
1259
|
+
...t10(
|
|
1260
|
+
{ summary: item.situation.summary, from: item.situation.from },
|
|
1261
|
+
e
|
|
1262
|
+
),
|
|
1263
|
+
question: item.situation.question,
|
|
1264
|
+
...usesLine(item) ? { line: lineOf(item) } : {}
|
|
1265
|
+
}
|
|
1266
|
+
};
|
|
1267
|
+
var shownFields = (item) => ({
|
|
1268
|
+
...Object.fromEntries(
|
|
1269
|
+
PAGE_FIELDS.flatMap((f) => {
|
|
1270
|
+
const v = item[f];
|
|
1271
|
+
return v === void 0 ? [] : [[f, v]];
|
|
1272
|
+
})
|
|
1273
|
+
),
|
|
1274
|
+
...item.unit === void 0 ? {} : {
|
|
1275
|
+
[UNIT_FIELD]: {
|
|
1276
|
+
text: item.unit.text,
|
|
1277
|
+
mark: item.unit.mark,
|
|
1278
|
+
part: item.unit.part,
|
|
1279
|
+
of: item.unit.of
|
|
1280
|
+
}
|
|
1281
|
+
},
|
|
1282
|
+
...item.readings === void 0 ? {} : { [READINGS_FIELD]: item.readings },
|
|
1283
|
+
...Object.fromEntries(
|
|
1284
|
+
BLOCK_FIELDS.filter((f) => f !== "repeat" && f !== "situation").flatMap(
|
|
1285
|
+
(f) => item[f] === void 0 ? [] : [[f, item[f]]]
|
|
1286
|
+
)
|
|
1287
|
+
),
|
|
1288
|
+
...situationShown(item),
|
|
1289
|
+
...item.repeat === void 0 ? {} : { repeat: { index: item.repeat.index, of: item.repeat.of } }
|
|
1290
|
+
});
|
|
1291
|
+
var ruleKey = (r) => `${r.linter} ${r.id}`;
|
|
1292
|
+
function describeRules(cfg, fromDir) {
|
|
1293
|
+
return new Map(
|
|
1294
|
+
cardsOf(cfg).flatMap(
|
|
1295
|
+
([at2, card]) => (card.rules ?? []).flatMap(
|
|
1296
|
+
(r, k) => {
|
|
1297
|
+
const text2 = describeRule(r, fromDir);
|
|
1298
|
+
return text2 === void 0 ? fail(
|
|
1299
|
+
`${at2}.rules[${String(k)}]: ${r.linter} ${repr(r.id)} has no description from the linter (${r.linter === "ruff" ? "`ruff rule` does not know it, or ruff is not on PATH" : "not a rule of the ESLint, or of a plugin, installed where items.json is"}) \u2014 check the id`
|
|
1300
|
+
) : [[ruleKey(r), text2]];
|
|
1301
|
+
}
|
|
1302
|
+
)
|
|
1303
|
+
)
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
var rulesShown = (card, described) => card.rules === void 0 ? {} : {
|
|
1307
|
+
[RULES_FIELD]: card.rules.map((r) => ({
|
|
1308
|
+
linter: r.linter,
|
|
1309
|
+
id: r.id,
|
|
1310
|
+
...described.get(ruleKey(r))
|
|
1311
|
+
}))
|
|
1312
|
+
};
|
|
1313
|
+
function itemsOnPage(cfg, keyPath) {
|
|
1314
|
+
const knows = cfg.labeller_knows;
|
|
1315
|
+
const items = cfg.items.filter(
|
|
1316
|
+
(i) => knows === void 0 || knows.includes(i.group)
|
|
1317
|
+
);
|
|
1318
|
+
if (!items.length) fail("no item is left after `labeller_knows`");
|
|
1319
|
+
const { ids, entries } = assignIds(items, cfg.seed, keyPath);
|
|
1320
|
+
const idOf = (i) => String(ids.get(i.source_id));
|
|
1321
|
+
const sorted = [...items].sort((a, b) => byText(idOf(a), idOf(b)));
|
|
1322
|
+
return { items: sorted, ids, entries, idOf };
|
|
1323
|
+
}
|
|
1324
|
+
var practiceRound = (cfg) => cfg.practice_round ?? 1;
|
|
1325
|
+
var practiceShown = (cfg, described) => practiceOf(cfg).map((p) => ({
|
|
1326
|
+
id: p.id,
|
|
1327
|
+
expected: p.expected,
|
|
1328
|
+
why: p.why,
|
|
1329
|
+
...shownFields(p),
|
|
1330
|
+
...rulesShown(p, described)
|
|
1331
|
+
}));
|
|
1332
|
+
var DEFAULT_LABELS = {
|
|
1333
|
+
place: "{id} \xB7 {n} of {total}",
|
|
1334
|
+
line: "Line",
|
|
1335
|
+
line_part: "{line} \xB7 part {part} of {of}",
|
|
1336
|
+
repeat: "Card {index} of {of}",
|
|
1337
|
+
practice_example: "",
|
|
1338
|
+
hidden: "lines {from}\u2013{to}",
|
|
1339
|
+
hidden_one: "line {from}",
|
|
1340
|
+
wait: "\u23F3 You can answer in {s} s",
|
|
1341
|
+
how_to: "\u{1F4D6} How to answer",
|
|
1342
|
+
for_example: "For example: "
|
|
1343
|
+
};
|
|
1344
|
+
var sourcesShown = (cfg) => cfg.sources === void 0 ? {} : {
|
|
1345
|
+
sources: Object.entries(cfg.sources).map(([id, label]) => ({
|
|
1346
|
+
id,
|
|
1347
|
+
label
|
|
1348
|
+
}))
|
|
1349
|
+
};
|
|
1350
|
+
var optionalKeys = (cfg) => t10(
|
|
1351
|
+
{
|
|
1352
|
+
answerers: cfg.answerers,
|
|
1353
|
+
min_seconds: cfg.min_seconds,
|
|
1354
|
+
practice_gate: cfg.practice_gate
|
|
1355
|
+
},
|
|
1356
|
+
e
|
|
1357
|
+
);
|
|
1358
|
+
function payloadOf(cfg, page, described, translated) {
|
|
1359
|
+
const used = Object.keys(cfg.groups).filter(
|
|
1360
|
+
(g) => page.items.some((i) => i.group === g)
|
|
1361
|
+
);
|
|
1362
|
+
return {
|
|
1363
|
+
title: cfg.title,
|
|
1364
|
+
intro: cfg.intro ?? "",
|
|
1365
|
+
labels: { ...DEFAULT_LABELS, ...cfg.labels },
|
|
1366
|
+
...sourcesShown(cfg),
|
|
1367
|
+
groups: used.map((g) => ({ id: g, label: String(cfg.groups[g]) })),
|
|
1368
|
+
familiarity_question: asksFamiliarity(cfg) ? cfg.familiarity_question ?? "How well do you know {group}?" : "",
|
|
1369
|
+
familiarity: asksFamiliarity(cfg) ? FAMILIARITY : [],
|
|
1370
|
+
allowed: allowedValues(cfg.questions, asksFamiliarity(cfg)),
|
|
1371
|
+
...optionalKeys(cfg),
|
|
1372
|
+
...languagesOf(cfg, translated),
|
|
1373
|
+
questions: cfg.questions,
|
|
1374
|
+
practice: {
|
|
1375
|
+
round: practiceRound(cfg),
|
|
1376
|
+
items: practiceShown(cfg, described)
|
|
1377
|
+
},
|
|
1378
|
+
items: page.items.map((i) => itemShown(i, page.idOf(i), described))
|
|
1379
|
+
};
|
|
1380
|
+
}
|
|
1381
|
+
var itemShown = (i, id, described) => ({
|
|
1382
|
+
id,
|
|
1383
|
+
...shownFields(i),
|
|
1384
|
+
...rulesShown(i, described),
|
|
1385
|
+
...t10({ answerers: i.answerers }, e)
|
|
1386
|
+
});
|
|
1387
|
+
function idsOf(cfg, page) {
|
|
1388
|
+
const shown = new Set(page.items.map(page.idOf));
|
|
1389
|
+
const groupOf = new Map([
|
|
1390
|
+
...Object.values(page.entries).map((v) => [
|
|
1391
|
+
v.source_id,
|
|
1392
|
+
v.group
|
|
1393
|
+
]),
|
|
1394
|
+
...cfg.items.map((i) => [i.source_id, i.group])
|
|
1395
|
+
]);
|
|
1396
|
+
const bySource = t6(cfg.items, (i) => i.source_id);
|
|
1397
|
+
const byId = [...page.ids].sort(([, a], [, b]) => byText(a, b));
|
|
1398
|
+
return t11(
|
|
1399
|
+
byId,
|
|
1400
|
+
([, nid]) => nid,
|
|
1401
|
+
([sid, nid]) => ({
|
|
1402
|
+
source_id: sid,
|
|
1403
|
+
group: String(groupOf.get(sid)),
|
|
1404
|
+
on_page: shown.has(nid),
|
|
1405
|
+
...t10({ answerers: bySource[sid]?.answerers }, e)
|
|
1406
|
+
})
|
|
1407
|
+
);
|
|
1408
|
+
}
|
|
1409
|
+
var roundsIn = (p) => p.rounds ?? { [String(p.round)]: p.expected };
|
|
1410
|
+
var roundsOf = (keyPath) => {
|
|
1411
|
+
if (!existsSync(keyPath)) return {};
|
|
1412
|
+
const old = readJson(keyPath);
|
|
1413
|
+
assertKey(old);
|
|
1414
|
+
return old.practice === void 0 ? {} : roundsIn(old.practice);
|
|
1415
|
+
};
|
|
1416
|
+
function keyOf(cfg, page, earlier) {
|
|
1417
|
+
const practice = practiceOf(cfg);
|
|
1418
|
+
return {
|
|
1419
|
+
study: cfg.study,
|
|
1420
|
+
seed: cfg.seed,
|
|
1421
|
+
...optionalKeys(cfg),
|
|
1422
|
+
questions: cfg.questions.map((q) => q.id),
|
|
1423
|
+
// What counts as an answer: the same lists the page was given (`allowedValues`). `export`
|
|
1424
|
+
// checks every stored answer and familiarity against them, so the build's options decide.
|
|
1425
|
+
allowed: allowedValues(cfg.questions, asksFamiliarity(cfg)),
|
|
1426
|
+
// The practice is scored against this at export; its answers never go into the labels.
|
|
1427
|
+
practice: {
|
|
1428
|
+
round: practiceRound(cfg),
|
|
1429
|
+
expected: Object.fromEntries(practice.map((p) => [p.id, p.expected])),
|
|
1430
|
+
// Every round's items and expected answers, kept across rebuilds: an earlier round's stored
|
|
1431
|
+
// answers stay known to the export (a later round may have fewer items) and are scored too.
|
|
1432
|
+
rounds: {
|
|
1433
|
+
...earlier,
|
|
1434
|
+
[String(practiceRound(cfg))]: Object.fromEntries(
|
|
1435
|
+
practice.map((p) => [p.id, p.expected])
|
|
1436
|
+
)
|
|
1437
|
+
}
|
|
1438
|
+
},
|
|
1439
|
+
ids: idsOf(cfg, page)
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
1442
|
+
function prepareBuild(configPath, outDir) {
|
|
1443
|
+
const cfg = withPreset(readJson(configPath));
|
|
1444
|
+
assertConfig(cfg);
|
|
1445
|
+
checkValue(cfg, configSchema(cfg.kind), "");
|
|
1446
|
+
checkConfig(cfg);
|
|
1447
|
+
checkItemFields(cfg);
|
|
1448
|
+
checkUnits(cfg);
|
|
1449
|
+
checkBlocks(cfg);
|
|
1450
|
+
checkCardText(cfg);
|
|
1451
|
+
checkSources(cfg);
|
|
1452
|
+
checkNoSourceShown(cfg);
|
|
1453
|
+
const translated = translationsOf(cfg);
|
|
1454
|
+
const described = describeRules(cfg, dirname(configPath));
|
|
1455
|
+
mkdirSync(outDir, { recursive: true });
|
|
1456
|
+
const keyPath = join(outDir, "key.json");
|
|
1457
|
+
const earlier = roundsOf(keyPath);
|
|
1458
|
+
const page = itemsOnPage(cfg, keyPath);
|
|
1459
|
+
writeJson(keyPath, keyOf(cfg, page, earlier));
|
|
1460
|
+
return {
|
|
1461
|
+
payload: payloadOf(cfg, page, described, translated),
|
|
1462
|
+
dropped: cfg.items.length - page.items.length
|
|
1463
|
+
};
|
|
1464
|
+
}
|
|
1465
|
+
function assertKey(_value) {
|
|
1466
|
+
}
|
|
1467
|
+
var ANSWER_DOC_FIELDS = ["answers", "reasons", "savedAt", "source"];
|
|
1468
|
+
var answerDocFields = (key) => key.min_seconds === void 0 ? ANSWER_DOC_FIELDS : ["answers", "reasons", "savedAt", "seconds", "source"];
|
|
1469
|
+
var GROUP_DOC_FIELDS = ["familiarity", "savedAt", "source"];
|
|
1470
|
+
var DOC_FIELD_TYPES = [
|
|
1471
|
+
["answers", isObject, "an object {question: value}"],
|
|
1472
|
+
["reasons", isObject, "an object {question: text}"],
|
|
1473
|
+
[
|
|
1474
|
+
"familiarity",
|
|
1475
|
+
(v) => v === null || typeof v === "string",
|
|
1476
|
+
"a familiarity value (a string) or null"
|
|
1477
|
+
],
|
|
1478
|
+
[
|
|
1479
|
+
"savedAt",
|
|
1480
|
+
(v) => v === null || typeof v === "string",
|
|
1481
|
+
"a timestamp string or null"
|
|
1482
|
+
],
|
|
1483
|
+
[
|
|
1484
|
+
"seconds",
|
|
1485
|
+
(v) => v === null || typeof v === "number" && Number.isInteger(v) && v >= 0,
|
|
1486
|
+
"a whole number of seconds or null"
|
|
1487
|
+
]
|
|
1488
|
+
];
|
|
1489
|
+
var COLLECTIONS = ["answers", "groups", "practice"];
|
|
1490
|
+
var COLLECTION_NAMES = COLLECTIONS;
|
|
1491
|
+
var NOUN = {
|
|
1492
|
+
answers: "answer",
|
|
1493
|
+
groups: "group",
|
|
1494
|
+
practice: "practice item"
|
|
1495
|
+
};
|
|
1496
|
+
var practiceDocs = (key) => {
|
|
1497
|
+
const p = key.practice;
|
|
1498
|
+
if (p === void 0) return [];
|
|
1499
|
+
return Object.entries(roundsIn(p)).flatMap(
|
|
1500
|
+
([r, expected]) => Object.entries(expected).map(
|
|
1501
|
+
([pid, answers]) => [`r${r}-${pid}`, Object.keys(answers)]
|
|
1502
|
+
)
|
|
1503
|
+
);
|
|
1504
|
+
};
|
|
1505
|
+
var shownTo = (all, meta) => meta.answerers ?? all;
|
|
1506
|
+
function knownNames(key, whose) {
|
|
1507
|
+
const entries = Object.entries(key.ids).filter(
|
|
1508
|
+
([, m]) => whose.who === void 0 || shownTo(whose.all, m).includes(whose.who)
|
|
1509
|
+
);
|
|
1510
|
+
const shown = entries.filter(([, m]) => m.on_page);
|
|
1511
|
+
const practice = practiceDocs(key);
|
|
1512
|
+
return {
|
|
1513
|
+
answers: {
|
|
1514
|
+
known: entries.map(([nid]) => [nid, key.questions]),
|
|
1515
|
+
onPage: shown.map(([nid]) => nid),
|
|
1516
|
+
fields: answerDocFields(key)
|
|
1517
|
+
},
|
|
1518
|
+
groups: {
|
|
1519
|
+
known: entries.map(([, m]) => [m.group, key.questions]),
|
|
1520
|
+
onPage: shown.map(([, m]) => m.group),
|
|
1521
|
+
fields: GROUP_DOC_FIELDS
|
|
1522
|
+
},
|
|
1523
|
+
practice: {
|
|
1524
|
+
known: practice,
|
|
1525
|
+
onPage: practice.map(([n3]) => n3),
|
|
1526
|
+
fields: answerDocFields(key)
|
|
1527
|
+
}
|
|
1528
|
+
};
|
|
1529
|
+
}
|
|
1530
|
+
var sortedEntries = (o) => Object.entries(o).sort(([a], [b]) => byText(a, b));
|
|
1531
|
+
function checkDumpShape(dump) {
|
|
1532
|
+
if (!isObject(dump))
|
|
1533
|
+
return fail(
|
|
1534
|
+
`store: ${typeName(dump)} \u2014 must be an object {answers, groups, practice}`
|
|
1535
|
+
);
|
|
1536
|
+
for (const kind of COLLECTIONS) {
|
|
1537
|
+
const c = dump[kind];
|
|
1538
|
+
if (c !== void 0 && !isObject(c))
|
|
1539
|
+
fail(`${kind}: ${typeName(c)} \u2014 must be an object {id: stored document}`);
|
|
1540
|
+
}
|
|
1541
|
+
const stray = Object.keys(dump).sort().find((top) => !COLLECTION_NAMES.includes(top));
|
|
1542
|
+
if (stray !== void 0)
|
|
1543
|
+
fail(
|
|
1544
|
+
`${stray}: the store has no such collection (expected ${COLLECTIONS.join(", ")})`
|
|
1545
|
+
);
|
|
1546
|
+
}
|
|
1547
|
+
function answererOf(where, docId, key) {
|
|
1548
|
+
const all = key.answerers;
|
|
1549
|
+
if (all === void 0) return { name: docId };
|
|
1550
|
+
const at2 = docId.indexOf(SEP);
|
|
1551
|
+
if (at2 === -1)
|
|
1552
|
+
return fail(
|
|
1553
|
+
`${where}: this page has answerers (${all.join(", ")}), so a document's id is <answerer>${SEP}<id>`
|
|
1554
|
+
);
|
|
1555
|
+
const who = docId.slice(0, at2);
|
|
1556
|
+
if (!all.includes(who))
|
|
1557
|
+
fail(
|
|
1558
|
+
`${where}: ${repr(who)} is not an answerer of this page (${all.join(", ")})`
|
|
1559
|
+
);
|
|
1560
|
+
return { who, all, name: docId.slice(at2 + 1) };
|
|
1561
|
+
}
|
|
1562
|
+
function checkDocName(where, { who, name }, kind, names) {
|
|
1563
|
+
const hit = names.known.find(([n3]) => n3 === name);
|
|
1564
|
+
if (hit === void 0) {
|
|
1565
|
+
return fail(
|
|
1566
|
+
`${where}: the key does not know this ${NOUN[kind]}${who === void 0 ? "" : ` for ${who}`} (${n2(
|
|
1567
|
+
names.known.map(([n3]) => n3)
|
|
1568
|
+
).sort().join(", ")})`
|
|
1569
|
+
);
|
|
1570
|
+
}
|
|
1571
|
+
if (!names.onPage.includes(name)) {
|
|
1572
|
+
fail(
|
|
1573
|
+
`${where}: stored, but no longer on the page (a rebuild left it out), so the export would drop it; rebuild with it on the page, or take it out of the dump on purpose`
|
|
1574
|
+
);
|
|
1575
|
+
}
|
|
1576
|
+
return hit[1];
|
|
1577
|
+
}
|
|
1578
|
+
function checkDocFields(where, doc, kind, fields) {
|
|
1579
|
+
if (!isObject(doc))
|
|
1580
|
+
return fail(`${where}: must be an object with ${fields.join(", ")}`);
|
|
1581
|
+
const stray = Object.keys(doc).sort().find((f) => !fields.includes(f));
|
|
1582
|
+
if (stray !== void 0)
|
|
1583
|
+
fail(
|
|
1584
|
+
`${where}/${stray}: not a field of a stored ${NOUN[kind]} (${fields.join(", ")})`
|
|
1585
|
+
);
|
|
1586
|
+
if (!has(doc, "source"))
|
|
1587
|
+
fail(
|
|
1588
|
+
`${where}: no source; add "source": "chat" or "page" (one of ${SOURCES_SHOWN})`
|
|
1589
|
+
);
|
|
1590
|
+
const source = doc["source"];
|
|
1591
|
+
if (typeof source !== "string" || !SOURCES.includes(source)) {
|
|
1592
|
+
fail(`${where}/source: ${repr(source)}; expected one of ${SOURCES_SHOWN}`);
|
|
1593
|
+
}
|
|
1594
|
+
for (const [field, ok, what] of DOC_FIELD_TYPES) {
|
|
1595
|
+
const v = doc[field];
|
|
1596
|
+
if (v !== void 0 && !ok(v))
|
|
1597
|
+
fail(`${where}/${field}: ${repr(v)} \u2014 must be ${what}`);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
function checkDocAnswers(where, doc, questions) {
|
|
1601
|
+
const known = (part, q) => {
|
|
1602
|
+
if (!questions.includes(q))
|
|
1603
|
+
fail(
|
|
1604
|
+
`${where}/${part}/${q}: not a question of the key (${questions.join(", ")})`
|
|
1605
|
+
);
|
|
1606
|
+
};
|
|
1607
|
+
for (const [q, v] of sortedEntries(
|
|
1608
|
+
isObject(doc["answers"]) ? doc["answers"] : {}
|
|
1609
|
+
)) {
|
|
1610
|
+
known("answers", q);
|
|
1611
|
+
if (v !== null && typeof v !== "string")
|
|
1612
|
+
fail(
|
|
1613
|
+
`${where}/answers/${q}: ${repr(v)} \u2014 must be an option value (a string) or null`
|
|
1614
|
+
);
|
|
1615
|
+
}
|
|
1616
|
+
for (const [q, v] of sortedEntries(
|
|
1617
|
+
isObject(doc["reasons"]) ? doc["reasons"] : {}
|
|
1618
|
+
)) {
|
|
1619
|
+
known("reasons", q);
|
|
1620
|
+
if (typeof v !== "string")
|
|
1621
|
+
fail(`${where}/reasons/${q}: ${repr(v)} \u2014 must be text`);
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
function checkDump(dump, key) {
|
|
1625
|
+
checkDumpShape(dump);
|
|
1626
|
+
const plain = knownNames(key, {});
|
|
1627
|
+
for (const kind of COLLECTIONS) {
|
|
1628
|
+
const collection = dump[kind];
|
|
1629
|
+
for (const [docId, doc] of sortedEntries(
|
|
1630
|
+
isObject(collection) ? collection : {}
|
|
1631
|
+
)) {
|
|
1632
|
+
const where = `${kind}/${docId}`;
|
|
1633
|
+
const named = answererOf(where, docId, key);
|
|
1634
|
+
const names = (named.who === void 0 ? plain : knownNames(key, named))[kind];
|
|
1635
|
+
const questions = checkDocName(where, named, kind, names);
|
|
1636
|
+
checkDocFields(where, doc, kind, names.fields);
|
|
1637
|
+
checkDocAnswers(where, doc, questions);
|
|
1638
|
+
}
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
function checkNoFamiliarity(dump, allowed) {
|
|
1642
|
+
const groups = isObject(dump["groups"]) ? dump["groups"] : {};
|
|
1643
|
+
const [first] = Object.keys(groups).sort(byText);
|
|
1644
|
+
if (allowed.familiarity.length === 0 && first !== void 0) {
|
|
1645
|
+
fail(
|
|
1646
|
+
`groups/${first}: familiarity is off for this page (items.json does not set \`"familiarity": true\`), so no group document is expected \u2014 take it out of the dump`
|
|
1647
|
+
);
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
function assertAllowed(a, key, keyPath) {
|
|
1651
|
+
const questions = isObject(a) ? a["questions"] : void 0;
|
|
1652
|
+
const complete = isObject(a) && has(a, "familiarity") && isObject(questions) && key.questions.every((q) => has(questions, q));
|
|
1653
|
+
if (!complete) {
|
|
1654
|
+
fail(
|
|
1655
|
+
`${keyPath} has no \`allowed\` values for every question and for familiarity (made by an older build). Run \`build\` again with the same items.json and seed: ids are kept, the key gains them.`
|
|
1656
|
+
);
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
var docOf = (collection, name) => {
|
|
1660
|
+
const doc = isObject(collection) ? collection[name] : void 0;
|
|
1661
|
+
return isObject(doc) ? doc : {};
|
|
1662
|
+
};
|
|
1663
|
+
var valuesOf = (lists, q) => Object.entries(lists).filter(([k]) => k === q).flatMap(([, vs]) => vs);
|
|
1664
|
+
var listed = (lists, q, v) => valuesOf(lists, q).includes(v);
|
|
1665
|
+
var textOf = (v) => typeof v === "string" ? v : null;
|
|
1666
|
+
var textsOf = (o) => Object.fromEntries(
|
|
1667
|
+
Object.entries(isObject(o) ? o : {}).filter(
|
|
1668
|
+
(e3) => typeof e3[1] === "string"
|
|
1669
|
+
)
|
|
1670
|
+
);
|
|
1671
|
+
function answersOf(doc, ctx) {
|
|
1672
|
+
const stored = textsOf(doc["answers"]);
|
|
1673
|
+
const raw = ctx.key.questions.map((q) => [
|
|
1674
|
+
q,
|
|
1675
|
+
stored[q] ?? null
|
|
1676
|
+
]);
|
|
1677
|
+
const invalid = Object.fromEntries(
|
|
1678
|
+
raw.filter(([q, v]) => v !== null && !listed(ctx.allowed.questions, q, v))
|
|
1679
|
+
);
|
|
1680
|
+
const given = Object.fromEntries(
|
|
1681
|
+
raw.map(([q, v]) => [q, has(invalid, q) ? null : v])
|
|
1682
|
+
);
|
|
1683
|
+
return { given, invalid };
|
|
1684
|
+
}
|
|
1685
|
+
function answerOf(doc, ctx) {
|
|
1686
|
+
const { given, invalid } = answersOf(doc, ctx);
|
|
1687
|
+
const reasons = Object.fromEntries(
|
|
1688
|
+
Object.entries(textsOf(doc["reasons"])).filter(([, r]) => r.trim())
|
|
1689
|
+
);
|
|
1690
|
+
const needsWhy = ctx.allowed.needs_why ?? {};
|
|
1691
|
+
const unexplained = Object.entries(given).filter(
|
|
1692
|
+
([q, v]) => v !== null && listed(needsWhy, q, v) && !has(reasons, q)
|
|
1693
|
+
).map(([q]) => q);
|
|
1694
|
+
return {
|
|
1695
|
+
answers: given,
|
|
1696
|
+
invalid_answers: invalid,
|
|
1697
|
+
reasons,
|
|
1698
|
+
unexplained,
|
|
1699
|
+
complete: Object.values(given).every((v) => v !== null) && !unexplained.length,
|
|
1700
|
+
source: textOf(doc["source"]),
|
|
1701
|
+
saved_at: doc["savedAt"] ?? null,
|
|
1702
|
+
// Only a page built with `min_seconds` stores how long the item was on screen.
|
|
1703
|
+
...t9({ seconds: doc["seconds"] ?? null }, secondsOf(ctx.key))
|
|
1704
|
+
};
|
|
1705
|
+
}
|
|
1706
|
+
var secondsOf = (key) => key.min_seconds === void 0 ? [] : ["seconds"];
|
|
1707
|
+
var labelOf = (nid, meta, doc, ctx) => ({
|
|
1708
|
+
id: nid,
|
|
1709
|
+
source_id: meta.source_id,
|
|
1710
|
+
group: meta.group,
|
|
1711
|
+
...answerOf(doc, ctx)
|
|
1712
|
+
});
|
|
1713
|
+
function groupsOf(groupIds, groups, allowed, prefix = "") {
|
|
1714
|
+
return Object.fromEntries(
|
|
1715
|
+
groupIds.map((g) => {
|
|
1716
|
+
const doc = docOf(groups, `${prefix}${g}`);
|
|
1717
|
+
const fam = textOf(doc["familiarity"]);
|
|
1718
|
+
const bad = fam !== null && !allowed.familiarity.includes(fam);
|
|
1719
|
+
return [
|
|
1720
|
+
g,
|
|
1721
|
+
{
|
|
1722
|
+
familiarity: bad ? null : fam,
|
|
1723
|
+
invalid_familiarity: bad ? fam : null,
|
|
1724
|
+
source: textOf(doc["source"]),
|
|
1725
|
+
saved_at: doc["savedAt"] ?? null
|
|
1726
|
+
}
|
|
1727
|
+
];
|
|
1728
|
+
})
|
|
1729
|
+
);
|
|
1730
|
+
}
|
|
1731
|
+
function reportLabels(items, allowed) {
|
|
1732
|
+
for (const i of items) {
|
|
1733
|
+
for (const [q, v] of Object.entries(i.invalid_answers)) {
|
|
1734
|
+
say(
|
|
1735
|
+
` invalid ${i.id}: ${q}=${repr(v)} is not one of ${valuesOf(allowed.questions, q).join(", ")} (not counted)`
|
|
1736
|
+
);
|
|
1737
|
+
}
|
|
1738
|
+
const missing = Object.entries(i.answers).filter(([q, v]) => v === null && !has(i.invalid_answers, q)).map(([q]) => q);
|
|
1739
|
+
if (missing.length)
|
|
1740
|
+
say(` incomplete ${i.id}: missing ${missing.join(", ")}`);
|
|
1741
|
+
for (const q of i.unexplained)
|
|
1742
|
+
say(` incomplete ${i.id}: ${q}=${String(i.answers[q])} needs a reason`);
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
function reportGroups(groups, allowed) {
|
|
1746
|
+
for (const [g, v] of Object.entries(groups)) {
|
|
1747
|
+
if (v.invalid_familiarity !== null) {
|
|
1748
|
+
say(
|
|
1749
|
+
` invalid group ${g}: familiarity=${repr(v.invalid_familiarity)} is not one of ${allowed.familiarity.join(", ")} (not counted as rated)`
|
|
1750
|
+
);
|
|
1751
|
+
} else if (v.familiarity === null)
|
|
1752
|
+
say(` group ${g}: no familiarity answer`);
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
function scorePractice(dump, round, all, prefix) {
|
|
1756
|
+
return Object.entries(all).map(([pid, expected]) => {
|
|
1757
|
+
const doc = docOf(dump["practice"], `${prefix}r${round}-${pid}`);
|
|
1758
|
+
const answers = isObject(doc["answers"]) ? doc["answers"] : {};
|
|
1759
|
+
const matched = Object.entries(expected).every(
|
|
1760
|
+
([q, v]) => answers[q] === v
|
|
1761
|
+
);
|
|
1762
|
+
return {
|
|
1763
|
+
id: pid,
|
|
1764
|
+
answers,
|
|
1765
|
+
expected,
|
|
1766
|
+
matched,
|
|
1767
|
+
savedAt: doc["savedAt"] ?? null
|
|
1768
|
+
};
|
|
1769
|
+
});
|
|
1770
|
+
}
|
|
1771
|
+
var practiceFile = (study, round, day, who) => `${study}-practice-r${String(round)}${who === void 0 ? "" : `-${who}`}-${day}.json`;
|
|
1772
|
+
function exportPractice(dump, key, target, day) {
|
|
1773
|
+
const p = key.practice;
|
|
1774
|
+
if (p === void 0) return;
|
|
1775
|
+
const rounds = t12(t2(roundsIn(p)), ([r]) => Number(r));
|
|
1776
|
+
(key.answerers ?? [void 0]).forEach((who) => {
|
|
1777
|
+
rounds.forEach(([round, expected]) => {
|
|
1778
|
+
exportRound(
|
|
1779
|
+
key.study,
|
|
1780
|
+
Number(round),
|
|
1781
|
+
scorePractice(
|
|
1782
|
+
dump,
|
|
1783
|
+
round,
|
|
1784
|
+
expected,
|
|
1785
|
+
who === void 0 ? "" : `${who}${SEP}`
|
|
1786
|
+
),
|
|
1787
|
+
{ target, day, current: Number(round) === p.round, who }
|
|
1788
|
+
);
|
|
1789
|
+
});
|
|
1790
|
+
});
|
|
1791
|
+
}
|
|
1792
|
+
function exportRound(study, round, items, at2) {
|
|
1793
|
+
const named = `practice round ${String(round)}${at2.who === void 0 ? "" : ` (${at2.who})`}`;
|
|
1794
|
+
if (!items.some((i) => Object.keys(i.answers).length)) {
|
|
1795
|
+
if (at2.current) say(`${named}: not answered`);
|
|
1796
|
+
return;
|
|
1797
|
+
}
|
|
1798
|
+
const missed = items.filter((i) => !i.matched).map((i) => i.id);
|
|
1799
|
+
const matched = items.length - missed.length;
|
|
1800
|
+
const path = join(at2.target, practiceFile(study, round, at2.day, at2.who));
|
|
1801
|
+
writeJson(path, {
|
|
1802
|
+
study,
|
|
1803
|
+
round,
|
|
1804
|
+
...t10({ answerer: at2.who }, e),
|
|
1805
|
+
exported: at2.day,
|
|
1806
|
+
matched,
|
|
1807
|
+
total: items.length,
|
|
1808
|
+
items
|
|
1809
|
+
});
|
|
1810
|
+
say(
|
|
1811
|
+
`${named}: ${String(matched)} of ${String(items.length)} matched${missed.length ? `; missed ${missed.join(", ")}` : ""} -> ${path}`
|
|
1812
|
+
);
|
|
1813
|
+
}
|
|
1814
|
+
var tally = (items, groups) => ({
|
|
1815
|
+
groups,
|
|
1816
|
+
complete_items: items.filter((i) => i.complete).length,
|
|
1817
|
+
invalid_items: items.filter((i) => Object.keys(i.invalid_answers).length).length,
|
|
1818
|
+
invalid_groups: Object.values(groups).filter(
|
|
1819
|
+
(g) => g.invalid_familiarity !== null
|
|
1820
|
+
).length,
|
|
1821
|
+
total_items: items.length
|
|
1822
|
+
});
|
|
1823
|
+
var groupsFor = (onPage, dump, ctx, prefix = "") => ctx.allowed.familiarity.length ? groupsOf(
|
|
1824
|
+
n2(onPage.map(([, m]) => m.group)),
|
|
1825
|
+
dump["groups"],
|
|
1826
|
+
ctx.allowed,
|
|
1827
|
+
prefix
|
|
1828
|
+
) : {};
|
|
1829
|
+
function labelsOf(dump, ctx, day) {
|
|
1830
|
+
const onPage = Object.entries(ctx.key.ids).filter(([, m]) => m.on_page);
|
|
1831
|
+
const items = onPage.map(
|
|
1832
|
+
([nid, meta]) => labelOf(nid, meta, docOf(dump["answers"], nid), ctx)
|
|
1833
|
+
);
|
|
1834
|
+
return {
|
|
1835
|
+
study: ctx.key.study,
|
|
1836
|
+
exported: day,
|
|
1837
|
+
questions: ctx.key.questions,
|
|
1838
|
+
items,
|
|
1839
|
+
...tally(items, groupsFor(onPage, dump, ctx))
|
|
1840
|
+
};
|
|
1841
|
+
}
|
|
1842
|
+
function answererLabelsOf(dump, ctx, day, all) {
|
|
1843
|
+
const onPage = Object.entries(ctx.key.ids).filter(([, m]) => m.on_page);
|
|
1844
|
+
const docFor = (who, nid) => answerOf(docOf(dump["answers"], `${who}${SEP}${nid}`), ctx);
|
|
1845
|
+
const items = onPage.map(([nid, meta]) => ({
|
|
1846
|
+
id: nid,
|
|
1847
|
+
source_id: meta.source_id,
|
|
1848
|
+
group: meta.group,
|
|
1849
|
+
answerers: shownTo(all, meta),
|
|
1850
|
+
by_answerer: t5(shownTo(all, meta), (who) => docFor(who, nid))
|
|
1851
|
+
}));
|
|
1852
|
+
return {
|
|
1853
|
+
study: ctx.key.study,
|
|
1854
|
+
exported: day,
|
|
1855
|
+
questions: ctx.key.questions,
|
|
1856
|
+
answerers: all,
|
|
1857
|
+
items,
|
|
1858
|
+
by_answerer: t5(
|
|
1859
|
+
all,
|
|
1860
|
+
(who) => tallyOf({ who, all }, onPage, dump, ctx)
|
|
1861
|
+
)
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
function tallyOf({ who, all }, onPage, dump, ctx) {
|
|
1865
|
+
const mine = onPage.filter(([, m]) => shownTo(all, m).includes(who));
|
|
1866
|
+
return tally(
|
|
1867
|
+
mine.map(
|
|
1868
|
+
([nid]) => answerOf(docOf(dump["answers"], `${who}${SEP}${nid}`), ctx)
|
|
1869
|
+
),
|
|
1870
|
+
groupsFor(mine, dump, ctx, `${who}${SEP}`)
|
|
1871
|
+
);
|
|
1872
|
+
}
|
|
1873
|
+
function reportAnswerers(out, allowed) {
|
|
1874
|
+
t4(out.by_answerer, (t13, who) => {
|
|
1875
|
+
reportLabels(
|
|
1876
|
+
out.items.flatMap((i) => {
|
|
1877
|
+
const a = i.by_answerer[who];
|
|
1878
|
+
return a === void 0 ? [] : [{ id: `${who}${SEP}${i.id}`, ...a }];
|
|
1879
|
+
}),
|
|
1880
|
+
allowed
|
|
1881
|
+
);
|
|
1882
|
+
reportGroups(
|
|
1883
|
+
t7(t13.groups, (g) => `${who}${SEP}${g}`),
|
|
1884
|
+
allowed
|
|
1885
|
+
);
|
|
1886
|
+
});
|
|
1887
|
+
}
|
|
1888
|
+
var completeLine = (t13) => `${String(t13.complete_items)} of ${String(t13.total_items)} items complete`;
|
|
1889
|
+
function exportLabels(dumpPath, keyPath, outDir, day) {
|
|
1890
|
+
const dump = readJson(dumpPath);
|
|
1891
|
+
const key = readJson(keyPath);
|
|
1892
|
+
assertKey(key);
|
|
1893
|
+
const allowed = key.allowed;
|
|
1894
|
+
assertAllowed(allowed, key, keyPath);
|
|
1895
|
+
checkDump(dump, key);
|
|
1896
|
+
checkNoFamiliarity(dump, allowed);
|
|
1897
|
+
const ctx = { key, allowed };
|
|
1898
|
+
const all = key.answerers;
|
|
1899
|
+
const out = all === void 0 ? labelsOf(dump, ctx, day) : answererLabelsOf(dump, ctx, day, all);
|
|
1900
|
+
mkdirSync(outDir, { recursive: true });
|
|
1901
|
+
const path = join(outDir, `${key.study}-labels-${day}.json`);
|
|
1902
|
+
writeJson(path, out);
|
|
1903
|
+
exportPractice(dump, key, outDir, day);
|
|
1904
|
+
reportExport(path, out, allowed);
|
|
1905
|
+
}
|
|
1906
|
+
function reportExport(path, out, allowed) {
|
|
1907
|
+
if ("by_answerer" in out) {
|
|
1908
|
+
say(
|
|
1909
|
+
`wrote ${path}: ${t2(out.by_answerer).map(([who, t13]) => `${who}: ${completeLine(t13)}`).join("; ")}`
|
|
1910
|
+
);
|
|
1911
|
+
reportAnswerers(out, allowed);
|
|
1912
|
+
} else {
|
|
1913
|
+
say(`wrote ${path}: ${completeLine(out)}`);
|
|
1914
|
+
reportLabels(out.items, allowed);
|
|
1915
|
+
reportGroups(out.groups, allowed);
|
|
1916
|
+
}
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
// skills/build-label-page/scripts/label-page-html.ts
|
|
1920
|
+
import { readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
1921
|
+
import { join as join2 } from "node:path";
|
|
1922
|
+
var TEMPLATE = readFileSync2(
|
|
1923
|
+
join2(__plDirname(__plFileURLToPath(new URL("../../../skills/build-label-page/scripts/label-page-html.ts", import.meta.url).href)), "page-template.html"),
|
|
1924
|
+
"utf8"
|
|
1925
|
+
);
|
|
1926
|
+
var escapeHtml = (text2) => text2.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
1927
|
+
var compactJson = (v) => {
|
|
1928
|
+
if (Array.isArray(v)) return `[${v.map(compactJson).join(", ")}]`;
|
|
1929
|
+
if (typeof v !== "object" || v === null) return JSON.stringify(v);
|
|
1930
|
+
return `{${Object.entries(v).map(([k, x]) => `${JSON.stringify(k)}: ${compactJson(x)}`).join(", ")}}`;
|
|
1931
|
+
};
|
|
1932
|
+
function pageHtml(payload) {
|
|
1933
|
+
const data = compactJson(payload).replaceAll("</", "<\\/");
|
|
1934
|
+
return TEMPLATE.replaceAll(
|
|
1935
|
+
"__TITLE__",
|
|
1936
|
+
() => escapeHtml(payload.title)
|
|
1937
|
+
).replaceAll("__DATA__", () => data);
|
|
1938
|
+
}
|
|
1939
|
+
function build(configPath, outDir) {
|
|
1940
|
+
const { payload, dropped } = prepareBuild(configPath, outDir);
|
|
1941
|
+
writeFileSync2(join2(outDir, "page.html"), pageHtml(payload), "utf8");
|
|
1942
|
+
say(
|
|
1943
|
+
`built ${join2(outDir, "page.html")}: ${String(payload.items.length)} items, ${String(payload.groups.length)} groups${dropped ? `, ${String(dropped)} left out (labeller does not know their group)` : ""}`
|
|
1944
|
+
);
|
|
1945
|
+
say(
|
|
1946
|
+
`wrote ${join2(outDir, "key.json")}: the only link from G-ids to source ids; it is NOT part of the page`
|
|
1947
|
+
);
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
// skills/build-label-page/scripts/label-page-cli.ts
|
|
1951
|
+
var USAGE = "usage: label-page.js build <items.json> <out-dir> | export <dump.json> <key.json> <data-dir> [--date YYYY-MM-DD] | schema";
|
|
1952
|
+
var usage = () => {
|
|
1953
|
+
throw new Refusal(2, USAGE);
|
|
1954
|
+
};
|
|
1955
|
+
var today = () => {
|
|
1956
|
+
const d = /* @__PURE__ */ new Date();
|
|
1957
|
+
const two = (n3) => String(n3).padStart(2, "0");
|
|
1958
|
+
return `${String(d.getFullYear())}-${two(d.getMonth() + 1)}-${two(d.getDate())}`;
|
|
1959
|
+
};
|
|
1960
|
+
var ARITY = {
|
|
1961
|
+
[COMMANDS.schema]: { paths: 0, dated: false },
|
|
1962
|
+
[COMMANDS.build]: { paths: 2, dated: false },
|
|
1963
|
+
[COMMANDS.export]: { paths: 3, dated: true }
|
|
1964
|
+
};
|
|
1965
|
+
var parsedArgs = (argv) => {
|
|
1966
|
+
try {
|
|
1967
|
+
return parseArgs({
|
|
1968
|
+
args: [...argv],
|
|
1969
|
+
options: { date: { type: "string" } },
|
|
1970
|
+
allowPositionals: true
|
|
1971
|
+
});
|
|
1972
|
+
} catch {
|
|
1973
|
+
return usage();
|
|
1974
|
+
}
|
|
1975
|
+
};
|
|
1976
|
+
function exportCommand(paths, date) {
|
|
1977
|
+
const [dump = "", key = "", out = ""] = paths;
|
|
1978
|
+
const day = date ?? today();
|
|
1979
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(day)) fail("--date must be YYYY-MM-DD");
|
|
1980
|
+
exportLabels(dump, key, out, day);
|
|
1981
|
+
}
|
|
1982
|
+
function main(argv) {
|
|
1983
|
+
const { values, positionals } = parsedArgs(argv);
|
|
1984
|
+
const [cmd = "", ...paths] = positionals;
|
|
1985
|
+
const arity = ARITY[cmd];
|
|
1986
|
+
const fits = arity !== void 0 && arity.paths === paths.length && (arity.dated || values.date === void 0);
|
|
1987
|
+
if (!fits) usage();
|
|
1988
|
+
if (cmd === COMMANDS.schema) say(JSON.stringify(CONFIG_SCHEMA, null, 2));
|
|
1989
|
+
else if (cmd === COMMANDS.build) build(String(paths[0]), String(paths[1]));
|
|
1990
|
+
else exportCommand(paths, values.date);
|
|
1991
|
+
}
|
|
1992
|
+
function cli(argv) {
|
|
1993
|
+
takePrinted();
|
|
1994
|
+
try {
|
|
1995
|
+
main(argv);
|
|
1996
|
+
return { status: 0, stdout: takePrinted(), stderr: "" };
|
|
1997
|
+
} catch (e3) {
|
|
1998
|
+
const message = String(Reflect.get(Object(e3), "message"));
|
|
1999
|
+
const status = e3 instanceof Refusal ? e3.exitCode : 1;
|
|
2000
|
+
return { status, stdout: takePrinted(), stderr: `${message}
|
|
2001
|
+
` };
|
|
2002
|
+
}
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
// skills/build-label-page/scripts/label-page.ts
|
|
2006
|
+
var run = cli(process.argv.slice(2));
|
|
95
2007
|
process.stdout.write(run.stdout);
|
|
96
2008
|
process.stderr.write(run.stderr);
|
|
97
2009
|
process.exitCode = run.status;
|