2018-01-07 19:01:18 +01:00
|
|
|
|
#FileIO and JLD2 are imported only if cache is used
|
2015-01-08 17:52:42 +01:00
|
|
|
|
|
2015-01-07 15:37:02 +01:00
|
|
|
|
function write_cache(doc::WeaveDoc, cache_path)
|
2016-12-23 09:25:23 +01:00
|
|
|
|
cache_dir = joinpath(doc.cwd, cache_path)
|
2015-01-08 17:52:42 +01:00
|
|
|
|
isdir(cache_dir) || mkpath(cache_dir)
|
2018-01-07 19:01:18 +01:00
|
|
|
|
Base.invokelatest(FileIO.save, joinpath(cache_dir, doc.basename * ".jld2"), Dict("doc" => doc))
|
2015-01-07 15:37:02 +01:00
|
|
|
|
return nothing
|
|
|
|
|
end
|
2015-01-07 11:20:48 +01:00
|
|
|
|
|
2015-01-07 15:37:02 +01:00
|
|
|
|
function read_cache(doc::WeaveDoc, cache_path)
|
2018-01-02 15:21:45 +01:00
|
|
|
|
name = joinpath(doc.cwd, cache_path, doc.basename * ".jld2")
|
2015-01-07 15:37:02 +01:00
|
|
|
|
isfile(name) || return nothing
|
2018-01-07 19:01:18 +01:00
|
|
|
|
return Base.invokelatest(FileIO.load, name, "doc")
|
2015-01-07 11:20:48 +01:00
|
|
|
|
end
|
|
|
|
|
|
2016-04-23 17:15:59 +02:00
|
|
|
|
function restore_chunk(chunk::CodeChunk, cached)
|
|
|
|
|
chunks = filter(x -> x.number == chunk.number &&
|
|
|
|
|
string(typeof(x)) == "Weave.CodeChunk", cached.chunks)
|
2015-01-08 17:52:42 +01:00
|
|
|
|
|
2016-04-23 18:18:44 +02:00
|
|
|
|
#Chunk types, don't match after loading. Fix by constructing chunks
|
|
|
|
|
#from loaded content
|
2016-04-23 17:15:59 +02:00
|
|
|
|
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
|
2016-04-23 18:18:44 +02:00
|
|
|
|
newc.output = c.output
|
2016-05-02 22:21:34 +02:00
|
|
|
|
newc.rich_output = c.rich_output
|
2016-04-23 17:15:59 +02:00
|
|
|
|
push!(new_chunks, newc)
|
|
|
|
|
end
|
|
|
|
|
return new_chunks
|
2015-01-08 17:52:42 +01:00
|
|
|
|
end
|
2015-01-07 15:37:02 +01:00
|
|
|
|
|
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
|
2016-04-23 17:15:59 +02:00
|
|
|
|
end
|