Remove plot library hack and use Requires instead

pull/137/head
Matti Pastell 2018-07-26 20:32:10 +03:00
parent 44455a1fda
commit 160017b6e8
10 changed files with 30 additions and 91 deletions

View File

@ -6,4 +6,5 @@ Highlights
Mustache
YAML
Compat 0.25.0
Requires
Plots

View File

@ -41,7 +41,6 @@ source = args["source"]
delete!(args, "source")
args_col = []
args["plotlib"] == "nothing" && (args["plotlib"] = nothing)
#Check for special values of out_path
#args["informat"] == ":auto" && (args["informat"] = :auto)

View File

@ -2,6 +2,11 @@ __precompile__()
module Weave
import Highlights
using Compat
using Requires
function __init__()
@require Plots="91a5bcdd-55d7-5caf-9e0b-520d859cae80" include("plots.jl")
end
"""
`list_out_formats()`
@ -46,7 +51,7 @@ end
"""
weave(source ; doctype = :auto, plotlib=:auto,
weave(source ; doctype = :auto,
informat=:auto, out_path=:doc, args = Dict(),
mod::Union{Module, Symbol} = Main,
fig_path = "figures", fig_ext = nothing,
@ -58,7 +63,6 @@ Weave an input document to output file.
* `doctype`: :auto = set based on file extension or specify one of the supported formats.
See `list_out_formats()`
* `plotlib`: `"PyPlot"`, `"Gadfly"` or `nothing`
* `informat`: :auto = set based on file extension or set to `"noweb"`, `"markdown"` or `script`
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`:
Julia working directory, `"somepath"`: output directory as a String e.g `"/home/mpastell/weaveout"` or filename as
@ -80,7 +84,7 @@ Weave an input document to output file.
**Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
"""
function weave(source ; doctype = :auto, plotlib=:auto,
function weave(source ; doctype = :auto,
informat=:auto, out_path=:doc, args = Dict(),
mod::Union{Module, Symbol} = :sandbox,
fig_path = "figures", fig_ext = nothing,
@ -96,7 +100,7 @@ function weave(source ; doctype = :auto, plotlib=:auto,
template != nothing && (doc.template = template)
try
doc = run(doc, doctype = doctype, plotlib=plotlib,
doc = run(doc, doctype = doctype,
mod = mod,
out_path=out_path, args = args,
fig_path = fig_path, fig_ext = fig_ext, cache_path = cache_path, cache=cache,
@ -214,6 +218,8 @@ include("format.jl")
include("pandoc.jl")
include("writers.jl")
export weave, list_out_formats, tangle, convert_doc, notebook,
set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults,
include_weave

View File

@ -2,11 +2,9 @@
#Default options
const defaultParams =
Dict{Symbol,Any}(:plotlib => nothing,
:plotlib_set => false,
:storeresults => false,
:doc_number => 0,
:chunk_defaults =>
Dict{Symbol,Any}(:storeresults => false,
:doc_number => 0,
:chunk_defaults =>
Dict{Symbol,Any}(
:echo=> true,
:results=> "markup",

View File

@ -1,7 +1,7 @@
using Base64
"""
run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
run(doc::WeaveDoc; doctype = :auto,
mod::Union{Module, Symbol} = :sandbox, out_path=:doc,
args=Dict(), fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache = :off, throw_errors=false)
@ -10,7 +10,6 @@ Run code chunks and capture output from parsed document.
* `doctype`: :auto = set based on file extension or specify one of the supported formats.
See `list_out_formats()`
* `plotlib`: `"PyPlot"`, `"Gadfly"`, or `"Plots`
* `out_path`: Path where the output is generated. Can be: `:doc`: Path of the source document, `:pwd`: Julia working directory,
`"somepath"`: Path as a AbstractString e.g `"/home/mpastell/weaveout"`
* `args`: dictionary of arguments to pass to document. Available as WEAVE_ARGS.
@ -24,7 +23,7 @@ Run code chunks and capture output from parsed document.
**Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output.
"""
function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
function Base.run(doc::WeaveDoc; doctype = :auto,
mod::Union{Module, Symbol} = :sandbox, out_path=:doc,
args=Dict(), fig_path = "figures", fig_ext = nothing,
cache_path = "cache", cache = :off, throw_errors=false)
@ -66,10 +65,6 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto,
mimetypes = default_mime_types
end
#Reset plotting
rcParams[:plotlib_set] = false
plotlib == :auto || init_plotting(plotlib)
report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes, throw_errors)
pushdisplay(report)
@ -210,27 +205,20 @@ function run_code(chunk::CodeChunk, report::Report, SandBox::Module)
for (str_expr, expr) = expressions
reset_report(report)
lastline = (result_no == N)
rcParams[:plotlib_set] || detect_plotlib(chunk, SandBox) #Try to autodetect plotting library
(obj, out) = capture_output(expr, SandBox, chunk.options[:term],
chunk.options[:display], rcParams[:plotlib], lastline, report.throw_errors)
chunk.options[:display], lastline, report.throw_errors)
figures = report.figures #Captured figures
result = ChunkOutput(str_expr, out, report.cur_result, report.rich_output, figures)
report.rich_output = ""
push!(results, result)
result_no += 1
end
#Save figures only in the end of chunk for PyPlot
if rcParams[:plotlib] == "PyPlot"
Compat.invokelatest(savefigs_pyplot, report)
end
return results
end
getstdout() = stdout
function capture_output(expr, SandBox::Module, term, disp, plotlib,
function capture_output(expr, SandBox::Module, term, disp,
lastline, throw_errors=false)
#oldSTDOUT = STDOUT
oldSTDOUT = getstdout()
@ -244,8 +232,6 @@ function capture_output(expr, SandBox::Module, term, disp, plotlib,
obj != nothing && display(obj)
elseif typeof(expr) == Symbol
display(obj)
elseif plotlib == "Gadfly" && typeof(obj) == Gadfly.Plot
obj != nothing && display(obj)
#This shows images and lone variables, result can
#Handle last line sepately
elseif lastline && obj != nothing
@ -351,30 +337,6 @@ function get_figname(report::Report, chunk; fignum = nothing, ext = nothing)
return full_name, rel_name
end
function init_plotting(plotlib)
srcdir = escape_string(dirname(@__FILE__))
rcParams[:plotlib_set] = true
if plotlib == nothing
rcParams[:plotlib] = nothing
else
l_plotlib = lowercase(plotlib)
rcParams[:chunk_defaults][:fig] = true
if l_plotlib == "pyplot"
eval(Meta.parse("""include("$srcdir/pyplot.jl")"""))
rcParams[:plotlib] = "PyPlot"
elseif l_plotlib == "plots"
eval(Meta.parse("""include("$srcdir/plots.jl")"""))
rcParams[:plotlib] = "Plots"
elseif l_plotlib == "gadfly"
eval(Meta.parse("""include("$srcdir/gadfly.jl")"""))
rcParams[:plotlib] = "Gadfly"
end
end
return true
end
function get_cwd(doc::WeaveDoc, out_path)
#Set the output directory
if out_path == :doc
@ -490,17 +452,3 @@ function collect_results(chunk::CodeChunk, fmt::CollectResult)
end
return [chunk]
end
function detect_plotlib(chunk::CodeChunk, mod)
if isdefined(mod, :Plots)
init_plotting("Plots")
@info("Init Plots")
#Need to set size before plots are created
Compat.invokelatest(plots_set_size, chunk)
return
end
isdefined(mod, :PyPlot) && init_plotting("PyPlot") && return
isdefined(mod, :Gadfly) && init_plotting("Gadfly") && return
end

View File

@ -3,20 +3,20 @@ using Test
#Test if running document with and without cache works
isdir("documents/cache") && rm("documents/cache", recursive = true)
weave("documents/chunk_options.noweb", plotlib=nothing, cache=:all)
weave("documents/chunk_options.noweb", cache=:all)
result = read("documents/chunk_options.md", String)
rm("documents/chunk_options.md")
weave("documents/chunk_options.noweb", plotlib=nothing, cache=:all)
weave("documents/chunk_options.noweb", cache=:all)
cached_result = read("documents/chunk_options.md", String)
@test result == cached_result
# cache = :user
isdir("documents/cache") && rm("documents/cache", recursive = true)
out = "documents/chunk_cache.md"
Weave.weave("documents/chunk_cache.noweb", plotlib=nothing, cache=:user);
Weave.weave("documents/chunk_cache.noweb", cache=:user);
result = read(out, String)
rm(out)
Weave.weave("documents/chunk_cache.noweb", plotlib=nothing, cache=:user);
Weave.weave("documents/chunk_cache.noweb", cache=:user);
cached_result = read(out, String)
@test result == cached_result
@ -29,17 +29,3 @@ rm(out)
Weave.weave("documents/chunk_cache.noweb", cache=:all);
cached_result = read(out, String)
@test result == cached_result
if VERSION.minor == 5
using Gadfly
isdir("documents/cache") && rm("documents/cache", recursive = true)
#Caching with Gadfly
weave("documents/gadfly_formats_test.txt", doctype="tex", plotlib="gadfly", cache=:all)
result = read("documents/gadfly_formats_test.tex", String)
rm("documents/gadfly_formats_test.tex")
weave("documents/gadfly_formats_test.txt", doctype="tex", plotlib="gadfly", cache=:all)
cached_result = read("documents/gadfly_formats_test.tex", String)
@test result == cached_result
end

View File

@ -5,13 +5,12 @@ cleanup = true
VER = "$(VERSION.major).$(VERSION.minor)"
weave("documents/chunk_options.noweb", plotlib=nothing)
weave("documents/chunk_options.noweb")
result = read("documents/chunk_options.md", String)
ref = read("documents/$VER/chunk_options_ref.md", String)
@test result == ref
cleanup && rm("documents/chunk_options.md")
tangle("documents/chunk_options.noweb", out_path = "documents/tangle")
result = read("documents/tangle/chunk_options.jl", String)
ref = read("documents/tangle/chunk_options.jl.ref", String)

View File

@ -6,11 +6,8 @@ function pljtest(source, resfile, doctype)
result = read("documents/plotsjl/$resfile", String)
ref = read("documents/plotsjl/$resfile.ref", String)
@test result == ref
#rm("documents/plotsjl/$resfile")
rm("documents/plotsjl/$resfile")
end
#pljtest("plotsjl_test.jmd", "plotsjl_test.md", "pandoc")
#pljtest("plotsjl_test.jmd", "plotsjl_test.tex", "tex")
pljtest("plotsjl_test_gr.jmd", "plotsjl_test_gr.md", "pandoc")
pljtest("plotsjl_test_gr.jmd", "plotsjl_test_gr.tex", "tex")

View File

@ -3,7 +3,7 @@ using Test
function mmtest(source, resfile, doctype)
weave("documents/$source", out_path = "documents/multimedia/$resfile", mod=:sandbox,
doctype=doctype, plotlib=nothing, template = "templates/mini.tpl")
doctype=doctype, template = "templates/mini.tpl")
result = read("documents/multimedia/$resfile", String)
ref = read("documents/multimedia/$resfile.ref", String)
@test result == ref

View File

@ -34,6 +34,11 @@ using Test
include("rich_output.jl")
end
@testset "Plots" begin
info("Test: Weaving with Plots.jl")
include("plotsjl_test.jl")
end
#@testset "Caching" begin
# @info("Test: Caching")
# include("cache_test.jl")