116 lines
3.6 KiB
JavaScript
116 lines
3.6 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const nunjucks_1 = __importDefault(require("nunjucks"));
|
|
const immutable_1 = __importDefault(require("immutable"));
|
|
class TemplateEngine extends immutable_1.default.Record({
|
|
// Map of {TemplateBlock}
|
|
blocks: immutable_1.default.Map(),
|
|
// Map of Extension
|
|
extensions: immutable_1.default.Map(),
|
|
// Map of filters: {string} name -> {Function} fn
|
|
filters: immutable_1.default.Map(),
|
|
// Map of globals: {string} name -> {Mixed}
|
|
globals: immutable_1.default.Map(),
|
|
// Context for filters / blocks
|
|
context: Object(),
|
|
// Nunjucks loader
|
|
loader: new nunjucks_1.default.FileSystemLoader("views")
|
|
}, "TemplateEngine") {
|
|
getBlocks() {
|
|
return this.get("blocks");
|
|
}
|
|
getGlobals() {
|
|
return this.get("globals");
|
|
}
|
|
getFilters() {
|
|
return this.get("filters");
|
|
}
|
|
getShortcuts() {
|
|
return this.get("shortcuts");
|
|
}
|
|
getLoader() {
|
|
return this.get("loader");
|
|
}
|
|
getContext() {
|
|
return this.get("context");
|
|
}
|
|
getExtensions() {
|
|
return this.get("extensions");
|
|
}
|
|
/**
|
|
Return a block by its name (or undefined)
|
|
|
|
@param {string} name
|
|
@return {TemplateBlock}
|
|
*/
|
|
getBlock(name) {
|
|
const blocks = this.getBlocks();
|
|
return blocks.find((block) => {
|
|
return block.getName() === name;
|
|
});
|
|
}
|
|
/**
|
|
Return a nunjucks environment from this configuration
|
|
*/
|
|
toNunjucks(blocksOutput) {
|
|
const loader = this.getLoader();
|
|
const blocks = this.getBlocks();
|
|
const filters = this.getFilters();
|
|
const globals = this.getGlobals();
|
|
const extensions = this.getExtensions();
|
|
const context = this.getContext();
|
|
const env = new nunjucks_1.default.Environment(loader, {
|
|
// Escaping is done after by the asciidoc/markdown parser
|
|
autoescape: false,
|
|
// Syntax
|
|
tags: {
|
|
blockStart: "{%",
|
|
blockEnd: "%}",
|
|
variableStart: "{{",
|
|
variableEnd: "}}",
|
|
commentStart: "{###",
|
|
commentEnd: "###}"
|
|
}
|
|
});
|
|
// Add filters
|
|
filters.forEach((filterFn, filterName) => {
|
|
env.addFilter(filterName, filterFn.bind(context));
|
|
});
|
|
// Add blocks
|
|
blocks.forEach((block) => {
|
|
const extName = block.getExtensionName();
|
|
const Ext = block.toNunjucksExt(context, blocksOutput);
|
|
env.addExtension(extName, new Ext());
|
|
});
|
|
// Add globals
|
|
globals.forEach((globalValue, globalName) => {
|
|
env.addGlobal(globalName, globalValue);
|
|
});
|
|
// Add other extensions
|
|
extensions.forEach((ext, extName) => {
|
|
env.addExtension(extName, ext);
|
|
});
|
|
return env;
|
|
}
|
|
/**
|
|
Create a template engine
|
|
|
|
@param {Object} def
|
|
@return {TemplateEngine}
|
|
*/
|
|
static create(def) {
|
|
return new TemplateEngine({
|
|
blocks: immutable_1.default.List(def.blocks || []),
|
|
extensions: immutable_1.default.Map(def.extensions || {}),
|
|
filters: immutable_1.default.Map(def.filters || {}),
|
|
globals: immutable_1.default.Map(def.globals || {}),
|
|
context: def.context,
|
|
loader: def.loader
|
|
});
|
|
}
|
|
}
|
|
exports.default = TemplateEngine;
|