Skip to content

Commit

Permalink
Merge pull request #299 from gustaphe/renderlog
Browse files Browse the repository at this point in the history
Informative error messages for failed renders
  • Loading branch information
gustaphe authored Aug 7, 2024
2 parents c93bc3d + b327d73 commit 4a3e41b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Latexify.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using Markdown
using MacroTools: postwalk
import MacroTools
using Format
import Base.showerror

export latexify, md, copy_to_clipboard, auto_display, set_default, get_default,
reset_default, @latexrecipe, render, @latexify, @latexrun, @latexdefine
Expand Down
29 changes: 28 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ function _compile(s::LaTeXString, cmd::Cmd, ext::String;
open=true,
kw...
)
name = abspath(name)
mktempdir() do source_dir
cd(source_dir) do
_writetex(s; name="main", kw...)
debug || (cmd = pipeline(cmd, devnull))
run(cmd)
try
run(cmd)
catch err
isa(err, ProcessFailedException) || rethrow(err)
mv("$source_dir/main.log", "$name.log"; force=true)
rethrow(LatexifyRenderError("$name.log"))
end
end
mv("$source_dir/main.$ext", "$name.$ext"; force=true)
end
Expand Down Expand Up @@ -226,3 +233,23 @@ end

_packagename(x::AbstractString) = "{$x}"
_packagename(x::Tuple) = "[$(join(x[2:end], ", "))]{$(first(x))}"

struct LatexifyRenderError <: Exception
logfilename::String
end
function Base.showerror(io::IO, e::LatexifyRenderError)
isfile(e.logfilename) || return println(io, "an error occured while rendering LaTeX, no log file available.")
println(io, "an error occured while rendering LaTeX: ")
secondline = false
for l = eachline(e.logfilename)
if secondline
println(io, "\t", l)
break;
end
m = match(r"^! (.*)$", l)
isnothing(m) && continue
println(io, "\t", m[1])
secondline = true
end
println(io, "Check the log file at ", e.logfilename, " for more information")
end
26 changes: 26 additions & 0 deletions test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,29 @@ tex = read(filename, String)

@test occursin("MathJax", Latexify.html_wrap(latexify(:(sin(α)))))
@test Latexify.best_displayable() isa MIME

#@test_throws Latexify.LatexifyRenderError render(L"x^2^3") # Does not run on Windows and Mac CI
logfile = tempname()
open(logfile, "w") do io
println(io, raw"""
This is LuaHBTeX, Version 1.18.0 (TeX Live 2024/Arch Linux) (format=lualatex 2024.4.3) 7 AUG 2024 14:19
restricted system commands enabled.
** Skipping many files **
LaTeX Font Info: Trying to load font information for U+msb on input line 5.
(/usr/share/texmf-dist/tex/latex/amsfonts/umsb.fd
File: umsb.fd 2013/01/14 v3.01 AMS symbols B
)
! Double superscript.
l.8 $x^2^
3$
I treat `x^1^2' essentially like `x^1{}^2'.
** More lines skipped **
""")
end
e = Latexify.LatexifyRenderError(logfile)
@test sprint(showerror, e) == """
an error occured while rendering LaTeX: \n\tDouble superscript.
\tl.8 \$x^2^
Check the log file at $logfile for more information
"""

0 comments on commit 4a3e41b

Please sign in to comment.