rm header_args

pull/325/head
Shuhei Kadowaki 2020-05-10 15:23:24 +09:00
parent 2c7f3da7cc
commit bc0f687352
2 changed files with 36 additions and 93 deletions

View File

@ -116,39 +116,41 @@ function weave(
) )
doc = WeaveDoc(source, informat) doc = WeaveDoc(source, informat)
isnothing(doctype) && (doctype = detect_doctype(doc.source)) isnothing(doctype) && (doctype = detect_doctype(doc.source))
doc.doctype = doctype
# Read args from document header, overrides command line args # overwrites given options with header options, which have more precedence
if haskey(doc.header, WEAVE_OPTION_NAME) # NOTE:
( # - support format specific option specification
doctype, # - fix paths relative to `source`
informat, weave_options = get(doc.header, WEAVE_OPTION_NAME, Dict())
out_path, if !isempty(weave_options)
args, doctype = get(weave_options, "doctype", doctype)
fig_path, weave_options = combine_args(weave_options, doctype)
fig_ext, if haskey(weave_options, "out_path")
cache_path, out_path = let
cache, out_path = weave_options["out_path"]
throw_errors, if out_path == ":doc" || out_path == ":pwd"
template, Symbol(out_path)
highlight_theme, else
css, joinpath(dirname(source), out_path)
pandoc_options, end
latex_cmd, end
) = header_args( end
doc, mod = get(weave_options, "mod", mod)
out_path, mod isa AbstractString && (mod = Main.eval(Meta.parse(mod)))
fig_ext, fig_path = get(weave_options, "fig_path", fig_path)
fig_path, fig_ext = get(weave_options, "fig_ext", fig_ext)
cache_path, cache_path = get(weave_options, "cache_path", cache_path)
cache, cache = Symbol(get(weave_options, "cache", cache))
throw_errors, throw_errors = get(weave_options, "throw_errors", throw_errors)
template, if haskey(weave_options, "template")
highlight_theme, template = weave_options["template"]
css, template isa AbstractString && (template = joinpath(dirname(source), template))
pandoc_options, end
latex_cmd, highlight_theme = get(weave_options, "highlight_theme", highlight_theme)
) css = get(weave_options, "css", css)
pandoc_options = get(weave_options, "pandoc_options", pandoc_options)
latex_cmd = get(weave_options, "latex_cmd", latex_cmd)
latex_keep_unicode = get(weave_options, "latex_cmd", latex_keep_unicode)
end end
isnothing(template) || (doc.template = template) isnothing(template) || (doc.template = template)
@ -169,13 +171,12 @@ function weave(
throw_errors = throw_errors, throw_errors = throw_errors,
latex_keep_unicode = latex_keep_unicode, latex_keep_unicode = latex_keep_unicode,
) )
formatted = format(doc) formatted = format(doc)
outname = get_outname(out_path, doc) outname = get_outname(out_path, doc)
open(outname, "w") do io open(io->write(io,formatted), outname, "w")
write(io, formatted)
end
# Special for that need external programs # Special for that need external programs
if doc.doctype == "pandoc2html" if doc.doctype == "pandoc2html"

View File

@ -77,61 +77,3 @@ function combine_args(args, doctype)
haskey(specific, doctype) && merge!(common, specific[doctype]) haskey(specific, doctype) && merge!(common, specific[doctype])
common common
end end
"""
header_args(doc::WeaveDoc)
Get weave arguments from document header.
"""
function header_args(
doc::WeaveDoc,
out_path,
fig_ext,
fig_path,
cache_path,
cache,
throw_errors,
template,
highlight_theme,
css,
pandoc_options,
latex_cmd,
)
args = get(doc.header, WEAVE_OPTION_NAME, Dict())
doctype = get(args, "doctype", doc.doctype)
args = combine_args(args, doctype)
informat = get(args, "informat", nothing)
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))
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)
if template != nothing && !isa(template, Mustache.MustacheTokens) && !isempty(template)
template = joinpath(dirname(doc.source), template)
end
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)
return (
doctype,
informat,
out_path,
args,
fig_path,
fig_ext,
cache_path,
cache,
throw_errors,
template,
highlight_theme,
css,
pandoc_options,
latex_cmd,
)
end