2020-01-11 09:05:32 +01:00
|
|
|
import blog from Hablo.Config;
|
|
|
|
|
2020-01-10 18:56:59 +01:00
|
|
|
return {
|
|
|
|
render: render
|
|
|
|
};
|
|
|
|
|
|
|
|
function render(template, environment) {
|
|
|
|
if(blog.wording[template] != undefined) {
|
|
|
|
var template = blog.wording[template];
|
|
|
|
}
|
|
|
|
template = template.replace(/{\?((?:[^?]|\?[^}])*)\?}/g, renderSub(environment));
|
|
|
|
var failed = [false];
|
|
|
|
var result = template.replace(
|
|
|
|
/([^$]|^)\$(?:{(\w+)}|(\w+)\b)/g,
|
|
|
|
substitute(environment, failed)
|
|
|
|
);
|
|
|
|
return failed[0] ? null : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderSub(environment) {
|
|
|
|
return function(_, sub) {
|
|
|
|
return render(sub, environment) || '';
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function substitute(environment, failed) {
|
|
|
|
return function(_, before, bracketed, raw) {
|
|
|
|
var replaced = environment[bracketed || raw];
|
|
|
|
if(replaced != undefined) {
|
|
|
|
return before + replaced;
|
|
|
|
} else {
|
|
|
|
failed[0] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|