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, isanon = tonumber(iar) == 1,
posted = os.date("%B %d %Y",tonumber(dater)), posted = os.date("%B %d %Y",tonumber(dater)),
author = author, author = author,
taglist = libtags.get(idr), tags = libtags.get(idr),
hits = hits, hits = hits,
ncomments = cmts ncomments = cmts
} }

View File

@ -41,6 +41,7 @@ local global_env_m = {
__index=global_env __index=global_env
} }
local pages = {} local pages = {}
local etlua_short_pat = '%[string "etlua"%]'
for k,v in pairs(pagenames) do for k,v in pairs(pagenames) do
local path = string.format(config.approot .. "pages/%s.etlua",v) local path = string.format(config.approot .. "pages/%s.etlua",v)
local parser = et.Parser() local parser = et.Parser()
@ -59,16 +60,35 @@ for k,v in pairs(pagenames) do
pages[v] = function(env) pages[v] = function(env)
assert(type(env) == "table","env must be a table") assert(type(env) == "table","env must be a table")
-- Add our global metatable functions at the bottom metatable's __index -- Add our global metatable functions at the bottom metatable's __index
local cursor = env local cursor,max_depth = env, 10
while cursor ~= nil and getmetatable(cursor) and getmetatable(cursor).__index do while cursor ~= nil and getmetatable(cursor) and getmetatable(cursor).__index and max_depth > 0 do
cursor = getmetatable(cursor) 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 end
setmetatable(cursor,global_env_m) setmetatable(cursor,global_env_m)
local buff, err = parser:run(func,env) local success, ret = xpcall(function()
if not buff then return parser:run(func, env)
errorf("Failed to render %s : %s", path, err) 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 end
return table.concat(buff) return table.concat(ret)
end end
end end