PkgTemplates.jl/src/plugins/documenter.jl

88 lines
2.5 KiB
Julia
Raw Normal View History

2017-12-01 17:33:57 +00:00
import Base.show
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).
"""
abstract type Documenter <: CustomPlugin end
2017-08-11 22:18:09 +00:00
2017-08-23 17:50:52 +00:00
function gen_plugin(
plugin::Documenter,
template::Template,
dir::AbstractString,
pkg_name::AbstractString,
)
path = joinpath(dir, pkg_name)
2017-08-11 22:18:09 +00:00
docs_dir = joinpath(path, "docs", "src")
mkpath(docs_dir)
2017-08-14 18:12:57 +00:00
if !isempty(plugin.assets)
2017-08-11 22:18:09 +00:00
mkpath(joinpath(docs_dir, "assets"))
2017-08-14 18:12:57 +00:00
for file in plugin.assets
2017-08-11 22:18:09 +00:00
cp(file, joinpath(docs_dir, "assets", basename(file)))
end
# We want something that looks like the following:
# [
# assets/file1,
# assets/file2,
# ]
2018-09-17 19:40:09 +00:00
tab = repeat(" ", 4)
2017-08-15 14:19:37 +00:00
assets_string = "[\n"
for asset in plugin.assets
2018-09-17 19:40:09 +00:00
assets_string *= """$(tab^2)"assets/$(basename(asset))",\n"""
2017-08-11 22:18:09 +00:00
end
2018-09-17 19:40:09 +00:00
assets_string *= "$tab]"
else
assets_string = "[]"
2017-08-11 22:18:09 +00:00
end
text = """
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=:html,
pages=[
"Home" => "index.md",
],
repo="https://$(template.host)/$(template.user)/$pkg_name.jl/blob/{commit}{path}#L{line}",
2017-08-11 22:18:09 +00:00
sitename="$pkg_name.jl",
authors="$(template.authors)",
2017-08-15 14:19:37 +00:00
assets=$assets_string,
2017-08-11 22:18:09 +00:00
)
"""
gen_file(joinpath(dirname(docs_dir), "make.jl"), text)
open(joinpath(docs_dir, "index.md"), "w") do fp
write(fp, "# $pkg_name")
end
2017-08-23 17:50:52 +00:00
readme_path = joinpath(dir, pkg_name, "README.md")
2017-08-11 22:18:09 +00:00
if isfile(readme_path)
cp(readme_path, joinpath(docs_dir, "index.md"), remove_destination=true)
end
end
2017-12-01 17:33:57 +00:00
function show(io::IO, p::Documenter)
2017-12-12 15:15:55 +00:00
spc = " "
println(io, "$(Base.datatype_name(typeof(p))):")
2017-12-01 17:33:57 +00:00
n = length(p.assets)
s = n == 1 ? "" : "s"
2017-12-12 15:15:55 +00:00
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), ", "))")
end
n = length(p.gitignore)
s = n == 1 ? "" : "s"
2017-12-12 15:15:55 +00:00
print(io, "$spc→ $n gitignore entrie$s")
2017-12-01 17:33:57 +00:00
n > 0 && print(io, ": $(join(map(g -> "\"$g\"", p.gitignore), ", "))")
end
function interactive(plugin_type::Type{<:Documenter})
2017-12-12 15:15:55 +00:00
t = Base.datatype_name(plugin_type)
print("$t: Enter any Documenter asset files (separated by spaces) []: ")
return plugin_type(; assets=String.(split(readline())))
end