summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/highlight.js54
-rw-r--r--lib/main.js103
-rw-r--r--lib/uuid.js17
3 files changed, 174 insertions, 0 deletions
diff --git a/lib/highlight.js b/lib/highlight.js
new file mode 100644
index 0000000..ae9a331
--- /dev/null
+++ b/lib/highlight.js
@@ -0,0 +1,54 @@
+// load syntax highlighter
+var hljs = require(__dirname + "/../deps/highlight.js/highlight.js").hljs;
+
+// load langs
+require(__dirname + "/../deps/highlight.js/languages/cs.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/python.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/mel.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/perl.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/axapta.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/cpp.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/sql.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/smalltalk.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/profile.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/bash.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/lua.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/html-xml.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/renderman.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/1c.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/delphi.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/dos.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/django.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/vbscript.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/ini.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/apache.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/nginx.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/ruby.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/css.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/lisp.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/java.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/javascript.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/php.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/diff.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/avrasm.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/parser3.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/scala.js").lang(hljs);
+require(__dirname + "/../deps/highlight.js/languages/tex.js").lang(hljs);
+
+hljs.initialize();
+
+var highlight = function(code, language) {
+ if (language == 'plain') {
+ return code;
+ }
+
+ if (language && hljs.LANGUAGES[language]) {
+ return hljs.highlight(language, code).value;
+ }
+
+ return hljs.highlightText(code);
+}
+
+module.exports = {
+ highlight: highlight
+};
diff --git a/lib/main.js b/lib/main.js
new file mode 100644
index 0000000..6e2f36a
--- /dev/null
+++ b/lib/main.js
@@ -0,0 +1,103 @@
+var http = require('http'),
+router = require('choreographer').router(),
+qs = require('querystring'),
+uuid = require(__dirname + '/uuid'),
+hl = require(__dirname + '/highlight'),
+kyoto = require('kyoto');
+
+var db = new kyoto.open(__dirname + '/../db/pastes.kch', 'a+', kyotoOpen);
+
+function generateId(callback) {
+ var id = uuid.generate(14);
+ db.get(id, function(err, value) {
+ if (value) {
+ generateId();
+ }
+ else {
+ callback(id);
+ }
+ });
+}
+
+function getPaste(plain, req, res, paste) {
+ db.get(paste, function(err, value) {
+ if (err) {
+ res.writeHead(404, {'Content-Type': 'text/plain'});
+ res.end('404: ' + req.url + ' not found:\n' + err + '\n');
+ }
+ else {
+ var data = JSON.parse(value);
+ if (plain) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write(data.content);
+ }
+ else {
+ res.writeHead(200, {'Content-Type': 'text/html'});
+ res.write(hl.highlight(data.content, data.language));
+ }
+ res.end('\n');
+ }
+ });
+}
+
+router
+ .get('/plain/*', function(req, res, paste) {
+ getPaste(true, req, res, paste);
+ })
+ .get('/get/*', function(req, res, paste) {
+ getPaste(false, req, res, paste);
+ })
+ .get('/', function(req, res) {
+ })
+ .post('/add', function(req, res) {
+ var content = '';
+
+ req.on('data', function(chunk) {
+ content += chunk;
+ });
+
+ req.on('end', function() {
+ var post = qs.parse(content);
+
+ generateId(function(id) {
+ var data = {
+ content: post.content,
+ language: post.language,
+ time: Date()
+ };
+ db.set(id, JSON.stringify(data), function(err) {
+ if (err) {
+ res.writeHead(500, {'Content-Type': 'text/plain'});
+ res.write(err);
+ res.end('\n');
+ }
+ else {
+ console.log('new paste: %s', id);
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.write(id);
+ res.end('\n');
+ }
+ });
+ });
+ });
+ })
+ .notFound(function(req, res) {
+ res.writeHead(404, {'Content-Type': 'text/plain'});
+ res.end('404: ' + req.url + ' not found.\n');
+ });
+
+
+function kyotoOpen(err) {
+ if (err) throw err;
+
+ http.createServer(router).listen(8080);
+ console.log('Listening on port 8080...');
+}
+
+process.on('uncaughtException', function(exeption) {
+ process.exit(1);
+});
+
+process.on('exit', function() {
+ db.close(function(err) { console.log(err); });
+});
diff --git a/lib/uuid.js b/lib/uuid.js
new file mode 100644
index 0000000..e8d9c6c
--- /dev/null
+++ b/lib/uuid.js
@@ -0,0 +1,17 @@
+
+var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
+
+var generate = function(len) {
+ var uuid = [];
+
+ len = len || 14;
+ for (i = 0; i < len; i++) {
+ uuid[i] = CHARS[0 | Math.random()* CHARS.length];
+ }
+
+ return uuid.join('');
+};
+
+module.exports = {
+ generate: generate
+};