Skip to content

Commit 9945d26

Browse files
committed
[module:nlp_tag] natural language process tag helper
- nlp tag helper to build .tag file
1 parent ecec141 commit 9945d26

File tree

6 files changed

+466
-0
lines changed

6 files changed

+466
-0
lines changed

modules/nlp_tag/config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name=NLP Tag
2+
port=9090

modules/nlp_tag/index.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
const express = require('express');
4+
const body_parser = require('body-parser');
5+
const app = express();
6+
7+
const static_dir = path.join(__dirname, 'static');
8+
9+
function send_json(res, obj) {
10+
res.setHeader('Content-Type', 'application/json');
11+
res.send(JSON.stringify(obj));
12+
}
13+
14+
function get_ip (req) {
15+
let ip = null;
16+
if (req.headers['x-forwarded-for']) {
17+
ip = req.headers['x-forwarded-for'].split(",")[0];
18+
} else if (req.connection && req.connection.remoteAddress) {
19+
ip = req.connection.remoteAddress;
20+
} else {
21+
ip = req.ip;
22+
}
23+
return ip;
24+
}
25+
26+
app.use(body_parser.urlencoded({ extended: false }));
27+
app.use(body_parser.json());
28+
29+
app.get('/test', (req, res) => {
30+
res.send('hello world! ' + get_ip(req));
31+
});
32+
33+
app.post('/api/nodebase/nodepad/v1/list', (req, res) => {
34+
if (!req.body) return res.sendStatus(400);
35+
if (!req.body.path) return res.sendStatus(400);
36+
let parent = req.body.path,
37+
symbols = fs.readdirSync(parent),
38+
files = [],
39+
dirs = [];
40+
symbols.forEach((x) => {
41+
try {
42+
if (fs.lstatSync(path.join(parent, x)).isDirectory()) {
43+
dirs.push(x);
44+
} else {
45+
files.push(x);
46+
}
47+
} catch (e) {
48+
// no permission
49+
}
50+
});
51+
send_json(res, { dirs, files });
52+
});
53+
54+
app.post('/api/nodebase/nodepad/v1/open', (req, res) => {
55+
let file = req.body.path;
56+
send_json(res, {
57+
path: file,
58+
text: fs.readFileSync(file).toString()
59+
});
60+
});
61+
62+
app.post('/api/nodebase/nodepad/v1/save', (req, res) => {
63+
let file = req.body.path,
64+
text = req.body.text;
65+
fs.writeFileSync(file, text);
66+
send_json(res, { path: file });
67+
});
68+
69+
app.post('/api/nodebase/nodepad/v1/plugins', (req, res) => {
70+
let parent = path.join(static_dir, 'plugin'),
71+
symbols = fs.readdirSync(parent),
72+
plugins = [];
73+
symbols.forEach((x) => {
74+
try {
75+
if (fs.lstatSync(path.join(parent, x)).isDirectory()) {
76+
plugins.push(x);
77+
}
78+
} catch (e) {
79+
// no permission
80+
}
81+
});
82+
send_json(res, { plugins, path: parent });
83+
});
84+
85+
app.use('/', express.static(static_dir));
86+
87+
app.listen(9090, '0.0.0.0', () => {
88+
console.log(`Nodepad is listening at 0.0.0.0:9090`);
89+
});

modules/nlp_tag/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nodepad",
3+
"version": "0.1.0",
4+
"description": "Simple Notepad for Android NodeBase",
5+
"main": "index.js",
6+
"dependencies": {
7+
"body-parser": "^1.16.0",
8+
"bootstrap": "^3.3.7",
9+
"express": "^4.14.0",
10+
"uuid": "^3.0.1"
11+
},
12+
"devDependencies": {},
13+
"scripts": {
14+
"test": "echo \"Error: no test specified\" && exit 1"
15+
},
16+
"keywords": [
17+
"module",
18+
"nodebase",
19+
"android",
20+
"editor",
21+
"notepad"
22+
],
23+
"author": "Seven Lju",
24+
"license": "MIT"
25+
}

