From 6a8a422b9d36bb180e9b56654cb270bfc574f64c Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 13:14:35 -0700 Subject: Render markdown and man pages through one function Everything that can raise already runs inside the parse and emit pcalls, so the outer one in filter_close could only ever have sent the page a second time. The size check moves to the dispatch with it. --- custom/extensions/about-render.lua | 46 +++++++++++++++----------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'custom') diff --git a/custom/extensions/about-render.lua b/custom/extensions/about-render.lua index f330d04..3ae57e7 100644 --- a/custom/extensions/about-render.lua +++ b/custom/extensions/about-render.lua @@ -339,6 +339,18 @@ end -- Both parse into a node tree before anything is emitted, so a failed parse -- falls back to plain text without leaving half a page behind. +local function render_blocks(text, parse) + local ok, blocks = pcall(parse, text) + if not ok then + return render_plaintext(text) + end + html("
") + -- Emitting is wrapped so a bug there can at worst truncate the page, + -- never end the request in an error. + pcall(emit_blocks, blocks) + html("
") +end + local render_markdown, render_man if has_lpeg then @@ -680,19 +692,9 @@ if has_lpeg then end render_markdown = function(text) - if #text > max_bytes then - return render_plaintext(text) - end - local ok, blocks = pcall(parse_blocks, split_lines(text), 0) - if not ok then - return render_plaintext(text) - end - html("
") - -- Emitting is wrapped so a bug there can at worst - -- truncate the page, never raise and let the outer - -- fallback duplicate what has already been sent. - pcall(emit_blocks, blocks) - html("
") + render_blocks(text, function(t) + return parse_blocks(split_lines(t), 0) + end) end -- Roff fonts are a current state carried across a run rather than @@ -987,16 +989,7 @@ if has_lpeg then end render_man = function(text) - if #text > max_bytes then - return render_plaintext(text) - end - local ok, blocks = pcall(parse_man, text) - if not ok then - return render_plaintext(text) - end - html("
") - pcall(emit_blocks, blocks) - html("
") + render_blocks(text, parse_man) end else -- No lpeg, so markdown and man readmes read as escaped source, not @@ -1052,10 +1045,7 @@ function filter_close() local text = replace_nul(table.concat(chunks)) chunks = {} local ext = (filename:match("%.([^.]+)$") or ""):lower() - local render = handlers[ext] or render_plaintext - local ok = pcall(render, text) - if not ok then - render_plaintext(text) - end + local render = #text <= max_bytes and handlers[ext] or render_plaintext + render(text) return 0 end -- cgit