2018-07-26 10:35:17 +02:00
|
|
|
|
using Base64
|
2017-03-14 20:06:47 +01:00
|
|
|
|
|
2016-04-19 14:23:48 +02:00
|
|
|
|
"""
|
2018-07-26 19:32:10 +02:00
|
|
|
|
run(doc::WeaveDoc; doctype = :auto,
|
2018-07-26 01:01:10 +02:00
|
|
|
|
mod::Union{Module, Symbol} = :sandbox, out_path=:doc,
|
2018-07-25 22:24:25 +02:00
|
|
|
|
args=Dict(), fig_path = "figures", fig_ext = nothing,
|
|
|
|
|
cache_path = "cache", cache = :off, throw_errors=false)
|
2016-04-19 14:23:48 +02:00
|
|
|
|
|
|
|
|
|
Run code chunks and capture output from parsed document.
|
2015-01-08 17:52:42 +01:00
|
|
|
|
|
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()`
|
2015-01-08 17:52:42 +01:00
|
|
|
|
* `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.
|
2018-07-26 01:01:10 +02:00
|
|
|
|
* `mod`: Module where Weave `eval`s code. Defaults to `:sandbox`
|
|
|
|
|
to create new sandbox module, you can also pass a module e.g. `Main`.
|
2015-01-08 17:52:42 +01:00
|
|
|
|
* `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,
|
2018-07-25 22:24:25 +02:00
|
|
|
|
`:user` = cache based on chunk options, `:refresh`, run all code chunks and save new cache.
|
2015-01-08 17:52:42 +01:00
|
|
|
|
|
|
|
|
|
**Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
|
2016-04-19 14:23:48 +02:00
|
|
|
|
"""
|
2018-07-26 19:32:10 +02:00
|
|
|
|
function Base.run(doc::WeaveDoc; doctype = :auto,
|
2018-07-26 01:01:10 +02:00
|
|
|
|
mod::Union{Module, Symbol} = :sandbox, out_path=:doc,
|
2018-07-25 22:24:25 +02:00
|
|
|
|
args=Dict(), fig_path = "figures", fig_ext = nothing,
|
2019-10-15 21:48:05 +02:00
|
|
|
|
cache_path = "cache", cache = :off, throw_errors=false, latex_keep_unicode=false)
|
2015-01-08 17:52:42 +01:00
|
|
|
|
#cache :all, :user, :off, :refresh
|
2015-01-07 15:37:02 +01:00
|
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
|
doc.cwd = get_cwd(doc, out_path)
|
2016-04-22 15:16:12 +02:00
|
|
|
|
doctype == :auto && (doctype = detect_doctype(doc.source))
|
2015-01-07 15:37:02 +01:00
|
|
|
|
doc.doctype = doctype
|
2015-01-06 23:01:25 +01:00
|
|
|
|
doc.format = formats[doctype]
|
2019-10-15 21:48:05 +02:00
|
|
|
|
|
|
|
|
|
if (haskey(doc.format.formatdict, :keep_unicode))
|
|
|
|
|
doc.format.formatdict[:keep_unicode] = latex_keep_unicode
|
|
|
|
|
end
|
2020-03-18 00:53:25 +01:00
|
|
|
|
|
2016-12-29 16:58:21 +01:00
|
|
|
|
isdir(doc.cwd) || mkpath(doc.cwd)
|
2015-01-06 23:01:25 +01:00
|
|
|
|
|
2018-07-26 10:35:17 +02:00
|
|
|
|
if occursin("2pdf", doctype) && cache == :off
|
2016-12-29 16:58:21 +01:00
|
|
|
|
fig_path = mktempdir(abspath(doc.cwd))
|
2018-07-26 10:35:17 +02:00
|
|
|
|
elseif occursin("2html", doctype)
|
2016-12-29 16:58:21 +01:00
|
|
|
|
fig_path = mktempdir(abspath(doc.cwd))
|
2016-12-23 07:34:54 +01:00
|
|
|
|
end
|
2016-12-29 16:58:21 +01:00
|
|
|
|
|
2019-01-03 14:40:32 +01:00
|
|
|
|
cache == :off || @eval import Serialization
|
2018-01-07 19:01:18 +01:00
|
|
|
|
|
2016-12-23 11:27:10 +01:00
|
|
|
|
#This is needed for latex and should work on all output formats
|
2018-07-23 19:35:15 +02:00
|
|
|
|
Sys.iswindows() && (fig_path = replace(fig_path, "\\" => "/"))
|
2015-01-07 11:20:48 +01:00
|
|
|
|
|
2016-12-23 07:34:54 +01:00
|
|
|
|
doc.fig_path = fig_path
|
2019-03-05 18:36:27 +01:00
|
|
|
|
set_rc_params(doc, fig_path, fig_ext)
|
2016-04-27 15:05:11 +02:00
|
|
|
|
|
2016-12-26 12:26:36 +01:00
|
|
|
|
#New sandbox for each document with args exposed
|
2018-07-25 22:24:25 +02:00
|
|
|
|
if mod == :sandbox
|
2018-07-26 01:01:10 +02:00
|
|
|
|
sandbox = "WeaveSandBox$(rcParams[:doc_number])"
|
|
|
|
|
mod = Core.eval(Main, Meta.parse("module $sandbox\nend"))
|
2018-07-25 22:24:25 +02:00
|
|
|
|
end
|
|
|
|
|
@eval mod WEAVE_ARGS = Dict()
|
|
|
|
|
merge!(mod.WEAVE_ARGS, args)
|
|
|
|
|
|
2015-01-07 11:20:48 +01:00
|
|
|
|
rcParams[:doc_number] += 1
|
2015-01-06 23:01:25 +01:00
|
|
|
|
|
2016-04-27 15:05:11 +02:00
|
|
|
|
if haskey(doc.format.formatdict, :mimetypes)
|
|
|
|
|
mimetypes = doc.format.formatdict[:mimetypes]
|
|
|
|
|
else
|
|
|
|
|
mimetypes = default_mime_types
|
|
|
|
|
end
|
|
|
|
|
|
2018-01-07 10:53:07 +01:00
|
|
|
|
report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes, throw_errors)
|
2015-01-06 23:01:25 +01:00
|
|
|
|
pushdisplay(report)
|
|
|
|
|
|
2015-01-08 22:45:26 +01:00
|
|
|
|
if cache != :off && cache != :refresh
|
2015-01-07 15:37:02 +01:00
|
|
|
|
cached = read_cache(doc, cache_path)
|
2018-07-23 12:37:25 +02:00
|
|
|
|
cached == nothing && @info("No cached results found, running code")
|
2015-01-07 15:37:02 +01:00
|
|
|
|
else
|
|
|
|
|
cached = nothing
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
|
executed = Any[]
|
2015-01-07 15:37:02 +01:00
|
|
|
|
n = length(doc.chunks)
|
|
|
|
|
|
|
|
|
|
for i = 1:n
|
|
|
|
|
chunk = doc.chunks[i]
|
2016-04-23 18:18:44 +02:00
|
|
|
|
|
2016-12-26 20:21:55 +01:00
|
|
|
|
if isa(chunk, CodeChunk)
|
2019-03-05 18:36:27 +01:00
|
|
|
|
options = merge(doc.chunk_defaults, chunk.options)
|
2016-04-23 18:18:44 +02:00
|
|
|
|
merge!(chunk.options, options)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
|
|
|
|
|
|
|
|
|
|
if cached != nothing && (cache == :all || restore)
|
2016-04-23 17:15:59 +02:00
|
|
|
|
result_chunks = restore_chunk(chunk, cached)
|
|
|
|
|
else
|
2019-03-05 18:36:27 +01:00
|
|
|
|
result_chunks = run_chunk(chunk, doc, report, mod)
|
2016-04-23 17:15:59 +02:00
|
|
|
|
end
|
2016-04-29 15:41:18 +02:00
|
|
|
|
|
2015-01-11 00:20:28 +01:00
|
|
|
|
executed = [executed; result_chunks]
|
2015-01-06 23:01:25 +01:00
|
|
|
|
end
|
2015-01-07 15:37:02 +01:00
|
|
|
|
|
2016-11-02 22:45:32 +01:00
|
|
|
|
doc.header_script = report.header_script
|
2016-04-23 17:15:59 +02:00
|
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
|
popdisplay(report)
|
|
|
|
|
|
2016-04-23 17:15:59 +02:00
|
|
|
|
#Clear variables from used sandbox
|
2018-07-25 22:24:25 +02:00
|
|
|
|
mod == :sandbox && clear_sandbox(SandBox)
|
2016-04-23 17:15:59 +02:00
|
|
|
|
doc.chunks = executed
|
|
|
|
|
|
2015-01-07 15:37:02 +01:00
|
|
|
|
if cache != :off
|
|
|
|
|
write_cache(doc, cache_path)
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
|
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"
|
2018-07-26 10:35:17 +02:00
|
|
|
|
occursin("md", ext) && return "md2html"
|
|
|
|
|
occursin("rst", ext) && return "rst"
|
|
|
|
|
occursin("tex", ext) && return "texminted"
|
|
|
|
|
occursin("txt", ext) && return "asciidoc"
|
2016-04-22 15:16:12 +02:00
|
|
|
|
|
|
|
|
|
return "pandoc"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2019-03-05 18:36:27 +01:00
|
|
|
|
function run_chunk(chunk::CodeChunk, doc::WeaveDoc, report::Report, SandBox::Module)
|
2018-07-23 19:29:07 +02:00
|
|
|
|
@info("Weaving chunk $(chunk.number) from line $(chunk.start_line)")
|
2016-12-23 11:02:28 +01:00
|
|
|
|
result_chunks = eval_chunk(chunk, report, SandBox)
|
2018-07-26 10:35:17 +02:00
|
|
|
|
occursin("2html", report.formatdict[:doctype]) && (result_chunks = embed_figures(result_chunks, report.cwd))
|
2016-12-23 11:02:28 +01:00
|
|
|
|
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
|
|
|
|
|
|
2016-12-23 11:02:28 +01:00
|
|
|
|
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"
|
2019-02-08 15:01:25 +01:00
|
|
|
|
return "data:image/svg+xml;base64," * stringmime(MIME("image/svg"), raw)
|
2018-01-04 20:45:43 +01:00
|
|
|
|
elseif ext == ".gif"
|
|
|
|
|
return "data:image/gif;base64," * stringmime(MIME("image/gif"), raw)
|
2016-12-23 11:02:28 +01:00
|
|
|
|
else
|
|
|
|
|
return(fig)
|
|
|
|
|
end
|
2015-01-08 17:52:42 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-03-05 18:36:27 +01:00
|
|
|
|
function run_chunk(chunk::DocChunk, doc::WeaveDoc, report::Report, SandBox::Module)
|
|
|
|
|
chunk.content = [run_inline(c, doc, report, SandBox) for c in chunk.content]
|
2015-01-08 17:52:42 +01:00
|
|
|
|
return chunk
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-05 18:36:27 +01:00
|
|
|
|
function run_inline(inline::InlineText, doc::WeaveDoc, report::Report, SandBox::Module)
|
2016-12-26 20:21:55 +01:00
|
|
|
|
return inline
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-05 18:36:27 +01:00
|
|
|
|
function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, SandBox::Module)
|
2016-12-26 20:21:55 +01:00
|
|
|
|
#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))
|
2019-03-05 18:36:27 +01:00
|
|
|
|
options = merge(doc.chunk_defaults, chunk.options)
|
2016-12-26 20:21:55 +01:00
|
|
|
|
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)
|
2018-07-26 10:35:17 +02:00
|
|
|
|
occursin("2html", report.formatdict[:doctype]) && (chunks = embed_figures(chunks, report.cwd))
|
2016-12-26 20:28:57 +01:00
|
|
|
|
|
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
|
|
|
|
|
|
2015-01-11 00:20:28 +01:00
|
|
|
|
function reset_report(report::Report)
|
|
|
|
|
report.cur_result = ""
|
2016-04-11 17:40:18 +02:00
|
|
|
|
report.figures = AbstractString[]
|
2015-01-11 00:20:28 +01:00
|
|
|
|
report.term_state = :text
|
|
|
|
|
end
|
2015-01-06 23:01:25 +01:00
|
|
|
|
|
2015-01-11 22:38:58 +01:00
|
|
|
|
function run_code(chunk::CodeChunk, report::Report, SandBox::Module)
|
|
|
|
|
expressions = parse_input(chunk.content)
|
2016-04-27 13:51:15 +02:00
|
|
|
|
N = length(expressions)
|
2015-01-11 22:38:58 +01:00
|
|
|
|
#@show expressions
|
|
|
|
|
result_no = 1
|
|
|
|
|
results = ChunkOutput[ ]
|
|
|
|
|
|
|
|
|
|
for (str_expr, expr) = expressions
|
|
|
|
|
reset_report(report)
|
2016-04-27 13:51:15 +02:00
|
|
|
|
lastline = (result_no == N)
|
|
|
|
|
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
|
2018-07-26 19:32:10 +02:00
|
|
|
|
chunk.options[:display], lastline, report.throw_errors)
|
2015-01-11 22:38:58 +01:00
|
|
|
|
figures = report.figures #Captured figures
|
2016-04-27 16:14:42 +02:00
|
|
|
|
result = ChunkOutput(str_expr, out, report.cur_result, report.rich_output, figures)
|
2016-04-27 15:05:11 +02:00
|
|
|
|
report.rich_output = ""
|
2015-01-11 22:38:58 +01:00
|
|
|
|
push!(results, result)
|
2016-04-27 13:51:15 +02:00
|
|
|
|
result_no += 1
|
2015-01-11 22:38:58 +01:00
|
|
|
|
end
|
|
|
|
|
return results
|
|
|
|
|
end
|
|
|
|
|
|
2018-07-23 19:29:07 +02:00
|
|
|
|
getstdout() = stdout
|
2016-12-15 18:54:50 +01:00
|
|
|
|
|
2018-07-26 19:32:10 +02:00
|
|
|
|
function capture_output(expr, SandBox::Module, term, disp,
|
2018-01-07 10:53:07 +01:00
|
|
|
|
lastline, throw_errors=false)
|
2016-12-15 18:54:50 +01:00
|
|
|
|
#oldSTDOUT = STDOUT
|
|
|
|
|
oldSTDOUT = getstdout()
|
2015-01-10 19:10:40 +01:00
|
|
|
|
out = nothing
|
|
|
|
|
obj = nothing
|
|
|
|
|
rw, wr = redirect_stdout()
|
2018-07-23 19:29:07 +02:00
|
|
|
|
reader = @async read(rw, String)
|
2015-01-10 19:10:40 +01:00
|
|
|
|
try
|
2018-07-23 19:29:07 +02:00
|
|
|
|
obj = Core.eval(SandBox, expr)
|
2019-08-21 15:03:22 +02:00
|
|
|
|
if (term || disp) && (typeof(expr) != Expr || expr.head != :toplevel)
|
2015-01-11 22:38:58 +01:00
|
|
|
|
obj != nothing && display(obj)
|
2016-04-27 16:52:38 +02:00
|
|
|
|
#This shows images and lone variables, result can
|
2016-04-27 18:18:17 +02:00
|
|
|
|
#Handle last line sepately
|
2016-04-27 16:52:38 +02:00
|
|
|
|
elseif lastline && obj != nothing
|
2019-08-21 15:03:22 +02:00
|
|
|
|
(typeof(expr) != Expr || expr.head != :toplevel) && display(obj)
|
2015-01-11 22:38:58 +01:00
|
|
|
|
end
|
2018-01-07 10:53:07 +01:00
|
|
|
|
catch E
|
|
|
|
|
throw_errors && throw(E)
|
|
|
|
|
display(E)
|
2018-07-24 11:50:28 +02:00
|
|
|
|
@warn("ERROR: $(typeof(E)) occurred, including output in Weaved document")
|
2015-01-10 19:10:40 +01:00
|
|
|
|
finally
|
|
|
|
|
redirect_stdout(oldSTDOUT)
|
|
|
|
|
close(wr)
|
2018-07-23 19:29:07 +02:00
|
|
|
|
out = fetch(reader)
|
2015-01-10 19:10:40 +01:00
|
|
|
|
close(rw)
|
|
|
|
|
end
|
2019-02-27 15:19:14 +01:00
|
|
|
|
out = replace(out, r"\u001b\[.*?m" => "") #Remove ANSI color codes
|
2015-01-10 19:10:40 +01:00
|
|
|
|
return (obj, out)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Parse chunk input to array of expressions
|
2016-04-11 17:40:18 +02:00
|
|
|
|
function parse_input(input::AbstractString)
|
2016-04-27 16:30:33 +02:00
|
|
|
|
parsed = Tuple{AbstractString, Any}[]
|
2016-12-26 20:21:55 +01:00
|
|
|
|
input = lstrip(input)
|
2019-06-04 20:02:40 +02:00
|
|
|
|
n = sizeof(input)
|
2016-12-26 20:21:55 +01:00
|
|
|
|
pos = 1 #The first character is extra line end
|
2016-04-27 16:30:33 +02:00
|
|
|
|
while pos ≤ n
|
2015-01-10 19:10:40 +01:00
|
|
|
|
oldpos = pos
|
2018-07-23 19:29:07 +02:00
|
|
|
|
code, pos = Meta.parse(input, pos)
|
2015-01-11 00:20:28 +01:00
|
|
|
|
push!(parsed, (input[oldpos:pos-1] , code ))
|
2015-01-10 19:10:40 +01:00
|
|
|
|
end
|
|
|
|
|
parsed
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-11 00:20:28 +01:00
|
|
|
|
|
2015-01-07 11:20:48 +01:00
|
|
|
|
function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
2015-01-04 14:45:41 +01:00
|
|
|
|
if !chunk.options[:eval]
|
|
|
|
|
chunk.output = ""
|
|
|
|
|
chunk.options[:fig] = false
|
|
|
|
|
return chunk
|
|
|
|
|
end
|
|
|
|
|
|
2016-04-29 15:41:18 +02:00
|
|
|
|
#Run preexecute_hooks
|
|
|
|
|
for hook in preexecute_hooks
|
2020-03-18 00:53:25 +01:00
|
|
|
|
chunk = Base.invokelatest(hook, chunk)
|
2016-04-29 15:41:18 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 14:45:41 +01:00
|
|
|
|
report.fignum = 1
|
|
|
|
|
report.cur_chunk = chunk
|
2015-01-08 17:52:42 +01:00
|
|
|
|
|
2015-01-04 14:45:41 +01:00
|
|
|
|
if haskey(report.formatdict, :out_width) && chunk.options[:out_width] == nothing
|
|
|
|
|
chunk.options[:out_width] = report.formatdict[:out_width]
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-11 22:38:58 +01:00
|
|
|
|
chunk.result = run_code(chunk, report, SandBox)
|
2016-04-29 15:41:18 +02:00
|
|
|
|
|
2016-12-23 11:02:28 +01:00
|
|
|
|
|
2016-04-29 15:41:18 +02:00
|
|
|
|
#Run post_execute chunks
|
|
|
|
|
for hook in postexecute_hooks
|
2020-03-18 00:53:25 +01:00
|
|
|
|
chunk = Base.invokelatest(hook, chunk)
|
2016-04-29 15:41:18 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-04 14:45:41 +01:00
|
|
|
|
if chunk.options[:term]
|
2015-01-11 22:38:58 +01:00
|
|
|
|
chunks = collect_results(chunk, TermResult())
|
2016-04-19 15:38:03 +02:00
|
|
|
|
elseif chunk.options[:hold]
|
2015-01-11 22:38:58 +01:00
|
|
|
|
chunks = collect_results(chunk, CollectResult())
|
2015-01-04 14:45:41 +01:00
|
|
|
|
else
|
2015-01-11 22:38:58 +01:00
|
|
|
|
chunks = collect_results(chunk, ScriptResult())
|
2015-01-04 14:45:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-04-11 21:08:41 +02:00
|
|
|
|
#else
|
2015-01-11 00:20:28 +01:00
|
|
|
|
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
|
|
|
|
#end
|
2016-04-23 17:15:59 +02:00
|
|
|
|
|
2015-01-11 00:20:28 +01:00
|
|
|
|
chunks
|
2015-01-04 14:45:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
2015-01-10 19:10:40 +01:00
|
|
|
|
|
2015-01-08 18:35:47 +01:00
|
|
|
|
#function eval_chunk(chunk::DocChunk, report::Report, SandBox)
|
|
|
|
|
# chunk
|
|
|
|
|
#end
|
2015-01-08 17:52:42 +01:00
|
|
|
|
|
2015-01-07 11:20:48 +01:00
|
|
|
|
#Set all variables to nothing
|
|
|
|
|
function clear_sandbox(SandBox::Module)
|
2018-07-23 12:37:25 +02:00
|
|
|
|
for name = names(SandBox, all=true)
|
2015-01-07 11:20:48 +01:00
|
|
|
|
if name != :eval && name != names(SandBox)[1]
|
2018-07-23 12:37:25 +02:00
|
|
|
|
try eval(SandBox, Meta.parse(AbstractString(AbstractString(name), "=nothing"))) catch; end
|
2015-01-07 11:20:48 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
2016-04-27 13:51:15 +02:00
|
|
|
|
function get_figname(report::Report, chunk; fignum = nothing, ext = nothing)
|
2015-01-07 09:26:11 +01:00
|
|
|
|
figpath = joinpath(report.cwd, chunk.options[:fig_path])
|
2015-01-08 17:52:42 +01:00
|
|
|
|
isdir(figpath) || mkpath(figpath)
|
2016-04-27 13:51:15 +02:00
|
|
|
|
ext == nothing && (ext = chunk.options[:fig_ext])
|
2015-01-07 09:26:11 +01:00
|
|
|
|
fignum == nothing && (fignum = report.fignum)
|
|
|
|
|
|
2018-07-27 09:42:14 +02:00
|
|
|
|
chunkid = (chunk.options[:label] == nothing) ? chunk.number : chunk.options[:label]
|
2015-01-07 09:26:11 +01:00
|
|
|
|
full_name = joinpath(report.cwd, chunk.options[:fig_path],
|
2016-12-23 11:27:10 +01:00
|
|
|
|
"$(report.basename)_$(chunkid)_$(fignum)$ext")
|
2015-01-07 09:26:11 +01:00
|
|
|
|
rel_name = "$(chunk.options[:fig_path])/$(report.basename)_$(chunkid)_$(fignum)$ext" #Relative path is used in output
|
|
|
|
|
return full_name, rel_name
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-06 23:01:25 +01:00
|
|
|
|
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
|
2015-01-06 23:01:25 +01:00
|
|
|
|
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
|
|
|
|
|
|
2019-03-05 18:36:27 +01:00
|
|
|
|
function set_rc_params(doc::WeaveDoc, fig_path, fig_ext)
|
|
|
|
|
formatdict = doc.format.formatdict
|
2015-01-06 23:01:25 +01:00
|
|
|
|
if fig_ext == nothing
|
2019-03-05 18:36:27 +01:00
|
|
|
|
doc.chunk_defaults[:fig_ext] = formatdict[:fig_ext]
|
2015-01-06 23:01:25 +01:00
|
|
|
|
else
|
2019-03-05 18:36:27 +01:00
|
|
|
|
doc.chunk_defaults[:fig_ext] = fig_ext
|
2015-01-06 23:01:25 +01:00
|
|
|
|
end
|
2019-03-05 18:36:27 +01:00
|
|
|
|
doc.chunk_defaults[:fig_path] = fig_path
|
2015-01-06 23:01:25 +01:00
|
|
|
|
return nothing
|
|
|
|
|
end
|
2015-01-11 22:38:58 +01:00
|
|
|
|
|
|
|
|
|
function collect_results(chunk::CodeChunk, fmt::ScriptResult)
|
|
|
|
|
content = ""
|
2016-04-30 23:59:12 +02:00
|
|
|
|
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
|
|
|
|
|
|
2015-01-11 22:38:58 +01:00
|
|
|
|
function collect_results(chunk::CodeChunk, fmt::TermResult)
|
|
|
|
|
output = ""
|
2016-05-01 00:21:14 +02:00
|
|
|
|
prompt = chunk.options[:prompt]
|
2015-01-11 22:38:58 +01:00
|
|
|
|
result_no = 1
|
|
|
|
|
result_chunks = CodeChunk[ ]
|
2016-05-01 00:21:14 +02:00
|
|
|
|
for r = chunk.result
|
2015-01-11 22:38:58 +01:00
|
|
|
|
output *= prompt * r.code
|
2016-05-01 00:21:14 +02:00
|
|
|
|
output *= r.displayed * r.stdout
|
2015-01-11 22:38:58 +01:00
|
|
|
|
if !isempty(r.figures)
|
2016-04-23 17:15:59 +02:00
|
|
|
|
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
|
2015-01-11 22:38:58 +01:00
|
|
|
|
rchunk.output = output
|
|
|
|
|
output = ""
|
|
|
|
|
rchunk.figures = r.figures
|
|
|
|
|
push!(result_chunks, rchunk)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
if output != ""
|
2016-04-23 17:15:59 +02:00
|
|
|
|
rchunk = CodeChunk("", chunk.number, chunk.start_line, chunk.optionstring, copy(chunk.options))
|
2015-01-11 22:38:58 +01:00
|
|
|
|
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
|
2016-04-27 16:14:42 +02:00
|
|
|
|
chunk.rich_output *= r.rich_output
|
2016-04-19 16:07:07 +02:00
|
|
|
|
chunk.figures = [chunk.figures; r.figures]
|
2015-01-11 22:38:58 +01:00
|
|
|
|
end
|
|
|
|
|
return [chunk]
|
|
|
|
|
end
|