Skip to content

Commit

Permalink
Merge pull request #370 from JunoLab/avi/type
Browse files Browse the repository at this point in the history
fix `Report` typings, and hooks
  • Loading branch information
aviatesk committed Jun 13, 2020
2 parents d468912 + c1bc2fd commit e0a9d04
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
12 changes: 0 additions & 12 deletions src/Weave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,6 @@ end

include_weave(source, informat = nothing) = include_weave(Main, source, informat)

# Hooks to run before and after chunks, this is form IJulia,
# but note that Weave hooks take the chunk as input
const preexecute_hooks = Function[]
push_preexecute_hook(f::Function) = push!(preexecute_hooks, f)
pop_preexecute_hook(f::Function) =
splice!(preexecute_hooks, findfirst(x -> x == f, preexecute_hooks))

const postexecute_hooks = Function[]
push_postexecute_hook(f::Function) = push!(postexecute_hooks, f)
pop_postexecute_hook(f::Function) =
splice!(postexecute_hooks, findfirst(x -> x == f, postexecute_hooks))

include("types.jl")
include("config.jl")
include("WeaveMarkdown/markdown.jl")
Expand Down
3 changes: 1 addition & 2 deletions src/config.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# TODO: follow RMarkdown convention more
const _DEFAULT_PARAMS = Dict{Symbol,Any}(
:echo => true,
:results => "markup",
:hold => false,
:fig => true,
:include => true,
:eval => true,
:tangle => true,
:cache => false,
Expand All @@ -18,7 +18,6 @@ const _DEFAULT_PARAMS = Dict{Symbol,Any}(
:label => nothing,
:wrap => true,
:line_width => 75,
:engine => "julia",
:fig_ext => nothing,
:fig_pos => nothing,
:fig_env => nothing,
Expand Down
10 changes: 5 additions & 5 deletions src/display_methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ using Markdown, .WeaveMarkdown

# Contains report global properties
mutable struct Report <: AbstractDisplay
cwd::AbstractString
basename::AbstractString
cwd::String
basename::String
format::WeaveFormat
rich_output::AbstractString
rich_output::String
fignum::Int
figures::Vector{String}
cur_chunk::Any
mimetypes::Array{AbstractString}
cur_chunk::Union{Nothing,CodeChunk}
mimetypes::Vector{String}
first_plot::Bool
header_script::String
throw_errors::Bool
Expand Down
5 changes: 2 additions & 3 deletions src/plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ using Base64, ..Plots, ..Weave


# Pre-execute hooks to set the plot size for the chunk
function plots_set_size(chunk)
function plots_set_size!(chunk)
w = chunk.options[:fig_width] * chunk.options[:dpi]
h = chunk.options[:fig_height] * chunk.options[:dpi]
Plots.default(size = (w, h))
return chunk
end

Weave.push_preexecute_hook(plots_set_size)
Weave.push_preexecution_hook!(plots_set_size)

# PNG or SVG is not working, output html
function Base.display(
Expand Down
4 changes: 1 addition & 3 deletions src/rendering/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ function format_chunk(chunk::CodeChunk, docformat)

# Handle figures
if chunk.options[:fig] && length(chunk.figures) > 0
if chunk.options[:include]
result *= formatfigures(chunk, docformat)
end
result *= formatfigures(chunk, docformat)
end

return result
Expand Down
33 changes: 21 additions & 12 deletions src/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module
return chunk
end

# Run preexecute_hooks
for hook in preexecute_hooks
chunk = Base.invokelatest(hook, chunk)
end
execute_prehooks!(chunk)

report.fignum = 1
report.cur_chunk = chunk
Expand All @@ -283,10 +280,7 @@ function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module

chunk.result = run_code(doc, chunk, report, mod)

# Run post_execute chunks
for hook in postexecute_hooks
chunk = Base.invokelatest(hook, chunk)
end
execute_posthooks!(chunk)

chunks = if chunk.options[:term]
collect_term_results(chunk)
Expand All @@ -296,13 +290,28 @@ function eval_chunk(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module
collect_results(chunk)
end

# else
# chunk.options[:fig] && (chunk.figures = copy(report.figures))
# end

return chunks
end

# Hooks to run before and after chunks, this is form IJulia,
const preexecution_hooks = Function[]
push_preexecution_hook!(f::Function) = push!(preexecution_hooks, f)
function pop_preexecution_hook!(f::Function)
i = findfirst(x -> x == f, preexecution_hooks)
isnothing(i) && error("this function has not been registered in the pre-execution hook yet")
return splice!(preexecution_hooks, i)
end
execute_prehooks!(chunk::CodeChunk) = for prehook in preexecution_hooks; Base.invokelatest(prehook, chunk); end

const postexecution_hooks = Function[]
push_postexecution_hook!(f::Function) = push!(postexecution_hooks, f)
function pop_postexecution_hook!(f::Function)
i = findfirst(x -> x == f, postexecution_hooks)
isnothing(i) && error("this function has not been registered in the post-execution hook yet")
return splice!(postexecution_hooks, i)
end
execute_posthooks!(chunk::CodeChunk) = for posthook in postexecution_hooks; Base.invokelatest(posthook, chunk); end

"""
clear_module!(mod::Module)
Expand Down

0 comments on commit e0a9d04

Please sign in to comment.