thebe-react
Version:
React providers and components for thebe-core
45 lines • 1.8 kB
JavaScript
;
/**
* Very basic interpolation
*
* Only parses inline `#| @param` comment tags, python like comments only
* Always assumes variable assignment
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.interpolatorFactoryFn = exports.PYTHON_PARAM = void 0;
exports.PYTHON_PARAM = /^(.*)=([^;]*);*\s*#\|*\s*@param\s*(.*)$/;
/**
*
* @param parameterMap - dictionary of known variables and values to inerpolate them to
* @returns (function) performs interpolation on the source string using the parameterMap
*/
function interpolatorFactoryFn(parameterMap) {
return (source) => {
const params = new Set(Object.keys(parameterMap));
const lines = source.split('\n');
const interpolated = lines.map((line) => {
if (exports.PYTHON_PARAM.test(line)) {
const match = line.match(exports.PYTHON_PARAM);
if (match != null) {
const [_, variable, value, schemaString] = match;
let schema = {};
try {
if (schemaString !== '')
schema = JSON.parse(schemaString);
}
catch (err) {
console.error('Could not parse schema from', line, err);
}
if (params.has(variable.trim())) {
return `${variable}= ${parameterMap[variable.trim()]} #| @param${schema ? ` ${JSON.stringify(Object.assign(Object.assign({}, schema), { last: value }))}` : ''}`;
}
}
}
return line;
});
return interpolated.join('\n');
};
}
exports.interpolatorFactoryFn = interpolatorFactoryFn;
//# sourceMappingURL=interpolate.js.map