etherpad-lite/src/node/eejs/index.js

149 lines
4 KiB
JavaScript
Raw Normal View History

/*
* Copyright (c) 2011 RedHog (Egil Möller) <egil.moller@freecode.no>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Basic usage:
*
* require("./index").require("./examples/foo.ejs")
*/
2020-11-23 13:24:19 -05:00
const ejs = require('ejs');
const fs = require('fs');
const path = require('path');
const hooks = require('ep_etherpad-lite/static/js/pluginfw/hooks.js');
const resolve = require('resolve');
const settings = require('../utils/Settings');
2020-11-23 13:24:19 -05:00
const templateCache = new Map();
2012-03-13 17:31:40 +01:00
exports.info = {
2015-04-11 09:53:27 +01:00
__output_stack: [],
2012-03-13 17:31:40 +01:00
block_stack: [],
file_stack: [],
2020-11-23 13:24:19 -05:00
args: [],
2012-03-13 17:31:40 +01:00
};
function getCurrentFile() {
2020-11-23 13:24:19 -05:00
return exports.info.file_stack[exports.info.file_stack.length - 1];
}
function createBlockId(name) {
2020-11-23 13:24:19 -05:00
return `${getCurrentFile().path}|${name}`;
}
2012-03-13 17:42:15 +01:00
exports._init = function (b, recursive) {
2015-04-11 09:53:27 +01:00
exports.info.__output_stack.push(exports.info.__output);
exports.info.__output = b;
2020-11-23 13:24:19 -05:00
};
2012-03-13 17:42:15 +01:00
exports._exit = function (b, recursive) {
2020-11-23 13:24:19 -05:00
getCurrentFile().inherit.forEach((item) => {
exports._require(item.name, item.args);
2012-03-13 17:42:15 +01:00
});
2015-04-11 09:53:27 +01:00
exports.info.__output = exports.info.__output_stack.pop();
2020-11-23 13:24:19 -05:00
};
2012-03-13 17:42:15 +01:00
2020-11-23 13:24:19 -05:00
exports.begin_capture = function () {
2015-04-11 09:53:27 +01:00
exports.info.__output_stack.push(exports.info.__output.concat());
exports.info.__output.splice(0, exports.info.__output.length);
2020-11-23 13:24:19 -05:00
};
exports.end_capture = function () {
2020-11-23 13:24:19 -05:00
const res = exports.info.__output.join('');
2015-04-11 09:53:27 +01:00
exports.info.__output.splice.apply(
2020-11-23 13:24:19 -05:00
exports.info.__output,
[0, exports.info.__output.length].concat(exports.info.__output_stack.pop()));
return res;
2020-11-23 13:24:19 -05:00
};
exports.begin_define_block = function (name) {
exports.info.block_stack.push(name);
exports.begin_capture();
2020-11-23 13:24:19 -05:00
};
exports.end_define_block = function () {
2020-11-23 13:24:19 -05:00
const content = exports.end_capture();
return content;
2020-11-23 13:24:19 -05:00
};
exports.end_block = function () {
2020-11-23 13:24:19 -05:00
const name = exports.info.block_stack.pop();
const renderContext = exports.info.args[exports.info.args.length - 1];
const args = {content: exports.end_define_block(), renderContext};
hooks.callAll(`eejsBlock_${name}`, args);
2015-04-11 09:53:27 +01:00
exports.info.__output.push(args.content);
2020-11-23 13:24:19 -05:00
};
exports.begin_block = exports.begin_define_block;
2012-03-13 17:42:15 +01:00
exports.inherit = function (name, args) {
2020-11-23 13:24:19 -05:00
getCurrentFile().inherit.push({name, args});
};
2012-03-13 17:42:15 +01:00
exports.require = function (name, args, mod) {
if (args == undefined) args = {};
2020-11-23 13:24:19 -05:00
let basedir = __dirname;
let paths = [];
if (exports.info.file_stack.length) {
basedir = path.dirname(getCurrentFile().path);
2012-03-08 21:01:01 +01:00
}
if (mod) {
basedir = path.dirname(mod.filename);
paths = mod.paths;
}
2020-11-23 13:24:19 -05:00
const ejspath = resolve.sync(
name,
{
paths,
basedir,
extensions: ['.html', '.ejs'],
},
);
2012-03-08 21:01:01 +01:00
2012-03-13 17:31:40 +01:00
args.e = exports;
2012-03-22 18:34:38 +01:00
args.require = require;
2020-11-23 13:24:19 -05:00
let template;
if (settings.maxAge !== 0) { // don't cache if maxAge is 0
if (!templateCache.has(ejspath)) {
2020-11-23 13:24:19 -05:00
template = `<% e._init(__output); %>${fs.readFileSync(ejspath).toString()}<% e._exit(); %>`;
templateCache.set(ejspath, template);
} else {
2020-11-23 13:24:19 -05:00
template = templateCache.get(ejspath);
}
2020-11-23 13:24:19 -05:00
} else {
template = `<% e._init(__output); %>${fs.readFileSync(ejspath).toString()}<% e._exit(); %>`;
}
exports.info.args.push(args);
2012-03-13 17:42:15 +01:00
exports.info.file_stack.push({path: ejspath, inherit: []});
2020-11-23 13:24:19 -05:00
if (settings.maxAge !== 0) {
var res = ejs.render(template, args, {cache: true, filename: ejspath});
} else {
var res = ejs.render(template, args, {cache: false, filename: ejspath});
}
2012-03-08 21:01:01 +01:00
exports.info.file_stack.pop();
exports.info.args.pop();
2012-03-08 21:01:01 +01:00
return res;
2020-11-23 13:24:19 -05:00
};
exports._require = function (name, args) {
2015-04-11 09:53:27 +01:00
exports.info.__output.push(exports.require(name, args));
2020-11-23 13:24:19 -05:00
};