Embed images for html at runtime and cache result

pull/80/head
Matti Pastell 2016-12-23 12:02:28 +02:00
parent 68b9bfda08
commit 4d499da343
3 changed files with 39 additions and 24 deletions

View File

@ -112,7 +112,10 @@ function weave(source ; doctype = :auto, plotlib=:auto,
end
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)
end

View File

@ -125,18 +125,7 @@ type MultiMarkdown
formatdict::Dict{Symbol,Any}
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)
fignames = chunk.figures
@ -159,10 +148,6 @@ function formatfigures(chunk, docformat::JMarkdown2HTML)
end
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"""
end

View File

@ -28,8 +28,10 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
doc.doctype = doctype
doc.format = formats[doctype]
if (contains(doctype, "2html") || contains(doctype, "2pdf")) && cache == :off
fig_path = mktempdir(doc.cwd)
if contains(doctype, "2pdf") && cache == :off
fig_path = mktempdir(doc.cwd)
elseif contains(doctype, "2html")
fig_path = mktempdir(doc.cwd)
end
doc.fig_path = fig_path
@ -70,8 +72,6 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
if typeof(chunk) == CodeChunk
options = merge(rcParams[:chunk_defaults], chunk.options)
merge!(chunk.options, options)
end
restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache])
@ -115,7 +115,34 @@ end
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
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)
#Run post_execute chunks
for hook in postexecute_hooks
chunk = hook(chunk)
@ -242,8 +270,6 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module)
chunks = collect_results(chunk, ScriptResult())
end
#else
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
#end
@ -356,6 +382,7 @@ function set_rc_params(formatdict, fig_path, fig_ext)
return nothing
end
function collect_results(chunk::CodeChunk, fmt::ScriptResult)
content = ""
result_no = 1