Browse code

initial import

devnewton authored on 06/09/2017 08:32:29
Showing 7 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,3 @@
1
+/node_modules/
2
+/nbproject/project.xml
3
+/nbproject/project.properties
0 4
\ No newline at end of file
1 5
new file mode 100644
... ...
@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright (c) 2017 devnewton <devnewton@bci.im>
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy
6
+of this software and associated documentation files (the "Software"), to deal
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+copies of the Software, and to permit persons to whom the Software is
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in
13
+all copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+THE SOFTWARE.
0 22
\ No newline at end of file
1 23
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+Node Red node to parse and transform message payload using [PEG.js](https://pegjs.org).
0 2
\ No newline at end of file
1 3
new file mode 100644
... ...
@@ -0,0 +1,7 @@
1
+{
2
+    "pegjs": {
3
+        "label": {
4
+            "grammar": "Grammar"
5
+        }
6
+    }
7
+}
0 8
new file mode 100644
... ...
@@ -0,0 +1,21 @@
1
+{
2
+  "name": "node-red-contrib-pegjs",
3
+  "version": "1.0.0",
4
+  "keywords": [
5
+    "util",
6
+    "functional",
7
+    "server",
8
+    "client",
9
+    "browser"
10
+  ],
11
+  "author": "devnewton",
12
+  "contributors": [],
13
+  "dependencies": {
14
+    "pegjs": "^0.10.0"
15
+  },
16
+  "node-red": {
17
+    "nodes": {
18
+      "pegjs": "pegjs.js"
19
+    }
20
+  }
21
+}
0 22
new file mode 100644
... ...
@@ -0,0 +1,46 @@
1
+<script type="text/x-red" data-template-name="pegjs">
2
+    <div class="form-row">
3
+        <label for="node-input-grammar"><i class="fa fa-filter"></i> <span data-i18n="pegjs.label.grammar"></span></label>
4
+        <textarea id="node-input-grammar" placeholder="grammar">
5
+    </div>
6
+    <br/>
7
+    <div class="form-row">
8
+        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
9
+        <input type="text" id="node-input-name" style="width:70%" data-i18n="[placeholder]common.label.name">
10
+    </div>
11
+</script>
12
+
13
+<script type="text/x-red" data-help-name="pegjs">
14
+    <p>Parse and transform message payload using <a href="https://pegjs.org" >PEG.js</a></p>
15
+    <h3>Inputs</h3>
16
+    <dl class="message-properties">
17
+        <dt>payload <span class="property-type">string</span></dt>
18
+        <dd>the string to parse.</dd>
19
+        <dt class="optional">grammar <span class="property-type">string</span></dt>
20
+        <dd>PEG.js grammar</dd>
21
+    </dl>
22
+    <h3>Output</h3>
23
+    <dl class="message-properties">
24
+        <dt>payload <span class="property-type">string</span></dt>
25
+        <dd>Parser output</dd>
26
+    </dl>
27
+</script>
28
+
29
+<script type="text/javascript">
30
+    RED.nodes.registerType('pegjs', {
31
+        category: 'function',
32
+        color: "#DEBD5C",
33
+        defaults: {
34
+            grammar: {value: ""}
35
+        },
36
+        inputs: 1,
37
+        outputs: 1,
38
+        icon: "parser-html.png",
39
+        label: function () {
40
+            return this.name || this.tag || "pegjs";
41
+        },
42
+        labelStyle: function () {
43
+            return this.name ? "node_label_italic" : "";
44
+        },
45
+    });
46
+</script>
0 47
\ No newline at end of file
1 48
new file mode 100644
... ...
@@ -0,0 +1,22 @@
1
+module.exports = function (RED) {
2
+    "use strict";
3
+    var pegjs = require("pegjs");
4
+    function PegjsNode(n) {
5
+        RED.nodes.createNode(this, n);
6
+        this.parser = pegjs.generate(n.grammar);
7
+        var node = this;
8
+        this.on("input", function (msg) {
9
+            if (msg.hasOwnProperty("payload")) {
10
+                try {
11
+                    msg.payload = node.parser.parse(msg.payload);
12
+                    node.send(msg);
13
+                } catch (error) {
14
+                    node.error(error.message, msg);
15
+                }
16
+            } else {
17
+                node.send(msg);
18
+            }
19
+        });
20
+    }
21
+    RED.nodes.registerType("pegjs", PegjsNode);
22
+};
0 23
\ No newline at end of file