2017-05-12 15:12:05 +02:00
|
|
|
using Compat
|
2018-07-24 11:50:28 +02:00
|
|
|
using Markdown
|
2019-02-27 17:21:05 +01:00
|
|
|
import .WeaveMarkdown
|
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" ]
|
|
|
|
|
2019-02-08 15:01:25 +01:00
|
|
|
const mimetype_ext =
|
2019-02-01 16:59:39 +01:00
|
|
|
Dict(".png" => "image/png",
|
|
|
|
".jpg" => "image/jpeg",
|
|
|
|
".jpeg" => "image/jpeg",
|
|
|
|
".svg" => "image/svg+xml",
|
|
|
|
".pdf" => "application/pdf")
|
|
|
|
|
2016-04-27 16:14:42 +02:00
|
|
|
function Base.display(report::Report, data)
|
|
|
|
#Set preferred mimetypes for report based on format
|
2019-02-01 16:59:39 +01:00
|
|
|
fig_ext = report.cur_chunk.options[:fig_ext]
|
2019-02-08 15:01:25 +01:00
|
|
|
for m in unique([mimetype_ext[fig_ext] ; report.mimetypes])
|
2019-03-11 09:23:31 +01:00
|
|
|
if Base.invokelatest(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)
|
2018-07-26 11:42:52 +02:00
|
|
|
figname = add_figure(report, data, m, ".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)
|
2019-02-27 14:03:33 +01:00
|
|
|
# Convert to "richer" type of possible
|
|
|
|
for m in report.mimetypes
|
|
|
|
if m == "text/html" || m == "text/latex"
|
2019-02-27 17:21:05 +01:00
|
|
|
display(Markdown.parse(s, flavor=WeaveMarkdown.weavemd))
|
2019-02-27 14:03:33 +01:00
|
|
|
break
|
|
|
|
elseif m == "text/markdown"
|
|
|
|
report.rich_output *= "\n" * s
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2016-04-27 20:52:55 +02:00
|
|
|
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
|
2018-07-26 11:42:52 +02:00
|
|
|
if ext == ".pdf"
|
|
|
|
write(io, repr(m, data))
|
|
|
|
else
|
|
|
|
show(io, m, data)
|
|
|
|
end
|
2016-10-31 16:55:38 +01:00
|
|
|
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
|