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 //string contains :: its a function if (m.indexOf("::") > -1) { /* program */ re = "[not found " + m + "]" var ma = m.split("::"); fun = ma.shift(); if (fun.indexOf("/") > -1) { //path mode ... go get //holy var funs = fun.split("/"); fun = funs.pop(); var zfunobj = helper.oget(funs.join("/")) var zfun = zfunobj[fun]; if (typeof(zfun) === "function") { //ok ma is [arguments rest].. //if last has | var last = ma.pop(); if (last) { var chain = last.split("|"); if (chain.length > 1) { ma.push(chain.shift()) } else { ma.push(last); } } ma.unshift(zfunobj); re = zfun.apply(obj, ma); while (chain.length) { var ffuna = chain.shift(); if (ffuna.indexOf(":") > -1) { var ffunas = ffuna.split(":"); ffuna = ffunas.shift(); re = templatehelpers[ffuna] ? templatehelpers[ffuna].apply(ffuna, [re].concat(ffunas)) : re; } else { re = templatehelpers[ffuna] ? templatehelpers[ffuna](re) : re; } } } } else { //string contains direct hit on templatehelper functions ? if (templatehelpers[fun]) { // rest skal være arguments re = templatehelpers[fun].apply(helper, [obj].concat(ma)); } else { // does obj have fun ? if (typeof(obj[fun]) === "function") { re = obj[fun].apply(obj, ma) } else { //not found // console.log("FUUUUUUUUUU", fun, ma, obj[fun]) } } } } else { //string contains /. its a path oget if (m.indexOf("/.") > -1) { //path mode re = helper.oget(m) } else { //string contains || its a chain 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)); if(re){ }else{ re = m; } } } else { //string contains just the prop ? 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;