Skip to content

Commit 403c0b7

Browse files
committed
iss1757 - Add regex extractors
1 parent 9819f01 commit 403c0b7

6 files changed

Lines changed: 37 additions & 12 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Extractor: regexall
2+
// [[extractor targetinput="ans2" type="regexall" regex="^f\\(x\\)\\s*=\\s*" /]]
3+
// Searches the entire raw input for all lines matching entry.regex and returns
4+
// a JSON object of the form {"matches":[...]} set as answerEl.value.
5+
export default function regexall(raw, answerEl, blocks, entry) {
6+
if (!entry || !entry.regex) {
7+
return;
8+
}
9+
const pattern = new RegExp(entry.regex);
10+
const matches = [];
11+
12+
for (const line of raw.split('\n')) {
13+
const trimmed = line.trim();
14+
if (trimmed && pattern.test(trimmed)) {
15+
matches.push(trimmed);
16+
}
17+
}
18+
19+
answerEl.value = JSON.stringify({ matches });
20+
answerEl.dispatchEvent(new Event('change'));
21+
}

corsscripts/ascii/extractors/regexmatch.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Extractor: regexmatch
2+
// [[extractor targetinput="ans2" type="regexmatch" regex="^f\\(x\\)\\s*=\\s*" /]]
3+
// Note the scaped backslashes. Searches for a trimmed line beginning 'f(x) = ' where
4+
// there can be any amount of whitespace around the equals. Returns 'f(x) = expr'.
25
// Scans blocks (bottom-up) for the last code_inline or asciimath_block line
36
// matching entry.regex, then sets answerEl.value to the matched string.
47
export default function regexmatch(raw, answerEl, blocks, entry) {
@@ -13,7 +16,7 @@ export default function regexmatch(raw, answerEl, blocks, entry) {
1316
if (block.type === 'code_inline') {
1417
const trimmed = block.raw.trim();
1518
if (pattern.test(trimmed)) {
16-
answerEl.value = trimmed.match(pattern)[0];
19+
answerEl.value = trimmed;
1720
answerEl.dispatchEvent(new Event('change'));
1821
return;
1922
}
@@ -23,7 +26,7 @@ export default function regexmatch(raw, answerEl, blocks, entry) {
2326
for (let j = lines.length - 1; j >= 0; j--) {
2427
const trimmed = lines[j].trim();
2528
if (pattern.test(trimmed)) {
26-
answerEl.value = trimmed.match(pattern)[0];
29+
answerEl.value = trimmed;
2730
answerEl.dispatchEvent(new Event('change'));
2831
return;
2932
}
@@ -39,7 +42,7 @@ export default function regexmatch(raw, answerEl, blocks, entry) {
3942
for (const line of lines) {
4043
const trimmed = line.trim();
4144
if (pattern.test(trimmed)) {
42-
answerEl.value = trimmed.match(pattern)[0];
45+
answerEl.value = trimmed;
4346
answerEl.dispatchEvent(new Event('change'));
4447
return;
4548
}

corsscripts/ascii/stackascii.bundle.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

corsscripts/ascii/stackascii.bundle.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

corsscripts/ascii/stackascii.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ import finalfunction from './extractors/finalfunction.js';
1818
import lastexpr from './extractors/lastexpr.js';
1919
import lastblock from './extractors/lastblock.js';
2020
import regexmatch from './extractors/regexmatch.js';
21+
import regexall from './extractors/regexall.js';
2122

22-
const extractorlib = { finalfunction, lastexpr, lastblock, regexmatch };
23+
const extractorlib = { finalfunction, lastexpr, lastblock, regexmatch, regexall };
2324

24-
export default function init(inputIds, filters, operatorsjson) {
25+
export default function init(inputIds, filters, operations) {
2526
const markdownContainerId = inputIds[0];
2627
// inputIds[1..N] correspond to each parsed answer entry in order.
27-
const operations = JSON.parse(operatorsjson);
2828
const extractors = operations.filter(operator => operator.operation === 'extractor');
2929
const inputFilters = filters ? filters : 'latexwrap,boldfilter';
3030

stack/cas/castext2/blocks/ascii.block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function compile($format, $options): ?MP_Node {
113113
return 'stack_js.request_access_to_input("' . $item . '"' . $extra . ')';
114114
}, $inputs, array_keys($inputs)));
115115
$linkcode = 'Promise.all([' . $answercalls . '])';
116-
$linkcode .= ".then((inputIds) => {init(inputIds,'" . $xpars['filters'] . "','" . json_encode($operations) . "');});";
116+
$linkcode .= ".then((inputIds) => {init(inputIds,'" . $xpars['filters'] . "'," . json_encode($operations) . ");});";
117117

118118
$r->items[] = new MP_String($linkcode);
119119
$r->items[] = new MP_String("\n</script>");

0 commit comments

Comments
 (0)