Skip to content

Commit 72b236a

Browse files
committed
Allow specifying a global default for @meta block settings
This allows to e.g. use a common doctest setup block on all pages, thus complementing `DocMeta.setdocmeta!`. Other nice applications include changing certain settings, such `CollapsedDocStrings = true` globally, or being able to specify `CurrentModule = YourPackageModule` globally.
1 parent ced42e5 commit 72b236a

File tree

7 files changed

+33
-2
lines changed

7 files changed

+33
-2
lines changed

src/doctests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct DocTestContext
1414
file::String
1515
doc::Documenter.Document
1616
meta::Dict{Symbol, Any}
17-
DocTestContext(file::String, doc::Documenter.Document) = new(file, doc, Dict())
17+
DocTestContext(file::String, doc::Documenter.Document) = new(file, doc, copy(doc.user.default_meta))
1818
end
1919

2020
"""

src/documents.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ struct User
341341
version::String # version string used in the version selector by default
342342
highlightsig::Bool # assume leading unlabeled code blocks in docstrings to be Julia.
343343
draft::Bool
344+
default_meta::Dict{Symbol, Any} # default @meta block data for pages
344345
end
345346

346347
"""
@@ -403,6 +404,7 @@ function Document(;
403404
version::AbstractString = "",
404405
highlightsig::Bool = true,
405406
draft::Bool = false,
407+
default_meta::Dict{Symbol, Any} = Dict{Symbol, Any}(),
406408
others...
407409
)
408410

@@ -468,6 +470,7 @@ function Document(;
468470
version,
469471
highlightsig,
470472
draft,
473+
default_meta,
471474
)
472475
internal = Internal(
473476
assetsdir(),

src/expander_pipeline.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function expand(doc::Documenter.Document)
5252
for src in Iterators.flatten([priority_pages, normal_pages])
5353
page = doc.blueprint.pages[src]
5454
@debug "Running ExpanderPipeline on $src"
55-
empty!(page.globals.meta)
55+
copy!(page.globals.meta, doc.user.default_meta)
5656
# We need to collect the child nodes here because we will end up changing the structure
5757
# of the tree in some cases.
5858
for node in collect(page.mdast.children)

src/makedocs.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ For example, if you have `foo.md` and `bar.md`, `bar.md` would normally be evalu
166166
Evaluation order among the `expandfirst` pages is according to the order they appear in the
167167
argument.
168168
169+
**`default_meta`** can be set to TODO. TODO default `@meta` block. TODO
170+
169171
**`draft`** can be set to `true` to build a draft version of the document. In draft mode
170172
some potentially time-consuming steps are skipped (e.g. running `@example` blocks), which is
171173
useful when iterating on the documentation. This setting can also be configured per-page

test/default_meta/src/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Testing
2+
3+
We set the `default_meta` feature by having it setting `DocTestSetup`.
4+
5+
```jldoctest
6+
julia> x
7+
42
8+
```

test/default_meta/tests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Documenter
2+
using Test
3+
4+
const pages = [
5+
"Home" => "index.md",
6+
]
7+
8+
makedocs(sitename = "Test", pages = pages, doctest = true,
9+
default_meta = Dict{Symbol,Any}(:DocTestSetup => :(x = 42)))
10+
# TODO: allow NamedTuples instead or in addition?
11+
# makedocs(sitename = "Test", pages = pages, doctest = true,
12+
# default_meta = (DocTestSetup => :(x = 42), ))
13+
14+
# the test is passing the doctest

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ end
9898
@info "Building clear_module/tests.jl"
9999
@quietly include("clear_module/tests.jl")
100100

101+
# A simple build verifying that default_meta for pages works
102+
@info "Building default_meta/tests.jl"
103+
@quietly include("default_meta/tests.jl")
104+
101105
# Passing a writer positionally (https://github.com/JuliaDocs/Documenter.jl/issues/1046)
102106
@test_throws MethodError makedocs(sitename = "", HTML())
103107

0 commit comments

Comments
 (0)