/
greadme
executable file
·136 lines (123 loc) · 3.38 KB
/
greadme
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env node
var express = require('express');
var app = express();
var path = require('path');
var fs = require('fs')
var md = require('marked')
var http = require('http')
var request = require('request')
// TODO configurable
var arg = process.argv[2];
var host = 'localhost'
var port = +(arg||8124)
function readme(dir) {
var exts = 'markdown md'.split(' ')
var file;
var names = 'README Readme readme'.split(' ')
if (names.some(function (name) {
return exts.some(function (ext) {
try {
var filename = dir + '/' + name + '.' + ext
var stat = fs.statSync(filename)
file = filename
return true
} catch (err) {
return false
}
})
})) {
return file;
}
}
function getStyle(cb) {
request({ uri: "https://github.com/aheckmann/greadme", timeout: 4000 }, function (err, res, body) {
try {
var css = []
if (err) {
// ignore error, use old css instead
console.log('\u001B[35m %s\u001B[0m', 'could not retreive latest github css. using old version');
css.unshift('/css/style.css')
} else {
var rgx = /href=["']([^"']+\.css)/g
;(body || '').replace(rgx, function (_, href) {
css.unshift(href);
})
}
} catch (ex) {
return cb(ex);
}
cb(null, css);
});
}
function listDir(dir) {
var all = fs.readdirSync(dir);
var md = [];
var dirs = dir == process.cwd() ? [] : ['..'];
all.forEach(function (item) {
var stat = fs.statSync(path.join(dir, item));
if (stat.isDirectory()) {
dirs.push(item);
} else if (stat.isFile() && (/\.md$/.test(item) || /\.markdown$/.test(item))) {
md.push(item);
}
});
function canonical(item) {
return {
href: path.join(dir, item).replace(process.cwd(), '').replace(/\\/g, '/') || '/',
name: item
};
}
return {
md: md.map(canonical),
dirs: dirs.map(canonical)
}
}
function render(fileContent, cb) {
request({
url: 'https://api.github.com/markdown/raw',
body: fileContent,
method: 'POST',
headers: {
'content-type': 'text/plain'
},
timeout: 2000
}, function (err, res, body) {
if (err) console.log(err);
if (err || res.statusCode != 200) {
return cb(null, md(fileContent));
} else {
return cb(null, body);
}
})
}
// get github css
getStyle(function (err, css) {
if (err) throw err;
app.use(express.favicon());
app.use('/css', express.static(path.join(__dirname, '..', 'css')))
app.use(function (req, res, next) {
var p = path.join(process.cwd(), req.url.substring(1));
var stat = fs.statSync(p);
var dir = stat.isDirectory();
var file = dir ? readme(p) : p;
var contents = file ? fs.readFileSync(file, 'utf8') : 'No readme found';
render(contents, function (err, markdown) {
res.render(path.join(__dirname, '..', 'view.ejs'), {
css: css,
markdown: markdown,
dir: dir && listDir(p)
});
});
});
var server = http.createServer(app).listen(port, host)
server.on('listening', function () {
var url = 'http://' + host + ':' + port
console.log("\n view your markdown at \u001B[32m%s/\u001B[0m", url)
console.log(' press CTRL+C to quit')
// open in browser on mac
if ('darwin' != process.platform) return
require('child_process').exec('open ' + url, function (err) {
// ignore errors
})
});
})