From e9ea54bc1b9d52da0a1606227ad8095fe7082998 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Mon, 4 Mar 2019 11:10:02 +0200 Subject: [PATCH] Allow setting options in document header. Closes #189 --- doc/src/usage.md | 16 ++++++++++++++++ src/Weave.jl | 11 ++++++++++- src/config.jl | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/doc/src/usage.md b/doc/src/usage.md index 57677ae..f698f4b 100644 --- a/doc/src/usage.md +++ b/doc/src/usage.md @@ -120,6 +120,22 @@ added to output e.g. to include a Plots figure in markdown you can use: ![A plot](`j plot(1:10)`) ``` +## Setting document options in header + +You can use a YAML header in the beginning of the input document delimited with "---" to set the document title, author and date e.g. and default document options. Each of Weave command line arguments can be set in header using `options` field. Below is an example that sets document `out_path` and `doctype` using the header. + + +``` +--- +title : Weave example +author : Matti Pastell +date: 15th December 2016 +options: + out_path : reports/example.md + doctype : github +--- +``` + ## Passing arguments to documents You can pass arguments as dictionary to the weaved document using the `args` argument diff --git a/src/Weave.jl b/src/Weave.jl index 7cb4232..e3c8f1c 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -98,6 +98,15 @@ function weave(source ; doctype = :auto, latex_cmd = "xelatex") doc = read_doc(source, informat) + + # Read args from document header, overrides command line args + if haskey(doc.header, "options") + (doctype, informat, out_path, args, mod, fig_path, fig_ext, + cache_path, cache, throw_errors, template, highlight_theme, css, + pandoc_options, latex_cmd) = parse_header_options(doc) + end + + highlight_theme != nothing && (doc.highlight_theme = highlight_theme) #theme != nothing && (doc.theme = theme) #Reserved for themes css != nothing && (doc.css = css) @@ -213,8 +222,8 @@ const postexecute_hooks = Function[] push_postexecute_hook(f::Function) = push!(postexecute_hooks, f) pop_postexecute_hook(f::Function) = splice!(postexecute_hooks, findfirst(postexecute_hooks, f)) -include("config.jl") include("chunks.jl") +include("config.jl") include("WeaveMarkdown/markdown.jl") include("display_methods.jl") include("readers.jl") diff --git a/src/config.jl b/src/config.jl index 7182903..96ff77b 100644 --- a/src/config.jl +++ b/src/config.jl @@ -85,3 +85,35 @@ function restore_chunk_defaults() merge!(rcParams[:chunk_defaults], docParams) return nothing end + + +getvalue(d::Dict, key , default) = haskey(d, key) ? d[key] : default + +""" +`parse_header_options(doc::WeaveDoc)` + +Parse document options from document header +""" +function parse_header_options(doc::WeaveDoc) + args = getvalue(doc.header, "options", Dict()) + + doctype = getvalue(args, "doctype", :auto) + informat = getvalue(args, "informat", :auto) + out_path = getvalue(args, "out_path", "doc") + out_path == ":pwd" && (out_path = :pwd) + mod = Symbol(getvalue(args, "mod", :sandbox)) + fig_path = getvalue(args, "fig_path", "figures") + fig_ext = getvalue(args, "fig_ext", nothing) + cache_path = getvalue(args, "cache_path", "cache") + cache = Symbol(getvalue(args, "cache", :off)) + throw_errors = getvalue(args, "throw_errors", false) + template = getvalue(args, "template", nothing) + highlight_theme = getvalue(args, "highlight_theme", nothing) + css = getvalue(args, "css", nothing) + pandoc_options = getvalue(args, "pandoc_options", String[]) + latex_cmd = getvalue(args, "latex_cmd", "xelatex") + + return (doctype, informat, out_path, args, mod, fig_path, fig_ext, + cache_path, cache, throw_errors, template, highlight_theme, css, + pandoc_options, latex_cmd) +end