deploy1 6 年之前
父節點
當前提交
c86a944486
共有 2 個文件被更改,包括 252 次插入0 次删除
  1. 15 0
      package.json
  2. 237 0
      parsen.js

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "parsen",
+  "version": "1.0.0",
+  "description": "",
+  "main": "parsen.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "repository": {
+    "type": "git",
+    "url": "ssh://git@git.tum.dk/tum.dk/parsen.git"
+  },
+  "author": "",
+  "license": "ISC"
+}

+ 237 - 0
parsen.js

@@ -0,0 +1,237 @@
+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;
+
+}