From c24a2621359b5d0af1bb6825f488e58cc11b8a9e Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Wed, 30 Sep 2020 18:43:15 +0900 Subject: [PATCH] allow arbitrary runtime argument --- doc/src/usage.md | 25 ++++--------------------- src/Weave.jl | 4 ++-- src/run.jl | 4 ++-- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/doc/src/usage.md b/doc/src/usage.md index a6cb589..a838417 100644 --- a/doc/src/usage.md +++ b/doc/src/usage.md @@ -165,29 +165,12 @@ See [Header Configuration](@ref) section for more details. ## Passing Runtime Arguments to Documents -You can pass arguments as `Dict` to the weaved document using the `args` argument -to `weave`. The arguments will be available as `WEAVE_ARGS` variable in the document. +You can pass arbitrary object to the weaved document using [`weave`](@ref)'s optional argument `args`. +It will be available as `WEAVE_ARGS` variable in the `weave`d document. -This makes it possible to create the same report easily for e.g. different -date ranges of input data from a database or from files with similar format giving the -filename as input. +This makes it possible to create the same report easily for e.g. different date ranges of input data from a database or from files with similar format giving the filename as input. -In order to pass a filename to a document you need call `weave` using: - -```julia -weave("mydoc.jmd", args = Dict("filename" => "somedata.h5")) -``` - -and you can access the filename from document as follows: - -``` - ```julia - print(WEAVE_ARGS["filename"]) - ``` -``` - -You can use the `out_path` argument to control the name of the -output document. +E.g. if you call `weave("weavefile.jmd", args = (datalocation = "somedata.h5",))`, and then you can retrieve `datalocation` in `weavefile.jmd` as follows: `WEAVE_ARGS.datalocation` ## `include_weave` diff --git a/src/Weave.jl b/src/Weave.jl index 898dabd..f740b4f 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -102,7 +102,7 @@ Weave an input document to output file. * `:doc`: Path of the source document (default) * `:pwd`: Julia working directory * `"somepath"`: `String` of output directory e.g. `"~/outdir"`, or of filename e.g. `"~/outdir/outfile.tex"` -- `args::Dict = Dict()`: Arguments to be passed to the weaved document; will be available as `WEAVE_ARGS` in the document +- `args::Any = Dict()`: A runtime object that is available as `WEAVE_ARGS` while `weave`ing - `mod::Union{Module,Nothing} = nothing`: Module where Weave `eval`s code. You can pass a `Module` object, otherwise create an new sandbox module. - `fig_path::Union{Nothing,AbstractString} = nothing`: Where figures will be generated, relative to `out_path`. By default (i.e. given `nothing`), Weave will automatically create `$(DEFAULT_FIG_PATH)` directory. - `fig_ext::Union{Nothing,AbstractString} = nothing`: Extension for saved figures e.g. `".pdf"`, `".png"`. Default setting depends on `doctype` @@ -127,7 +127,7 @@ function weave( doctype::Union{Nothing,AbstractString} = nothing, informat::Union{Nothing,AbstractString} = nothing, out_path::Union{Symbol,AbstractString} = :doc, - args::Dict = Dict(), + args::Any = Dict(), mod::Union{Module,Nothing} = nothing, fig_path::Union{Nothing,AbstractString} = nothing, fig_ext::Union{Nothing,AbstractString} = nothing, diff --git a/src/run.jl b/src/run.jl index 6e76e26..279e279 100644 --- a/src/run.jl +++ b/src/run.jl @@ -7,7 +7,7 @@ function run_doc( doc::WeaveDoc; doctype::Union{Nothing,AbstractString} = nothing, out_path::Union{Symbol,AbstractString} = :doc, - args::Dict = Dict(), + args::Any = Dict(), mod::Union{Module,Nothing} = nothing, fig_path::Union{Nothing,AbstractString} = nothing, fig_ext::Union{Nothing,AbstractString} = nothing, @@ -39,7 +39,7 @@ function run_doc( # New sandbox for each document with args exposed isnothing(mod) && (mod = sandbox = Core.eval(Main, :(module $(gensym(:WeaveSandBox)) end))::Module) - @eval mod WEAVE_ARGS = $args + Core.eval(mod, :(const WEAVE_ARGS = $(args))) mimetypes = doc.format.mimetypes