refactor pandoc code:

- more sensible err handling
- annotate TODO comments
pull/339/head
Shuhei Kadowaki 2020-05-23 20:07:59 +09:00
parent 3f4ba672b3
commit 54ccd9b1ed
2 changed files with 24 additions and 33 deletions

View File

@ -214,8 +214,7 @@ function weave(
pandoc2pdf(formatted, doc, outname, pandoc_options) pandoc2pdf(formatted, doc, outname, pandoc_options)
rm(mdname) rm(mdname)
elseif doctype == "md2pdf" elseif doctype == "md2pdf"
success = run_latex(doc, outname, latex_cmd) run_latex(doc, outname, latex_cmd)
success || return
outname = get_outname(out_path, doc, ext = "pdf") outname = get_outname(out_path, doc, ext = "pdf")
end end

View File

@ -12,7 +12,7 @@ function pandoc2html(
weavedir = dirname(@__FILE__) weavedir = dirname(@__FILE__)
html_template = joinpath(weavedir, "../templates/pandoc_skeleton.html") html_template = joinpath(weavedir, "../templates/pandoc_skeleton.html")
css_template = joinpath(weavedir, "../templates/pandoc_skeleton.css") css_template = joinpath(weavedir, "../templates/pandoc_skeleton.css")
css = stylesheet(MIME("text/html"), doc.highlight_theme) highlightcss = stylesheet(MIME("text/html"), doc.highlight_theme)
path, wsource = splitdir(abspath(doc.source)) path, wsource = splitdir(abspath(doc.source))
wversion, wdate = weave_info() wversion, wdate = weave_info()
@ -30,7 +30,7 @@ function pandoc2html(
end end
# Change path for pandoc # Change path for pandoc
old_wd = pwd() cd_back = let d = pwd(); () -> cd(d); end
cd(doc.cwd) cd(doc.cwd)
html = "" html = ""
outname = basename(outname) outname = basename(outname)
@ -40,18 +40,18 @@ function pandoc2html(
$filt $citeproc $pandoc_options $filt $citeproc $pandoc_options
--template $html_template -H $css_template $self_contained --template $html_template -H $css_template $self_contained
-V wversion=$wversion -V wdate=$wdate -V wsource=$wsource -V wversion=$wversion -V wdate=$wdate -V wsource=$wsource
-V highlightcss=$css -V highlightcss=$highlightcss
-V headerscript=$header_script -V headerscript=$header_script
-o $outname` -o $outname`
proc = open(cmd, "r+") proc = open(cmd, "r+")
println(proc.in, formatted) println(proc.in, formatted)
close(proc.in) close(proc.in)
proc_output = read(proc.out, String) proc_output = read(proc.out, String)
cd(old_wd) catch
catch e @warn "Error converting document to HTML"
cd(old_wd) rethrow() # TODO: just show error content instead of rethrow the err
@warn("Error converting document to HTML") finally
throw(e) cd_back()
end end
end end
@ -73,9 +73,8 @@ function pandoc2pdf(
outname = basename(outname) outname = basename(outname)
# Change path for pandoc # Change path for pandoc
old_wd = pwd() cd_back = let d = pwd(); () -> cd(d); end
cd(doc.cwd) cd(doc.cwd)
html = ""
if haskey(doc.header, "bibliography") if haskey(doc.header, "bibliography")
filt = "--filter" filt = "--filter"
@ -95,37 +94,30 @@ function pandoc2pdf(
println(proc.in, formatted) println(proc.in, formatted)
close(proc.in) close(proc.in)
proc_output = read(proc.out, String) proc_output = read(proc.out, String)
cd(old_wd) catch
catch e @warn "Error converting document to pdf"
cd(old_wd) rethrow()
@warn("Error converting document to pdf") finally
throw(e) cd_back()
end end
end end
function run_latex(doc::WeaveDoc, outname, latex_cmd = "xelatex") function run_latex(doc::WeaveDoc, outname, latex_cmd = "xelatex")
old_wd = pwd() cd_back = let d = pwd(); () -> cd(d); end
cd(doc.cwd) cd(doc.cwd)
xname = basename(outname) xname = basename(outname)
@info "Weaved code to $outname . Running $latex_cmd" # space before '.' added for link to be clickable in Juno terminal @info "Weaved code to $outname . Running $latex_cmd" # space before '.' added for link to be clickable in Juno terminal
textmp = mktempdir(".") textmp = mktempdir(".")
try try
out = read( cmd = `$latex_cmd -shell-escape $xname -aux-directory $textmp -include-directory $(doc.cwd)`
`$latex_cmd -shell-escape $xname -aux-directory $textmp -include-directory $(doc.cwd)`, run(cmd); run(cmd) # XXX: is twice enough for every case ?
String, catch
) @warn "Error converting document to pdf. Try running latex manually"
out = read( rethrow()
`$latex_cmd -shell-escape $xname -aux-directory $textmp -include-directory $(doc.cwd)`, finally
String,
)
rm(xname) rm(xname)
rm(textmp, recursive = true) rm(textmp, recursive = true)
cd(old_wd) cd_back()
return true
catch e
@warn("Error converting document to pdf. Try running latex manually")
cd(old_wd)
rm(textmp)
return false
end end
end end