Weave.jl/src/cache.jl

60 lines
2.0 KiB
Julia
Raw Normal View History

#FileIO and JLD2 are 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))
return nothing
end
function read_cache(doc::WeaveDoc, cache_path)
2018-01-02 15:21:45 +01:00
name = joinpath(doc.cwd, cache_path, doc.basename * ".jld2")
isfile(name) || return nothing
return Base.invokelatest(FileIO.load, name, "doc")
end
function restore_chunk(chunk::CodeChunk, cached)
chunks = filter(x -> x.number == chunk.number &&
string(typeof(x)) == "Weave.CodeChunk", cached.chunks)
#Chunk types, don't match after loading. Fix by constructing chunks
#from loaded content
new_chunks = Any[]
for c in chunks
newc = CodeChunk(c.content, c.number, c.start_line, c.optionstring, c.options)
newc.result_no = c.result_no
newc.figures = c.figures
newc.result = c.result
newc.output = c.output
2016-05-02 22:21:34 +02:00
newc.rich_output = c.rich_output
push!(new_chunks, newc)
end
return new_chunks
end
2016-12-27 20:43:13 +01:00
#Restore inline code
function restore_chunk(chunk::DocChunk, cached::WeaveDoc)
#Get chunk from cached doc
c_chunk = filter(x -> x.number == chunk.number &&
isa(x, DocChunk), cached.chunks)
isempty(c_chunk) && return chunk
c_chunk = c_chunk[1]
#Collect cached code
c_inline = filter(x -> isa(x, InlineCode), c_chunk.content)
isempty(c_inline) && return chunk
#Restore cached results for Inline code
n = length(chunk.content)
for i in 1:n
if isa(chunk.content[i], InlineCode)
ci = filter(x -> x.number == chunk.content[i].number, c_inline)
isempty(ci) && continue
chunk.content[i].output = ci[1].output
chunk.content[i].rich_output = ci[1].rich_output
chunk.content[i].figures = ci[1].figures
end
end
return chunk
end