-
Notifications
You must be signed in to change notification settings - Fork 2
/
mustache.lua
57 lines (44 loc) · 1.4 KB
/
mustache.lua
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
local lustache = require "lustache"
local cache = {}
local readAll = function(file)
if not config.base then
config.base = ''
end
local f = io.open(config.base..file, "rb")
content = f:read("*all")
f:close()
return content
end
local getFile = function(file, cache)
local content = cache[file]
if not content then
content = readAll(file)
content = lustache:compile(content)
cache[file] = content
end
return content
end
return {
handler = function(context)
context.response.headers["content-type"] = "text/html"
local partials = {}
local partialNames = context.template.partials or config.partials or {}
local templateName = context.template.name or config.template or ''
local template = getFile(templateName..'.mustache', cache)
for i,v in pairs(partialNames) do
partials[i] = getFile(v..'.mustache', cache)
end
lustache.renderer.partial_cache = partials
local content = template(context.output)
context.output = content
end,
options = {
predicate = function(context)
local accept = context.request.headers.accept or "text/html"
local content = context.request.headers["content-type"]
return ((accept and (accept:find("text/html") or accept:find("*/*"))) or
(content and content:find("text/html") and not accept)) and
context.template and context.template.type == "mustache"
end
}
}