2021-01-19 14:53:14 -05:00
|
|
|
'use strict';
|
2012-03-07 17:36:11 +01:00
|
|
|
/*
|
|
|
|
* 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:
|
|
|
|
*
|
2021-01-19 15:26:45 -05:00
|
|
|
* require("./index").require("./path/to/template.ejs")
|
2012-03-07 17:36:11 +01:00
|
|
|
*/
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const ejs = require('ejs');
|
|
|
|
const fs = require('fs');
|
2021-01-19 14:53:14 -05:00
|
|
|
const hooks = require('../../static/js/pluginfw/hooks.js');
|
2020-11-23 13:24:19 -05:00
|
|
|
const path = require('path');
|
|
|
|
const resolve = require('resolve');
|
|
|
|
const settings = require('../utils/Settings');
|
2012-03-07 17:36:11 +01:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
const templateCache = new Map();
|
2020-06-01 16:00:47 +02:00
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-01-19 14:53:14 -05:00
|
|
|
const getCurrentFile = () => exports.info.file_stack[exports.info.file_stack.length - 1];
|
2012-12-06 18:35:35 +01:00
|
|
|
|
2021-01-19 14:53:14 -05:00
|
|
|
exports._init = (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-07 17:36:11 +01:00
|
|
|
|
2021-01-19 14:53:14 -05:00
|
|
|
exports._exit = (b, recursive) => {
|
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
|
|
|
|
2021-01-19 16:01:37 -05:00
|
|
|
exports.begin_block = (name) => {
|
2012-03-07 17:36:11 +01:00
|
|
|
exports.info.block_stack.push(name);
|
2021-02-14 20:18:20 +01:00
|
|
|
exports.info.__output_stack.push(exports.info.__output.get());
|
|
|
|
exports.info.__output.set('');
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2012-03-07 17:36:11 +01:00
|
|
|
|
2021-01-19 14:53:14 -05:00
|
|
|
exports.end_block = () => {
|
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];
|
2021-02-14 20:18:20 +01:00
|
|
|
const content = exports.info.__output.get();
|
|
|
|
exports.info.__output.set(exports.info.__output_stack.pop());
|
2021-01-19 17:34:09 -05:00
|
|
|
const args = {content, renderContext};
|
2020-11-23 13:24:19 -05:00
|
|
|
hooks.callAll(`eejsBlock_${name}`, args);
|
2021-02-14 20:18:20 +01:00
|
|
|
exports.info.__output.set(exports.info.__output.get().concat(args.content));
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|
2012-03-07 17:36:11 +01:00
|
|
|
|
2021-01-19 14:53:14 -05:00
|
|
|
exports.require = (name, args, mod) => {
|
|
|
|
if (args == null) args = {};
|
2012-04-20 14:37:53 +02:00
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
let basedir = __dirname;
|
|
|
|
let paths = [];
|
2012-04-20 14:37:53 +02:00
|
|
|
|
|
|
|
if (exports.info.file_stack.length) {
|
2012-12-06 18:35:35 +01:00
|
|
|
basedir = path.dirname(getCurrentFile().path);
|
2012-03-08 21:01:01 +01:00
|
|
|
}
|
2012-04-20 14:37:53 +02:00
|
|
|
if (mod) {
|
|
|
|
basedir = path.dirname(mod.filename);
|
|
|
|
paths = mod.paths;
|
|
|
|
}
|
|
|
|
|
2021-01-19 18:49:45 -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-06-01 16:00:47 +02:00
|
|
|
|
2021-01-19 17:57:19 -05:00
|
|
|
const cache = settings.maxAge !== 0;
|
2021-01-19 18:43:46 -05:00
|
|
|
const template = cache && templateCache.get(ejspath) || ejs.compile(
|
2021-02-14 20:18:20 +01:00
|
|
|
`<% e._init({get: () => __output, set: (s) => { __output = s; }}); %>${fs.readFileSync(ejspath).toString()}<% e._exit(); %>`,
|
2021-01-19 18:43:46 -05:00
|
|
|
{filename: ejspath});
|
2021-01-19 17:57:19 -05:00
|
|
|
if (cache) templateCache.set(ejspath, template);
|
2020-06-01 16:00:47 +02:00
|
|
|
|
2012-11-01 13:44:06 +01:00
|
|
|
exports.info.args.push(args);
|
2021-01-19 16:01:37 -05:00
|
|
|
exports.info.file_stack.push({path: ejspath});
|
2021-01-19 18:43:46 -05:00
|
|
|
const res = template(args);
|
2012-03-08 21:01:01 +01:00
|
|
|
exports.info.file_stack.pop();
|
2012-11-01 13:44:06 +01:00
|
|
|
exports.info.args.pop();
|
2012-03-08 21:01:01 +01:00
|
|
|
|
2012-03-07 17:36:11 +01:00
|
|
|
return res;
|
2020-11-23 13:24:19 -05:00
|
|
|
};
|