diff --git a/Project.toml b/Project.toml index d4d9dc0..0daf68a 100644 --- a/Project.toml +++ b/Project.toml @@ -15,6 +15,7 @@ REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" RelocatableFolders = "05181044-ff0b-4ac5-8273-598c1e38db00" Requires = "ae029012-a4dd-5104-9daa-d747884805df" Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b" +SoftGlobalScope = "b85f4697-e234-5449-a836-ec8e2f98b302" YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6" [compat] diff --git a/src/Weave.jl b/src/Weave.jl index ce104b6..2478de1 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -1,6 +1,6 @@ module Weave -using Highlights, Mustache, Requires, Pkg, REPL, RelocatableFolders, Base64 +using Highlights, Mustache, Requires, Pkg, REPL, RelocatableFolders, Base64, SoftGlobalScope # directories const PKG_DIR = normpath(@__DIR__, "..") diff --git a/src/config.jl b/src/config.jl index 0dcf901..65cd233 100644 --- a/src/config.jl +++ b/src/config.jl @@ -24,6 +24,7 @@ const _DEFAULT_PARAMS = Dict{Symbol,Any}( :fig_env => nothing, :out_width => nothing, :out_height => nothing, + :softscope => false, ) const DEFAULT_PARAMS = deepcopy(_DEFAULT_PARAMS) # might be changed at runtime diff --git a/src/reader/notebook.jl b/src/reader/notebook.jl index 5c4dd5e..bc7a177 100644 --- a/src/reader/notebook.jl +++ b/src/reader/notebook.jl @@ -7,7 +7,7 @@ function parse_notebook(document_body) doc_no = 0 # TODO: handle some of options ? - options = Dict{Symbol,Any}() + options = Dict{Symbol,Any}(:softscope => true) opt_string = "" chunks = map(nb["cells"]) do cell diff --git a/src/run.jl b/src/run.jl index 45e99d2..0229c44 100644 --- a/src/run.jl +++ b/src/run.jl @@ -189,8 +189,9 @@ function run_code(doc::WeaveDoc, chunk::CodeChunk, report::Report, mod::Module) code = chunk.content path = doc.path error = chunk.options[:error] + softscope = chunk.options[:softscope] codes = chunk.options[:term] ? split_code(code) : [code] - capture = code -> capture_output(code, mod, path, error, report) + capture = code -> capture_output(code, mod, path, error, report, softscope) return capture.(codes) end @@ -207,7 +208,7 @@ function split_code(code) return res end -function capture_output(code, mod, path, error, report) +function capture_output(code, mod, path, error, report, softscope=false) reset_report!(report) old = stdout @@ -217,7 +218,7 @@ function capture_output(code, mod, path, error, report) local out = nothing task_local_storage(:SOURCE_PATH, path) do try - obj = include_string(mod, code, path) # TODO: fix line number + obj = softscope ? softscope_include_string(mod, code, path) : include_string(mod, code, path) # TODO: fix line number !isnothing(obj) && !REPL.ends_with_semicolon(code) && display(obj) catch _err err = unwrap_load_err(_err)