var _ = require("lodash"); function mker(helper, templatehelpers) { return function template(str, obj) { return (str + "").replace(/\{\{([a-zA-Z0-9-\|_\.\:/]*)\}\}/gi, function(a, m) { var re if (m.indexOf("::") > -1) { /* program */ re = " TEST " var ma = m.split("::"); fun = ma.shift(); if (fun.indexOf("/") > -1) { //path mode ... go get var funs = fun.split("/"); fun = funs.pop(); var zfun = helper.oget(funs.join("/"))[fun] if (typeof(zfun) === "function") { re = zfun.apply(obj, ma); } } else { if (templatehelpers[fun]) { // rest skal være arguments re = templatehelpers[fun](obj); } if (obj[fun]) { if (typeof(obj[fun]) === "function") { re = obj[fun].apply(obj, ma) } else { // console.log("FUUUUUUUUUU", fun, ma, obj[fun]) } } // } } else { if (m.indexOf("||") > -1) { var mm = m.split("||"); m = mm.shift(); re = (obj[m] ? obj[m] : gget(obj, m)); if (re) { } else { m = mm.shift(); re = (obj[m] ? obj[m] : gget(obj, m)); } } else { re = (obj[m] ? obj[m] : gget(obj, m)); if (typeof(re) === "undefined") { re = ""; } else if (typeof(re) === "number") { re = re + ""; } else if (typeof(re) === "date") { re = re + ""; } if (typeof(re) === "object") { re = _.keys(re).map(function(k) { var ka = typeof(re[k]) === "function" ? re[k]() : re[k]; return '' + ka.path + '' }).join(" "); } else { re = re + ""; } if (re.indexOf("$REF") === 0) { return re.split("=").slice(1).join("=") } else { if (re.substring(0, 2) === "$$") { return helper.t_content(re.substring(2)) } } } } return re }) } } function gget(obj, path) { path = path || ""; var arr = path.split("."); var t = obj; var done = false; if (arr.length) { while (arr.length && !done) { var check = arr.shift(); if (check !== "") { t = t[check]; } if (typeof t !== "object") { done = true; } } } return t; } function gset(obj, path, value) { if (typeof obj !== "object" || !obj) { throw new Error("obj is not Object"); } if (typeof path !== "string" || path === "") { throw new Error("path must be string with length > 0"); } var arr = path.split("."); var done = false; var t = obj; if (arr.length > 1) { while (arr.length && t && !done) { var check = arr.shift(); if (typeof t[check] === "object" && arr.length > 0) { t = t[check]; } else { done = true; arr.unshift(check); } } var xt = t; while (arr.length) { var tt = arr.shift(); if (arr.length) { //go deeper xt = xt[tt] = {}; } else { //last xt[tt] = value; } } } else { if (arr.length === 1 && arr[0] !== "") { t[arr[0]] = value; } } } module.exports = mker;