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:
|
|
|
|
*
|
2012-03-13 20:32:56 +01:00
|
|
|
* require("./index").require("./examples/foo.ejs")
|
2012-03-07 17:36:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
var ejs = require("ejs");
|
|
|
|
var fs = require("fs");
|
2012-03-08 21:01:01 +01:00
|
|
|
var path = require("path");
|
2012-03-13 20:32:56 +01:00
|
|
|
var hooks = require("ep_etherpad-lite/static/js/pluginfw/hooks.js");
|
2012-03-07 17:36:11 +01:00
|
|
|
|
2012-03-13 17:31:40 +01:00
|
|
|
exports.info = {
|
|
|
|
buf_stack: [],
|
|
|
|
block_stack: [],
|
|
|
|
blocks: {},
|
|
|
|
file_stack: [],
|
|
|
|
};
|
|
|
|
|
2012-03-13 17:42:15 +01:00
|
|
|
exports._init = function (b, recursive) {
|
|
|
|
exports.info.buf_stack.push(exports.info.buf);
|
2012-03-07 17:36:11 +01:00
|
|
|
exports.info.buf = b;
|
|
|
|
}
|
|
|
|
|
2012-03-13 17:42:15 +01:00
|
|
|
exports._exit = function (b, recursive) {
|
|
|
|
exports.info.file_stack[exports.info.file_stack.length-1].inherit.forEach(function (item) {
|
2012-03-21 19:27:06 +01:00
|
|
|
exports._require(item.name, item.args);
|
2012-03-13 17:42:15 +01:00
|
|
|
});
|
|
|
|
exports.info.buf = exports.info.buf_stack.pop();
|
|
|
|
}
|
|
|
|
|
2012-03-07 17:36:11 +01:00
|
|
|
exports.begin_capture = function() {
|
|
|
|
exports.info.buf_stack.push(exports.info.buf.concat());
|
|
|
|
exports.info.buf.splice(0, exports.info.buf.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.end_capture = function () {
|
|
|
|
var res = exports.info.buf.join("");
|
|
|
|
exports.info.buf.splice.apply(
|
|
|
|
exports.info.buf,
|
|
|
|
[0, exports.info.buf.length].concat(exports.info.buf_stack.pop()));
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.begin_define_block = function (name) {
|
|
|
|
if (typeof exports.info.blocks[name] == "undefined")
|
|
|
|
exports.info.blocks[name] = {};
|
|
|
|
exports.info.block_stack.push(name);
|
|
|
|
exports.begin_capture();
|
|
|
|
}
|
|
|
|
|
2012-03-13 20:32:56 +01:00
|
|
|
exports.super = function () {
|
|
|
|
exports.info.buf.push('<!eejs!super!>');
|
|
|
|
}
|
|
|
|
|
2012-03-07 17:36:11 +01:00
|
|
|
exports.end_define_block = function () {
|
|
|
|
content = exports.end_capture();
|
|
|
|
var name = exports.info.block_stack.pop();
|
|
|
|
if (typeof exports.info.blocks[name].content == "undefined")
|
|
|
|
exports.info.blocks[name].content = content;
|
2012-03-13 20:32:56 +01:00
|
|
|
else if (typeof exports.info.blocks[name].content.indexOf('<!eejs!super!>'))
|
|
|
|
exports.info.blocks[name].content = exports.info.blocks[name].content.replace('<!eejs!super!>', content);
|
|
|
|
|
2012-03-07 17:36:11 +01:00
|
|
|
return exports.info.blocks[name].content;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.end_block = function () {
|
2012-03-13 20:32:56 +01:00
|
|
|
var name = exports.info.block_stack[exports.info.block_stack.length-1];
|
|
|
|
var args = {content: exports.end_define_block()};
|
|
|
|
hooks.callAll("eejsBlock_" + name, args);
|
|
|
|
exports.info.buf.push(args.content);
|
2012-03-07 17:36:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.begin_block = exports.begin_define_block;
|
|
|
|
|
2012-03-13 17:42:15 +01:00
|
|
|
exports.inherit = function (name, args) {
|
|
|
|
exports.info.file_stack[exports.info.file_stack.length-1].inherit.push({name:name, args:args});
|
|
|
|
}
|
|
|
|
|
2012-03-07 17:36:11 +01:00
|
|
|
exports.require = function (name, args) {
|
|
|
|
if (args == undefined) args = {};
|
2012-03-08 21:01:01 +01:00
|
|
|
|
2012-03-13 17:31:40 +01:00
|
|
|
if ((name.indexOf("./") == 0 || name.indexOf("../") == 0) && exports.info.file_stack.length) {
|
2012-03-13 17:42:15 +01:00
|
|
|
name = path.join(path.dirname(exports.info.file_stack[exports.info.file_stack.length-1].path), name);
|
2012-03-08 21:01:01 +01:00
|
|
|
}
|
|
|
|
var ejspath = require.resolve(name)
|
|
|
|
|
2012-03-13 17:31:40 +01:00
|
|
|
args.e = exports;
|
2012-03-22 18:34:38 +01:00
|
|
|
args.require = require;
|
2012-03-13 17:42:15 +01:00
|
|
|
var template = '<% e._init(buf); %>' + fs.readFileSync(ejspath).toString() + '<% e._exit(); %>';
|
2012-03-13 17:31:40 +01:00
|
|
|
|
2012-03-13 17:42:15 +01:00
|
|
|
exports.info.file_stack.push({path: ejspath, inherit: []});
|
|
|
|
var res = ejs.render(template, args);
|
2012-03-08 21:01:01 +01:00
|
|
|
exports.info.file_stack.pop();
|
|
|
|
|
2012-03-07 17:36:11 +01:00
|
|
|
return res;
|
|
|
|
}
|
2012-03-21 19:27:06 +01:00
|
|
|
|
|
|
|
exports._require = function (name, args) {
|
|
|
|
exports.info.buf.push(exports.require(name, args));
|
|
|
|
}
|