--[[ Compiles all the pages under src/pages/ with etlua. See the etlua documentation for more info (https://github.com/leafo/etlua) ]] local et = require("etlua") local config = require("config") require("global") local pagenames = { "index", "author_index", "claim", "paste", "edit", "read", "nostory", "cantedit", "noauthor", "login", "author_paste", "author_edit", "search", "error", "edit_bio", "parts/header", "parts/footer", "parts/motd", "parts/search", "parts/story_breif", "parts/taglist" } --Functions available to all templates local global_env = { include = function(filename) local fp = assert(io.open(filename,"r")) local data = assert(fp:read("*a")) fp:close() return data end, } local global_env_m = { __index=global_env } local pages = {} for k,v in pairs(pagenames) do local path = string.format(config.approot .. "pages/%s.etlua",v) local parser = et.Parser() local f = assert(io.open(path,"r")) local fdata = assert(f:read("*a")) local code, err = parser:compile_to_lua(fdata) if not code then errorf("Failed to parse %s: %s",path,err) end local func, err = parser:load(code) if not func then error(string.format("Failed to load %s: %s",path, err)) end f:close() assert(func, "Failed to load " .. path) pages[v] = function(env) assert(type(env) == "table","env must be a table") -- Add our global metatable functions at the bottom metatable's __index local cursor = env while cursor ~= nil and getmetatable(cursor) and getmetatable(cursor).__index do cursor = getmetatable(cursor) end setmetatable(cursor,global_env_m) local buff, err = parser:run(func,env) if not buff then errorf("Failed to render %s : %s", path, err) end return table.concat(buff) end end return pages