modules/nlp_tag/readme

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NLP Tag
2+
running: 9090
3+
params: (no params)

modules/nlp_tag/static/common.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'use strict';
2+
function $(id){
3+
var el = 'string' == typeof id
4+
? document.getElementById(id)
5+
: id;
6+
7+
el.on = function(event, fn){
8+
if ('content loaded' == event) {
9+
event = window.attachEvent ? "load" : "DOMContentLoaded";
10+
}
11+
el.addEventListener
12+
? el.addEventListener(event, fn, false)
13+
: el.attachEvent("on" + event, fn);
14+
};
15+
16+
el.all = function(selector){
17+
return $(el.querySelectorAll(selector));
18+
};
19+
20+
el.each = function(fn){
21+
for (var i = 0, len = el.length; i < len; ++i) {
22+
fn($(el[i]), i);
23+
}
24+
};
25+
26+
el.getClasses = function(){
27+
return this.getAttribute('class').split(/\s+/);
28+
};
29+
30+
el.addClass = function(name){
31+
var classes = this.getAttribute('class');
32+
el.setAttribute('class', classes
33+
? classes + ' ' + name
34+
: name);
35+
};
36+
37+
el.removeClass = function(name){
38+
var classes = this.getClasses().filter(function(curr){
39+
return curr != name;
40+
});
41+
this.setAttribute('class', classes.join(' '));
42+
};
43+
44+
el.prepend = function (child) {
45+
this.insertBefore(child, this.firstChild);
46+
};
47+
48+
el.append = function (child) {
49+
this.appendChild(child);
50+
};
51+
52+
el.css = function (name, value) {
53+
this.style[name] = value;
54+
}
55+
56+
el.click = function () {
57+
var event = new MouseEvent('click', {
58+
view: window,
59+
bubbles: false,
60+
cancelable: true
61+
});
62+
this.dispatchEvent(event);
63+
};
64+
65+
return el;
66+
}
67+
68+
function uriencode(data) {
69+
if (!data) return data;
70+
return '?' + Object.keys(data).map(function (x) {
71+
return (encodeURIComponent(x) + '=' + encodeURIComponent(data[x]))}).join('&');
72+
}
73+
74+
function ajax (options, done_fn, fail_fn) {
75+
var xhr = new XMLHttpRequest(),
76+
payload = null;
77+
xhr.open(options.method || 'POST', options.url + (options.data?uriencode(options.data):''), true);
78+
xhr.addEventListener('readystatechange', function (evt) {
79+
if (evt.target.readyState === 4 /*XMLHttpRequest.DONE*/) {
80+
if (~~(evt.target.status/100) === 2) {
81+
done_fn && done_fn(JSON.parse(evt.target.response || 'null'));
82+
} else {
83+
fail_fn && fail_fn(evt.target.status);
84+
}
85+
}
86+
});
87+
if (options.json) {
88+
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
89+
payload = JSON.stringify(options.json);
90+
}
91+
xhr.send(payload);
92+
}
93+
function html (url, done_fn, fail_fn) {
94+
var xhr = new XMLHttpRequest(),
95+
payload = null;
96+
xhr.open('GET', url, true);
97+
xhr.addEventListener('readystatechange', function (evt) {
98+
if (evt.target.readyState === 4 /*XMLHttpRequest.DONE*/) {
99+
if (~~(evt.target.status/100) === 2) {
100+
done_fn && done_fn(evt.target.response || '<!-- empty -->');
101+
} else {
102+
fail_fn && fail_fn(evt.target.status);
103+
}
104+
}
105+
});
106+
xhr.send(null);
107+
}
108+
109+
function green_border(element) {
110+
element.style.border = '1px solid green';
111+
}
112+
function red_border(element) {
113+
element.style.border = '1px solid red';
114+
}
115+
116+
function clear_element(element) {
117+
while (element.hasChildNodes()) {
118+
element.removeChild(element.lastChild);
119+
}
120+
}
121+
122+
function ip_encode(ip) {
123+
return ip.split('.').join('-');
124+
}

0 commit comments

Comments
 (0)