From 6082786f92a68c93072cfb9ec4559637beb28315 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Tue, 5 Mar 2019 19:36:27 +0200 Subject: [PATCH] Allow setting document options in YAML header --- .gitignore | 2 ++ doc/make.jl | 7 ++++++- doc/src/chunk_options.md | 26 +++++++++----------------- doc/src/usage.md | 4 ++-- src/Weave.jl | 14 +++++++------- src/chunks.jl | 3 ++- src/config.jl | 30 ++++++++++++++++-------------- src/readers.jl | 1 + src/run.jl | 33 ++++++++++++++------------------- test/documents/header_test.jmd | 12 ++++++++++++ test/formatter_test.jl | 1 - test/options_test.jl | 3 +++ 12 files changed, 74 insertions(+), 62 deletions(-) create mode 100644 test/documents/header_test.jmd diff --git a/.gitignore b/.gitignore index 3768f0d..25a059d 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,8 @@ test/**/chunk_options.jl doc/build doc/site stable/ +doc/Manifest.toml +Manifest.toml tmp/ .idea diff --git a/doc/make.jl b/doc/make.jl index 8458a5c..ab4238c 100644 --- a/doc/make.jl +++ b/doc/make.jl @@ -1,11 +1,16 @@ using Documenter, Weave -cd("doc") +start_dir = pwd() + + makedocs( modules = Weave, sitename="Weave.jl", pages = ["index.md", "getting_started.md", "usage.md", "publish.md", "chunk_options.md", "notebooks.md", "function_index.md"] ) + +cd(@__DIR__) include("make_examples.jl") +cd(start_dir) deploydocs( repo = "github.com/mpastell/Weave.jl.git", diff --git a/doc/src/chunk_options.md b/doc/src/chunk_options.md index 4c97755..11b13ef 100644 --- a/doc/src/chunk_options.md +++ b/doc/src/chunk_options.md @@ -4,13 +4,7 @@ I've mostly followed [Knitr](http://yihui.name/knitr/options)'s naming for chunk Options are separated using ";" and need to be valid Julia expressions. Example: markdown code chunk that saves and displays a 12 cm wide image and hides the source code: - - ```julia; out_width="12cm"; echo=false - using Gadfly - x = linspace(0, 2Ο€, 200) - plot(x=x, y = sin(x), Geom.line) - ``` - +`julia; out_width="12cm"; echo=false` Weave currently supports the following chunk options with the following defaults: @@ -43,18 +37,16 @@ Weave currently supports the following chunk options with the following defaults ## Set default chunk options -You can set or change the default chunk options for a document either before -running weave or inside the weaved document. You can e.g. use a hidden chunk -in the beginning of the source document to set the options: +You can set the default chunk options (and `weave` arguments) for a document using the YAML header `options` field. e.g to set the default `out_width` of all figures you can use: - ```julia; echo = false>>= - import Weave - Weave.set_chunk_defaults(Dict{Symbol, Any}( - :out_width => "\\0.5linewidth", - :results => "tex" - )) - ``` +```yaml +--- +options: + out_width : 50% +--- +``` +You can also set or change the default chunk options for a document either before weave using the `set_chunk_defaults function`. ```@docs set_chunk_defaults(opts) diff --git a/doc/src/usage.md b/doc/src/usage.md index 28ae5f2..4d03ca3 100644 --- a/doc/src/usage.md +++ b/doc/src/usage.md @@ -123,10 +123,10 @@ added to output e.g. to include a Plots figure in markdown you can use: ## 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. +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 and chunk options can be set in header using `options` field. Below is an example that sets document `out_path` and `doctype` using the header. -``` +```yaml --- title : Weave example author : Matti Pastell diff --git a/src/Weave.jl b/src/Weave.jl index d31c7ac..e2f6781 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -38,7 +38,7 @@ function tangle(source ; out_path=:doc, informat=:auto) open(outname, "w") do io for chunk in doc.chunks if typeof(chunk) == CodeChunk - options = merge(rcParams[:chunk_defaults], chunk.options) + options = merge(doc.chunk_defaults, chunk.options) if options[:tangle] write(io, chunk.content*"\n") end @@ -105,7 +105,7 @@ function weave(source ; doctype = :auto, 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) + pandoc_options, latex_cmd) = header_args(doc) end highlight_theme != nothing && (doc.highlight_theme = highlight_theme) @@ -148,13 +148,13 @@ function weave(source ; doctype = :auto, doc.cwd == pwd() && (outname = basename(outname)) @info("Report weaved to $outname") return abspath(outname) - catch err - @warn("Something went wrong during weaving") - @error(sprint(showerror, err)) - return nothing + #catch err + # @warn("Something went wrong during weaving") + # @error(sprint(showerror, err)) + # return nothing finally doctype == :auto && (doctype = detect_doctype(doc.source)) - if occursin("pandoc2pdf", doctype) && cache == :off + if occursin("2pdf", doctype) rm(doc.fig_path, force = true, recursive = true) elseif occursin("2html", doctype) rm(doc.fig_path, force = true, recursive = true) diff --git a/src/chunks.jl b/src/chunks.jl index d67b490..9e9570b 100644 --- a/src/chunks.jl +++ b/src/chunks.jl @@ -17,11 +17,12 @@ mutable struct WeaveDoc css::AbstractString highlight_theme fig_path::AbstractString + chunk_defaults::Dict{Symbol,Any} function WeaveDoc(source, chunks, header) path, fname = splitdir(abspath(source)) basename = splitext(fname)[1] new(source, basename, path, chunks, "", nothing, "", "", header, - "", "", Highlights.Themes.DefaultTheme, "") + "", "", Highlights.Themes.DefaultTheme, "", deepcopy(rcParams[:chunk_defaults])) end end diff --git a/src/config.jl b/src/config.jl index e36b531..d352542 100644 --- a/src/config.jl +++ b/src/config.jl @@ -40,15 +40,6 @@ const defaultParams = #This one can be changed at runtime, initially a copy of defaults const rcParams = deepcopy(defaultParams) -#Parameters set per document -const docParams =Dict{Symbol,Any}( - :fig_path=> nothing, - :fig_ext => nothing, - ) - - - - """ `set_chunk_defaults(opts::Dict{Symbol, Any})` @@ -82,7 +73,6 @@ Restore Weave.jl default chunk options """ function restore_chunk_defaults() rcParams[:chunk_defaults] = defaultParams[:chunk_defaults] - merge!(rcParams[:chunk_defaults], docParams) return nothing end @@ -97,7 +87,6 @@ function combine_args(args, doctype) common[key] = args[key] end end - @info specific haskey(specific, doctype) && merge!(common, specific[doctype]) common end @@ -105,11 +94,11 @@ end getvalue(d::Dict, key , default) = haskey(d, key) ? d[key] : default """ -`parse_header_options(doc::WeaveDoc)` +header_args(doc::WeaveDoc)` -Parse document options from document header +Get weave arguments from document header """ -function parse_header_options(doc::WeaveDoc) +function header_args(doc::WeaveDoc) args = getvalue(doc.header, "options", Dict()) doctype = getvalue(args, "doctype", doc.doctype) args = combine_args(args, doctype) @@ -132,3 +121,16 @@ function parse_header_options(doc::WeaveDoc) cache_path, cache, throw_errors, template, highlight_theme, css, pandoc_options, latex_cmd) end + +""" +`header_chunk_defaults!(doc::WeaveDoc)` + +Get chunk defaults from header and update +""" +function header_chunk_defaults!(doc::WeaveDoc) + for key in keys(doc.chunk_defaults) + if haskey(doc.header["options"], String(key)) + doc.chunk_defaults[key] = doc.header["options"][String(key)] + end + end +end diff --git a/src/readers.jl b/src/readers.jl index b33c49b..9a3f958 100644 --- a/src/readers.jl +++ b/src/readers.jl @@ -56,6 +56,7 @@ function read_doc(source::AbstractString, format=:auto) parsed = parse_doc(document, format) header = parse_header(parsed[1]) doc = WeaveDoc(source, parsed, header) + haskey(header, "options") && header_chunk_defaults!(doc) return doc end diff --git a/src/run.jl b/src/run.jl index 31418b3..9e97265 100644 --- a/src/run.jl +++ b/src/run.jl @@ -47,7 +47,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, Sys.iswindows() && (fig_path = replace(fig_path, "\\" => "/")) doc.fig_path = fig_path - set_rc_params(doc.format.formatdict, fig_path, fig_ext) + set_rc_params(doc, fig_path, fig_ext) #New sandbox for each document with args exposed if mod == :sandbox @@ -82,7 +82,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, chunk = doc.chunks[i] if isa(chunk, CodeChunk) - options = merge(rcParams[:chunk_defaults], chunk.options) + options = merge(doc.chunk_defaults, chunk.options) merge!(chunk.options, options) end @@ -91,7 +91,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, if cached != nothing && (cache == :all ||Β restore) result_chunks = restore_chunk(chunk, cached) else - result_chunks = run_chunk(chunk, report, mod) + result_chunks = run_chunk(chunk, doc, report, mod) end executed = [executed; result_chunks] @@ -125,7 +125,7 @@ function detect_doctype(source::AbstractString) end -function run_chunk(chunk::CodeChunk, report::Report, SandBox::Module) +function run_chunk(chunk::CodeChunk, doc::WeaveDoc, report::Report, SandBox::Module) @info("Weaving chunk $(chunk.number) from line $(chunk.start_line)") result_chunks = eval_chunk(chunk, report, SandBox) occursin("2html", report.formatdict[:doctype]) && (result_chunks = embed_figures(result_chunks, report.cwd)) @@ -163,19 +163,19 @@ function img2base64(fig, cwd) end end -function run_chunk(chunk::DocChunk, report::Report, SandBox::Module) - chunk.content = [run_inline(c, report, SandBox) for c in chunk.content] +function run_chunk(chunk::DocChunk, doc::WeaveDoc, report::Report, SandBox::Module) + chunk.content = [run_inline(c, doc, report, SandBox) for c in chunk.content] return chunk end -function run_inline(inline::InlineText, report::Report, SandBox::Module) +function run_inline(inline::InlineText, doc::WeaveDoc, report::Report, SandBox::Module) return inline end -function run_inline(inline::InlineCode, report::Report, SandBox::Module) +function run_inline(inline::InlineCode, doc::WeaveDoc, report::Report, SandBox::Module) #Make a temporary CodeChunk for running code. Collect results and don't wrap chunk = CodeChunk(inline.content, 0, 0, "", Dict(:hold => true, :wrap => false)) - options = merge(rcParams[:chunk_defaults], chunk.options) + options = merge(doc.chunk_defaults, chunk.options) merge!(chunk.options, options) chunks = eval_chunk(chunk, report, SandBox) @@ -268,7 +268,6 @@ end function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) - if !chunk.options[:eval] chunk.output = "" chunk.options[:fig] = false @@ -375,21 +374,17 @@ function get_outname(out_path::AbstractString, doc::WeaveDoc; ext = nothing) end end - -function set_rc_params(formatdict, fig_path, fig_ext) +function set_rc_params(doc::WeaveDoc, fig_path, fig_ext) + formatdict = doc.format.formatdict if fig_ext == nothing - rcParams[:chunk_defaults][:fig_ext] = formatdict[:fig_ext] - docParams[:fig_ext] = formatdict[:fig_ext] + doc.chunk_defaults[:fig_ext] = formatdict[:fig_ext] else - rcParams[:chunk_defaults][:fig_ext] = fig_ext - docParams[:fig_ext] = fig_ext + doc.chunk_defaults[:fig_ext] = fig_ext end - rcParams[:chunk_defaults][:fig_path] = fig_path - docParams[:fig_path] = fig_path + doc.chunk_defaults[:fig_path] = fig_path return nothing end - function collect_results(chunk::CodeChunk, fmt::ScriptResult) content = "" result_no = 1 diff --git a/test/documents/header_test.jmd b/test/documents/header_test.jmd new file mode 100644 index 0000000..820c3a7 --- /dev/null +++ b/test/documents/header_test.jmd @@ -0,0 +1,12 @@ +--- +options: + md2html : + out_path : html + echo : false + out_width : 30% +--- + + +```julia +repeat("🐐", 10) +``` diff --git a/test/formatter_test.jl b/test/formatter_test.jl index 5870866..e074df4 100644 --- a/test/formatter_test.jl +++ b/test/formatter_test.jl @@ -89,7 +89,6 @@ testcows = """ πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„πŸ„""" wcows = Weave.wrapline(cows) -println(wcows) @test wcows == testcows @test length(split(wcows, "\n")[1]) == 75 diff --git a/test/options_test.jl b/test/options_test.jl index dc499f9..29544c3 100644 --- a/test/options_test.jl +++ b/test/options_test.jl @@ -21,3 +21,6 @@ args = header["options"] "out_path" => "md/") @test Weave.combine_args(args, "pandoc") == Dict("fig_ext" => ".png", "out_path" => "reports") +doc = Weave.read_doc(joinpath(@__DIR__, "documents", "header_test.jmd")) +@test doc.chunk_defaults[:echo] == false +@test doc.chunk_defaults[:out_width] == "30%"