This repository has been archived by the owner on Sep 13, 2018. It is now read-only.
forked from Mermade/shins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharapaho.js
74 lines (65 loc) · 2.12 KB
/
arapaho.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
var fs = require('fs');
var path = require('path');
var express = require('express');
var ejs = require('ejs');
var compression = require('compression');
var shins = require('./index.js');
var app = express();
app.use(compression());
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
function check(req,res,fpath) {
fpath = fpath.split('/').join('');
var srcStat = fs.statSync(path.join(__dirname,'source',fpath+'.md'));
var dstStat = {mtime:0};
try {
dstStat = fs.statSync(path.join(__dirname,fpath));
}
catch (ex) { }
if (srcStat.mtime>dstStat.mtime) {
console.log('Rebuilding '+fpath);
let source = path.join(__dirname,'source',fpath+'.md');
fs.readFile(source,'utf8',function(err,markdown){
if (markdown) {
let options = {};
if (req.query["customcss"]) {
options.customCss = true;
}
if (req.query["inline"]) {
options.inline = true;
}
if (req.query["minify"]) {
options.minify = true;
}
options.source = source;
shins.render(markdown,options,function(err,html){
if (err) {
console.warn(err);
res.send(err);
}
else {
res.send(html);
fs.writeFile(path.join(__dirname,fpath),html,'utf8',function(){});
}
});
}
});
}
else {
res.render(path.join(__dirname,fpath));
}
}
app.get('/', function(req,res) {
check(req,res,'index.html');
});
app.get('*.html', function(req,res) {
check(req,res,req.path);
});
app.use("/", express.static(__dirname));
var myport = process.env.PORT || 4567;
if (process.argv.length>2) myport = process.argv[2];
var server = app.listen(myport, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Arapaho server listening at http://%s:%s', host, port);
});