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"
lexer = "moonscript.py"
else
lexer = label or "text"
end
local f = io.popen(cmd:format(lexer, fname, errfname))
local out local out
do do
local _with_0 = p:read("*a") local _with_0 = f:read("*a")
p:close() f:close()
out = _with_0 out = _with_0
end end
return pandoc.RawBlock("html", out) if out == "" then
else local e = io.open(errfname, "r")
local status, handle = pcall(function() local err
return io.popen(cmd:format(label or "text", fname)) do
end) local _with_0 = e:read("*a")
local out = handle:read("*a") e:close()
if out:len() == 0 then err = _with_0
end
assert(err:match("Error: no lexer for alias '" .. tostring(label) .. "' found"), "Failed to parse pygmentize result, is pygments installed?")
return block return block
else else
return pandoc.RawBlock("html", out) return pandoc.RawBlock("html", out)
end 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"
f = io.popen cmd\format lexer, fname, errfname
out = with f\read"*a"
f\close!
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?"
if out\len! == 0
-- syntax highlight with pandoc
block block
else else
-- syntax highlight with pygments
pandoc.RawBlock "html", out pandoc.RawBlock "html", out