Fix bug on index page

This commit is contained in:
Robin Malley 2023-06-20 00:22:41 +00:00
parent 665616f1e3
commit 93cee859ce
2 changed files with 27 additions and 7 deletions

View File

@ -35,7 +35,7 @@ local function get_site_home(req, loggedin)
isanon = tonumber(iar) == 1,
posted = os.date("%B %d %Y",tonumber(dater)),
author = author,
taglist = libtags.get(idr),
tags = libtags.get(idr),
hits = hits,
ncomments = cmts
}

View File

@ -41,6 +41,7 @@ local global_env_m = {
__index=global_env
}
local pages = {}
local etlua_short_pat = '%[string "etlua"%]'
for k,v in pairs(pagenames) do
local path = string.format(config.approot .. "pages/%s.etlua",v)
local parser = et.Parser()
@ -59,16 +60,35 @@ for k,v in pairs(pagenames) do
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
local cursor,max_depth = env, 10
while cursor ~= nil and getmetatable(cursor) and getmetatable(cursor).__index and max_depth > 0 do
cursor = getmetatable(cursor)
max_depth = max_depth - 1
end
if max_depth == 0 then
log(
LOG_WARN,
string.format([[
Failed to set environment on page %s correctly,
exceeded max depth when applying global functions: %s
]],
path,
debug.traceback()
)
)
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)
local success, ret = xpcall(function()
return parser:run(func, env)
end,function(err)
-- A function to tell us what template we errored in
-- if an error occures
return debug.traceback(err:gsub(etlua_short_pat,path))
end)
if not success then
error(ret:gsub(etlua_short_pat,path))
end
return table.concat(buff)
return table.concat(ret)
end
end