PkgTemplates.jl/src/plugins/documenter.jl

128 lines
3.9 KiB
Julia
Raw Normal View History

2018-12-19 21:52:22 +00:00
const DOCUMENTER_UUID = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
const STANDARD_KWS = [:modules, :format, :pages, :repo, :sitename, :authors, :assets]
2017-08-11 22:18:09 +00:00
"""
Add a `Documenter` subtype to a template's plugins to add support for documentation
generation via [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl).
By default, the plugin generates a minimal index.md and a make.jl file. The make.jl
file contains the Documenter.makedocs command with predefined values for `modules`,
`format`, `pages`, `repo`, `sitename`, and `authors`.
The subtype is expected to include the following fields:
* `assets::Vector{AbstractString}`, a list of filenames to be included as the `assets`
kwarg to `makedocs`
* `gitignore::Vector{AbstractString}`, a list of files to be added to the `.gitignore`
It may optionally include the field `additional_kwargs::Union{AbstractDict, NamedTuple}`
to allow additional kwargs to be added to `makedocs`.
"""
abstract type Documenter <: CustomPlugin end
2017-08-11 22:18:09 +00:00
2018-09-28 20:30:10 +00:00
function gen_plugin(p::Documenter, t::Template, pkg_name::AbstractString)
path = joinpath(t.dir, pkg_name)
docs_dir = joinpath(path, "docs")
2017-08-11 22:18:09 +00:00
mkpath(docs_dir)
2018-09-28 20:30:10 +00:00
# Create the documentation project.
proj = Base.current_project()
try
Pkg.activate(docs_dir)
2018-12-19 21:52:22 +00:00
Pkg.add(PackageSpec(; name="Documenter", uuid=DOCUMENTER_UUID))
finally
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
end
tab = repeat(" ", 4)
2018-09-28 20:30:10 +00:00
assets_string = if !isempty(p.assets)
mkpath(joinpath(docs_dir, "src", "assets"))
2018-09-28 20:30:10 +00:00
for file in p.assets
cp(file, joinpath(docs_dir, "src", "assets", basename(file)))
2017-08-11 22:18:09 +00:00
end
2018-09-28 20:30:10 +00:00
2017-08-11 22:18:09 +00:00
# We want something that looks like the following:
# [
# assets/file1,
# assets/file2,
# ]
2018-09-28 20:30:10 +00:00
s = "[\n"
for asset in p.assets
s *= """$(tab^2)"assets/$(basename(asset))",\n"""
2017-08-11 22:18:09 +00:00
end
2018-09-28 20:30:10 +00:00
s *= "$tab]"
s
else
"String[]"
2017-08-11 22:18:09 +00:00
end
2018-09-28 20:30:10 +00:00
2018-11-05 22:11:23 +00:00
kwargs_string = if :additional_kwargs in fieldnames(typeof(p)) &&
fieldtype(typeof(p), :additional_kwargs) <: Union{AbstractDict, NamedTuple}
# We want something that looks like the following:
# key1="val1",
# key2="val2",
#
kws = [keys(p.additional_kwargs)...]
valid_keys = filter(k -> !in(Symbol(k), STANDARD_KWS), kws)
if length(p.additional_kwargs) > length(valid_keys)
invalid_keys = filter(k -> Symbol(k) in STANDARD_KWS, kws)
@warn string(
"Ignoring predefined Documenter kwargs ",
join(map(repr, invalid_keys), ", "),
" from additional kwargs"
)
end
join(map(k -> string(tab, k, "=", repr(p.additional_kwargs[k]), ",\n"), valid_keys))
else
""
end
make = """
2017-08-11 22:18:09 +00:00
using Documenter, $pkg_name
2017-08-18 21:08:48 +00:00
makedocs(;
2017-08-11 22:18:09 +00:00
modules=[$pkg_name],
format=Documenter.HTML(),
2017-08-11 22:18:09 +00:00
pages=[
"Home" => "index.md",
],
2018-09-28 20:30:10 +00:00
repo="https://$(t.host)/$(t.user)/$pkg_name.jl/blob/{commit}{path}#L{line}",
2017-08-11 22:18:09 +00:00
sitename="$pkg_name.jl",
2018-09-28 20:30:10 +00:00
authors="$(t.authors)",
2017-08-15 14:19:37 +00:00
assets=$assets_string,
$kwargs_string)
2017-08-11 22:18:09 +00:00
"""
docs = """
# $pkg_name.jl
2017-08-11 22:18:09 +00:00
```@index
```
2018-09-28 20:30:10 +00:00
```@autodocs
Modules = [$pkg_name]
```
"""
gen_file(joinpath(docs_dir, "make.jl"), make)
gen_file(joinpath(docs_dir, "src", "index.md"), docs)
2017-08-11 22:18:09 +00:00
end
2018-09-19 21:23:11 +00:00
function Base.show(io::IO, p::Documenter)
2017-12-12 15:15:55 +00:00
spc = " "
println(io, nameof(typeof(p)), ":")
2017-12-01 17:33:57 +00:00
n = length(p.assets)
s = n == 1 ? "" : "s"
print(io, spc, "$n asset file$s")
2017-12-01 17:33:57 +00:00
if n == 0
println(io)
else
println(io, ": ", join(map(a -> replace(a, homedir() => "~"), p.assets), ", "))
2017-12-01 17:33:57 +00:00
end
n = length(p.gitignore)
s = n == 1 ? "" : "s"
2017-12-12 15:15:55 +00:00
print(io, "$spc→ $n gitignore entrie$s")
n > 0 && print(io, ": ", join(map(repr, p.gitignore), ", "))
2017-12-01 17:33:57 +00:00
end