templates.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var _ = require("lodash");
  2. function mker(helper, templatehelpers) {
  3. return function template(str, obj) {
  4. return (str + "").replace(/\{\{([a-zA-Z0-9-\|_\!\.\=\: /]*)\}\}/gi, function(a, m) {
  5. var re
  6. //string contains :: its a function
  7. if (m.indexOf("::") > -1) {
  8. /* program */
  9. re = "[not found " + m + "]"
  10. var ma = m.split("::");
  11. fun = ma.shift();
  12. if (fun.indexOf("/") > -1) {
  13. //path mode ... go get
  14. //holy
  15. var funs = fun.split("/");
  16. fun = funs.pop();
  17. var zfunobj = helper.oget(funs.join("/"))
  18. var zfun = zfunobj[fun];
  19. if (typeof(zfun) === "function") {
  20. //ok ma is [arguments rest]..
  21. //if last has |
  22. var last = ma.pop();
  23. if (last) {
  24. var chain = last.split("|");
  25. if (chain.length > 1) {
  26. ma.push(chain.shift())
  27. } else {
  28. ma.push(last);
  29. }
  30. }
  31. ma.unshift(zfunobj);
  32. re = zfun.apply(obj, ma);
  33. while (chain.length) {
  34. var ffuna = chain.shift();
  35. if (ffuna.indexOf(":") > -1) {
  36. var ffunas = ffuna.split(":");
  37. ffuna = ffunas.shift();
  38. re = templatehelpers[ffuna] ? templatehelpers[ffuna].apply(ffuna, [re].concat(ffunas)) : re;
  39. } else {
  40. re = templatehelpers[ffuna] ? templatehelpers[ffuna](re) : re;
  41. }
  42. }
  43. }
  44. } else {
  45. //string contains direct hit on templatehelper functions ?
  46. if (templatehelpers[fun]) {
  47. // rest skal være arguments
  48. re = templatehelpers[fun].apply(helper, [obj].concat(ma));
  49. } else {
  50. // does obj have fun ?
  51. if (typeof(obj[fun]) === "function") {
  52. re = obj[fun].apply(obj, ma)
  53. } else {
  54. //not found
  55. // console.log("FUUUUUUUUUU", fun, ma, obj[fun])
  56. }
  57. }
  58. }
  59. } else {
  60. //string contains /. its a path oget
  61. if (m.indexOf("/.") > -1) {
  62. //path mode
  63. re = helper.oget(m)
  64. } else {
  65. //string contains || its a chain
  66. if (m.indexOf("||") > -1) {
  67. var mm = m.split("||");
  68. m = mm.shift();
  69. re = (obj[m] ? obj[m] : gget(obj, m));
  70. if (re) {
  71. } else {
  72. m = mm.shift();
  73. re = (obj[m] ? obj[m] : gget(obj, m));
  74. if(re){
  75. }else{
  76. re = m;
  77. }
  78. }
  79. } else {
  80. //string contains just the prop ?
  81. re = (obj[m] ? obj[m] : gget(obj, m));
  82. if (typeof(re) === "undefined") {
  83. re = "";
  84. } else if (typeof(re) === "number") {
  85. re = re + "";
  86. } else if (typeof(re) === "date") {
  87. re = re + "";
  88. }
  89. if (typeof(re) === "object") {
  90. re = _.keys(re).map(function(k) {
  91. var ka = typeof(re[k]) === "function" ? re[k]() : re[k];
  92. return '<a href="#' + ka.path + '" class="ele">' + ka.path + '</a>'
  93. }).join(" ");
  94. } else {
  95. re = re + "";
  96. }
  97. if (re.indexOf("$REF") === 0) {
  98. return re.split("=").slice(1).join("=")
  99. } else {
  100. if (re.substring(0, 2) === "$$") {
  101. return helper.t_content(re.substring(2))
  102. }
  103. }
  104. }
  105. }
  106. }
  107. return re
  108. })
  109. }
  110. }
  111. function gget(obj, path) {
  112. path = path || "";
  113. var arr = path.split(".");
  114. var t = obj;
  115. var done = false;
  116. if (arr.length) {
  117. while (arr.length && !done) {
  118. var check = arr.shift();
  119. if (check !== "") {
  120. t = t[check];
  121. }
  122. if (typeof t !== "object") {
  123. done = true;
  124. }
  125. }
  126. }
  127. return t;
  128. }
  129. function gset(obj, path, value) {
  130. if (typeof obj !== "object" || !obj) {
  131. throw new Error("obj is not Object");
  132. }
  133. if (typeof path !== "string" || path === "") {
  134. throw new Error("path must be string with length > 0");
  135. }
  136. var arr = path.split(".");
  137. var done = false;
  138. var t = obj;
  139. if (arr.length > 1) {
  140. while (arr.length && t && !done) {
  141. var check = arr.shift();
  142. if (typeof t[check] === "object" && arr.length > 0) {
  143. t = t[check];
  144. } else {
  145. done = true;
  146. arr.unshift(check);
  147. }
  148. }
  149. var xt = t;
  150. while (arr.length) {
  151. var tt = arr.shift();
  152. if (arr.length) { //go deeper
  153. xt = xt[tt] = {};
  154. } else {
  155. //last
  156. xt[tt] = value;
  157. }
  158. }
  159. } else {
  160. if (arr.length === 1 && arr[0] !== "") {
  161. t[arr[0]] = value;
  162. }
  163. }
  164. }
  165. module.exports = mker;