summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pkgs/simple/cgit-about-filter/cgit-about-filter.lua32
1 files changed, 25 insertions, 7 deletions
diff --git a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua
index 6e1ccdd..86468f5 100644
--- a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua
+++ b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua
@@ -46,6 +46,26 @@ local function url_escape(s)
end))
end
+-- cgit provides html() to Lua filters. It writes in order with cgit's own
+-- output, which io.write's buffered stdout doesn't. io.write is for running
+-- the filter outside of cgit.
+local emit = html or io.write
+
+-- Run md2html and return its output.
+local function md2html(markdown)
+ local input = os.tmpname()
+ local file = assert(io.open(input, "w"))
+ file:write(markdown)
+ file:close()
+ local handle = io.popen("@md2html@ --github < " .. input, "r")
+ local output = handle and handle:read("*a")
+ if handle then
+ handle:close()
+ end
+ os.remove(input)
+ return output
+end
+
local buffer = ""
local should_render = true
local current_dir = "" -- Stores the directory prefix of the file being processed
@@ -85,7 +105,7 @@ end
function filter_close()
if not should_render then
- io.write("<p><em>Error: Access Denied. Only README.md and documentation markdown files are viewable here.</em></p>")
+ emit("<p><em>Error: Access Denied. Only README.md and documentation markdown files are viewable here.</em></p>")
return
end
@@ -115,12 +135,10 @@ function filter_close()
end
end)
- -- Pipe everything into md2html
- local handle = io.popen("@md2html@ --github", "w")
- if handle then
- handle:write(processed)
- handle:close()
+ local output = md2html(processed)
+ if output then
+ emit(output)
else
- io.write("<p>Error: Could not execute md2html.</p>")
+ emit("<p>Error: Could not execute md2html.</p>")
end
end