-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (69 loc) · 2.51 KB
/
index.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
var content = null
var options = null
var defaultOptions = {
endpoint: '/readme.html',
filename: 'README.md',
htmlWrap: {}
}
exports.setOptions = setOptions = function(opts, callback) {
content = ""
options = opts
var addHead = function() {
content += "<!DOCTYPE html>\n<html>\n<head>\n"
if (options.htmlWrap.scripts) options.htmlWrap.scripts.forEach(function(script) {
content += '<script type="text/javascript" src="' + script + '"></script>' + "\n"
})
if (options.htmlWrap.styles) options.htmlWrap.styles.forEach(function(style) {
content += '<link href="' + style + '" rel="stylesheet" type="text/css" />' + "\n"
})
if (options.htmlWrap.meta) options.htmlWrap.meta.forEach(function(meta) {
content += '<meta '
if (meta.name) content += 'name="' + meta.name + '" '
if (meta.content) content += 'content="' + meta.content + '" '
if (meta['http-equiv']) content += 'http-equiv="' + meta['http-equiv'] + '" '
if (meta.charset) content += 'charset="' + meta.charset + '" '
content += ' />' + "\n"
})
if (options.htmlWrap.title) content += '<title>' + options.htmlWrap.title + "</title>\n"
content += "</head>\n<body>\n"
}
var addFoot = function() {
if (!options.htmlWrap) return
content += "\n</body>\n</html>"
}
var checkValues = function() {
if (!options.filename) options.filename = defaultOptions.filename
var endpoints = options.endpoint || defaultOptions.endpoint
if (typeof endpoints == 'string') endpoints = [ endpoints ]
options.endpoint = []
endpoints.forEach(function(endpoint) {
options.endpoint[endpoint] = true
})
if (options.htmlWrap) {
['scripts', 'styles', 'meta'].forEach(function(i) {
if (options.htmlWrap[i]) {
if (options.htmlWrap[i] && typeof options.htmlWrap[i] == 'string')
options.htmlWrap[i] = [ options.htmlWrap[i] ]
}
})
}
}
checkValues()
var readmeLocation = require('path').resolve(process.cwd(), options.filename)
require('fs').readFile(readmeLocation, 'utf8', function(error, data) {
if (error) throw(error)
content = ""
addHead()
content += require('ghm').parse(data)
addFoot()
if (callback) callback()
})
}
exports.run = function(req, res, next) {
var handleRequest = function() {
if (options.endpoint[req.path]) return res.send(content)
next()
}
if (!options) return setOptions(defaultOptions, handleRequest)
handleRequest()
}