Add optional softscoping

Uses `SoftGlobalScope` to mimic IJulia behaviour for notebooks.
This eliminates the need of `global`  in loops at top level.
Optionally, this could be enabled for jmd files as well
pull/471/head
lkdvos 2023-05-23 11:51:43 +02:00
parent e10aaefd94
commit cb90145edd
5 changed files with 8 additions and 5 deletions

View File

@ -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]

View File

@ -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__, "..")

View File

@ -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

View File

@ -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

View File

@ -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)