From 93cee859ce3b291d1eef3b80a61884c0e1f319e6 Mon Sep 17 00:00:00 2001 From: Robin Malley Date: Tue, 20 Jun 2023 00:22:41 +0000 Subject: [PATCH] Fix bug on index page --- src/lua/endpoints/index_get.lua | 2 +- src/lua/pages.lua | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/lua/endpoints/index_get.lua b/src/lua/endpoints/index_get.lua index d2b3d97..7197a87 100644 --- a/src/lua/endpoints/index_get.lua +++ b/src/lua/endpoints/index_get.lua @@ -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 } diff --git a/src/lua/pages.lua b/src/lua/pages.lua index e276b80..e897536 100644 --- a/src/lua/pages.lua +++ b/src/lua/pages.lua @@ -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