@astl/engine 0.0.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/LICENSE +202 -0
- package/README.md +41 -0
- package/dist/analyse.d.ts +68 -0
- package/dist/analyse.d.ts.map +1 -0
- package/dist/analyse.js +1179 -0
- package/dist/analyse.js.map +1 -0
- package/dist/checker.d.ts +19 -0
- package/dist/checker.d.ts.map +1 -0
- package/dist/checker.js +103 -0
- package/dist/checker.js.map +1 -0
- package/dist/compile.d.ts +16 -0
- package/dist/compile.d.ts.map +1 -0
- package/dist/compile.js +329 -0
- package/dist/compile.js.map +1 -0
- package/dist/conformance.d.ts +35 -0
- package/dist/conformance.d.ts.map +1 -0
- package/dist/conformance.js +101 -0
- package/dist/conformance.js.map +1 -0
- package/dist/engine.d.ts +13 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +231 -0
- package/dist/engine.js.map +1 -0
- package/dist/grade.d.ts +9 -0
- package/dist/grade.d.ts.map +1 -0
- package/dist/grade.js +192 -0
- package/dist/grade.js.map +1 -0
- package/dist/guard.d.ts +45 -0
- package/dist/guard.d.ts.map +1 -0
- package/dist/guard.js +327 -0
- package/dist/guard.js.map +1 -0
- package/dist/help.d.ts +32 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +120 -0
- package/dist/help.js.map +1 -0
- package/dist/html.d.ts +31 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +246 -0
- package/dist/html.js.map +1 -0
- package/dist/ids.d.ts +35 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +126 -0
- package/dist/ids.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/kit/attributes.d.ts +6 -0
- package/dist/kit/attributes.d.ts.map +1 -0
- package/dist/kit/attributes.js +20 -0
- package/dist/kit/attributes.js.map +1 -0
- package/dist/kit/index.d.ts +43 -0
- package/dist/kit/index.d.ts.map +1 -0
- package/dist/kit/index.js +118 -0
- package/dist/kit/index.js.map +1 -0
- package/dist/limits.d.ts +17 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +173 -0
- package/dist/limits.js.map +1 -0
- package/dist/messages/en.json +48 -0
- package/dist/messages/pl.json +48 -0
- package/dist/messages.d.ts +4 -0
- package/dist/messages.d.ts.map +1 -0
- package/dist/messages.js +9 -0
- package/dist/messages.js.map +1 -0
- package/dist/round.d.ts +3 -0
- package/dist/round.d.ts.map +1 -0
- package/dist/round.js +5 -0
- package/dist/round.js.map +1 -0
- package/dist/slot-kinds.d.ts +58 -0
- package/dist/slot-kinds.d.ts.map +1 -0
- package/dist/slot-kinds.js +151 -0
- package/dist/slot-kinds.js.map +1 -0
- package/dist/text.d.ts +23 -0
- package/dist/text.d.ts.map +1 -0
- package/dist/text.js +236 -0
- package/dist/text.js.map +1 -0
- package/package.json +51 -4
- package/src/analyse.ts +1396 -0
- package/src/checker.ts +132 -0
- package/src/compile.ts +394 -0
- package/src/conformance.ts +120 -0
- package/src/engine.ts +274 -0
- package/src/grade.ts +229 -0
- package/src/guard.ts +356 -0
- package/src/help.ts +141 -0
- package/src/html.ts +274 -0
- package/src/ids.ts +144 -0
- package/src/index.ts +12 -0
- package/src/kit/attributes.ts +21 -0
- package/src/kit/index.ts +140 -0
- package/src/limits.ts +153 -0
- package/src/messages/en.json +48 -0
- package/src/messages/pl.json +48 -0
- package/src/messages.ts +9 -0
- package/src/round.ts +4 -0
- package/src/slot-kinds.ts +212 -0
- package/src/text.ts +234 -0
package/src/limits.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pre-parse limits. The HTML tokenizer compares every new attribute with the earlier ones on the same tag, so a tag
|
|
3
|
+
* with tens of thousands of attributes takes quadratic time; this scan finds such tags in one pass, before parsing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const TAB = 9;
|
|
7
|
+
const LF = 10;
|
|
8
|
+
const FF = 12;
|
|
9
|
+
const CR = 13;
|
|
10
|
+
const SPACE = 32;
|
|
11
|
+
const DOUBLE_QUOTE = 34;
|
|
12
|
+
const SINGLE_QUOTE = 39;
|
|
13
|
+
const SLASH = 47;
|
|
14
|
+
const LESS = 60;
|
|
15
|
+
const EQUALS = 61;
|
|
16
|
+
const GREATER = 62;
|
|
17
|
+
|
|
18
|
+
// The tokenizer's states inside a tag.
|
|
19
|
+
const TAG_NAME = 0;
|
|
20
|
+
const BEFORE_NAME = 1;
|
|
21
|
+
const NAME = 2;
|
|
22
|
+
const AFTER_NAME = 3;
|
|
23
|
+
const BEFORE_VALUE = 4;
|
|
24
|
+
const DOUBLE_QUOTED = 5;
|
|
25
|
+
const SINGLE_QUOTED = 6;
|
|
26
|
+
const UNQUOTED = 7;
|
|
27
|
+
const AFTER_QUOTED = 8;
|
|
28
|
+
const SELF_CLOSING = 9;
|
|
29
|
+
const STATES = 10;
|
|
30
|
+
const DONE = -1;
|
|
31
|
+
|
|
32
|
+
function isSpace(c: number): boolean {
|
|
33
|
+
return c === SPACE || c === LF || c === TAB || c === FF || c === CR;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isAlpha(c: number): boolean {
|
|
37
|
+
return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Next state for character `c`; `starts` is set when an attribute name starts. */
|
|
41
|
+
function step(state: number, c: number, out: { starts: boolean }): number {
|
|
42
|
+
out.starts = false;
|
|
43
|
+
switch (state) {
|
|
44
|
+
case TAG_NAME:
|
|
45
|
+
if (c === GREATER) return DONE;
|
|
46
|
+
if (c === SLASH) return SELF_CLOSING;
|
|
47
|
+
return isSpace(c) ? BEFORE_NAME : TAG_NAME;
|
|
48
|
+
case BEFORE_NAME:
|
|
49
|
+
case SELF_CLOSING:
|
|
50
|
+
if (c === GREATER) return DONE;
|
|
51
|
+
if (c === SLASH) return SELF_CLOSING;
|
|
52
|
+
if (isSpace(c)) return BEFORE_NAME;
|
|
53
|
+
out.starts = true;
|
|
54
|
+
return NAME;
|
|
55
|
+
case NAME:
|
|
56
|
+
if (c === GREATER) return DONE;
|
|
57
|
+
if (c === SLASH) return SELF_CLOSING;
|
|
58
|
+
if (c === EQUALS) return BEFORE_VALUE;
|
|
59
|
+
return isSpace(c) ? AFTER_NAME : NAME;
|
|
60
|
+
case AFTER_NAME:
|
|
61
|
+
if (c === GREATER) return DONE;
|
|
62
|
+
if (c === SLASH) return SELF_CLOSING;
|
|
63
|
+
if (c === EQUALS) return BEFORE_VALUE;
|
|
64
|
+
if (isSpace(c)) return AFTER_NAME;
|
|
65
|
+
out.starts = true;
|
|
66
|
+
return NAME;
|
|
67
|
+
case BEFORE_VALUE:
|
|
68
|
+
if (c === GREATER) return DONE;
|
|
69
|
+
if (c === DOUBLE_QUOTE) return DOUBLE_QUOTED;
|
|
70
|
+
if (c === SINGLE_QUOTE) return SINGLE_QUOTED;
|
|
71
|
+
return isSpace(c) ? BEFORE_VALUE : UNQUOTED;
|
|
72
|
+
case DOUBLE_QUOTED:
|
|
73
|
+
return c === DOUBLE_QUOTE ? AFTER_QUOTED : DOUBLE_QUOTED;
|
|
74
|
+
case SINGLE_QUOTED:
|
|
75
|
+
return c === SINGLE_QUOTE ? AFTER_QUOTED : SINGLE_QUOTED;
|
|
76
|
+
case UNQUOTED:
|
|
77
|
+
if (c === GREATER) return DONE;
|
|
78
|
+
return isSpace(c) ? BEFORE_NAME : UNQUOTED;
|
|
79
|
+
default:
|
|
80
|
+
// AFTER_QUOTED: without a space, the next character starts the next attribute.
|
|
81
|
+
if (c === GREATER) return DONE;
|
|
82
|
+
if (c === SLASH) return SELF_CLOSING;
|
|
83
|
+
if (isSpace(c)) return BEFORE_NAME;
|
|
84
|
+
out.starts = true;
|
|
85
|
+
return NAME;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The largest number of attributes on any start or end tag, counted as the HTML tokenizer counts them, stopping
|
|
91
|
+
* early once a tag has more than `limit`.
|
|
92
|
+
*
|
|
93
|
+
* Every `<` or `</` followed by a letter is read as a possible tag, also inside comments, raw text such as
|
|
94
|
+
* `<script>` and attribute values: whether the tokenizer reads such text as markup depends on tree construction
|
|
95
|
+
* (SVG, MathML, `<select>`, script escapes), and a wrong guess could hide a real tag. All possible tags are followed
|
|
96
|
+
* at once with the tokenizer's attribute states; two in the same state behave the same from then on, so they merge
|
|
97
|
+
* and keep the higher count. That keeps the scan linear, and every real tag is counted from its own start, so the
|
|
98
|
+
* result is never too low. It is too high only for text with hundreds of attribute-like words in one tag-shaped run.
|
|
99
|
+
*/
|
|
100
|
+
export function mostAttributes(html: string, limit: number): number {
|
|
101
|
+
let counts = new Int32Array(STATES).fill(-1);
|
|
102
|
+
let next = new Int32Array(STATES).fill(-1);
|
|
103
|
+
const out = { starts: false };
|
|
104
|
+
let live = 0;
|
|
105
|
+
let most = 0;
|
|
106
|
+
for (let p = 0; p < html.length; p++) {
|
|
107
|
+
const c = html.charCodeAt(p);
|
|
108
|
+
const startsName = isAlpha(c) && p > 0 && startsTagAt(html, p);
|
|
109
|
+
if (live === 0 && !startsName) {
|
|
110
|
+
// Nothing to follow: move to just before the next possible tag name.
|
|
111
|
+
const before = nextNameStart(html, p) - 1;
|
|
112
|
+
if (before < 0) break;
|
|
113
|
+
p = before;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
next.fill(-1);
|
|
117
|
+
live = 0;
|
|
118
|
+
for (let state = 0; state < STATES; state++) {
|
|
119
|
+
const count = counts[state] ?? -1;
|
|
120
|
+
if (count < 0) continue;
|
|
121
|
+
const to = step(state, c, out);
|
|
122
|
+
const total = out.starts ? count + 1 : count;
|
|
123
|
+
if (total > most) most = total;
|
|
124
|
+
if (to === DONE) continue;
|
|
125
|
+
if ((next[to] ?? -1) < total) {
|
|
126
|
+
if ((next[to] ?? -1) < 0) live++;
|
|
127
|
+
next[to] = total;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (most > limit) return most;
|
|
131
|
+
if (startsName && (next[TAG_NAME] ?? -1) < 0) {
|
|
132
|
+
live++;
|
|
133
|
+
next[TAG_NAME] = 0;
|
|
134
|
+
}
|
|
135
|
+
[counts, next] = [next, counts];
|
|
136
|
+
}
|
|
137
|
+
return most;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Whether the letter at `p` starts a tag name: it follows `<` or `</`. */
|
|
141
|
+
function startsTagAt(html: string, p: number): boolean {
|
|
142
|
+
const before = html.charCodeAt(p - 1);
|
|
143
|
+
return before === LESS || (before === SLASH && p > 1 && html.charCodeAt(p - 2) === LESS);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Index of the first letter at or after `from` that starts a tag name, or -1. */
|
|
147
|
+
function nextNameStart(html: string, from: number): number {
|
|
148
|
+
for (let lt = html.indexOf('<', from); lt >= 0; lt = html.indexOf('<', lt + 1)) {
|
|
149
|
+
if (isAlpha(html.charCodeAt(lt + 1))) return lt + 1;
|
|
150
|
+
if (html.charCodeAt(lt + 1) === SLASH && isAlpha(html.charCodeAt(lt + 2))) return lt + 2;
|
|
151
|
+
}
|
|
152
|
+
return -1;
|
|
153
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"SOURCE_TOO_LARGE": "The document is too long. It can have at most {max, number} characters, so split it into several exercises.",
|
|
3
|
+
"SOURCE_TOO_DEEP": "Elements are nested more than {max, plural, one {# level} other {# levels}} deep. Simplify the structure, for example by removing wrapper elements.",
|
|
4
|
+
"SOURCE_TOO_COMPLEX": "An element in the document has more than {max, plural, one {# attribute} other {# attributes}}. Remove the attributes it does not need.",
|
|
5
|
+
"DOC_ROOT_INVALID": "Put the whole exercise inside one <astl-doc> element, with nothing outside it.",
|
|
6
|
+
"DOC_FORMAT_UNSUPPORTED": "Content format “{format}” is not supported. Use format=\"1\" or leave the attribute out.",
|
|
7
|
+
"DOC_LANG_MISSING": "Add the content language to <astl-doc>, for example lang=\"en\".",
|
|
8
|
+
"DOC_LANG_INVALID": "“{lang}” is not a language tag. Use a tag such as en, pl or pt-BR.",
|
|
9
|
+
"DOC_EMPTY": "The document is empty. Add some text or an exercise element.",
|
|
10
|
+
"DOC_MIXED_PAGES": "Some content is outside the pages. Put everything inside <astl-page> elements, or use no pages at all.",
|
|
11
|
+
"NO_COMPONENTS": "Nothing in this document is graded yet. Add at least one exercise element.",
|
|
12
|
+
"PAGE_EMPTY": "Page {page}{titled, select, yes { (“{title}”)} other {}} is empty. Add content or delete the page.",
|
|
13
|
+
"PAGE_NESTED": "A page is inside other content{page, select, 0 {} other { on page {page}{titled, select, yes { (“{title}”)} other {}}}}. Place pages directly inside <astl-doc>.",
|
|
14
|
+
"ID_MISSING": "Element {tag} has no id. Add an id attribute.",
|
|
15
|
+
"ID_INVALID": "“{id}” is not a valid id for element {tag}. Start with a letter and use only a-z, A-Z, 0-9, - and _ (at most 64 characters).",
|
|
16
|
+
"ID_DUPLICATE": "The id “{id}” is used more than once. Give every element its own id.",
|
|
17
|
+
"ELEMENT_UNKNOWN": "Unknown element {tag}. Check the spelling, or register the component that defines it.",
|
|
18
|
+
"ELEMENT_REMOVED": "Element {tag} is not allowed and was removed.",
|
|
19
|
+
"ATTRIBUTE_REMOVED": "Attribute {attribute} is not allowed on element {tag} and was removed.",
|
|
20
|
+
"ATTRIBUTE_INVALID": "Attribute {attribute} on element {tag} has an invalid value: “{value}”.{type, select, enum { Use one of: {values}.} other {}}",
|
|
21
|
+
"ATTRIBUTE_REQUIRED": "Element {tag} needs the {attribute} attribute.",
|
|
22
|
+
"CHILD_UNEXPECTED": "{child, select, text {Element {tag} contains loose text it does not accept. Remove it or put it inside an allowed child element.} other {Element {tag} does not accept <{child}>. Remove it or move it out.}}",
|
|
23
|
+
"CHILD_OUTSIDE": "Element <{child}> only goes inside {parents}. Move it into one, or remove it.",
|
|
24
|
+
"CHILD_COUNT": "Element {tag} {count, plural, =0 {has no {child} elements} one {has # {child} element} other {has # {child} elements}}, but {max, select, -1 {needs at least {min}} 1 {{min, select, 1 {needs exactly 1} other {allows at most 1}}} other {{min, select, 0 {allows at most {max}} other {needs {min} to {max}}}}}.",
|
|
25
|
+
"PLACEMENT_INVALID": "Element {tag} cannot be placed here. Put inline elements inside text (a paragraph, heading or list item) and block elements directly on a page.",
|
|
26
|
+
"COMPONENT_NESTED": "Element {tag} cannot be placed inside element {parent}. Move it out.",
|
|
27
|
+
"URL_BLOCKED": "{tag, select, img {The picture address “{url}” is not allowed, so the picture cannot be shown. Use an address the exercise's URL policy accepts (by default https: or http:).} other {The link “{url}” is not allowed and was removed. Use a web or email link (https:, http: or mailto:).}}",
|
|
28
|
+
"IMG_ALT_MISSING": "A picture has no alt text. Describe in alt what the picture shows, or write alt=\"\" when it is only decoration.",
|
|
29
|
+
"FIGURE_INVALID": "A figure holds exactly one picture (<img>) and at most one caption (<figcaption>), and nothing else.",
|
|
30
|
+
"SCORING_MODE_INVALID": "Element {tag} has no scoring mode “{mode}”. Use one of: {modes}.",
|
|
31
|
+
"POINTS_INVALID": "“{value}” is not a valid number of points. Use a number from 0.01 to 1000.",
|
|
32
|
+
"VERSION_UNSUPPORTED": "Element {tag} uses version {version}, which this version of Astrolabe does not support. Update Astrolabe or check the v attribute.",
|
|
33
|
+
"STYLE_TEMPLATE_UNKNOWN": "Style template “{name}” cannot be used on element {tag}: it is not registered, or not made for this element, so it is ignored.",
|
|
34
|
+
"STYLE_REMOVED": "The style of element {tag} sets {property}, which an exercise may not use, or to a value it may not use, so that declaration is removed.",
|
|
35
|
+
"STYLE_TOO_LONG": "The style of element {tag} is longer than {max} characters, so it is removed.",
|
|
36
|
+
"STYLE_UNKNOWN": "Element {tag} uses the style “{class}”, which no <astl-style> defines.",
|
|
37
|
+
"STYLE_NAME_INVALID": "The rule <astl-style> “{name}” cannot be used: it needs either a name (letters, digits, - and _, starting with a letter, not starting with astl-, tiptap or prosemirror, and used once) or a for naming an element of the format.",
|
|
38
|
+
"STYLE_RULES_TOO_MANY": "An exercise can have at most {max} <astl-style> rules.",
|
|
39
|
+
"STYLE_TOO_LARGE": "The <astl-style> rules write more than {max} characters of styles into the exercise. Use fewer rules, or shorter ones.",
|
|
40
|
+
"STYLE_LOW_CONTRAST": "The text of element {tag} is hard to read on its background: contrast {ratio}:1, at least {min}:1 is recommended.",
|
|
41
|
+
"COLUMNS_INVALID": "Element {tag} is not a valid layout: <astl-columns> goes directly on a page and holds 2 to 4 <astl-column> elements, with nothing else between them, and layouts do not nest.",
|
|
42
|
+
"HELP_UNGRADED": "Element <{tag}> works only inside a graded interaction or on a page with one. Move it there: for now it is left out.",
|
|
43
|
+
"HELP_EMPTY": "Element <{tag}> is empty, so it is left out. Write its text or remove it.",
|
|
44
|
+
"HINT_COST_INVALID": "The cost of a hint is a fraction of the points from 0 to 1, such as 0.25, not “{value}”.",
|
|
45
|
+
"FEEDBACK_WHEN_INVALID": "Feedback is for one verdict: when=\"correct\", \"incorrect\", \"partial\" or \"omitted\", not “{value}”.",
|
|
46
|
+
"FEEDBACK_DUPLICATE": "There are two feedback texts for the verdict “{when}” here. Keep one.",
|
|
47
|
+
"EXPLANATION_DUPLICATE": "There are two explanations here. Keep one."
|
|
48
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"SOURCE_TOO_LARGE": "Dokument jest za długi. Może mieć najwyżej {max, number} znaków, więc podziel go na kilka ćwiczeń.",
|
|
3
|
+
"SOURCE_TOO_DEEP": "Elementy są zagnieżdżone na więcej niż {max, plural, one {# poziomie} few {# poziomach} many {# poziomach} other {# poziomu}}. Uprość strukturę, na przykład usuń zbędne elementy otaczające.",
|
|
4
|
+
"SOURCE_TOO_COMPLEX": "Element w dokumencie ma więcej niż {max, plural, one {# atrybut} few {# atrybuty} many {# atrybutów} other {# atrybutu}}. Usuń atrybuty, których nie potrzebuje.",
|
|
5
|
+
"DOC_ROOT_INVALID": "Umieść całe ćwiczenie w jednym elemencie <astl-doc> i nie dodawaj niczego poza nim.",
|
|
6
|
+
"DOC_FORMAT_UNSUPPORTED": "Format treści „{format}” nie jest obsługiwany. Wpisz format=\"1\" albo usuń ten atrybut.",
|
|
7
|
+
"DOC_LANG_MISSING": "Dodaj język treści do elementu <astl-doc>, na przykład lang=\"pl\".",
|
|
8
|
+
"DOC_LANG_INVALID": "„{lang}” nie jest kodem języka. Użyj kodu takiego jak pl, en albo pt-BR.",
|
|
9
|
+
"DOC_EMPTY": "Dokument jest pusty. Dodaj tekst albo element ćwiczenia.",
|
|
10
|
+
"DOC_MIXED_PAGES": "Część treści jest poza stronami. Umieść całą treść w elementach <astl-page> albo nie używaj stron wcale.",
|
|
11
|
+
"NO_COMPONENTS": "W tym dokumencie nic jeszcze nie jest oceniane. Dodaj co najmniej jeden element ćwiczenia.",
|
|
12
|
+
"PAGE_EMPTY": "Strona {page}{titled, select, yes { („{title}”)} other {}} jest pusta. Dodaj treść albo usuń stronę.",
|
|
13
|
+
"PAGE_NESTED": "Strona jest wewnątrz innej treści{page, select, 0 {} other { na stronie {page}{titled, select, yes { („{title}”)} other {}}}}. Umieszczaj strony bezpośrednio w <astl-doc>.",
|
|
14
|
+
"ID_MISSING": "Element {tag} nie ma identyfikatora. Dodaj atrybut id.",
|
|
15
|
+
"ID_INVALID": "„{id}” nie jest poprawnym identyfikatorem elementu {tag}. Zacznij od litery i używaj tylko znaków a-z, A-Z, 0-9, - oraz _ (najwyżej 64 znaki).",
|
|
16
|
+
"ID_DUPLICATE": "Identyfikator „{id}” występuje więcej niż raz. Nadaj każdemu elementowi inny identyfikator.",
|
|
17
|
+
"ELEMENT_UNKNOWN": "Nieznany element {tag}. Sprawdź pisownię albo zarejestruj komponent, który go definiuje.",
|
|
18
|
+
"ELEMENT_REMOVED": "Element {tag} jest niedozwolony i został usunięty.",
|
|
19
|
+
"ATTRIBUTE_REMOVED": "Atrybut {attribute} jest niedozwolony w elemencie {tag} i został usunięty.",
|
|
20
|
+
"ATTRIBUTE_INVALID": "Atrybut {attribute} w elemencie {tag} ma niepoprawną wartość: „{value}”.{type, select, enum { Użyj jednej z wartości: {values}.} other {}}",
|
|
21
|
+
"ATTRIBUTE_REQUIRED": "Element {tag} wymaga atrybutu {attribute}.",
|
|
22
|
+
"CHILD_UNEXPECTED": "{child, select, text {Element {tag} zawiera luźny tekst, którego nie przyjmuje. Usuń go albo umieść w dozwolonym elemencie.} other {Element {tag} nie przyjmuje <{child}>. Usuń go albo przenieś poza ten element.}}",
|
|
23
|
+
"CHILD_OUTSIDE": "Element <{child}> może być tylko wewnątrz: {parents}. Przenieś go tam albo usuń.",
|
|
24
|
+
"CHILD_COUNT": "Element {tag} {count, plural, =0 {nie zawiera żadnego elementu {child}} one {zawiera # element {child}} few {zawiera # elementy {child}} many {zawiera # elementów {child}} other {zawiera # elementu {child}}}, a {max, select, -1 {musi mieć co najmniej {min}} 1 {{min, select, 1 {musi mieć dokładnie 1} other {może mieć najwyżej 1}}} other {{min, select, 0 {może mieć najwyżej {max}} other {musi mieć od {min} do {max}}}}}.",
|
|
25
|
+
"PLACEMENT_INVALID": "Elementu {tag} nie można tu umieścić. Elementy w tekście wstawiaj do akapitu, nagłówka lub punktu listy, a elementy blokowe bezpośrednio na stronę.",
|
|
26
|
+
"COMPONENT_NESTED": "Elementu {tag} nie można umieścić wewnątrz elementu {parent}. Przenieś go na zewnątrz.",
|
|
27
|
+
"URL_BLOCKED": "{tag, select, img {Adres obrazka „{url}” jest niedozwolony, więc obrazka nie można pokazać. Użyj adresu, który przyjmuje polityka adresów ćwiczenia (domyślnie https: albo http:).} other {Link „{url}” jest niedozwolony i został usunięty. Użyj adresu strony lub e-maila (https:, http: albo mailto:).}}",
|
|
28
|
+
"IMG_ALT_MISSING": "Obrazek nie ma tekstu alternatywnego. Opisz w alt, co przedstawia, albo wpisz alt=\"\", jeśli jest tylko ozdobą.",
|
|
29
|
+
"FIGURE_INVALID": "Element figure zawiera dokładnie jeden obrazek (<img>) i najwyżej jeden podpis (<figcaption>), nic więcej.",
|
|
30
|
+
"SCORING_MODE_INVALID": "Element {tag} nie ma trybu punktacji „{mode}”. Użyj jednego z: {modes}.",
|
|
31
|
+
"POINTS_INVALID": "„{value}” nie jest poprawną liczbą punktów. Użyj liczby od 0,01 do 1000.",
|
|
32
|
+
"VERSION_UNSUPPORTED": "Element {tag} używa wersji {version}, której ta wersja Astrolabe nie obsługuje. Zaktualizuj Astrolabe albo sprawdź atrybut v.",
|
|
33
|
+
"STYLE_TEMPLATE_UNKNOWN": "Szablonu stylu „{name}” nie można użyć w elemencie {tag}: nie jest zarejestrowany albo nie jest przeznaczony dla tego elementu, więc zostanie pominięty.",
|
|
34
|
+
"STYLE_REMOVED": "Styl elementu {tag} ustawia {property}, czego ćwiczenie nie może używać, albo używa niedozwolonej wartości, więc ta deklaracja zostanie usunięta.",
|
|
35
|
+
"STYLE_TOO_LONG": "Styl elementu {tag} jest dłuższy niż {max} znaków, więc zostanie usunięty.",
|
|
36
|
+
"STYLE_UNKNOWN": "Element {tag} używa stylu „{class}”, którego nie definiuje żadne <astl-style>.",
|
|
37
|
+
"STYLE_NAME_INVALID": "Reguły <astl-style> „{name}” nie można użyć: potrzebuje albo nazwy (litery, cyfry, - i _, na początku litera, bez przedrostków astl-, tiptap i prosemirror, użytej raz), albo atrybutu for z elementem formatu.",
|
|
38
|
+
"STYLE_RULES_TOO_MANY": "Ćwiczenie może mieć najwyżej {max} reguł <astl-style>.",
|
|
39
|
+
"STYLE_TOO_LARGE": "Reguły <astl-style> wpisują do ćwiczenia ponad {max} znaków stylów. Użyj mniejszej liczby reguł albo krótszych.",
|
|
40
|
+
"STYLE_LOW_CONTRAST": "Tekst elementu {tag} jest trudny do odczytania na swoim tle: kontrast {ratio}:1, zalecane co najmniej {min}:1.",
|
|
41
|
+
"COLUMNS_INVALID": "Element {tag} nie tworzy poprawnego układu: <astl-columns> stoi bezpośrednio na stronie i zawiera od 2 do 4 elementów <astl-column>, bez niczego między nimi, a układów nie można zagnieżdżać.",
|
|
42
|
+
"HELP_UNGRADED": "Element <{tag}> działa tylko w ocenianym zadaniu albo na stronie z takim zadaniem. Przenieś go tam: na razie jest pominięty.",
|
|
43
|
+
"HELP_EMPTY": "Element <{tag}> jest pusty, więc jest pominięty. Wpisz jego treść albo go usuń.",
|
|
44
|
+
"HINT_COST_INVALID": "Koszt podpowiedzi to część punktów od 0 do 1, np. 0.25, a nie „{value}”.",
|
|
45
|
+
"FEEDBACK_WHEN_INVALID": "Informacja zwrotna dotyczy jednego wyniku: when=\"correct\", \"incorrect\", \"partial\" albo \"omitted\", a nie „{value}”.",
|
|
46
|
+
"FEEDBACK_DUPLICATE": "Są tu dwie informacje zwrotne dla wyniku „{when}”. Zostaw jedną.",
|
|
47
|
+
"EXPLANATION_DUPLICATE": "Są tu dwa wyjaśnienia. Zostaw jedno."
|
|
48
|
+
}
|
package/src/messages.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ENGINE_NAMESPACE, type MessageCatalog } from '@astl/core';
|
|
2
|
+
import en from './messages/en.json' with { type: 'json' };
|
|
3
|
+
import pl from './messages/pl.json' with { type: 'json' };
|
|
4
|
+
|
|
5
|
+
/** Messages for every engine issue code (namespace `astl.engine`). */
|
|
6
|
+
export const engineMessages: readonly MessageCatalog[] = [
|
|
7
|
+
{ namespace: ENGINE_NAMESPACE, locale: 'en', messages: en },
|
|
8
|
+
{ namespace: ENGINE_NAMESPACE, locale: 'pl', messages: pl },
|
|
9
|
+
];
|
package/src/round.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in slot kinds. Keys are plain JSON and validated strictly, because they are stored and graded for years.
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
FLAG_SELECTED,
|
|
6
|
+
type JsonObject,
|
|
7
|
+
type NormalisationProfile,
|
|
8
|
+
OPEN_SLOT_KIND,
|
|
9
|
+
type SlotKind,
|
|
10
|
+
} from '@astl/core';
|
|
11
|
+
import { formatNumber, normalise, parseNumber } from './text.js';
|
|
12
|
+
|
|
13
|
+
/** A NormalisationProfile as JSON data (an object type, so it fits JsonObject). */
|
|
14
|
+
export type ProfileData = { readonly [K in keyof NormalisationProfile]: NormalisationProfile[K] };
|
|
15
|
+
|
|
16
|
+
export type TextKey = {
|
|
17
|
+
/** Accepted answers as the author wrote them; the first one is the solution. */
|
|
18
|
+
readonly accept: readonly string[];
|
|
19
|
+
readonly profile: ProfileData;
|
|
20
|
+
readonly lang: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type NumberKey = {
|
|
24
|
+
/** Accepted values; the first one is the solution. */
|
|
25
|
+
readonly values: readonly number[];
|
|
26
|
+
/** Accepted distance from a value. */
|
|
27
|
+
readonly tolerance: number;
|
|
28
|
+
readonly lang: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type ChoiceKey = {
|
|
32
|
+
/** The correct value: an opaque option id, or a fixed public value such as `true` / `false`. */
|
|
33
|
+
readonly correct: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type FlagKey = {
|
|
37
|
+
/** Whether the option should be selected. */
|
|
38
|
+
readonly correct: boolean;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type LetterKey = {
|
|
42
|
+
/** One letter (one code point) in NFC capitals, as the cell shows it. */
|
|
43
|
+
readonly letter: string;
|
|
44
|
+
/** The language capitals are made in (Turkish i and İ). */
|
|
45
|
+
readonly lang: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const PROFILE_VALUES: { readonly [K in keyof NormalisationProfile]: readonly NormalisationProfile[K][] } = {
|
|
49
|
+
form: ['NFC', 'NFKC'],
|
|
50
|
+
space: ['strict', 'collapse'],
|
|
51
|
+
case: ['sensitive', 'insensitive'],
|
|
52
|
+
marks: ['strict', 'fold'],
|
|
53
|
+
punctuation: ['strict', 'ignore-edge'],
|
|
54
|
+
quotes: ['strict', 'fold'],
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** True when `value` is a complete NormalisationProfile with known values only. */
|
|
58
|
+
export function isProfile(value: unknown): value is NormalisationProfile {
|
|
59
|
+
if (!isPlainObject(value) || !hasExactly(value, Object.keys(PROFILE_VALUES))) return false;
|
|
60
|
+
return Object.entries(PROFILE_VALUES).every(([name, allowed]) =>
|
|
61
|
+
(allowed as readonly unknown[]).includes(value[name]),
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
66
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function hasExactly(value: Record<string, unknown>, names: readonly string[]): boolean {
|
|
70
|
+
const keys = Object.keys(value);
|
|
71
|
+
return keys.length === names.length && names.every((n) => Object.hasOwn(value, n));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function isNonEmptyArray(value: unknown): value is readonly unknown[] {
|
|
75
|
+
return Array.isArray(value) && value.length > 0;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Typed text compared with the stored normalisation profile. */
|
|
79
|
+
export const textSlot: SlotKind<TextKey> = {
|
|
80
|
+
kind: 'text',
|
|
81
|
+
selection: false,
|
|
82
|
+
isKey(key: JsonObject): key is TextKey {
|
|
83
|
+
return (
|
|
84
|
+
hasExactly(key, ['accept', 'profile', 'lang']) &&
|
|
85
|
+
isNonEmptyArray(key.accept) &&
|
|
86
|
+
key.accept.every((a) => typeof a === 'string' && a !== '') &&
|
|
87
|
+
isProfile(key.profile) &&
|
|
88
|
+
typeof key.lang === 'string'
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
isCorrect(key: TextKey, answer: string): boolean {
|
|
92
|
+
const typed = normalise(answer, key.profile, key.lang);
|
|
93
|
+
return key.accept.some((a) => normalise(a, key.profile, key.lang) === typed);
|
|
94
|
+
},
|
|
95
|
+
solution(key: TextKey): string {
|
|
96
|
+
return key.accept[0] ?? '';
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** A number in the content language's notation, within a tolerance. */
|
|
101
|
+
export const numberSlot: SlotKind<NumberKey> = {
|
|
102
|
+
kind: 'number',
|
|
103
|
+
selection: false,
|
|
104
|
+
isKey(key: JsonObject): key is NumberKey {
|
|
105
|
+
return (
|
|
106
|
+
hasExactly(key, ['values', 'tolerance', 'lang']) &&
|
|
107
|
+
isNonEmptyArray(key.values) &&
|
|
108
|
+
key.values.every((v) => typeof v === 'number' && Number.isFinite(v)) &&
|
|
109
|
+
typeof key.tolerance === 'number' &&
|
|
110
|
+
Number.isFinite(key.tolerance) &&
|
|
111
|
+
key.tolerance >= 0 &&
|
|
112
|
+
typeof key.lang === 'string'
|
|
113
|
+
);
|
|
114
|
+
},
|
|
115
|
+
isCorrect(key: NumberKey, answer: string): boolean {
|
|
116
|
+
const value = parseNumber(answer, key.lang);
|
|
117
|
+
// The epsilon absorbs binary rounding, e.g. 0.1 + 0.2 against 0.3.
|
|
118
|
+
return value !== null && key.values.some((v) => Math.abs(value - v) <= key.tolerance + 1e-9);
|
|
119
|
+
},
|
|
120
|
+
solution(key: NumberKey): string {
|
|
121
|
+
const first = key.values[0];
|
|
122
|
+
return first === undefined ? '' : formatNumber(first, key.lang);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/** One picked value: an option's opaque id, or one of a fixed set of public values. */
|
|
127
|
+
export const choiceSlot: SlotKind<ChoiceKey> = {
|
|
128
|
+
kind: 'choice',
|
|
129
|
+
selection: false,
|
|
130
|
+
isKey(key: JsonObject): key is ChoiceKey {
|
|
131
|
+
return hasExactly(key, ['correct']) && typeof key.correct === 'string' && key.correct !== '';
|
|
132
|
+
},
|
|
133
|
+
isCorrect(key: ChoiceKey, answer: string): boolean {
|
|
134
|
+
return answer === key.correct;
|
|
135
|
+
},
|
|
136
|
+
solution(key: ChoiceKey): string {
|
|
137
|
+
return key.correct;
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** A selectable option (`'true'` when selected). Unselected flags are graded by the engine (see scoring). */
|
|
142
|
+
export const flagSlot: SlotKind<FlagKey> = {
|
|
143
|
+
kind: 'flag',
|
|
144
|
+
selection: true,
|
|
145
|
+
isKey(key: JsonObject): key is FlagKey {
|
|
146
|
+
return hasExactly(key, ['correct']) && typeof key.correct === 'boolean';
|
|
147
|
+
},
|
|
148
|
+
isCorrect(key: FlagKey, answer: string): boolean {
|
|
149
|
+
return (answer === FLAG_SELECTED) === key.correct;
|
|
150
|
+
},
|
|
151
|
+
solution(key: FlagKey): string {
|
|
152
|
+
return key.correct ? FLAG_SELECTED : '';
|
|
153
|
+
},
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
function upperCase(value: string, lang: string): string {
|
|
157
|
+
try {
|
|
158
|
+
return value.toLocaleUpperCase(lang);
|
|
159
|
+
} catch {
|
|
160
|
+
// A malformed language tag in a stored key must not break grading.
|
|
161
|
+
return value.toUpperCase();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* One letter in a square (a crossword cell): the answer in NFC capitals of the key's language, spaces around it
|
|
167
|
+
* ignored, equals the key's letter. Anything longer is wrong.
|
|
168
|
+
*/
|
|
169
|
+
export const letterSlot: SlotKind<LetterKey> = {
|
|
170
|
+
kind: 'letter',
|
|
171
|
+
selection: false,
|
|
172
|
+
isKey(key: JsonObject): key is LetterKey {
|
|
173
|
+
return (
|
|
174
|
+
hasExactly(key, ['letter', 'lang']) &&
|
|
175
|
+
typeof key.letter === 'string' &&
|
|
176
|
+
[...key.letter].length === 1 &&
|
|
177
|
+
typeof key.lang === 'string'
|
|
178
|
+
);
|
|
179
|
+
},
|
|
180
|
+
isCorrect(key: LetterKey, answer: string): boolean {
|
|
181
|
+
return upperCase(answer.trim().normalize('NFC'), key.lang).normalize('NFC') === key.letter;
|
|
182
|
+
},
|
|
183
|
+
solution(key: LetterKey): string {
|
|
184
|
+
return key.letter;
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
export type OpenKey = Record<string, never>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* An open answer (a long answer): stored with the attempt for a teacher, never graded, at most 2000 code points. Its
|
|
192
|
+
* key is empty: there is nothing to compare with.
|
|
193
|
+
*/
|
|
194
|
+
export const openSlot: SlotKind<OpenKey> = {
|
|
195
|
+
kind: OPEN_SLOT_KIND,
|
|
196
|
+
selection: false,
|
|
197
|
+
graded: false,
|
|
198
|
+
isKey(key: JsonObject): key is OpenKey {
|
|
199
|
+
return hasExactly(key, []);
|
|
200
|
+
},
|
|
201
|
+
isCorrect: () => false,
|
|
202
|
+
solution: () => '',
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export const BUILT_IN_SLOT_KINDS: readonly SlotKind[] = [
|
|
206
|
+
textSlot,
|
|
207
|
+
numberSlot,
|
|
208
|
+
choiceSlot,
|
|
209
|
+
flagSlot,
|
|
210
|
+
letterSlot,
|
|
211
|
+
openSlot,
|
|
212
|
+
];
|