Allow format specific options in header

pull/202/head
Matti Pastell 2019-03-05 16:44:10 +02:00
parent d62172ad88
commit 52f3a23d28
5 changed files with 56 additions and 6 deletions

View File

@ -137,6 +137,19 @@ options:
---
```
You can also set format specific options. Here is how to set different out_path for `md2html` and `md2pdf` and set `fig_ext` for both:
```
---
options:
md2html:
out_path : html
md2pdf:
out_path : pdf
fig_ext : .png
---
```
## Passing arguments to documents
You can pass arguments as dictionary to the weaved document using the `args` argument

View File

@ -98,6 +98,8 @@ function weave(source ; doctype = :auto,
latex_cmd = "xelatex")
doc = read_doc(source, informat)
doctype == :auto && (doctype = detect_doctype(doc.source))
doc.doctype = doctype
# Read args from document header, overrides command line args
if haskey(doc.header, "options")
@ -106,7 +108,6 @@ function weave(source ; doctype = :auto,
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)

View File

@ -86,6 +86,21 @@ function restore_chunk_defaults()
return nothing
end
"""Combine format specific and common options from document header"""
function combine_args(args, doctype)
common = Dict()
specific = Dict()
for key in keys(args)
if key keys(Weave.formats)
specific[key] = args[key]
else
common[key] = args[key]
end
end
@info specific
haskey(specific, doctype) && merge!(common, specific[doctype])
common
end
getvalue(d::Dict, key , default) = haskey(d, key) ? d[key] : default
@ -96,10 +111,10 @@ Parse document options from document header
"""
function parse_header_options(doc::WeaveDoc)
args = getvalue(doc.header, "options", Dict())
doctype = getvalue(args, "doctype", :auto)
doctype = getvalue(args, "doctype", doc.doctype)
args = combine_args(args, doctype)
informat = getvalue(args, "informat", :auto)
out_path = getvalue(args, "out_path", "doc")
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")

View File

@ -48,8 +48,6 @@ function detect_informat(source::AbstractString)
return "noweb"
end
"""Read and parse input document"""
function read_doc(source::AbstractString, format=:auto)
format == :auto && (format = detect_informat(source))

23
test/options_test.jl Normal file
View File

@ -0,0 +1,23 @@
using Weave, Test, YAML
header = YAML.load("""
---
options:
out_path: reports
md2html:
out_path : html/
md2pdf:
out_path : pdf/
github:
out_path : md/
fig_ext : .png
---
""")
args = header["options"]
@test Weave.combine_args(args, "md2html") == Dict("fig_ext" => ".png",
"out_path" => "html/")
@test Weave.combine_args(args, "github") == Dict("fig_ext" => ".png",
"out_path" => "md/")
@test Weave.combine_args(args, "pandoc") == Dict("fig_ext" => ".png",
"out_path" => "reports")