Add support for html output of Plots Plotly backend

pull/55/head
Matti Pastell 2016-10-31 17:55:38 +02:00
parent bfd163e50f
commit cbdf0e2886
4 changed files with 39 additions and 13 deletions

View File

@ -11,10 +11,11 @@ type Report <: Display
term_state::Symbol
cur_chunk
mimetypes::Array{AbstractString}
first_plot::Bool
end
function Report(cwd, basename, formatdict, mimetypes)
Report(cwd, basename, formatdict, "", "", "", 1, AbstractString[], :text, nothing, mimetypes)
Report(cwd, basename, formatdict, "", "", "", 1, AbstractString[], :text, nothing, mimetypes, true)
end
@ -28,7 +29,12 @@ function Base.display(report::Report, data)
#Set preferred mimetypes for report based on format
for m in report.mimetypes
if mimewritable(m, data)
display(report, m, data)
try
display(report, m, data)
catch
info(m)
continue
end
#Always show plain text as well for term mode
if m "text/plain" && report.cur_chunk.options[:term]
display(report, "text/plain", data)
@ -40,9 +46,6 @@ end
function Base.display(report::Report, m::MIME"image/png", data)
figname = add_figure(report, ".png")
open(figname, "w") do io
show(io, m, data)
end
end
function Base.display(report::Report, m::MIME"image/svg+xml", data)
@ -87,6 +90,11 @@ end
function add_figure(report::Report, ext)
chunk = report.cur_chunk
full_name, rel_name = get_figname(report, chunk, ext = ext)
open(full_name, "w") do io
show(io, m, data)
end
push!(report.figures, rel_name)
report.fignum += 1
return full_name

View File

@ -201,9 +201,9 @@ const md2html = Pandoc("Markdown to HTML (requires Pandoc)",
:codeend=> "````\n\n",
:outputstart=> "````",
:outputend=> "````\n\n",
:fig_ext=> ".png",
:fig_ext=> ".svg",
:extension=> "md",
:mimetypes => ["image/png", "image/svg+xml", "image/jpg",
:mimetypes => ["image/svg+xml", "image/png", "image/jpg",
"text/html", "text/markdown", "text/plain"],
:doctype=> "md2html"))

View File

@ -9,3 +9,21 @@ function plots_set_size(chunk)
end
push_preexecute_hook(plots_set_size)
function Base.display(report::Report, m::MIME"image/svg+xml", data::Plots.Plot{Plots.PlotlyBackend})#
#Remove extra spaces from start of line for pandoc
s = reprmime(MIME("text/html"), data)
splitted = split(s, "\n")
start = split(splitted[1], r"(?=<div)")
#script = lstrip(start[1]) #local
script = "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>"
div = lstrip(start[2])
plot = join(map(lstrip, splitted[2:end]), "\n")
if report.first_plot
report.rich_output *= "\n" * script
report.first_plot = false
end
report.rich_output *= "\n" * div * "\n" * plot
end

View File

@ -20,15 +20,15 @@ if VERSION < v"0.5-dev"
include("chunk_opts_gadfly.jl")
#Fails on travis, works locally.
info("Test: Weaving with Winston")
include("winston_formats.jl")
#info("Test: Weaving with Winston")#
#include("winston_formats.jl")
info("Test: Weaving with Gadfly")
include("gadfly_formats.jl")
info("Test: Weaving with PyPlot")
include("pyplot_formats.jl")
info("Test: Weaving with PyPlot")
include("pyplot_formats.jl")
info("Test: Weaving with Plots.jl")
include("plotsjl_test.jl")
info("Test: Weaving with Plots.jl")
include("plotsjl_test.jl")
end