mirror of https://github.com/mpastell/Weave.jl
Embed images for html at runtime and cache result
parent
68b9bfda08
commit
4d499da343
|
@ -112,7 +112,10 @@ function weave(source ; doctype = :auto, plotlib=:auto,
|
||||||
end
|
end
|
||||||
|
|
||||||
doctype == :auto && (doctype = detect_doctype(doc.source))
|
doctype == :auto && (doctype = detect_doctype(doc.source))
|
||||||
if (contains(doctype, "2html") || contains(doctype, "2pdf")) && cache == :off
|
|
||||||
|
if contains(doctype, "2pdf") && cache == :off
|
||||||
|
rm(doc.fig_path, force = true, recursive = true)
|
||||||
|
elseif contains(doctype, "2html")
|
||||||
rm(doc.fig_path, force = true, recursive = true)
|
rm(doc.fig_path, force = true, recursive = true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -125,18 +125,7 @@ type MultiMarkdown
|
||||||
formatdict::Dict{Symbol,Any}
|
formatdict::Dict{Symbol,Any}
|
||||||
end
|
end
|
||||||
|
|
||||||
function img_to_base64(fig, ext, cwd)
|
|
||||||
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)
|
|
||||||
else
|
|
||||||
return(fig)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function formatfigures(chunk, docformat::JMarkdown2HTML)
|
function formatfigures(chunk, docformat::JMarkdown2HTML)
|
||||||
fignames = chunk.figures
|
fignames = chunk.figures
|
||||||
|
@ -159,10 +148,6 @@ function formatfigures(chunk, docformat::JMarkdown2HTML)
|
||||||
end
|
end
|
||||||
|
|
||||||
for fig = fignames
|
for fig = fignames
|
||||||
ext = splitext(fig)[2]
|
|
||||||
if ext == ".png" || ext == ".svg"
|
|
||||||
fig = img_to_base64(fig, ext, docformat.formatdict[:cwd])
|
|
||||||
end
|
|
||||||
|
|
||||||
figstring *= """<img src="$fig" $attribs />\n"""
|
figstring *= """<img src="$fig" $attribs />\n"""
|
||||||
end
|
end
|
||||||
|
|
39
src/run.jl
39
src/run.jl
|
@ -28,7 +28,9 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
|
||||||
doc.doctype = doctype
|
doc.doctype = doctype
|
||||||
doc.format = formats[doctype]
|
doc.format = formats[doctype]
|
||||||
|
|
||||||
if (contains(doctype, "2html") || contains(doctype, "2pdf")) && cache == :off
|
if contains(doctype, "2pdf") && cache == :off
|
||||||
|
fig_path = mktempdir(doc.cwd)
|
||||||
|
elseif contains(doctype, "2html")
|
||||||
fig_path = mktempdir(doc.cwd)
|
fig_path = mktempdir(doc.cwd)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,8 +72,6 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
|
||||||
if typeof(chunk) == CodeChunk
|
if typeof(chunk) == CodeChunk
|
||||||
options = merge(rcParams[:chunk_defaults], chunk.options)
|
options = merge(rcParams[:chunk_defaults], chunk.options)
|
||||||
merge!(chunk.options, options)
|
merge!(chunk.options, options)
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
|
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
|
||||||
|
@ -115,7 +115,34 @@ end
|
||||||
|
|
||||||
|
|
||||||
function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
result_chunk = eval_chunk(chunk, report, SandBox)
|
result_chunks = eval_chunk(chunk, report, SandBox)
|
||||||
|
contains(report.formatdict[:doctype], "2html") && (result_chunks = embed_figures(result_chunks, report.cwd))
|
||||||
|
return result_chunks
|
||||||
|
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)
|
||||||
|
else
|
||||||
|
return(fig)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function run_chunk(chunk::DocChunk, report::Report, SandBox::Module)
|
function run_chunk(chunk::DocChunk, report::Report, SandBox::Module)
|
||||||
|
@ -229,6 +256,7 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
|
|
||||||
chunk.result = run_code(chunk, report, SandBox)
|
chunk.result = run_code(chunk, report, SandBox)
|
||||||
|
|
||||||
|
|
||||||
#Run post_execute chunks
|
#Run post_execute chunks
|
||||||
for hook in postexecute_hooks
|
for hook in postexecute_hooks
|
||||||
chunk = hook(chunk)
|
chunk = hook(chunk)
|
||||||
|
@ -242,8 +270,6 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
|
||||||
chunks = collect_results(chunk, ScriptResult())
|
chunks = collect_results(chunk, ScriptResult())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
|
||||||
#end
|
#end
|
||||||
|
@ -356,6 +382,7 @@ function set_rc_params(formatdict, fig_path, fig_ext)
|
||||||
return nothing
|
return nothing
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function collect_results(chunk::CodeChunk, fmt::ScriptResult)
|
function collect_results(chunk::CodeChunk, fmt::ScriptResult)
|
||||||
content = ""
|
content = ""
|
||||||
result_no = 1
|
result_no = 1
|
||||||
|
|
Loading…
Reference in New Issue