smr/src/lua/pages.lua

52 lines
1.1 KiB
Lua
Raw Normal View History

--[[
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")
2023-03-12 04:31:08 +01:00
require("global")
local pagenames = {
"index",
"author_index",
"claim",
"paste",
"edit",
"read",
"nostory",
"cantedit",
"noauthor",
"login",
"author_paste",
"author_edit",
"search",
"error",
"edit_bio",
}
local pages = {}
2021-01-04 04:05:50 +01:00
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"))
2023-03-12 06:13:54 +01:00
local code, err = parser:compile_to_lua(fdata)
if not code then
errorf("Failed to parse %s: %s",path,err)
end
2023-02-20 05:32:37 +01:00
local func, err = parser:load(code)
if not func then
error(string.format("Failed to load %s: %s",path, err))
end
f:close()
2023-02-20 05:32:37 +01:00
assert(func, "Failed to load " .. path)
2023-03-12 06:13:54 +01:00
pages[v] = function(env)
assert(type(env) == "table","env must be a table")
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