Diff options
| author | Bryce Kwon <bryce@brycekwon.com> | |
|---|---|---|
| committer | Bryce Kwon <bryce@brycekwon.com> | |
| commit | ||
| parent | ||
| tree | ||
| download | ||
Put a Lua filter's print output where it runs
LuaJIT and Lua 5.1 leave print's line in stdio's buffer until cgit
exits, after the end of the page.
Diffstat (limited to 'source')
| -rw-r--r-- | source/filter.c | 31 |
1 file changed, 31 insertions, 0 deletions
diff --git a/source/filter.c b/source/filter.c index 6cc5e80..82d8725 100644 --- a/source/filter.c +++ b/source/filter.c @@ -293,6 +293,36 @@ static int script_html_include(lua_State *lua_state) return emit_unfiltered(lua_state, html_include); } +/* + * The print of LuaJIT and Lua 5.1 leaves its line in stdio's buffer, which + * reaches stdout only when cgit exits, after the end of the page. This one + * builds the same line, each value through tostring with a tab between them, + * and writes it as html does. + */ +static int script_print(lua_State *lua_state) +{ + int i, count = lua_gettop(lua_state); + luaL_Buffer line; + + lua_getglobal(lua_state, "tostring"); + luaL_buffinit(lua_state, &line); + for (i = 1; i <= count; i++) { + if (i > 1) + luaL_addchar(&line, '\t'); + lua_pushvalue(lua_state, count + 1); + lua_pushvalue(lua_state, i); + lua_call(lua_state, 1, 1); + if (!lua_isstring(lua_state, -1)) + return luaL_error(lua_state, "'tostring' must return a string to 'print'"); + luaL_addvalue(&line); + } + luaL_addchar(&line, '\n'); + luaL_pushresult(&line); + // emit_unfiltered takes its text from the first slot. + lua_replace(lua_state, 1); + return emit_unfiltered(lua_state, html); +} + static const struct { const char *name; lua_CFunction fn; @@ -303,6 +333,7 @@ static const struct { { "html_url_path", script_html_url_path }, { "html_url_arg", script_html_url_arg }, { "html_include", script_html_include }, + { "print", script_print }, }; static void init_lua_filter(struct lua_filter *filter) |
