2018-12-19 21:52:22 +00:00
|
|
|
const DOCUMENTER_UUID = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
|
2018-11-05 21:39:14 +00:00
|
|
|
const STANDARD_KWS = [:modules, :format, :pages, :repo, :sitename, :authors, :assets]
|
|
|
|
|
2017-08-11 22:18:09 +00:00
|
|
|
"""
|
2017-08-17 22:06:05 +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).
|
2018-11-05 21:39:14 +00:00
|
|
|
|
|
|
|
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`.
|
|
|
|
"""
|
2017-08-17 06:57:58 +00:00
|
|
|
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)
|
2018-12-14 20:55:13 +00:00
|
|
|
docs_dir = joinpath(path, "docs")
|
2017-08-11 22:18:09 +00:00
|
|
|
mkpath(docs_dir)
|
2018-09-28 20:30:10 +00:00
|
|
|
|
2018-12-14 20:55:13 +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))
|
2018-12-14 20:55:13 +00:00
|
|
|
finally
|
|
|
|
proj === nothing ? Pkg.activate() : Pkg.activate(proj)
|
|
|
|
end
|
|
|
|
|
2018-11-02 21:38:17 +00:00
|
|
|
tab = repeat(" ", 4)
|
2018-09-28 20:30:10 +00:00
|
|
|
assets_string = if !isempty(p.assets)
|
2018-12-14 20:55:13 +00:00
|
|
|
mkpath(joinpath(docs_dir, "src", "assets"))
|
2018-09-28 20:30:10 +00:00
|
|
|
for file in p.assets
|
2018-12-14 20:55:13 +00:00
|
|
|
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
|
2017-08-14 22:49:44 +00:00
|
|
|
else
|
2019-05-14 20:11:20 +00:00
|
|
|
"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)) &&
|
2018-11-05 21:39:14 +00:00
|
|
|
fieldtype(typeof(p), :additional_kwargs) <: Union{AbstractDict, NamedTuple}
|
2018-11-02 21:38:17 +00:00
|
|
|
# We want something that looks like the following:
|
|
|
|
# key1="val1",
|
|
|
|
# key2="val2",
|
|
|
|
#
|
2018-11-05 21:39:14 +00:00
|
|
|
kws = [keys(p.additional_kwargs)...]
|
|
|
|
valid_keys = filter(k -> !in(Symbol(k), STANDARD_KWS), kws)
|
2018-11-05 16:49:57 +00:00
|
|
|
if length(p.additional_kwargs) > length(valid_keys)
|
2018-12-14 20:55:13 +00:00
|
|
|
invalid_keys = filter(k -> Symbol(k) in STANDARD_KWS, kws)
|
2018-11-05 16:49:57 +00:00
|
|
|
@warn string(
|
|
|
|
"Ignoring predefined Documenter kwargs ",
|
2018-11-05 21:39:14 +00:00
|
|
|
join(map(repr, invalid_keys), ", "),
|
|
|
|
" from additional kwargs"
|
2018-11-05 16:49:57 +00:00
|
|
|
)
|
|
|
|
end
|
2018-11-05 21:39:14 +00:00
|
|
|
join(map(k -> string(tab, k, "=", repr(p.additional_kwargs[k]), ",\n"), valid_keys))
|
2018-11-02 21:38:17 +00:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
|
2018-09-28 21:38:48 +00:00
|
|
|
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],
|
2018-12-14 20:55:13 +00:00
|
|
|
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,
|
2018-11-02 21:38:17 +00:00
|
|
|
$kwargs_string)
|
2017-08-11 22:18:09 +00:00
|
|
|
"""
|
2018-09-28 21:38:48 +00:00
|
|
|
docs = """
|
|
|
|
# $pkg_name.jl
|
2017-08-11 22:18:09 +00:00
|
|
|
|
2018-09-28 21:38:48 +00:00
|
|
|
```@index
|
|
|
|
```
|
2018-09-28 20:30:10 +00:00
|
|
|
|
2018-09-28 21:38:48 +00:00
|
|
|
```@autodocs
|
|
|
|
Modules = [$pkg_name]
|
|
|
|
```
|
|
|
|
"""
|
|
|
|
|
2018-12-14 20:55:13 +00:00
|
|
|
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
|
2017-08-28 21:34:34 +00:00
|
|
|
|
2018-09-19 21:23:11 +00:00
|
|
|
function Base.show(io::IO, p::Documenter)
|
2017-12-12 15:15:55 +00:00
|
|
|
spc = " "
|
2018-12-20 20:21:12 +00:00
|
|
|
println(io, nameof(typeof(p)), ":")
|
2017-12-01 17:33:57 +00:00
|
|
|
|
|
|
|
n = length(p.assets)
|
|
|
|
s = n == 1 ? "" : "s"
|
2018-12-20 20:21:12 +00:00
|
|
|
print(io, spc, "→ $n asset file$s")
|
2017-12-01 17:33:57 +00:00
|
|
|
if n == 0
|
|
|
|
println(io)
|
|
|
|
else
|
2018-12-20 20:21:12 +00:00
|
|
|
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")
|
2018-12-20 20:21:12 +00:00
|
|
|
n > 0 && print(io, ": ", join(map(repr, p.gitignore), ", "))
|
2017-12-01 17:33:57 +00:00
|
|
|
end
|