2017-05-12 15:12:05 +02:00
|
|
|
using Compat
|
2018-07-24 11:50:28 +02:00
|
|
|
using Markdown
|
2017-05-12 15:12:05 +02:00
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
#Contains report global properties
|
2018-07-23 12:37:25 +02:00
|
|
|
mutable struct Report <: AbstractDisplay
|
2016-04-27 16:14:42 +02:00
|
|
|
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
|
2018-01-07 10:53:07 +01:00
|
|
|
throw_errors::Bool
|
2016-04-27 16:14:42 +02:00
|
|
|
end
|
2016-04-27 13:51:15 +02:00
|
|
|
|
2018-01-07 10:53:07 +01:00
|
|
|
function Report(cwd, basename, formatdict, mimetypes, throw_errors)
|
|
|
|
Report(cwd, basename, formatdict, "", "", "", 1, AbstractString[], :text, nothing,
|
|
|
|
mimetypes, true, "", throw_errors)
|
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
|
2018-07-23 19:29:07 +02:00
|
|
|
if showable(m, data)
|
2016-10-31 16:55:38 +01:00
|
|
|
try
|
2018-01-03 18:32:10 +01:00
|
|
|
if !istextmime(m)
|
2018-01-07 10:53:07 +01:00
|
|
|
Compat.invokelatest(display, report, m, data)
|
2018-01-03 18:32:10 +01:00
|
|
|
elseif report.cur_chunk.options[:term]
|
2018-01-04 17:02:09 +01:00
|
|
|
Compat.invokelatest(display, report, "text/plain", data)
|
2018-01-07 10:53:07 +01:00
|
|
|
else
|
2018-01-03 18:32:10 +01:00
|
|
|
Compat.invokelatest(display, report, m, data)
|
|
|
|
end
|
2017-03-14 20:06:47 +01:00
|
|
|
catch e
|
2018-01-07 10:53:07 +01:00
|
|
|
throw(e)
|
2018-07-24 11:50:28 +02:00
|
|
|
@warn("Failed to display data in \"$m\" format")
|
2017-03-14 20:06:47 +01:00
|
|
|
continue
|
2016-10-31 16:55:38 +01:00
|
|
|
end
|
2016-04-27 16:14:42 +02:00
|
|
|
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)
|
2018-01-03 14:48:14 +01:00
|
|
|
io = PipeBuffer()
|
|
|
|
show(IOContext(io, :limit => true), m, data)
|
|
|
|
flush(io)
|
2018-07-23 12:37:25 +02:00
|
|
|
s = read(io, String)
|
2018-01-03 14:48:14 +01:00
|
|
|
close(io)
|
2017-03-13 14:05:07 +01:00
|
|
|
println(s)
|
2016-04-27 15:05:11 +02:00
|
|
|
end
|
|
|
|
|
2018-01-07 10:53:07 +01:00
|
|
|
function Base.display(report::Report, m::MIME"text/plain", data::Exception)
|
|
|
|
println("Error: " * sprint(showerror, data))
|
|
|
|
end
|
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"text/html", data::Exception)
|
|
|
|
report.rich_output = sprint(show, m, data)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Base.show(io, m::MIME"text/html", data::Exception)
|
|
|
|
println(io ,"<pre class=\"julia-error\">")
|
2018-07-24 11:50:28 +02:00
|
|
|
println(io, Markdown.htmlesc("ERROR: " * sprint(showerror, data)))
|
2018-01-07 10:53:07 +01:00
|
|
|
println(io ,"</pre>")
|
|
|
|
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)
|
2018-07-23 12:37:25 +02:00
|
|
|
s = repr(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)
|
2018-07-23 12:37:25 +02:00
|
|
|
s = repr(m, data)
|
2016-04-27 20:52:55 +02:00
|
|
|
report.rich_output *= "\n" * s
|
|
|
|
end
|
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"text/latex", data)
|
2018-07-23 12:37:25 +02:00
|
|
|
s = repr(m, data)
|
2016-04-27 20:52:55 +02:00
|
|
|
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
|