Add failure error message to pygments lua-filter

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

View File

@@ -1,26 +1,33 @@
CodeBlock: (block) ->
{ label } = block.classes
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
fname = os.tmpname!
fname, errfname = os.tmpname!, os.tmpname!
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s 2>%s"
with io.open fname, "w"
\write block.text
\close!
switch label
lexer = switch label
when "moon", "moonscript"
p = io.popen (cmd .. " -x")\format "moonscript.py", fname
out = with p\read"*a"
p\close!
-- syntax highlight with pygments
pandoc.RawBlock "html", out
cmd = cmd .. " -x"
"moonscript.py"
else
status, handle = pcall -> io.popen cmd\format label or "text", fname
out = handle\read"*a"
label or "text"
if out\len! == 0
-- syntax highlight with pandoc
block
else
-- syntax highlight with pygments
pandoc.RawBlock "html", out
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?"
block
else
pandoc.RawBlock "html", out