2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-29 23:13:21 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var mustache = require("mustache");
|
2017-11-30 14:38:50 +01:00
|
|
|
var yaml = require("js-yaml");
|
|
|
|
|
2014-05-05 09:58:53 +02:00
|
|
|
|
2016-11-16 15:08:14 +01:00
|
|
|
/**
|
2017-01-25 18:12:53 +01:00
|
|
|
* Custom Mustache Context capable to resolve message property and node
|
|
|
|
* flow and global context
|
2016-11-16 15:08:14 +01:00
|
|
|
*/
|
2017-09-21 14:38:45 +02:00
|
|
|
function NodeContext(msg, nodeContext, parent, escapeStrings) {
|
2017-01-23 16:34:34 +01:00
|
|
|
this.msgContext = new mustache.Context(msg,parent);
|
2016-11-16 15:08:14 +01:00
|
|
|
this.nodeContext = nodeContext;
|
2017-09-21 14:38:45 +02:00
|
|
|
this.escapeStrings = escapeStrings;
|
2016-11-16 15:08:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeContext.prototype = new mustache.Context();
|
|
|
|
|
|
|
|
NodeContext.prototype.lookup = function (name) {
|
|
|
|
// try message first:
|
2017-01-23 16:34:34 +01:00
|
|
|
try {
|
|
|
|
var value = this.msgContext.lookup(name);
|
|
|
|
if (value !== undefined) {
|
2017-09-21 14:38:45 +02:00
|
|
|
if (this.escapeStrings && typeof value === "string") {
|
|
|
|
value = value.replace(/\\/g, "\\\\");
|
|
|
|
value = value.replace(/\n/g, "\\n");
|
|
|
|
value = value.replace(/\t/g, "\\t");
|
|
|
|
value = value.replace(/\r/g, "\\r");
|
|
|
|
value = value.replace(/\f/g, "\\f");
|
|
|
|
value = value.replace(/[\b]/g, "\\b");
|
|
|
|
}
|
2017-01-23 16:34:34 +01:00
|
|
|
return value;
|
|
|
|
}
|
2016-11-16 15:08:14 +01:00
|
|
|
|
2017-01-23 16:34:34 +01:00
|
|
|
// try node context:
|
|
|
|
var dot = name.indexOf(".");
|
|
|
|
if (dot > 0) {
|
|
|
|
var contextName = name.substr(0, dot);
|
|
|
|
var variableName = name.substr(dot + 1);
|
2016-11-16 15:08:14 +01:00
|
|
|
|
2017-01-23 16:34:34 +01:00
|
|
|
if (contextName === "flow" && this.nodeContext.flow) {
|
|
|
|
return this.nodeContext.flow.get(variableName);
|
|
|
|
}
|
|
|
|
else if (contextName === "global" && this.nodeContext.global) {
|
|
|
|
return this.nodeContext.global.get(variableName);
|
|
|
|
}
|
2016-11-16 15:08:14 +01:00
|
|
|
}
|
2017-01-23 16:34:34 +01:00
|
|
|
}catch(err) {
|
|
|
|
throw err;
|
2016-11-16 15:08:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 16:34:34 +01:00
|
|
|
NodeContext.prototype.push = function push (view) {
|
|
|
|
return new NodeContext(view, this.nodeContext,this.msgContext);
|
|
|
|
};
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function TemplateNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.name = n.name;
|
2014-07-10 09:23:44 +02:00
|
|
|
this.field = n.field || "payload";
|
2014-05-04 00:32:04 +02:00
|
|
|
this.template = n.template;
|
2016-02-11 14:16:15 +01:00
|
|
|
this.syntax = n.syntax || "mustache";
|
2016-01-04 16:03:33 +01:00
|
|
|
this.fieldType = n.fieldType || "msg";
|
2017-01-25 18:12:53 +01:00
|
|
|
this.outputFormat = n.output || "str";
|
2014-07-10 09:23:44 +02:00
|
|
|
|
2016-01-04 16:03:33 +01:00
|
|
|
var node = this;
|
2014-07-10 09:23:44 +02:00
|
|
|
node.on("input", function(msg) {
|
2014-09-08 22:10:06 +02:00
|
|
|
try {
|
2016-02-11 14:16:15 +01:00
|
|
|
var value;
|
2017-12-05 13:24:06 +01:00
|
|
|
/***
|
|
|
|
* Allow template contents to be defined externally
|
|
|
|
* through inbound msg.template IFF node.template empty
|
|
|
|
*/
|
|
|
|
if (msg.hasOwnProperty("template")) {
|
|
|
|
if (node.template == "" || node.template === null) {
|
|
|
|
node.template = msg.template;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 14:16:15 +01:00
|
|
|
if (node.syntax === "mustache") {
|
2017-09-21 14:38:45 +02:00
|
|
|
if (node.outputFormat === "json") {
|
|
|
|
value = mustache.render(node.template,new NodeContext(msg, node.context(), null, true));
|
|
|
|
} else {
|
|
|
|
value = mustache.render(node.template,new NodeContext(msg, node.context(), null, false));
|
|
|
|
}
|
2016-02-11 14:16:15 +01:00
|
|
|
} else {
|
|
|
|
value = node.template;
|
|
|
|
}
|
2017-01-25 18:12:53 +01:00
|
|
|
if (node.outputFormat === "json") {
|
|
|
|
value = JSON.parse(value);
|
|
|
|
}
|
2017-11-30 14:38:50 +01:00
|
|
|
if (node.outputFormat === "yaml") {
|
|
|
|
value = yaml.load(value);
|
|
|
|
}
|
2017-01-25 18:12:53 +01:00
|
|
|
|
2016-01-04 16:03:33 +01:00
|
|
|
if (node.fieldType === 'msg') {
|
|
|
|
RED.util.setMessageProperty(msg,node.field,value);
|
|
|
|
} else if (node.fieldType === 'flow') {
|
|
|
|
node.context().flow.set(node.field,value);
|
|
|
|
} else if (node.fieldType === 'global') {
|
|
|
|
node.context().global.set(node.field,value);
|
|
|
|
}
|
|
|
|
node.send(msg);
|
2014-09-08 22:10:06 +02:00
|
|
|
} catch(err) {
|
|
|
|
node.error(err.message);
|
2014-05-05 09:58:53 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
2014-05-05 09:58:53 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("template",TemplateNode);
|
|
|
|
RED.library.register("templates");
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|