Weave.jl/src/config.jl

146 lines
3.7 KiB
Julia
Raw Normal View History

2020-05-08 16:39:17 +02:00
# Default options
const defaultParams = Dict{Symbol,Any}(
:storeresults => false,
:chunk_defaults => Dict{Symbol,Any}(
:echo => true,
:results => "markup",
:hold => false,
:fig => true,
:include => true,
:eval => true,
:tangle => true,
:cache => false,
:fig_cap => nothing,
# Size in inches
:fig_width => 6,
:fig_height => 4,
:fig_path => "figures",
:dpi => 96,
:term => false,
:display => false,
:prompt => "\njulia> ",
:label => nothing,
:wrap => true,
:line_width => 75,
:engine => "julia",
# :option_AbstractString=> "",
# Defined in formats
:fig_ext => nothing,
:fig_pos => nothing,
:fig_env => nothing,
:out_width => nothing,
:out_height => nothing,
:skip => false,
),
)
# This one can be changed at runtime, initially a copy of defaults
2016-12-23 09:54:48 +01:00
const rcParams = deepcopy(defaultParams)
"""
2020-03-06 02:14:21 +01:00
set_chunk_defaults(opts::Dict{Symbol, Any})
2016-12-23 09:54:48 +01:00
2020-03-06 02:14:21 +01:00
Set default options for code chunks, use [`get_chunk_defaults`](@ref) to see the current values.
2016-12-23 09:54:48 +01:00
2020-03-26 13:41:54 +01:00
E.g.: set default `dpi` to `200` and `fig_width` to `8`
2016-12-23 09:54:48 +01:00
2020-03-06 02:14:21 +01:00
```julia
2016-12-23 09:54:48 +01:00
julia> set_chunk_defaults(Dict{Symbol, Any}(:dpi => 200, fig_width => 8))
```
"""
2020-05-08 16:39:17 +02:00
function set_chunk_defaults(opts::Dict{Symbol,Any})
merge!(rcParams[:chunk_defaults], opts)
return nothing
2016-12-23 09:54:48 +01:00
end
"""
2020-03-26 13:41:54 +01:00
get_chunk_defaults()
2016-12-23 09:54:48 +01:00
Get default options used for code chunks.
"""
function get_chunk_defaults()
2020-05-08 16:39:17 +02:00
return (rcParams[:chunk_defaults])
2016-12-23 09:54:48 +01:00
end
"""
2020-03-26 13:41:54 +01:00
restore_chunk_defaults()
2016-12-23 09:54:48 +01:00
2020-03-26 13:41:54 +01:00
Restore Weave.jl default chunk options.
2016-12-23 09:54:48 +01:00
"""
function restore_chunk_defaults()
2020-05-08 16:39:17 +02:00
rcParams[:chunk_defaults] = defaultParams[:chunk_defaults]
return nothing
2016-12-23 09:54:48 +01:00
end
"""Combine format specific and common options from document header"""
function combine_args(args, doctype)
common = Dict()
specific = Dict()
for key in keys(args)
2020-05-09 16:55:11 +02:00
if key in keys(Weave.formats)
specific[key] = args[key]
else
common[key] = args[key]
end
end
haskey(specific, doctype) && merge!(common, specific[doctype])
common
end
"""
2020-03-26 13:41:54 +01:00
header_args(doc::WeaveDoc)
2020-03-26 13:41:54 +01:00
Get weave arguments from document header.
"""
2020-05-08 16:39:17 +02:00
function header_args(
doc::WeaveDoc,
out_path,
fig_ext,
fig_path,
cache_path,
cache,
throw_errors,
template,
highlight_theme,
css,
pandoc_options,
latex_cmd,
)
2020-05-09 15:42:05 +02:00
args = get(doc.header, WEAVE_OPTION_NAME, Dict())
2020-05-08 18:47:06 +02:00
doctype = get(args, "doctype", doc.doctype)
args = combine_args(args, doctype)
informat = get(args, "informat", nothing)
2020-05-08 18:47:06 +02:00
out_path = get(args, "out_path", out_path)
out_path == ":pwd" && (out_path = :pwd)
isa(out_path, Symbol) || (out_path = joinpath(dirname(doc.source), out_path))
2020-05-08 18:47:06 +02:00
fig_path = get(args, "fig_path", fig_path)
fig_ext = get(args, "fig_ext", fig_ext)
cache_path = get(args, "cache_path", cache_path)
cache = Symbol(get(args, "cache", cache))
throw_errors = get(args, "throw_errors", throw_errors)
template = get(args, "template", template)
2019-03-11 12:39:21 +01:00
if template != nothing && !isa(template, Mustache.MustacheTokens) && !isempty(template)
template = joinpath(dirname(doc.source), template)
end
2020-05-08 18:47:06 +02:00
highlight_theme = get(args, "highlight_theme", highlight_theme)
css = get(args, "css", css)
pandoc_options = get(args, "pandoc_options", pandoc_options)
latex_cmd = get(args, "latex_cmd", latex_cmd)
2020-05-08 16:39:17 +02:00
return (
doctype,
informat,
out_path,
args,
fig_path,
fig_ext,
cache_path,
cache,
throw_errors,
template,
highlight_theme,
css,
pandoc_options,
latex_cmd,
)
end