Add failure error message to pygments lua-filter

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 20:18:05 -07:00
parent 41baffb445
commit 3793a7ac6e
2 changed files with 48 additions and 34 deletions

View File

@ -2,33 +2,40 @@ return {
CodeBlock = function(block) CodeBlock = function(block)
local label local label
label = block.classes[1] label = block.classes[1]
local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s" local fname, errfname = os.tmpname(), os.tmpname()
local fname = os.tmpname() local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s 2>%s"
do do
local _with_0 = io.open(fname, "w") local _with_0 = io.open(fname, "w")
_with_0:write(block.text) _with_0:write(block.text)
_with_0:close() _with_0:close()
end end
local lexer
local _exp_0 = label local _exp_0 = label
if "moon" == _exp_0 or "moonscript" == _exp_0 then if "moon" == _exp_0 or "moonscript" == _exp_0 then
local p = io.popen((cmd .. " -x"):format("moonscript.py", fname)) cmd = cmd .. " -x"
local out lexer = "moonscript.py"
do
local _with_0 = p:read("*a")
p:close()
out = _with_0
end
return pandoc.RawBlock("html", out)
else else
local status, handle = pcall(function() lexer = label or "text"
return io.popen(cmd:format(label or "text", fname)) end
end) local f = io.popen(cmd:format(lexer, fname, errfname))
local out = handle:read("*a") local out
if out:len() == 0 then do
return block local _with_0 = f:read("*a")
else f:close()
return pandoc.RawBlock("html", out) out = _with_0
end
if out == "" then
local e = io.open(errfname, "r")
local err
do
local _with_0 = e:read("*a")
e:close()
err = _with_0
end end
assert(err:match("Error: no lexer for alias '" .. tostring(label) .. "' found"), "Failed to parse pygmentize result, is pygments installed?")
return block
else
return pandoc.RawBlock("html", out)
end end
end end
} }

View File

@ -1,26 +1,33 @@
CodeBlock: (block) -> CodeBlock: (block) ->
{ label } = block.classes { label } = block.classes
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s" fname, errfname = os.tmpname!, os.tmpname!
fname = os.tmpname! cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s 2>%s"
with io.open fname, "w" with io.open fname, "w"
\write block.text \write block.text
\close! \close!
switch label lexer = switch label
when "moon", "moonscript" when "moon", "moonscript"
p = io.popen (cmd .. " -x")\format "moonscript.py", fname cmd = cmd .. " -x"
out = with p\read"*a" "moonscript.py"
p\close!
-- syntax highlight with pygments
pandoc.RawBlock "html", out
else else
status, handle = pcall -> io.popen cmd\format label or "text", fname label or "text"
out = handle\read"*a"
if out\len! == 0 f = io.popen cmd\format lexer, fname, errfname
-- syntax highlight with pandoc
block out = with f\read"*a"
else f\close!
-- syntax highlight with pygments
pandoc.RawBlock "html", out if out == ""
e = io.open errfname, "r"
err = with e\read"*a"
e\close!
assert err\match("Error: no lexer for alias '#{label}' found"),
"Failed to parse pygmentize result, is pygments installed?"
block
else
pandoc.RawBlock "html", out