Weave.jl/src/run.jl

494 lines
15 KiB
Julia
Raw Normal View History

2017-03-14 20:06:47 +01:00
"""
2016-04-22 15:16:12 +02:00
`run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly",
out_path=:doc, fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache = :off)`
Run code chunks and capture output from parsed document.
2016-04-22 15:16:12 +02:00
* `doctype`: :auto = set based on file extension or specify one of the supported formats.
See `list_out_formats()`
2017-03-13 19:57:39 +01:00
* `plotlib`: `"PyPlot"`, `"Gadfly"`, or `"Plots`
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory,
2016-04-22 15:16:12 +02:00
`"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"`
2017-03-09 21:09:36 +01:00
* `args`: dictionary of arguments to pass to document. Available as WEAVE_ARGS.
* `fig_path`: where figures will be generated, relative to out_path
* `fig_ext`: Extension for saved figures e.g. `".pdf"`, `".png"`. Default setting depends on `doctype`.
* `cache_path`: where of cached output will be saved.
* `cache`: controls caching of code: `:off` = no caching, `:all` = cache everything,
`:user` = cache based on chunk options, `:refresh`, run all code chunks and save new cache.
**Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
"""
function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
2016-12-26 12:26:36 +01:00
out_path=:doc, args=Dict(), fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache = :off)
#cache :all, :user, :off, :refresh
doc.cwd = get_cwd(doc, out_path)
2016-04-22 15:16:12 +02:00
doctype == :auto && (doctype = detect_doctype(doc.source))
doc.doctype = doctype
doc.format = formats[doctype]
isdir(doc.cwd) || mkpath(doc.cwd)
if contains(doctype, "2pdf") && cache == :off
fig_path = mktempdir(abspath(doc.cwd))
elseif contains(doctype, "2html")
fig_path = mktempdir(abspath(doc.cwd))
2016-12-23 07:34:54 +01:00
end
2016-12-23 11:27:10 +01:00
#This is needed for latex and should work on all output formats
is_windows() && (fig_path = replace(fig_path, "\\", "/"))
2016-12-23 07:34:54 +01:00
doc.fig_path = fig_path
set_rc_params(doc.format.formatdict, fig_path, fig_ext)
2016-12-26 12:26:36 +01:00
#New sandbox for each document with args exposed
sandbox = "ReportSandBox$(rcParams[:doc_number])"
2016-12-26 12:26:36 +01:00
eval(parse("module $sandbox\nWEAVE_ARGS=Dict()\nend"))
SandBox = eval(parse(sandbox))
2016-12-26 12:26:36 +01:00
merge!(SandBox.WEAVE_ARGS, args)
rcParams[:doc_number] += 1
if haskey(doc.format.formatdict, :mimetypes)
mimetypes = doc.format.formatdict[:mimetypes]
else
mimetypes = default_mime_types
end
#Reset plotting
2016-04-29 15:56:05 +02:00
rcParams[:plotlib_set] = false
plotlib == :auto || init_plotting(plotlib)
report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes)
pushdisplay(report)
if cache != :off && cache != :refresh
cached = read_cache(doc, cache_path)
cached == nothing && info("No cached results found, running code")
else
cached = nothing
end
executed = Any[]
n = length(doc.chunks)
for i = 1:n
chunk = doc.chunks[i]
2016-12-26 20:21:55 +01:00
if isa(chunk, CodeChunk)
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
end
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
if cached != nothing && (cache == :all || restore)
result_chunks = restore_chunk(chunk, cached)
else
2016-12-26 20:21:55 +01:00
result_chunks = run_chunk(chunk, report, SandBox)
end
executed = [executed; result_chunks]
end
doc.header_script = report.header_script
popdisplay(report)
#Clear variables from used sandbox
clear_sandbox(SandBox)
doc.chunks = executed
if cache != :off
write_cache(doc, cache_path)
end
return doc
end
2016-04-22 15:16:12 +02:00
"""Detect the output format based on file extension"""
function detect_doctype(source::AbstractString)
ext = lowercase(splitext(source)[2])
ext == ".jl" && return "md2html"
contains(ext, "md") && return "md2html"
contains(ext, "rst") && return "rst"
contains(ext, "tex") && return "texminted"
contains(ext, "txt") && return "asciidoc"
2016-04-22 15:16:12 +02:00
return "pandoc"
end
function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
2016-12-26 20:28:57 +01:00
info("Weaving chunk $(chunk.number) from line $(chunk.start_line)")
result_chunks = eval_chunk(chunk, report, SandBox)
contains(report.formatdict[:doctype], "2html") && (result_chunks = embed_figures(result_chunks, report.cwd))
return result_chunks
end
2016-12-23 11:27:10 +01:00
function embed_figures(chunk::CodeChunk, cwd)
chunk.figures = [img2base64(fig, cwd) for fig in chunk.figures]
return chunk
end
function embed_figures(result_chunks, cwd)
for i in 1:length(result_chunks)
figs = result_chunks[i].figures
if !isempty(figs)
result_chunks[i].figures = [img2base64(fig, cwd) for fig in figs]
end
end
return result_chunks
end
function img2base64(fig, cwd)
ext = splitext(fig)[2]
f = open(joinpath(cwd, fig), "r")
raw = read(f)
close(f)
if ext == ".png"
return "data:image/png;base64," * stringmime(MIME("image/png"), raw)
elseif ext == ".svg"
return "data:image/svg+xml;base64," * stringmime(MIME("image/svg+xml"), raw)
elseif ext == ".gif"
return "data:image/gif;base64," * stringmime(MIME("image/gif"), raw)
else
return(fig)
end
end
function run_chunk(chunk::DocChunk, report::Report, SandBox::Module)
2016-12-26 20:21:55 +01:00
chunk.content = [run_inline(c, report, SandBox) for c in chunk.content]
return chunk
end
2016-12-26 20:21:55 +01:00
function run_inline(inline::InlineText, report::Report, SandBox::Module)
return inline
end
function run_inline(inline::InlineCode, report::Report, SandBox::Module)
#Make a temporary CodeChunk for running code. Collect results and don't wrap
2017-03-09 21:09:36 +01:00
chunk = CodeChunk(inline.content, 0, 0, "", Dict(:hold => true, :wrap => false))
2016-12-26 20:21:55 +01:00
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
2017-03-09 21:09:36 +01:00
2016-12-26 20:28:57 +01:00
chunks = eval_chunk(chunk, report, SandBox)
contains(report.formatdict[:doctype], "2html") && (chunks = embed_figures(chunks, report.cwd))
2016-12-26 20:21:55 +01:00
output = chunks[1].output
2017-04-02 01:07:15 +02:00
endswith(output, "\n") && (output = output[1:end-1])
2016-12-26 20:21:55 +01:00
inline.output = output
inline.rich_output = chunks[1].rich_output
inline.figures = chunks[1].figures
return inline
end
function reset_report(report::Report)
report.cur_result = ""
2016-04-11 17:40:18 +02:00
report.figures = AbstractString[]
report.term_state = :text
end
function run_code(chunk::CodeChunk, report::Report, SandBox::Module)
expressions = parse_input(chunk.content)
N = length(expressions)
#@show expressions
result_no = 1
results = ChunkOutput[ ]
for (str_expr, expr) = expressions
reset_report(report)
lastline = (result_no == N)
rcParams[:plotlib_set] || detect_plotlib(chunk) #Try to autodetect plotting library
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
chunk.options[:display], rcParams[:plotlib], lastline)
figures = report.figures #Captured figures
result = ChunkOutput(str_expr, out, report.cur_result, report.rich_output, figures)
report.rich_output = ""
push!(results, result)
result_no += 1
end
#Save figures only in the end of chunk for PyPlot
if rcParams[:plotlib] == "PyPlot"
2017-12-31 13:02:28 +01:00
Compat.invokelatest(savefigs_pyplot, report)
end
return results
end
2016-12-15 18:54:50 +01:00
getstdout() = Base.STDOUT
function capture_output(expr, SandBox::Module, term, disp, plotlib,
lastline)
2016-12-15 18:54:50 +01:00
#oldSTDOUT = STDOUT
oldSTDOUT = getstdout()
out = nothing
obj = nothing
rw, wr = redirect_stdout()
2016-11-02 19:03:04 +01:00
reader = @async readstring(rw)
try
obj = eval(SandBox, expr)
if (term || disp) && typeof(expr) == Expr && expr.head != :toplevel
obj != nothing && display(obj)
elseif typeof(expr) == Symbol
display(obj)
elseif plotlib == "Gadfly" && typeof(obj) == Gadfly.Plot
obj != nothing && display(obj)
#This shows images and lone variables, result can
#Handle last line sepately
elseif lastline && obj != nothing
(expr.head != :toplevel && expr.head != :(=)) && display(obj)
end
finally
redirect_stdout(oldSTDOUT)
close(wr)
2016-11-02 19:03:04 +01:00
out = wait(reader)
close(rw)
end
return (obj, out)
end
#Parse chunk input to array of expressions
2016-04-11 17:40:18 +02:00
function parse_input(input::AbstractString)
parsed = Tuple{AbstractString, Any}[]
2016-12-26 20:21:55 +01:00
input = lstrip(input)
2016-04-11 18:13:00 +02:00
n = length(input)
2016-12-26 20:21:55 +01:00
pos = 1 #The first character is extra line end
while pos n
oldpos = pos
code, pos = parse(input, pos)
push!(parsed, (input[oldpos:pos-1] , code ))
end
parsed
end
function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
if !chunk.options[:eval]
chunk.output = ""
chunk.options[:fig] = false
return chunk
end
#Run preexecute_hooks
for hook in preexecute_hooks
2017-12-31 13:02:28 +01:00
chunk = Compat.invokelatest(hook, chunk)
end
report.fignum = 1
report.cur_chunk = chunk
if haskey(report.formatdict, :out_width) && chunk.options[:out_width] == nothing
chunk.options[:out_width] = report.formatdict[:out_width]
end
chunk.result = run_code(chunk, report, SandBox)
#Run post_execute chunks
for hook in postexecute_hooks
2017-12-31 13:02:28 +01:00
chunk = Compat.invokelatest(hook, chunk)
end
if chunk.options[:term]
chunks = collect_results(chunk, TermResult())
2016-04-19 15:38:03 +02:00
elseif chunk.options[:hold]
chunks = collect_results(chunk, CollectResult())
else
chunks = collect_results(chunk, ScriptResult())
end
#else
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
#end
chunks
end
2015-01-08 18:35:47 +01:00
#function eval_chunk(chunk::DocChunk, report::Report, SandBox)
# chunk
#end
#Set all variables to nothing
function clear_sandbox(SandBox::Module)
for name = names(SandBox, true)
if name != :eval && name != names(SandBox)[1]
2016-04-11 17:40:18 +02:00
try eval(SandBox, parse(AbstractString(AbstractString(name), "=nothing"))) end
end
end
end
function get_figname(report::Report, chunk; fignum = nothing, ext = nothing)
figpath = joinpath(report.cwd, chunk.options[:fig_path])
isdir(figpath) || mkpath(figpath)
ext == nothing && (ext = chunk.options[:fig_ext])
fignum == nothing && (fignum = report.fignum)
chunkid = (chunk.options[:name] == nothing) ? chunk.number : chunk.options[:name]
full_name = joinpath(report.cwd, chunk.options[:fig_path],
2016-12-23 11:27:10 +01:00
"$(report.basename)_$(chunkid)_$(fignum)$ext")
rel_name = "$(chunk.options[:fig_path])/$(report.basename)_$(chunkid)_$(fignum)$ext" #Relative path is used in output
return full_name, rel_name
end
function init_plotting(plotlib)
srcdir = escape_string(dirname(@__FILE__))
2016-04-29 15:56:05 +02:00
rcParams[:plotlib_set] = true
if plotlib == nothing
2016-04-11 17:29:19 +02:00
rcParams[:plotlib] = nothing
else
l_plotlib = lowercase(plotlib)
rcParams[:chunk_defaults][:fig] = true
2017-03-13 19:57:39 +01:00
if l_plotlib == "pyplot"
2016-12-12 18:25:51 +01:00
eval(parse("""include("$srcdir/pyplot.jl")"""))
rcParams[:plotlib] = "PyPlot"
elseif l_plotlib == "plots"
2016-12-12 18:25:51 +01:00
eval(parse("""include("$srcdir/plots.jl")"""))
rcParams[:plotlib] = "Plots"
elseif l_plotlib == "gadfly"
2016-12-12 18:25:51 +01:00
eval(parse("""include("$srcdir/gadfly.jl")"""))
rcParams[:plotlib] = "Gadfly"
end
end
return true
end
function get_cwd(doc::WeaveDoc, out_path)
#Set the output directory
if out_path == :doc
cwd = doc.path
elseif out_path == :pwd
cwd = pwd()
else
2016-04-24 14:02:03 +02:00
#If there is no extension, use as path
splitted = splitext(out_path)
if splitted[2] == ""
cwd = expanduser(out_path)
else
cwd = splitdir(expanduser(out_path))[1]
end
end
return cwd
end
2016-04-24 14:02:03 +02:00
"""Get output file name based on out_path"""
function get_outname(out_path::Symbol, doc::WeaveDoc; ext = nothing)
ext == nothing && (ext = doc.format.formatdict[:extension])
outname = "$(doc.cwd)/$(doc.basename).$ext"
end
"""Get output file name based on out_path"""
function get_outname(out_path::AbstractString, doc::WeaveDoc; ext = nothing)
ext == nothing && (ext = doc.format.formatdict[:extension])
splitted = splitext(out_path)
if (splitted[2]) == ""
outname = "$(doc.cwd)/$(doc.basename).$ext"
else
outname = expanduser(out_path)
end
end
function set_rc_params(formatdict, fig_path, fig_ext)
if fig_ext == nothing
rcParams[:chunk_defaults][:fig_ext] = formatdict[:fig_ext]
docParams[:fig_ext] = formatdict[:fig_ext]
else
rcParams[:chunk_defaults][:fig_ext] = fig_ext
docParams[:fig_ext] = fig_ext
end
rcParams[:chunk_defaults][:fig_path] = fig_path
docParams[:fig_path] = fig_path
return nothing
end
function collect_results(chunk::CodeChunk, fmt::ScriptResult)
content = ""
result_no = 1
result_chunks = CodeChunk[ ]
for r = chunk.result
#Check if there is any output from chunk
if strip(r.stdout) == "" && isempty(r.figures) && strip(r.rich_output) == ""
content *= r.code
else
content = "\n" * content * r.code
rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
content = ""
rchunk.result_no = result_no
result_no *=1
rchunk.figures = r.figures
rchunk.output = r.stdout * r.displayed
rchunk.rich_output = r.rich_output
push!(result_chunks, rchunk)
end
end
if content != ""
startswith(content, "\n") || (content = "\n" * content)
rchunk = CodeChunk(content, chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
push!(result_chunks, rchunk)
end
return result_chunks
end
function collect_results(chunk::CodeChunk, fmt::TermResult)
output = ""
2016-05-01 00:21:14 +02:00
prompt = chunk.options[:prompt]
result_no = 1
result_chunks = CodeChunk[ ]
2016-05-01 00:21:14 +02:00
for r = chunk.result
output *= prompt * r.code
2016-05-01 00:21:14 +02:00
output *= r.displayed * r.stdout
if !isempty(r.figures)
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
rchunk.output = output
output = ""
rchunk.figures = r.figures
push!(result_chunks, rchunk)
end
end
if output != ""
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
rchunk.output = output
push!(result_chunks, rchunk)
end
return result_chunks
end
function collect_results(chunk::CodeChunk, fmt::CollectResult)
result_no = 1
for r =chunk.result
chunk.output *= r.stdout
chunk.rich_output *= r.rich_output
chunk.figures = [chunk.figures; r.figures]
end
return [chunk]
end
function detect_plotlib(chunk::CodeChunk)
2017-03-14 08:27:16 +01:00
if isdefined(:Plots)
init_plotting("Plots")
#Need to set size before plots are created
2017-12-31 13:02:28 +01:00
Compat.invokelatest(plots_set_size, chunk)
return
end
2017-03-14 08:27:16 +01:00
isdefined(:PyPlot) && init_plotting("PyPlot") && return
isdefined(:Gadfly) && init_plotting("Gadfly") && return
end