2020-05-01 04:41:02 +08:00
|
|
|
import Handlebars from "handlebars";
|
|
|
|
|
2016-07-01 05:10:08 +08:00
|
|
|
// This is a mechanism for quickly rendering templates which is Ember aware
|
|
|
|
// templates are highly compatible with Ember so you don't need to worry about calling "get"
|
2019-11-08 05:38:28 +08:00
|
|
|
// and discourseComputed properties function, additionally it uses stringParams like Ember does
|
2016-07-01 05:10:08 +08:00
|
|
|
const RawHandlebars = Handlebars.create();
|
|
|
|
|
2016-07-14 03:36:34 +08:00
|
|
|
function buildPath(blk, args) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let result = {
|
2016-07-14 03:36:34 +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();
|
2016-07-14 03:36:34 +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];
|
2016-07-14 03:36:34 +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);
|
|
|
|
}
|
|
|
|
|
2016-07-01 05:10:08 +08:00
|
|
|
if (Handlebars.Compiler) {
|
|
|
|
RawHandlebars.Compiler = function () {};
|
2019-11-01 23:01:16 +08:00
|
|
|
RawHandlebars.Compiler.prototype = Object.create(
|
2016-07-01 05:10:08 +08:00
|
|
|
Handlebars.Compiler.prototype
|
|
|
|
);
|
|
|
|
RawHandlebars.Compiler.prototype.compiler = RawHandlebars.Compiler;
|
|
|
|
|
|
|
|
RawHandlebars.JavaScriptCompiler = function () {};
|
|
|
|
|
2019-11-01 23:01:16 +08:00
|
|
|
RawHandlebars.JavaScriptCompiler.prototype = Object.create(
|
2016-07-01 05:10:08 +08:00
|
|
|
Handlebars.JavaScriptCompiler.prototype
|
|
|
|
);
|
|
|
|
RawHandlebars.JavaScriptCompiler.prototype.compiler =
|
|
|
|
RawHandlebars.JavaScriptCompiler;
|
|
|
|
RawHandlebars.JavaScriptCompiler.prototype.namespace = "RawHandlebars";
|
|
|
|
|
2022-09-01 18:50:46 +08:00
|
|
|
RawHandlebars.precompile = function (value, asObject, { plugins = [] } = {}) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let ast = Handlebars.parse(value);
|
2016-07-01 05:10:08 +08:00
|
|
|
replaceGet(ast);
|
2022-09-01 18:50:46 +08:00
|
|
|
plugins.forEach((plugin) => plugin(ast));
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let options = {
|
2016-07-01 05:10:08 +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);
|
2016-07-01 05:10:08 +08:00
|
|
|
return new RawHandlebars.JavaScriptCompiler().compile(
|
|
|
|
environment,
|
|
|
|
options,
|
|
|
|
undefined,
|
|
|
|
asObject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-09-01 18:50:46 +08:00
|
|
|
RawHandlebars.compile = function (string, { plugins = [] } = {}) {
|
2021-01-27 19:39:20 +08:00
|
|
|
let ast = Handlebars.parse(string);
|
2016-07-01 05:10:08 +08:00
|
|
|
replaceGet(ast);
|
2022-09-01 18:50:46 +08:00
|
|
|
plugins.forEach((plugin) => plugin(ast));
|
2016-07-01 05:10:08 +08:00
|
|
|
|
|
|
|
// 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(
|
2016-07-01 05:10:08 +08:00
|
|
|
environment,
|
|
|
|
options,
|
|
|
|
undefined,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2021-01-27 19:39:20 +08:00
|
|
|
let t = RawHandlebars.template(templateSpec);
|
2016-07-05 02:15:51 +08:00
|
|
|
t.isMethod = false;
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
return t;
|
2016-07-01 05:10:08 +08:00
|
|
|
};
|
2016-07-05 02:15:51 +08:00
|
|
|
}
|
2016-07-01 05:10:08 +08:00
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
export function template() {
|
|
|
|
return RawHandlebars.template.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
export function precompile() {
|
|
|
|
return RawHandlebars.precompile.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-05 02:15:51 +08:00
|
|
|
export function compile() {
|
|
|
|
return RawHandlebars.compile.apply(this, arguments);
|
2016-07-01 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default RawHandlebars;
|