2016-04-20 14:41:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
2016-04-20 17:34:24 +02:00
|
|
|
`pandoc2html(formatted::AbstractString, doc::WeaveDoc)`
|
2016-04-20 14:41:54 +02:00
|
|
|
|
|
|
|
Convert output from pandoc markdown to html using Weave.jl template
|
|
|
|
"""
|
2018-07-27 13:18:33 +02:00
|
|
|
function pandoc2html(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString, pandoc_options)
|
2016-12-12 18:25:51 +01:00
|
|
|
weavedir = dirname(@__FILE__)
|
|
|
|
html_template = joinpath(weavedir, "../templates/pandoc_skeleton.html")
|
|
|
|
css_template = joinpath(weavedir, "../templates/pandoc_skeleton.css")
|
2018-01-04 17:00:01 +01:00
|
|
|
css = stylesheet(MIME("text/html"), doc.highlight_theme)
|
2016-04-20 14:41:54 +02:00
|
|
|
|
|
|
|
path, wsource = splitdir(abspath(doc.source))
|
2018-07-23 19:29:07 +02:00
|
|
|
#wversion = string(Pkg.installed("Weave"))
|
|
|
|
wversion = ""
|
2016-04-29 16:32:05 +02:00
|
|
|
wtime = string(Date(now()))
|
2016-04-20 14:41:54 +02:00
|
|
|
|
2016-11-02 22:45:32 +01:00
|
|
|
#Header is inserted from displayed plots
|
|
|
|
header_script = doc.header_script
|
2018-07-27 13:18:33 +02:00
|
|
|
self_contained = (header_script ≠ "") ? [] : "--self-contained"
|
2016-11-02 22:45:32 +01:00
|
|
|
|
2017-03-14 15:08:52 +01:00
|
|
|
if haskey(doc.header, "bibliography")
|
|
|
|
filt = "--filter"
|
|
|
|
citeproc = "pandoc-citeproc"
|
|
|
|
else
|
|
|
|
filt = []
|
|
|
|
citeproc = []
|
|
|
|
end
|
|
|
|
|
2016-04-20 14:41:54 +02:00
|
|
|
#Change path for pandoc
|
|
|
|
old_wd = pwd()
|
|
|
|
cd(doc.cwd)
|
2016-04-20 16:04:38 +02:00
|
|
|
html =""
|
2016-04-24 15:08:18 +02:00
|
|
|
outname = basename(outname)
|
2016-04-20 14:41:54 +02:00
|
|
|
|
2018-07-27 13:18:33 +02:00
|
|
|
open("temp.md", "w") do io
|
|
|
|
println(io, formatted)
|
|
|
|
end
|
|
|
|
|
2016-04-20 16:04:38 +02:00
|
|
|
try
|
2018-07-27 13:18:33 +02:00
|
|
|
cmd = `pandoc -f markdown+raw_html -s --mathjax=""
|
|
|
|
$filt $citeproc $pandoc_options
|
2016-11-02 22:45:32 +01:00
|
|
|
--template $html_template -H $css_template $self_contained
|
2016-04-25 08:11:59 +02:00
|
|
|
-V wversion=$wversion -V wtime=$wtime -V wsource=$wsource
|
2018-01-04 17:00:01 +01:00
|
|
|
-V highlightcss=$css
|
2016-11-02 22:45:32 +01:00
|
|
|
-V headerscript=$header_script
|
2018-07-27 13:18:33 +02:00
|
|
|
-o $outname`
|
|
|
|
proc = open(cmd, "r+")
|
|
|
|
println(proc.in, formatted)
|
|
|
|
close(proc.in)
|
|
|
|
proc_output = read(proc.out, String)
|
|
|
|
cd(old_wd)
|
2016-04-28 15:11:45 +02:00
|
|
|
catch e
|
2016-04-20 16:04:38 +02:00
|
|
|
cd(old_wd)
|
2018-07-23 12:37:25 +02:00
|
|
|
@warn("Error converting document to HTML")
|
2016-04-28 15:11:45 +02:00
|
|
|
throw(e)
|
2016-04-20 16:04:38 +02:00
|
|
|
end
|
2016-04-20 17:34:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
"""
|
|
|
|
`pandoc2pdf(formatted::AbstractString, doc::WeaveDoc)`
|
|
|
|
|
|
|
|
Convert output from pandoc markdown to pdf using Weave.jl template
|
|
|
|
"""
|
2018-07-27 13:18:33 +02:00
|
|
|
function pandoc2pdf(formatted::AbstractString, doc::WeaveDoc, outname::AbstractString, pandoc_options)
|
2016-12-12 18:25:51 +01:00
|
|
|
weavedir = dirname(@__FILE__)
|
|
|
|
header_template = joinpath(weavedir, "../templates/pandoc_header.txt")
|
2016-04-20 17:34:24 +02:00
|
|
|
|
|
|
|
path, wsource = splitdir(abspath(doc.source))
|
2018-07-23 19:29:07 +02:00
|
|
|
#wversion = string(Pkg.installed("Weave"))
|
|
|
|
wversion = ""
|
2016-04-20 17:34:24 +02:00
|
|
|
wtime = Date(now())
|
2016-04-24 15:08:18 +02:00
|
|
|
outname = basename(outname)
|
2016-04-20 14:41:54 +02:00
|
|
|
|
2016-04-20 17:34:24 +02:00
|
|
|
#Change path for pandoc
|
|
|
|
old_wd = pwd()
|
|
|
|
cd(doc.cwd)
|
|
|
|
html =""
|
|
|
|
|
2017-03-14 15:08:52 +01:00
|
|
|
if haskey(doc.header, "bibliography")
|
|
|
|
filt = "--filter"
|
|
|
|
citeproc = "pandoc-citeproc"
|
|
|
|
else
|
|
|
|
filt = []
|
|
|
|
citeproc = []
|
|
|
|
end
|
|
|
|
|
2018-07-23 12:37:25 +02:00
|
|
|
@info("Done executing code. Running xelatex")
|
2016-04-20 17:34:24 +02:00
|
|
|
try
|
2018-07-27 13:18:33 +02:00
|
|
|
cmd = `pandoc -f markdown+raw_tex -s --pdf-engine=xelatex --highlight-style=tango
|
|
|
|
$filt $citeproc $pandoc_options
|
2016-04-25 08:11:59 +02:00
|
|
|
--include-in-header=$header_template
|
2018-07-27 13:18:33 +02:00
|
|
|
-V fontsize=12pt -o $outname`
|
|
|
|
proc = open(cmd, "r+")
|
|
|
|
println(proc.in, formatted)
|
|
|
|
close(proc.in)
|
|
|
|
proc_output = read(proc.out, String)
|
2016-04-20 17:34:24 +02:00
|
|
|
cd(old_wd)
|
2016-04-28 15:11:45 +02:00
|
|
|
catch e
|
2016-04-20 17:34:24 +02:00
|
|
|
cd(old_wd)
|
2018-07-23 12:37:25 +02:00
|
|
|
@warn("Error converting document to pdf")
|
2016-04-28 15:11:45 +02:00
|
|
|
throw(e)
|
2016-04-20 17:34:24 +02:00
|
|
|
end
|
2016-04-20 14:41:54 +02:00
|
|
|
end
|
2016-12-15 20:28:29 +01:00
|
|
|
|
2018-01-03 16:23:57 +01:00
|
|
|
function run_latex(doc::WeaveDoc, outname, latex_cmd = "xelatex")
|
2016-12-15 20:28:29 +01:00
|
|
|
old_wd = pwd()
|
|
|
|
cd(doc.cwd)
|
|
|
|
xname = basename(outname)
|
2018-07-23 19:29:07 +02:00
|
|
|
@info("Weaved code to $outname. Running $latex_cmd")
|
2018-08-17 12:12:37 +02:00
|
|
|
textmp = mktempdir(".")
|
2016-12-15 20:28:29 +01:00
|
|
|
try
|
2018-08-23 07:22:08 +02:00
|
|
|
out = read(`$latex_cmd -shell-escape $xname -aux-directory $textmp -include-directory $(doc.cwd)`, String)
|
2018-08-17 12:12:37 +02:00
|
|
|
out = read(`$latex_cmd -shell-escape $xname -aux-directory $textmp -include-directory $(doc.cwd)`, String)
|
2016-12-23 08:25:44 +01:00
|
|
|
rm(xname)
|
2018-08-17 12:12:37 +02:00
|
|
|
rm(textmp, recursive=true)
|
2016-12-15 20:28:29 +01:00
|
|
|
cd(old_wd)
|
2016-12-23 08:25:44 +01:00
|
|
|
return true
|
2016-12-15 20:28:29 +01:00
|
|
|
catch e
|
2018-07-23 12:37:25 +02:00
|
|
|
@warn("Error converting document to pdf. Try running latex manually")
|
2018-07-27 13:18:33 +02:00
|
|
|
cd(old_wd)
|
2018-08-17 12:12:37 +02:00
|
|
|
rm(textmp)
|
2016-12-23 08:25:44 +01:00
|
|
|
return false
|
2016-12-15 20:28:29 +01:00
|
|
|
end
|
|
|
|
end
|