/* CodeQ: an online programming tutor.
Copyright (C) 2016 UL FRI
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see ';
return output;
}
};
// convert latex math notation to MathML
var renderMath = function (str) {
// inline math delimited by \( \)
str = str.replace(/\\\((.*?)\\\)/g, function (match, expression) {
return katex.renderToString(expression);
});
// display math delimited by \[ \]
str = str.replace(/\\\[(.*?)\\\]/g, function (match, expression) {
return katex.renderToString(expression, {'displayMode': true});
});
return str;
};
// instantiate template with args
var process = function (template, templatePath, args) {
var templateName,
key, subpart, start, end, match, handler,
output = '';
if (!templatePath) templatePath = [];
templateName = templatePath.join('/');
end = 0;
while ((start = template.indexOf('[%', end)) != -1) {
// add remaining text from previous iteration
output += template.substring(end, start);
if (template.substr(start+2, 2) === '--') { // a comment start
if ((end = template.indexOf('--%]', start+4)) == -1) {
break;
}
end += 4;
}
else { // a start of a statement or of a value reference
if ((end = template.indexOf('%]', start+2)) == -1) {
break;
}
subpart = template.substring(start+2, end).trim();
end += 2;
if ((match = subpart.match(/(.)(\S+)(.*)/)) === null) {
codeq.log.error('Invalide template in ' + templateName + ': ' + subpart);
continue;
}
switch (match[1]) {
case '=': // a value reference
var value = args[match[2]];
if (Array.isArray(value)) {
var i;
for (i = 0; i < value.length; i++) {
output += '