2021-01-12 23:13:21 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Filter = require("broccoli-filter");
|
|
|
|
const Handlebars = require("handlebars");
|
|
|
|
|
|
|
|
const RawHandlebars = Handlebars.create();
|
|
|
|
|
|
|
|
function buildPath(blk, args) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let result = {
|
2021-01-12 23:13:21 +08:00
|
|
|
type: "PathExpression",
|
|
|
|
data: false,
|
|
|
|
depth: blk.path.depth,
|
|
|
|
loc: blk.path.loc,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Server side precompile doesn't have jquery.extend
|
|
|
|
Object.keys(args).forEach(function (a) {
|
|
|
|
result[a] = args[a];
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceGet(ast) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let visitor = new Handlebars.Visitor();
|
2021-01-12 23:13:21 +08:00
|
|
|
visitor.mutating = true;
|
|
|
|
|
|
|
|
visitor.MustacheStatement = function (mustache) {
|
|
|
|
if (!(mustache.params.length || mustache.hash)) {
|
|
|
|
mustache.params[0] = mustache.path;
|
|
|
|
mustache.path = buildPath(mustache, {
|
|
|
|
parts: ["get"],
|
|
|
|
original: "get",
|
|
|
|
strict: true,
|
|
|
|
falsy: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return Handlebars.Visitor.prototype.MustacheStatement.call(this, mustache);
|
|
|
|
};
|
|
|
|
|
|
|
|
// rewrite `each x as |y|` as each y in x`
|
|
|
|
// This allows us to use the same syntax in all templates
|
|
|
|
visitor.BlockStatement = function (block) {
|
|
|
|
if (block.path.original === "each" && block.params.length === 1) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let paramName = block.program.blockParams[0];
|
2021-01-12 23:13:21 +08:00
|
|
|
block.params = [
|
|
|
|
buildPath(block, { original: paramName }),
|
|
|
|
{ type: "CommentStatement", value: "in" },
|
|
|
|
block.params[0],
|
|
|
|
];
|
|
|
|
delete block.program.blockParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Handlebars.Visitor.prototype.BlockStatement.call(this, block);
|
|
|
|
};
|
|
|
|
|
|
|
|
visitor.accept(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
RawHandlebars.Compiler = function () {};
|
|
|
|
RawHandlebars.Compiler.prototype = Object.create(Handlebars.Compiler.prototype);
|
|
|
|
RawHandlebars.Compiler.prototype.compiler = RawHandlebars.Compiler;
|
|
|
|
|
|
|
|
RawHandlebars.JavaScriptCompiler = function () {};
|
|
|
|
|
|
|
|
RawHandlebars.JavaScriptCompiler.prototype = Object.create(
|
|
|
|
Handlebars.JavaScriptCompiler.prototype
|
|
|
|
);
|
|
|
|
RawHandlebars.JavaScriptCompiler.prototype.compiler =
|
|
|
|
RawHandlebars.JavaScriptCompiler;
|
|
|
|
RawHandlebars.JavaScriptCompiler.prototype.namespace = "RawHandlebars";
|
|
|
|
|
|
|
|
RawHandlebars.precompile = function (value, asObject) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let ast = Handlebars.parse(value);
|
2021-01-12 23:13:21 +08:00
|
|
|
replaceGet(ast);
|
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let options = {
|
2021-01-12 23:13:21 +08:00
|
|
|
knownHelpers: {
|
|
|
|
get: true,
|
|
|
|
},
|
|
|
|
data: true,
|
|
|
|
stringParams: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
asObject = asObject === undefined ? true : asObject;
|
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let environment = new RawHandlebars.Compiler().compile(ast, options);
|
2021-01-12 23:13:21 +08:00
|
|
|
return new RawHandlebars.JavaScriptCompiler().compile(
|
|
|
|
environment,
|
|
|
|
options,
|
|
|
|
undefined,
|
|
|
|
asObject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
RawHandlebars.compile = function (string) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let ast = Handlebars.parse(string);
|
2021-01-12 23:13:21 +08:00
|
|
|
replaceGet(ast);
|
|
|
|
|
|
|
|
// this forces us to rewrite helpers
|
2021-01-27 19:39:20 +08:00
|
|
|
let options = { data: true, stringParams: true };
|
|
|
|
let environment = new RawHandlebars.Compiler().compile(ast, options);
|
|
|
|
let templateSpec = new RawHandlebars.JavaScriptCompiler().compile(
|
2021-01-12 23:13:21 +08:00
|
|
|
environment,
|
|
|
|
options,
|
|
|
|
undefined,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let t = RawHandlebars.template(templateSpec);
|
2021-01-12 23:13:21 +08:00
|
|
|
t.isMethod = false;
|
|
|
|
|
|
|
|
return t;
|
|
|
|
};
|
|
|
|
function TemplateCompiler(inputTree, options) {
|
|
|
|
if (!(this instanceof TemplateCompiler)) {
|
|
|
|
return new TemplateCompiler(inputTree, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
Filter.call(this, inputTree, options); // this._super()
|
|
|
|
|
|
|
|
this.options = options || {};
|
|
|
|
this.inputTree = inputTree;
|
|
|
|
}
|
|
|
|
|
|
|
|
TemplateCompiler.prototype = Object.create(Filter.prototype);
|
|
|
|
TemplateCompiler.prototype.constructor = TemplateCompiler;
|
|
|
|
TemplateCompiler.prototype.extensions = ["hbr"];
|
|
|
|
TemplateCompiler.prototype.targetExtension = "js";
|
|
|
|
|
|
|
|
TemplateCompiler.prototype.registerPlugins = function registerPlugins() {};
|
|
|
|
|
2022-07-06 16:37:54 +08:00
|
|
|
TemplateCompiler.prototype.initializeFeatures =
|
|
|
|
function initializeFeatures() {};
|
2021-01-12 23:13:21 +08:00
|
|
|
|
|
|
|
TemplateCompiler.prototype.processString = function (string, relativePath) {
|
2022-08-22 16:56:39 +08:00
|
|
|
let filename;
|
|
|
|
|
|
|
|
const pluginName = relativePath.match(/^discourse\/plugins\/([^\/]+)\//)?.[1];
|
|
|
|
|
|
|
|
if (pluginName) {
|
|
|
|
filename = relativePath
|
|
|
|
.replace(`discourse/plugins/${pluginName}/`, "")
|
2023-07-14 01:57:45 +08:00
|
|
|
.replace(/^(discourse\/)?raw-templates\//, "javascripts/");
|
2022-08-22 16:56:39 +08:00
|
|
|
} else {
|
2023-07-14 01:57:45 +08:00
|
|
|
filename = relativePath.replace(/^raw-templates\//, "");
|
2022-08-22 16:56:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
filename = filename.replace(/\.hbr$/, "");
|
2021-01-12 23:13:21 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
'import { template as compiler } from "discourse-common/lib/raw-handlebars";\n' +
|
|
|
|
'import { addRawTemplate } from "discourse-common/lib/raw-templates";\n\n' +
|
|
|
|
"let template = compiler(" +
|
|
|
|
this.precompile(string, false) +
|
|
|
|
");\n\n" +
|
|
|
|
'addRawTemplate("' +
|
|
|
|
filename +
|
2021-05-08 02:41:06 +08:00
|
|
|
'", template, { core: true });\n' +
|
2021-01-12 23:13:21 +08:00
|
|
|
"export default template;"
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
TemplateCompiler.prototype.precompile = function (value, asObject) {
|
|
|
|
return RawHandlebars.precompile(value, asObject);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = TemplateCompiler;
|