From 4cd0b92792e219cb77a42d1a74000234f00b5880 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Fri, 27 Jul 2018 14:18:33 +0300 Subject: [PATCH] Fix pandoc support, add raw options to pandoc requires pandoc 2. Closes #132. --- src/Weave.jl | 18 +++++++++------- src/formatters.jl | 7 ++++--- src/pandoc.jl | 52 +++++++++++++++++++++-------------------------- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/src/Weave.jl b/src/Weave.jl index 60c31495..9f7bd756 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -56,7 +56,8 @@ end mod::Union{Module, Symbol} = Main, fig_path = "figures", fig_ext = nothing, cache_path = "cache", cache=:off, - template = nothing, highlight_theme = nothing, css = nothing + template = nothing, highlight_theme = nothing, css = nothing, + pandoc_options = "", latex_cmd = "xelatex") Weave an input document to output file. @@ -80,6 +81,8 @@ Weave an input document to output file. * `template` : Template (file path) for md2html or md2tex formats. * `highlight_theme` : Theme (Highlights.AbstractTheme) for used syntax highlighting * `css` : CSS (file path) used for md2html format +* `pandoc_options` = String array of options to pass to pandoc for `pandoc2html` and + `pandoc2pdf` formats e.g. ["--toc", "-N"] * `latex_cmd` the command used to make pdf from .tex **Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output. @@ -91,6 +94,7 @@ function weave(source ; doctype = :auto, cache_path = "cache", cache=:off, throw_errors = false, template = nothing, highlight_theme = nothing, css = nothing, + pandoc_options = String[]::Array{String}, latex_cmd = "xelatex") doc = read_doc(source, informat) @@ -117,12 +121,12 @@ function weave(source ; doctype = :auto, if doc.doctype == "pandoc2html" mdname = outname outname = get_outname(out_path, doc, ext = "html") - pandoc2html(formatted, doc, outname) + pandoc2html(formatted, doc, outname, pandoc_options) rm(mdname) elseif doc.doctype == "pandoc2pdf" mdname = outname outname = get_outname(out_path, doc, ext = "pdf") - pandoc2pdf(formatted, doc, outname) + pandoc2pdf(formatted, doc, outname, pandoc_options) rm(mdname) elseif doc.doctype == "md2pdf" success = run_latex(doc, outname, latex_cmd) @@ -133,14 +137,14 @@ function weave(source ; doctype = :auto, doc.cwd == pwd() && (outname = basename(outname)) @info("Report weaved to $outname") - catch e - @warn("Something went wrong during weaving") - println(e) + # catch err + # @warn("Something went wrong during weaving") + # println(e) finally doctype == :auto && (doctype = detect_doctype(doc.source)) if occursin("pandoc2pdf", doctype) && cache == :off rm(doc.fig_path, force = true, recursive = true) - elseif occursin("2html", doctype) + elseif occursin("2html", doctype) rm(doc.fig_path, force = true, recursive = true) end end diff --git a/src/formatters.jl b/src/formatters.jl index f106f6b0..ba98be83 100644 --- a/src/formatters.jl +++ b/src/formatters.jl @@ -63,12 +63,12 @@ struct Pandoc2HTML formatdict::Dict{Symbol,Any} end -const pdoc2html = Pandoc2HTML("Markdown to HTML (requires Pandoc)", +const pdoc2html = Pandoc2HTML("Markdown to HTML (requires Pandoc 2)", Dict{Symbol,Any}( :codestart => "\n", :codeend=> "\n", - :outputstart=> "````", - :outputend=> "````\n\n", + :outputstart=> "\n", + :outputend=> "\n", :fig_ext=> ".png", :extension=> "md", :mimetypes => ["image/png", "image/svg+xml", "image/jpg", @@ -368,6 +368,7 @@ function formatfigures(chunk, docformat::JMarkdown2tex) return result end +formatfigures(chunk, docformat::Pandoc2HTML) = formatfigures(chunk, pandoc) function formatfigures(chunk, docformat::Pandoc) fignames = chunk.figures diff --git a/src/pandoc.jl b/src/pandoc.jl index 3e8e10d1..b888609a 100644 --- a/src/pandoc.jl +++ b/src/pandoc.jl @@ -5,7 +5,7 @@ Convert output from pandoc markdown to html using Weave.jl template """ -function pandoc2html(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString) +function pandoc2html(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString, pandoc_options) weavedir = dirname(@__FILE__) html_template = joinpath(weavedir, "../templates/pandoc_skeleton.html") css_template = joinpath(weavedir, "../templates/pandoc_skeleton.css") @@ -18,12 +18,7 @@ function pandoc2html(formatted::AbstractString, doc::WeaveDoc, outname::Abstract #Header is inserted from displayed plots header_script = doc.header_script - - if header_script ≠ "" - self_contained = [] - else - self_contained = "--self-contained" - end + self_contained = (header_script ≠ "") ? [] : "--self-contained" if haskey(doc.header, "bibliography") filt = "--filter" @@ -39,18 +34,23 @@ function pandoc2html(formatted::AbstractString, doc::WeaveDoc, outname::Abstract html ="" outname = basename(outname) + open("temp.md", "w") do io + println(io, formatted) + end + try - pandoc_out, pandoc_in, proc = readandwrite(`pandoc -R -s --mathjax="" - $filt $citeproc + cmd = `pandoc -f markdown+raw_html -s --mathjax="" + $filt $citeproc $pandoc_options --template $html_template -H $css_template $self_contained -V wversion=$wversion -V wtime=$wtime -V wsource=$wsource -V highlightcss=$css -V headerscript=$header_script - -o $outname`) - println(pandoc_in, formatted) - close(pandoc_in) - proc_output = read(pandoc_out, String) - cd(old_wd) + -o $outname` + proc = open(cmd, "r+") + println(proc.in, formatted) + close(proc.in) + proc_output = read(proc.out, String) + cd(old_wd) catch e cd(old_wd) @warn("Error converting document to HTML") @@ -63,7 +63,7 @@ end Convert output from pandoc markdown to pdf using Weave.jl template """ -function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString) +function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString, pandoc_options) weavedir = dirname(@__FILE__) header_template = joinpath(weavedir, "../templates/pandoc_header.txt") @@ -88,14 +88,14 @@ function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc, outname::AbstractS @info("Done executing code. Running xelatex") try - pandoc_out, pandoc_in, proc = readandwrite(`pandoc -R -s --latex-engine=xelatex --highlight-style=tango - $filt $citeproc + cmd = `pandoc -f markdown+raw_tex -s --pdf-engine=xelatex --highlight-style=tango + $filt $citeproc $pandoc_options --include-in-header=$header_template - -V fontsize=12pt -o $outname`) - println(pandoc_in, formatted) - - close(pandoc_in) - proc_output = read(pandoc_out, String) + -V fontsize=12pt -o $outname` + proc = open(cmd, "r+") + println(proc.in, formatted) + close(proc.in) + proc_output = read(proc.out, String) cd(old_wd) catch e cd(old_wd) @@ -111,19 +111,13 @@ function run_latex(doc::WeaveDoc, outname, latex_cmd = "xelatex") @info("Weaved code to $outname. Running $latex_cmd") try textmp = mktempdir(".") - #out = readstring(`$latex_cmd -shell-escape --output-directory=$textmp $xname`) out = read(`$latex_cmd -shell-escape $xname`, String) - #info(out) - #pdf = joinpath(textmp, "$(doc.basename).pdf") - #cp(pdf, "$(doc.basename).pdf", remove_destination=true) - #rm(textmp, recursive=true) rm(xname) cd(old_wd) return true catch e - cd(old_wd) @warn("Error converting document to pdf. Try running latex manually") + cd(old_wd) return false - #throw(e) end end