parsen.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. module.exports = function parsen(str) {
  2. function setpath(obj, path, value) {
  3. if (typeof obj !== "object" || !obj) {
  4. throw new Error("obj is not Object");
  5. }
  6. if (typeof path !== "string" || path === "") {
  7. throw new Error("path must be string with length > 0");
  8. }
  9. var arr = path.split(".");
  10. var done = false;
  11. var t = obj;
  12. if (arr.length > 1) {
  13. while (arr.length && t && !done) {
  14. var check = arr.shift();
  15. if (typeof t[check] === "object" && arr.length > 0) {
  16. t = t[check];
  17. } else {
  18. done = true;
  19. arr.unshift(check);
  20. }
  21. }
  22. var xt = t;
  23. while (arr.length) {
  24. var tt = arr.shift();
  25. if (arr.length) { //go deeper
  26. xt = xt[tt] = {};
  27. } else {
  28. //last
  29. xt[tt] = value;
  30. }
  31. }
  32. } else {
  33. if (arr.length === 1 && arr[0] !== "") {
  34. t[arr[0]] = value;
  35. }
  36. }
  37. }
  38. var r = {
  39. name: "",
  40. title: "",
  41. content: "",
  42. data: {},
  43. templates: {},
  44. scripts: {},
  45. styles: {},
  46. pages: {},
  47. elements: {},
  48. elements_c: {},
  49. commands: {},
  50. comments: {},
  51. structures: {},
  52. libs: [],
  53. roots: [],
  54. langs: {},
  55. meta: {}
  56. };
  57. //r.raw = str;
  58. var reg = new RegExp(/\<!--page::([a-zAz0-9\/_\.\-]*?)::begin--\>([\s\S]*?)\<!--page::([a-zAz0-9_\/\.\-]*?)::end--\>/gi);
  59. str = str.replace(reg, function(matched, name, content, nameend, line, d) {
  60. r.pages[name.trim()] = content.trim();
  61. return "";
  62. })
  63. var reg = new RegExp(/\<!--template::([a-zAz0-9\/_\.\-]*?)::begin--\>([\s\S]*?)\<!--template::([a-zAz0-9_\/\.\-]*?)::end--\>/gi);
  64. str = str.replace(reg, function(matched, name, content, nameend, line, d) {
  65. r.templates[name.trim()] = content.trim();
  66. return "";
  67. })
  68. var reg = new RegExp(/\<script(.*)\>([\s\S]*?)\<\/script\>/gi);
  69. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  70. var attr = {};
  71. r.scripts[attrstr.trim()] = r.scripts[attrstr.trim()] || [];
  72. r.scripts[attrstr.trim()].push(content);
  73. return "";
  74. })
  75. var reg = new RegExp(/\<style(.*)\>([\s\S]*?)\<\/style\>/gi);
  76. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  77. var attr = {};
  78. r.styles[attrstr.trim()] = r.styles[attrstr.trim()] || [];
  79. r.styles[attrstr.trim()].push(content);
  80. return "";
  81. })
  82. var reg = new RegExp(/\<!--data::([a-zAz0-9_]*?)::begin--\>([\s\S]*?)\<!--data::([a-zAz0-9_]*?)::end--\>/gi);
  83. str = str.replace(reg, function(matched, name, content, nameend, line, d) {
  84. try {
  85. var rr = JSON.parse(content.trim());
  86. r.data[name.trim()] = rr
  87. } catch (e) {
  88. r.data[name.trim()] = content.trim()
  89. }
  90. return "";
  91. })
  92. var reg = new RegExp(/\<!--structure::([a-zAz0-9_\-\.]*?)::begin--\>([\s\S]*?)\<!--structure::([a-zAz0-9_\-\.]*?)::end--\>/gi);
  93. str = str.replace(reg, function(matched, name, content, nameend, line, d) {
  94. r.structures[name.trim()] = content.trim();
  95. return "";
  96. })
  97. var reg = new RegExp(/\/\*([\s\S]*?)\*\//gi);
  98. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  99. //console.log("COMMENT:;",attrstr)
  100. return "";
  101. })
  102. var reg = new RegExp(/\/\/\/\/(.*)([\s\S]*?)\n\n\n/gi);
  103. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  104. var attr = {};
  105. r.comments[attrstr.trim()] = r.comments[attrstr.trim()] || [];
  106. r.comments[attrstr.trim()].push(content.trim());
  107. return "";
  108. })
  109. var reg = new RegExp(/\>\>\>\>(.*)\n/gi);
  110. str = str.replace(reg, function(matched, attrstr, line, d) {
  111. var attr = {};
  112. r.libs.push(attrstr.trim());
  113. return "";
  114. })
  115. var reg = new RegExp(/meta\.([0-9a-zA-Z\.\_-]*) (.*)/gi);
  116. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  117. setpath(r.meta, attrstr, content);
  118. return "";
  119. })
  120. var reg = new RegExp(/!!add_filters (.*)([\s\S]*?)!!\n\n/gi);
  121. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  122. /* console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxARRRRR")
  123. console.log("match::::",matched);
  124. console.log("attrstr::::",attrstr);
  125. console.log("content::::",content);*/
  126. return "";
  127. })
  128. var reg = new RegExp(/\!\!\!\!(.*)([\s\S]*?)\!\!\!\!/gi);
  129. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  130. var attr = {};
  131. r.commands[attrstr.trim()] = r.commands[attrstr.trim()] || [];
  132. r.commands[attrstr.trim()].push(content.trim());
  133. return "";
  134. })
  135. var reg = new RegExp(/\#\#\#\#\!(.*)([\s\S]*?)\n\n\n/gi);
  136. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  137. var attr = {};
  138. r.elements_c[attrstr.trim()] = r.elements_c[attrstr.trim()] || [];
  139. r.elements_c[attrstr.trim()].push(content.trim());
  140. return "";
  141. })
  142. var reg = new RegExp(/\#\#\#\#(.*)([\s\S]*?)\n\n\n/gi);
  143. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  144. var attr = {};
  145. r.elements[attrstr.trim()] = r.elements[attrstr.trim()] || [];
  146. r.elements[attrstr.trim()].push(content.trim());
  147. return "";
  148. })
  149. var reg = new RegExp(/:::: (.*) \n/gi);
  150. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  151. var attr = {};
  152. var x = attrstr.split(" ");
  153. var prop = x.shift();
  154. var val = x.join(" ");
  155. r.roots.push([prop, val]);
  156. return "";
  157. })
  158. var reg = new RegExp(/langs ([a-zAz0-9_\-\.\(\)]*?)\n/gi);
  159. str = str.replace(reg, function(matched, attrstr, content, line, d) {
  160. var attr = {};
  161. var x = attrstr.split(" ");
  162. var prop = x.shift();
  163. var val = x.join(" ");
  164. var t = val.replace(/"([^".]*)" "([^".]*)"/gi, function(mat, valo, valt) {
  165. r.langs[prop] = r.langs[prop] || {};
  166. r.langs[prop][valo] = valt;
  167. })
  168. // r.roots.push([prop,val]);
  169. return "";
  170. })
  171. r.content = str.trim();
  172. var v = r.content.split("\n");
  173. r.title = v.shift();
  174. r.content = v.join("\n");
  175. return r;
  176. }