@@ -10,6 +10,7 @@ let correctRe = []; // Array of compiled regex of correct answer
10
10
let expected = [ ] ; // Array of an expected (correct) answer
11
11
let info = { } ; // General info
12
12
let hints = [ ] ; // Array of hint objects
13
+ let page_definitions = { } ; // Definitions used when preprocessing regexes
13
14
14
15
// This array contains the default pattern preprocessing commands, in order.
15
16
// We process every pattern through these (in order) to create a final regex
@@ -57,6 +58,18 @@ function trimNewlines(s) {
57
58
. replace ( / [ \n \r ] + $ / , '' ) ) ;
58
59
}
59
60
61
+ /**
62
+ * Apply all page_definitions to string s (which is presumably a regex
63
+ * in string form). These are simple text replacements.
64
+ */
65
+ function processDefinitions ( s ) {
66
+ let result = s ;
67
+ for ( definition in page_definitions ) {
68
+ result = result . replaceAll ( definition , page_definitions [ definition ] ) ;
69
+ } ;
70
+ return result ;
71
+ }
72
+
60
73
/**
61
74
* Escape unsafe HTML, e.g., & becomes &
62
75
*/
@@ -107,12 +120,16 @@ function showDebugOutput(debugOutput, alwaysAlert = true) {
107
120
108
121
/**
109
122
* Given take a regex string, preprocess it (using our array of
110
- * preprocessing regexes), and return a processed regex as a String.
123
+ * definitions and preprocessing regexes),
124
+ * and return a processed regex as a String.
111
125
* @regexString - String to be converted into a compiled Regex
112
126
* @fullMatch - require full match (insert "^" at beginning, "$" at end).
113
127
*/
114
128
function processRegexToString ( regexString , fullMatch = true ) {
115
- let processedRegexString = regexString ;
129
+ // Replace all definitions. This makes regexes much easier to use,
130
+ // as we can now defined named fragments.
131
+ let processedRegexString = processDefinitions ( regexString ) ;
132
+ // Preprocess. This lets us define what whitespace etc. means.
116
133
for ( preprocessRegex of preprocessRegexes ) {
117
134
processedRegexString = processedRegexString . replace (
118
135
preprocessRegex [ 0 ] , preprocessRegex [ 1 ]
@@ -440,14 +457,21 @@ function processInfo(configurationInfo) {
440
457
441
458
const allowedInfoFields = new Set ( [
442
459
'hints' , 'successes' , 'failures' , 'correct' , 'expected' ,
443
- 'preprocessing' , 'preprocessingTests' , 'debug' ] ) ;
460
+ 'definitions' , ' preprocessing', 'preprocessingTests' , 'debug' ] ) ;
444
461
let usedFields = new Set ( Object . keys ( info ) ) ;
445
462
let forbiddenFields = setDifference ( usedFields , allowedInfoFields ) ;
446
463
if ( forbiddenFields . size != 0 ) {
447
464
showDebugOutput (
448
465
`Unknown field(s) in info: ` +
449
466
Array . from ( forbiddenFields ) . join ( ' ' ) ) ;
450
467
}
468
+ if ( info . definitions ) {
469
+ for ( let definition of info . definitions ) {
470
+ // Preprocess with all existing definitions
471
+ newValue = trimNewlines ( processDefinitions ( definition . value ) ) ;
472
+ page_definitions [ definition . term ] = newValue ;
473
+ }
474
+ }
451
475
452
476
// Set up pattern preprocessing, if set. ADVANCED USERS ONLY.
453
477
// This must be done *before* we load & process any other patterns.
0 commit comments