Add failure error message to pygments lua-filter
This commit is contained in:
parent
41baffb445
commit
3793a7ac6e
33
pygments.lua
33
pygments.lua
@ -2,33 +2,40 @@ return {
|
||||
CodeBlock = function(block)
|
||||
local label
|
||||
label = block.classes[1]
|
||||
local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
|
||||
local fname = os.tmpname()
|
||||
local fname, errfname = os.tmpname(), os.tmpname()
|
||||
local cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s 2>%s"
|
||||
do
|
||||
local _with_0 = io.open(fname, "w")
|
||||
_with_0:write(block.text)
|
||||
_with_0:close()
|
||||
end
|
||||
local lexer
|
||||
local _exp_0 = label
|
||||
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
|
||||
do
|
||||
local _with_0 = p:read("*a")
|
||||
p:close()
|
||||
local _with_0 = f:read("*a")
|
||||
f:close()
|
||||
out = _with_0
|
||||
end
|
||||
return pandoc.RawBlock("html", out)
|
||||
else
|
||||
local status, handle = pcall(function()
|
||||
return io.popen(cmd:format(label or "text", fname))
|
||||
end)
|
||||
local out = handle:read("*a")
|
||||
if out:len() == 0 then
|
||||
if out == "" then
|
||||
local e = io.open(errfname, "r")
|
||||
local err
|
||||
do
|
||||
local _with_0 = e:read("*a")
|
||||
e:close()
|
||||
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
|
||||
else
|
||||
return pandoc.RawBlock("html", out)
|
||||
end
|
||||
end
|
||||
end
|
||||
}
|
||||
|
@ -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"
|
||||
|
||||
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
|
||||
else
|
||||
-- syntax highlight with pygments
|
||||
pandoc.RawBlock "html", out
|
||||
|
Loading…
x
Reference in New Issue
Block a user