-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
41 lines (37 loc) · 1.14 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
var app = require('app')
, compileFile = require('jade').compileFile
, extend = require('lodash.assign')
, path = require('path')
, readFileSync = require('fs').readFileSync
, mime = require('mime')
module.exports = function (jadeOptions, globalLocals) {
var locals = {}
app.on('ready', function () {
var protocol = require('protocol')
protocol.interceptBufferProtocol('file'
, function (req, cb) {
var reqPath = path.normalize(req.url.substr(7))
if (path.extname(reqPath) === '.jade') {
var fn = compileFile(reqPath, jadeOptions)
, html = fn(extend({}, globalLocals, locals))
cb({ mimeType: 'text/html', data: new Buffer(html) })
} else {
var file = readFileSync(reqPath)
cb({ mimeType: mime.lookup(reqPath), data: file })
}
locals = {}
}
, function (err) {
if (err) {
console.error('Failed to register jade protocol')
} else {
console.log('Successfully registered jade protocol')
}
}
)
})
function updateLocals (newLocals) {
locals = newLocals
}
return updateLocals
}