Make HTML templates output Javascript, part 1. #829

This commit is contained in:
ccd0 2019-07-25 12:57:19 -07:00
parent cba171b9d0
commit c64acead26

View File

@ -24,8 +24,8 @@ tools.multiline = function(text) {
return text.replace(/\n+/g, '\n').split(/^/m).map(JSON.stringify).join('+').replace(/"\+"/g, '\\\n'); return text.replace(/\n+/g, '\n').split(/^/m).map(JSON.stringify).join('+').replace(/"\+"/g, '\\\n');
}; };
// Convert JSON object to Coffeescript expression (via embedded JS). // Convert JSONify-able object to Javascript expression.
var constExpression = data => '`' + JSON.stringify(data).replace(/`/g, '\\`') + '`'; var constExpression = data => JSON.stringify(data).replace(/`/g, '\\`');
function TextStream(text) { function TextStream(text) {
this.text = text; this.text = text;
@ -151,11 +151,11 @@ Placeholder.prototype.build = function() {
throw new Error(`JavaScript in template is not an expression (${expr})`); throw new Error(`JavaScript in template is not an expression (${expr})`);
} }
switch(this.type) { switch(this.type) {
case '$': return `\`E(${expr})\``; // $ : escaped text case '$': return `E(${expr})`; // $ : escaped text
case '&': return `\`(${expr}).innerHTML\``; // & : contents of HTML element or template (of form {innerHTML: "safeHTML"}) case '&': return `(${expr}).innerHTML`; // & : contents of HTML element or template (of form {innerHTML: "safeHTML"})
case '@': return `\`E.cat(${expr})\``; // @ : contents of array of HTML elements or templates (see src/General/Globals.coffee for E.cat) case '@': return `E.cat(${expr})`; // @ : contents of array of HTML elements or templates (see src/General/Globals.coffee for E.cat)
case '?': case '?':
return `(if \`(${expr})\` then ${this.args[1] || '""'} else ${this.args[2] || '""'})`; // ? : conditional expression return `((${expr}) ? ${this.args[1] || '""'} : ${this.args[2] || '""'})`; // ? : conditional expression
} }
throw new Error(`Unrecognized placeholder type (${this.type})`); throw new Error(`Unrecognized placeholder type (${this.type})`);
}; };
@ -168,11 +168,11 @@ tools.html = function(template) {
if (stream.text) { if (stream.text) {
throw new Error(`Unexpected characters in template (${stream.text}): ${template}`); throw new Error(`Unexpected characters in template (${stream.text}): ${template}`);
} }
return `(innerHTML: ${output})`; return `(innerHTML: \`${output}\`)`;
}; };
tools.assert = function(statement) { tools.assert = function(statement) {
return `throw new Error 'Assertion failed: ' + ${constExpression(statement)} unless ${statement}`; return `throw new Error 'Assertion failed: ' + \`${constExpression(statement)}\` unless ${statement}`;
}; };
function includesDir(templateName) { function includesDir(templateName) {