-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathprettify.js
More file actions
53 lines (52 loc) · 2.05 KB
/
prettify.js
File metadata and controls
53 lines (52 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"use strict";
var http = require('http'),
url = require('url'),
formidable = require('formidable'),
fs = require('fs');
var highlight,
$style = 'default', //可选项:ascetic,brown_paper,dark,default,far,github,idea,ir_black,magula,school_book,sunburst,vs,zenburn
style;
try{
highlight = require('highlight').Highlight;
style = fs.readFileSync( "node_modules/highlight/lib/vendor/highlight.js/styles/" + $style + ".css",'utf-8');
}catch(e){
highlight = function(){ return '<h1>hightlight is required! </h1>'; };
style = 'h1{text-align:center}';
}
exports.execute = function(req, resp){
if( req.method === 'POST' ){
var form = new formidable.IncomingForm(), fields = {};
form.on('field', function(field, value) {
fields[field] = value;
}).on('end',function(){
resp.writeHead(200, {"Content-Type": "application/json"});
resp.end( JSON.stringify({
code: fields.code,
output: highlight(fields.code),
style: style
}) );
});
return form.parse(req);
}
var query = decodeURI( url.parse( req.url ).query), data = '',
$url = /.*(http[s]?[:])/.test(query) ? query.replace(/.*(http[s]?[:])/,"$1") : 'http://' + req.headers.host + query;
http.get($url, function(res) {
resp.writeHead(res.statusCode, {"Content-Type": "text/html"});
res.setEncoding('utf-8');
res.on('data',function(d){
data += d;
}).on('end',function(){
resp.end( '<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>' + $url.split('/').pop() + '</title><style>' + style + '</style></head><body><pre>' + highlight(data) + '</pre></body></html>');
});
}).on('error', function(e) {
resp.writeHead(500, {"Content-Type": "text/html"});
resp.end( e.stack.toString().replace(/\n/g,"<br>") );
});
};
exports.parse = function(code){
return {
code: code,
output: highlight(code),
style: style
};
};