mirror of https://github.com/mpastell/Weave.jl
change cache to work with figures (#158)
* Changed read/write cache to use serialize/deserialize instead of JLD2. JLD2 fails to save strings longer than 2^16 characters, which can occur in the stringmime() of large figures. * Added test for cache with figures and html output. Updated REQUIRE & Project.toml to include Serialization instead of FileIO and JLD2 * Added "Import Pkg;" to expected output of errors, so that tests pass.pull/163/head
parent
bf57bf8a07
commit
ab09bddcd8
|
@ -9,8 +9,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
|
|||
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
|
||||
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
|
||||
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
|
||||
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
|
||||
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
|
||||
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
|
||||
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
|
||||
Highlights = "eafb193a-b7ab-5a9e-9068-77385905fa72"
|
||||
Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"
|
||||
|
|
3
REQUIRE
3
REQUIRE
|
@ -1,6 +1,5 @@
|
|||
julia 0.7
|
||||
JLD2
|
||||
FileIO
|
||||
Serialization
|
||||
JSON
|
||||
Highlights 0.3.0
|
||||
Mustache
|
||||
|
|
13
src/cache.jl
13
src/cache.jl
|
@ -1,16 +1,21 @@
|
|||
#FileIO and JLD2 are imported only if cache is used
|
||||
#Serialization is imported only if cache is used
|
||||
|
||||
function write_cache(doc::WeaveDoc, cache_path)
|
||||
cache_dir = joinpath(doc.cwd, cache_path)
|
||||
isdir(cache_dir) || mkpath(cache_dir)
|
||||
Base.invokelatest(FileIO.save, joinpath(cache_dir, doc.basename * ".jld2"), Dict("doc" => doc))
|
||||
open(joinpath(cache_dir, doc.basename * ".cache"),"w") do io
|
||||
Serialization.serialize(io, doc)
|
||||
end
|
||||
return nothing
|
||||
end
|
||||
|
||||
function read_cache(doc::WeaveDoc, cache_path)
|
||||
name = joinpath(doc.cwd, cache_path, doc.basename * ".jld2")
|
||||
name = joinpath(doc.cwd, cache_path, doc.basename * ".cache")
|
||||
isfile(name) || return nothing
|
||||
return Base.invokelatest(FileIO.load, name, "doc")
|
||||
open(name,"r") do io
|
||||
doc = Serialization.deserialize(io)
|
||||
end
|
||||
return doc
|
||||
end
|
||||
|
||||
function restore_chunk(chunk::CodeChunk, cached)
|
||||
|
|
|
@ -41,7 +41,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto,
|
|||
fig_path = mktempdir(abspath(doc.cwd))
|
||||
end
|
||||
|
||||
cache == :off || @eval import FileIO, JLD2
|
||||
cache == :off || @eval import Serialization
|
||||
|
||||
#This is needed for latex and should work on all output formats
|
||||
Sys.iswindows() && (fig_path = replace(fig_path, "\\" => "/"))
|
||||
|
|
|
@ -26,8 +26,7 @@ doc1 = Weave.run(doc, doctype = "pandoc")
|
|||
|
||||
doc1.chunks[1].output
|
||||
|
||||
@test doc1.chunks[1].output == "Error: ArgumentError: Package NonExisting not found in current path:\n- Run `Pkg.add(\"NonExisting\")` to install the NonExisting package.\n\n"
|
||||
|
||||
@test doc1.chunks[1].output == "Error: ArgumentError: Package NonExisting not found in current path:\n- Run `import Pkg; Pkg.add(\"NonExisting\")` to install the NonExisting package.\n\n"
|
||||
@test doc1.chunks[2].output == "Error: syntax: incomplete: premature end of input\n"
|
||||
@test doc1.chunks[3].output == "\njulia> plot(x)\nError: UndefVarError: plot not defined\n\njulia> y = 10\n10\n\njulia> print(y\nError: syntax: incomplete: premature end of input\n"
|
||||
|
||||
|
@ -35,7 +34,7 @@ doc1.chunks[1].output
|
|||
|
||||
doc = Weave.WeaveDoc("dummy1.jmd", p1, Dict())
|
||||
doc3 = Weave.run(doc, doctype = "md2html")
|
||||
@test doc3.chunks[1].rich_output == "<pre class=\"julia-error\">\nERROR: ArgumentError: Package NonExisting not found in current path:\n- Run `Pkg.add("NonExisting")` to install the NonExisting package.\n\n</pre>\n"
|
||||
@test doc3.chunks[1].rich_output == "<pre class=\"julia-error\">\nERROR: ArgumentError: Package NonExisting not found in current path:\n- Run `import Pkg; Pkg.add("NonExisting")` to install the NonExisting package.\n\n</pre>\n"
|
||||
@test doc3.chunks[2].rich_output == "<pre class=\"julia-error\">\nERROR: syntax: incomplete: premature end of input\n</pre>\n"
|
||||
@test doc3.chunks[3].output == "\njulia> plot(x)\nError: UndefVarError: plot not defined\n\njulia> y = 10\n10\n\njulia> print(y\nError: syntax: incomplete: premature end of input\n"
|
||||
@test doc3.chunks[3].rich_output == ""
|
||||
|
|
|
@ -11,3 +11,12 @@ end
|
|||
|
||||
pljtest("plotsjl_test_gr.jmd", "plotsjl_test_gr.md", "pandoc")
|
||||
pljtest("plotsjl_test_gr.jmd", "plotsjl_test_gr.tex", "tex")
|
||||
|
||||
# test cache with plots
|
||||
isdir("documents/cache") && rm("documents/cache", recursive = true)
|
||||
weave("documents/plotsjl_test_gr.jmd", cache=:all)
|
||||
result = read("documents/plotsjl_test_gr.html", String)
|
||||
rm("documents/plotsjl_test_gr.html")
|
||||
weave("documents/plotsjl_test_gr.jmd", cache=:all)
|
||||
cached_result = read("documents/plotsjl_test_gr.html", String)
|
||||
@test result == cached_result
|
||||
|
|
Loading…
Reference in New Issue