Added more general display methods for png and svg, gadfly modified to catch svg's

pull/37/head
Matti Pastell 2016-04-27 14:51:15 +03:00
parent f24b2157a6
commit 37f0c7f796
4 changed files with 48 additions and 20 deletions

View File

@ -18,17 +18,13 @@ function Report(cwd, basename, formatdict)
Report(cwd, basename, formatdict, "", "", 1, AbstractString[], :text, nothing)
end
#const report = Report()
const supported_mime_types =
[MIME"image/png",
MIME"text/plain"]
const supported_mime_types = ["image/svg+xml", "image/png", "text/plain"]
#const supported_mime_types = [ "text/html", "text/latex", "image/svg+xml", "image/png", "image/jpeg", "text/plain", "text/markdown" ]
function Base.display(doc::Report, data)
for m in supported_mime_types
if mimewritable(m(), data)
display(doc, m(), data)
if mimewritable(m, data)
display(doc, m, data)
break
end
end
@ -60,7 +56,7 @@ function tangle(source ; out_path=:doc, informat=:auto)
doc.cwd = get_cwd(doc, out_path)
outname = get_outname(out_path, doc, ext = "jl")
open(outname, "w") do io
for chunk in doc.chunks
if typeof(chunk) == CodeChunk
@ -128,13 +124,15 @@ function weave(source ; doctype = :auto, plotlib="Gadfly",
info("Report weaved to $outname")
end
function Base.display(report::Report, m::MIME"text/plain", data)
s = reprmime(m, data)
print("\n" * s)
#report.cur_result *= "\n" * s
end
#This should not go to stdout, need to think of a good way to catch it!
function Base.display(report::Report, m::MIME"text/html", data)
s = reprmime(m, data)
print("\n" * s)
end
function weave(doc::AbstractString, doctype::AbstractString)
@ -146,6 +144,7 @@ export weave, list_out_formats, tangle,
include("config.jl")
include("chunks.jl")
include("display_methods.jl")
include("readers.jl")
include("run.jl")
include("cache.jl")

23
src/display_methods.jl Normal file
View File

@ -0,0 +1,23 @@
"""Add saved figure name to results and return the name"""
function add_figure(report::Report, ext)
chunk = report.cur_chunk
full_name, rel_name = get_figname(report, chunk, ext = ext)
push!(report.figures, rel_name)
report.fignum += 1
return full_name
end
function Base.display(report::Report, m::MIME"image/png", data)
figname = add_figure(report, ".png")
open(figname, "w") do io
writemime(io, m, data)
end
end
function Base.display(report::Report, m::MIME"image/svg+xml", data)
figname = add_figure(report, ".svg")
open(figname, "w") do io
writemime(io, m, data)
end
end

View File

@ -1,9 +1,9 @@
using Gadfly
Gadfly.set_default_plot_format(:png)
Gadfly.set_default_plot_format(:svg)
#Captures figures
function Base.display(report::Report, m::MIME"image/png", p::Plot)
function Base.display(report::Report, m::MIME"image/svg+xml", p::Plot)
chunk = report.cur_chunk
full_name, rel_name = get_figname(report, chunk)

View File

@ -112,17 +112,21 @@ 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)
(obj, out) = capture_output(expr, SandBox, chunk.options[:term], rcParams[:plotlib])
lastline = (result_no == N)
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
rcParams[:plotlib], lastline)
displayed = report.cur_result #Not needed?
figures = report.figures #Captured figures
result = ChunkOutput(str_expr, out, displayed, figures)
push!(results, result)
result_no += 1
end
#Save figures only in the end of chunk for PyPlot
@ -133,7 +137,7 @@ function run_code(chunk::CodeChunk, report::Report, SandBox::Module)
return results
end
function capture_output(expr::Expr, SandBox::Module, term, plotlib)
function capture_output(expr::Expr, SandBox::Module, term, plotlib, lastline)
oldSTDOUT = STDOUT
out = nothing
obj = nothing
@ -144,7 +148,9 @@ function capture_output(expr::Expr, SandBox::Module, term, plotlib)
obj != nothing && display(obj)
elseif plotlib == "Gadfly" && typeof(obj) == Gadfly.Plot
obj != nothing && display(obj)
elseif plotlib == "Plots" && issubtype(typeof(obj), Plots.Plot)
#Display Plots.jl plots if they are the last expr in script mode
#elseif plotlib == "Plots" && lastline && issubtype(typeof(obj), Plots.Plot)
elseif lastline && mimewritable("image/png", obj)
obj != nothing && display(obj)
end
@ -219,10 +225,10 @@ function clear_sandbox(SandBox::Module)
end
function get_figname(report::Report, chunk; fignum = nothing)
function get_figname(report::Report, chunk; fignum = nothing, ext = nothing)
figpath = joinpath(report.cwd, chunk.options[:fig_path])
isdir(figpath) || mkpath(figpath)
ext = chunk.options[:fig_ext]
ext == nothing && (ext = chunk.options[:fig_ext])
fignum == nothing && (fignum = report.fignum)
chunkid = (chunk.options[:name] == nothing) ? chunk.number : chunk.options[:name]