Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 63d3c14181
Fetching contributors…

Cannot retrieve contributors at this time

file 68 lines (50 sloc) 1.285 kb
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
local lfs = require "lfs"

local setfenv = setfenv
local assert = assert
local loadstring = loadstring
local concat = table.concat
local insert = table.insert
local open = io.open
local time = os.time

module "luamvc.view"

local function lastModified(path)
    return assert(lfs.attributes(path)).modification
end

local cache = {}

function load(path)
    do
        local cached = cache[path]
        if cached and cached.timestamp >= lastModified(path) then
            return cached.f
        end
    end
    
    local f = assert(open(path))
local source = f:read("*a")
f:close()

local code, buffer = {}, {}

local index = 1
while true do
local start, stop, echo, snippet = source:find("<%%(=?)(.-)%%>", index)
insert(code, ("echo[[%s]]"):format(source:sub(index, (start or 0) - 1)))
if not start then
break
end

if echo:len() == 1 then
snippet = ("echo(%s)"):format(snippet)
end

insert(code, snippet)

index = stop + 1
end

local code = assert(loadstring(concat(code, "\r\n"), "@" .. path:match("([^/\\]+)$")))

local function run(env)
local reply = {}

function env.echo(str)
insert(reply, str)
end

setfenv(code, env)
code()

return concat(reply)
end

    cache[path] = {timestamp = time(), f = run}
    
return run
end
Something went wrong with that request. Please try again.