mirror of https://github.com/mpastell/Weave.jl
Allow setting options in document header. Closes #189
parent
a14b7a8f80
commit
e9ea54bc1b
|
@ -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
|
||||
|
|
11
src/Weave.jl
11
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")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue