Skip to content

Commit

Permalink
added filter function to @autodocs (#885)
Browse files Browse the repository at this point in the history
  • Loading branch information
timziebart authored and mortenpi committed Dec 5, 2018
1 parent ff5690b commit a4079c7
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 13 deletions.
23 changes: 22 additions & 1 deletion docs/src/man/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ docstrings. Note that page matching is done using the end of the provided string
`a.jl` will be matched by *any* source file that ends in `a.jl`, i.e. `src/a.jl` or
`src/foo/a.jl`.

To filter out certain docstrings by your own criteria, you can provide function with them
`Filter` keyword:

````markdown
```@autodocs
Modules = [Foo]
Filter = t -> typeof(t) === DataType && t <: Foo.C
```
````

In the given example, only the docstrings of the subtypes of `Foo.C` are shown. Instead
of an [anonymous function](https://docs.julialang.org/en/v1/manual/functions/index.html#man-anonymous-functions-1)
you can give the name of a function you defined beforehand, too:

````markdown
```@autodocs
Modules = [Foo]
Filter = myCustomFilterFunction
```
````

To include only the exported names from the modules listed in `Modules` use `Private = false`.
In a similar way `Public = false` can be used to only show the unexported names. By
default both of these are set to `true` so that all names will be shown.
Expand All @@ -109,7 +130,7 @@ Order = [:type]

!!! note

When more complex sorting and filtering is needed then use `@docs` to define it
When more complex sorting is needed then use `@docs` to define it
explicitly.

## `@ref` link
Expand Down
33 changes: 22 additions & 11 deletions src/Expanders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ function Selectors.runner(::Type{AutoDocsBlocks}, x, page, doc)
for (ex, str) in Utilities.parseblock(x.code, doc, page)
if Utilities.isassign(ex)
try
fields[ex.args[1]] = Core.eval(curmod, ex.args[2])
if ex.args[1] == :Filter
fields[ex.args[1]] = Core.eval(Main, ex.args[2])
else
fields[ex.args[1]] = Core.eval(curmod, ex.args[2])
end
catch err
push!(doc.internal.errors, :autodocs_block)
@warn "failed to evaluate `$(strip(str))` in `@autodocs` block in $(Utilities.locrepr(page.source))" exception = err
Expand All @@ -346,6 +350,7 @@ function Selectors.runner(::Type{AutoDocsBlocks}, x, page, doc)
pages = map(normpath, get(fields, :Pages, []))
public = get(fields, :Public, true)
private = get(fields, :Private, true)
filterfunc = get(fields, :Filter, x -> true)
results = []
for mod in modules
for (binding, multidoc) in Documenter.DocSystem.getmeta(mod)
Expand All @@ -355,16 +360,22 @@ function Selectors.runner(::Type{AutoDocsBlocks}, x, page, doc)
# What category does the binding belong to?
category = Documenter.DocSystem.category(binding)
if category in order && included
for (typesig, docstr) in multidoc.docs
path = normpath(docstr.data[:path])
object = Utilities.Object(binding, typesig)
if isempty(pages)
push!(results, (mod, path, category, object, isexported, docstr))
else
for p in pages
if endswith(path, p)
push!(results, (mod, p, category, object, isexported, docstr))
break
# filter the elements after category/order has been evaluated
# to ensure that e.g. when `Order = [:type]` is given, the filter
# function really receives only types
filtered = Base.invokelatest(filterfunc, Core.eval(binding.mod, binding.var))
if filtered
for (typesig, docstr) in multidoc.docs
path = normpath(docstr.data[:path])
object = Utilities.Object(binding, typesig)
if isempty(pages)
push!(results, (mod, path, category, object, isexported, docstr))
else
for p in pages
if endswith(path, p)
push!(results, (mod, p, category, object, isexported, docstr))
break
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/examples/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ module AutoDocs
"Macro `B.@m`."
macro m() end
end

module Filter
"abstract super type"
abstract type Major end

"abstract sub type 1"
abstract type Minor1 <: Major end

"abstract sub type 2"
abstract type Minor2 <: Major end

"random constant"
qq = 3.14

"random function"
function qqq end
end
end

# Build example docs
Expand Down
15 changes: 15 additions & 0 deletions test/examples/src/lib/autodocs.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ Modules = [AutoDocs.Pages.E]
Order = [:type]
```

## Filtering

Should include docs for

* [`AutoDocs.Filter.Major`](@ref)
* [`AutoDocs.Filter.Minor1`](@ref)
* [`AutoDocs.Filter.Minor2`](@ref)

in that order.

```@autodocs
Modules = [AutoDocs.Filter]
Order = [:type]
Filter = t -> t <: AutoDocs.Filter.Major
```
2 changes: 1 addition & 1 deletion test/examples/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ end
end
end

@test length(doc.internal.objects) == 38
@test length(doc.internal.objects) == 41
end

@testset "HTML: local" begin
Expand Down

0 comments on commit a4079c7

Please sign in to comment.