2020-05-08 16:39:17 +02:00
|
|
|
# Serialization is 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)
|
2020-05-08 16:39:17 +02:00
|
|
|
isdir(cache_dir) || mkpath(cache_dir)
|
|
|
|
open(joinpath(cache_dir, doc.basename * ".cache"), "w") do io
|
2019-01-03 14:40:32 +01:00
|
|
|
Serialization.serialize(io, doc)
|
|
|
|
end
|
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)
|
2019-01-03 14:40:32 +01:00
|
|
|
name = joinpath(doc.cwd, cache_path, doc.basename * ".cache")
|
2020-05-08 16:39:17 +02:00
|
|
|
isfile(name) || return nothing
|
|
|
|
open(name, "r") do io
|
2019-01-03 14:40:32 +01:00
|
|
|
doc = Serialization.deserialize(io)
|
|
|
|
end
|
|
|
|
return doc
|
2015-01-07 11:20:48 +01:00
|
|
|
end
|
|
|
|
|
2016-04-23 17:15:59 +02:00
|
|
|
function restore_chunk(chunk::CodeChunk, cached)
|
2020-05-08 16:39:17 +02:00
|
|
|
chunks = filter(
|
|
|
|
x -> x.number == chunk.number && string(typeof(x)) == "Weave.CodeChunk",
|
|
|
|
cached.chunks,
|
|
|
|
)
|
2015-01-08 17:52:42 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
# Chunk types, don't match after loading. Fix by constructing chunks
|
|
|
|
# from loaded content
|
2020-05-16 12:52:24 +02:00
|
|
|
new_chunks = []
|
2016-04-23 17:15:59 +02:00
|
|
|
for c in chunks
|
2020-05-08 16:39:17 +02:00
|
|
|
newc = CodeChunk(c.content, c.number, c.start_line, c.optionstring, c.options)
|
|
|
|
newc.figures = c.figures
|
|
|
|
newc.result = c.result
|
|
|
|
newc.output = c.output
|
|
|
|
newc.rich_output = c.rich_output
|
|
|
|
push!(new_chunks, newc)
|
2016-04-23 17:15:59 +02:00
|
|
|
end
|
|
|
|
return new_chunks
|
2015-01-08 17:52:42 +01:00
|
|
|
end
|
2015-01-07 15:37:02 +01:00
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
# Restore inline code
|
2016-12-27 20:43:13 +01:00
|
|
|
function restore_chunk(chunk::DocChunk, cached::WeaveDoc)
|
2020-05-08 16:39:17 +02:00
|
|
|
# Get chunk from cached doc
|
|
|
|
c_chunk = filter(x -> x.number == chunk.number && isa(x, DocChunk), cached.chunks)
|
2016-12-27 20:43:13 +01:00
|
|
|
isempty(c_chunk) && return chunk
|
|
|
|
c_chunk = c_chunk[1]
|
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
# Collect cached code
|
2016-12-27 20:43:13 +01:00
|
|
|
c_inline = filter(x -> isa(x, InlineCode), c_chunk.content)
|
|
|
|
isempty(c_inline) && return chunk
|
|
|
|
|
2020-05-08 16:39:17 +02:00
|
|
|
# Restore cached results for Inline code
|
2019-01-03 14:40:32 +01:00
|
|
|
n = length(chunk.content)
|
2020-05-08 16:39:17 +02:00
|
|
|
for i = 1:n
|
2016-12-27 20:43:13 +01:00
|
|
|
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
|