123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- module.exports = function parsen(str) {
- function setpath(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;
- }
- }
- }
- var r = {
- name: "",
- title: "",
- content: "",
- data: {},
- templates: {},
- scripts: {},
- styles: {},
- pages: {},
- elements: {},
- elements_c: {},
- commands: {},
- comments: {},
- structures: {},
- libs: [],
- roots: [],
- langs: {},
- meta: {}
- };
- //r.raw = str;
- var reg = new RegExp(/\<!--page::([a-zAz0-9\/_\.\-]*?)::begin--\>([\s\S]*?)\<!--page::([a-zAz0-9_\/\.\-]*?)::end--\>/gi);
- str = str.replace(reg, function(matched, name, content, nameend, line, d) {
- r.pages[name.trim()] = content.trim();
- return "";
- })
- var reg = new RegExp(/\<!--template::([a-zAz0-9\/_\.\-]*?)::begin--\>([\s\S]*?)\<!--template::([a-zAz0-9_\/\.\-]*?)::end--\>/gi);
- str = str.replace(reg, function(matched, name, content, nameend, line, d) {
- r.templates[name.trim()] = content.trim();
- return "";
- })
- var reg = new RegExp(/\<script(.*)\>([\s\S]*?)\<\/script\>/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- r.scripts[attrstr.trim()] = r.scripts[attrstr.trim()] || [];
- r.scripts[attrstr.trim()].push(content);
- return "";
- })
- var reg = new RegExp(/\<style(.*)\>([\s\S]*?)\<\/style\>/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- r.styles[attrstr.trim()] = r.styles[attrstr.trim()] || [];
- r.styles[attrstr.trim()].push(content);
- return "";
- })
- var reg = new RegExp(/\<!--data::([a-zAz0-9_]*?)::begin--\>([\s\S]*?)\<!--data::([a-zAz0-9_]*?)::end--\>/gi);
- str = str.replace(reg, function(matched, name, content, nameend, line, d) {
- try {
- var rr = JSON.parse(content.trim());
- r.data[name.trim()] = rr
- } catch (e) {
- r.data[name.trim()] = content.trim()
- }
- return "";
- })
- var reg = new RegExp(/\<!--structure::([a-zAz0-9_\-\.]*?)::begin--\>([\s\S]*?)\<!--structure::([a-zAz0-9_\-\.]*?)::end--\>/gi);
- str = str.replace(reg, function(matched, name, content, nameend, line, d) {
- r.structures[name.trim()] = content.trim();
- return "";
- })
- var reg = new RegExp(/\/\*([\s\S]*?)\*\//gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- //console.log("COMMENT:;",attrstr)
- return "";
- })
- var reg = new RegExp(/\/\/\/\/(.*)([\s\S]*?)\n\n\n/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- r.comments[attrstr.trim()] = r.comments[attrstr.trim()] || [];
- r.comments[attrstr.trim()].push(content.trim());
- return "";
- })
- var reg = new RegExp(/\>\>\>\>(.*)\n/gi);
- str = str.replace(reg, function(matched, attrstr, line, d) {
- var attr = {};
- r.libs.push(attrstr.trim());
- return "";
- })
- var reg = new RegExp(/meta\.([0-9a-zA-Z\.\_-]*) (.*)/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- setpath(r.meta, attrstr, content);
- return "";
- })
- var reg = new RegExp(/!!add_filters (.*)([\s\S]*?)!!\n\n/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- /* console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxARRRRR")
- console.log("match::::",matched);
- console.log("attrstr::::",attrstr);
- console.log("content::::",content);*/
- return "";
- })
- var reg = new RegExp(/\!\!\!\!(.*)([\s\S]*?)\!\!\!\!/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- r.commands[attrstr.trim()] = r.commands[attrstr.trim()] || [];
- r.commands[attrstr.trim()].push(content.trim());
- return "";
- })
- var reg = new RegExp(/\#\#\#\#\!(.*)([\s\S]*?)\n\n\n/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- r.elements_c[attrstr.trim()] = r.elements_c[attrstr.trim()] || [];
- r.elements_c[attrstr.trim()].push(content.trim());
- return "";
- })
- var reg = new RegExp(/\#\#\#\#(.*)([\s\S]*?)\n\n\n/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- r.elements[attrstr.trim()] = r.elements[attrstr.trim()] || [];
- r.elements[attrstr.trim()].push(content.trim());
- return "";
- })
- var reg = new RegExp(/:::: (.*) \n/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- var x = attrstr.split(" ");
- var prop = x.shift();
- var val = x.join(" ");
- r.roots.push([prop, val]);
- return "";
- })
- var reg = new RegExp(/langs ([a-zAz0-9_\-\.\(\)]*?)\n/gi);
- str = str.replace(reg, function(matched, attrstr, content, line, d) {
- var attr = {};
- var x = attrstr.split(" ");
- var prop = x.shift();
- var val = x.join(" ");
- var t = val.replace(/"([^".]*)" "([^".]*)"/gi, function(mat, valo, valt) {
- r.langs[prop] = r.langs[prop] || {};
- r.langs[prop][valo] = valt;
- })
- // r.roots.push([prop,val]);
- return "";
- })
- r.content = str.trim();
- var v = r.content.split("\n");
- r.title = v.shift();
- r.content = v.join("\n");
- return r;
- }
|