-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.js
101 lines (84 loc) · 2.79 KB
/
app.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Module dependencies.
*/
var fs = require('fs'),
express = require('express'),
app = module.exports = express.createServer(),
config = require('./config.js'),
hinter = require('./lib/hinter.js');
// Configuration
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
function combine(t, o) {
var n;
for (n in o) {
if (is_own(o, n)) {
t[n] = o[n];
}
}
}
// Goodness
app.get('/', function (req, res){
var filename = ('/' + req.query.file).replace(/hint$/, '');
fs.readFile(filename, function (err, data) {
if (err) {
res.render('sadface', {
filename: filename
});
} else {
var originalSource = data.toString('utf8'),
source, result,
errors = [],
sourceLines,
numLines,
errorContext = 2;
result = hinter(originalSource, config);
source = result.source;
if (!result.passed && result.errors[1]) {
sourceLines = source.split("\n");
numLines = sourceLines.length;
result.errors.forEach(function (error) {
if (!error) {
return;
}
var startIndex = error.line - (errorContext + 1) > 0 ? error.line - (errorContext + 1) : 0,
endIndex = error.line + errorContext > numLines ? numLines : error.line + errorContext,
errorLineContents;
// Generate a source except
error.excerpt = {};
sourceLines.slice(startIndex, endIndex).forEach(function (line, lineOffset) {
error.excerpt[startIndex + 1 + lineOffset] = line;
});
// Insert a span to highlight the error itself
errorLineContents = injectString(error.excerpt[error.line], '<span>', error.character - 2);
errorLineContents = injectString(errorLineContents, '</span>', error.character + 6);
error.excerpt[error.line] = errorLineContents;
errors.push(error);
});
}
res.render('index', {
errors: errors,
skipped: errors.map(function (error) {
return error.skipped ? error.hash : null
}).filter(function(val) {
return !!val;
}).join(',')
});
}
});
});
// Only listen on $ node app.js
if (!module.parent) {
app.listen(config.port);
console.log("Express server listening on port %d", config.port);
}
function injectString(string, inject, where) {
return string.substr(0, where) + inject + string.substr(where);
}