2016-04-27 16:14:42 +02:00
|
|
|
#Contains report global properties
|
|
|
|
type Report <: Display
|
|
|
|
cwd::AbstractString
|
|
|
|
basename::AbstractString
|
|
|
|
formatdict::Dict{Symbol,Any}
|
|
|
|
pending_code::AbstractString
|
|
|
|
cur_result::AbstractString
|
|
|
|
rich_output::AbstractString
|
|
|
|
fignum::Int
|
|
|
|
figures::Array{AbstractString}
|
|
|
|
term_state::Symbol
|
|
|
|
cur_chunk
|
|
|
|
mimetypes::Array{AbstractString}
|
2016-10-31 16:55:38 +01:00
|
|
|
first_plot::Bool
|
2016-11-02 22:45:32 +01:00
|
|
|
header_script::String
|
2016-04-27 16:14:42 +02:00
|
|
|
end
|
2016-04-27 13:51:15 +02:00
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
function Report(cwd, basename, formatdict, mimetypes)
|
2016-11-02 22:45:32 +01:00
|
|
|
Report(cwd, basename, formatdict, "", "", "", 1, AbstractString[], :text, nothing, mimetypes, true, "")
|
2016-04-27 16:14:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
#Default mimetypes in order, can be overridden for some inside `run method` formats
|
|
|
|
const default_mime_types = ["image/svg+xml", "image/png", "text/html", "text/plain"]
|
2016-10-30 22:47:43 +01:00
|
|
|
#const default_mime_types = ["image/png", "image/svg+xml", "text/html", "text/plain"]
|
2016-04-27 16:14:42 +02:00
|
|
|
#From IJulia as a reminder
|
|
|
|
#const supported_mime_types = [ "text/html", "text/latex", "image/svg+xml", "image/png", "image/jpeg", "text/plain", "text/markdown" ]
|
|
|
|
|
|
|
|
function Base.display(report::Report, data)
|
|
|
|
#Set preferred mimetypes for report based on format
|
|
|
|
for m in report.mimetypes
|
|
|
|
if mimewritable(m, data)
|
2016-10-31 16:55:38 +01:00
|
|
|
try
|
2017-03-14 20:06:47 +01:00
|
|
|
if VERSION >= v"0.6-alpha"
|
|
|
|
new_dp(x, y, z) = eval(
|
|
|
|
Expr(:call, (x, y, z) -> display(x, y, z), x, y, z))
|
|
|
|
new_dp(report, m, data)
|
|
|
|
else
|
|
|
|
display(report, m, data)
|
|
|
|
end
|
|
|
|
catch e
|
|
|
|
warn("Failed to display data in \"$m\" format")
|
|
|
|
continue
|
2016-10-31 16:55:38 +01:00
|
|
|
end
|
2016-04-28 10:16:02 +02:00
|
|
|
#Always show plain text as well for term mode
|
2016-04-27 16:14:42 +02:00
|
|
|
if m ≠ "text/plain" && report.cur_chunk.options[:term]
|
2017-03-14 20:06:47 +01:00
|
|
|
display(report, "text/plain", data)
|
2016-04-27 16:14:42 +02:00
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2016-04-27 13:51:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"image/png", data)
|
2016-11-01 14:23:14 +01:00
|
|
|
figname = add_figure(report, data, m, ".png")
|
2016-04-27 13:51:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"image/svg+xml", data)
|
2016-11-01 14:23:14 +01:00
|
|
|
figname = add_figure(report, data, m, ".svg")
|
2016-04-27 13:51:15 +02:00
|
|
|
end
|
2016-04-27 15:05:11 +02:00
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"application/pdf", data)
|
2016-11-01 14:23:14 +01:00
|
|
|
figname = add_figure(report, m, data, ".pdf")
|
2016-04-27 15:05:11 +02:00
|
|
|
end
|
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
#Text is written to stdout, called from "term" mode chunks
|
2016-04-27 15:05:11 +02:00
|
|
|
function Base.display(report::Report, m::MIME"text/plain", data)
|
|
|
|
s = reprmime(m, data)
|
2017-03-13 14:05:07 +01:00
|
|
|
println(s)
|
2016-04-27 15:05:11 +02:00
|
|
|
end
|
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
#Catch "rich_output"
|
2016-04-27 15:05:11 +02:00
|
|
|
function Base.display(report::Report, m::MIME"text/html", data)
|
|
|
|
s = reprmime(m, data)
|
2016-04-27 16:14:42 +02:00
|
|
|
report.rich_output *= "\n" * s
|
|
|
|
end
|
|
|
|
|
2016-04-27 20:52:55 +02:00
|
|
|
#Catch "rich_output"
|
|
|
|
function Base.display(report::Report, m::MIME"text/markdown", data)
|
|
|
|
s = reprmime(m, data)
|
|
|
|
report.rich_output *= "\n" * s
|
|
|
|
end
|
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"text/latex", data)
|
|
|
|
s = reprmime(m, data)
|
|
|
|
report.rich_output *= "\n" * s
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
"""Add saved figure name to results and return the name"""
|
2016-11-01 14:23:14 +01:00
|
|
|
function add_figure(report::Report, data, m, ext)
|
2016-04-27 16:14:42 +02:00
|
|
|
chunk = report.cur_chunk
|
|
|
|
full_name, rel_name = get_figname(report, chunk, ext = ext)
|
2016-10-31 16:55:38 +01:00
|
|
|
|
|
|
|
open(full_name, "w") do io
|
|
|
|
show(io, m, data)
|
|
|
|
end
|
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
push!(report.figures, rel_name)
|
|
|
|
report.fignum += 1
|
|
|
|
return full_name
|
2016-04-27 15:05:11 +02:00
|
|
|
end
|