Line data Source code
1 : using Pkg: Pkg
2 : using PKGNAME
3 : using Documenter
4 : using Random # Loads PKGNAME Random extension.
5 :
6 : using Literate
7 :
8 : const REPO_ROOT = joinpath(@__DIR__, "..")
9 : const DOC_SRC = joinpath(@__DIR__, "src")
10 : const DOC_STAGE = joinpath(@__DIR__, "stage")
11 : const DOC_BUILD = joinpath(@__DIR__, "build")
12 :
13 : # Move src files to staging area.
14 : mkpath(DOC_STAGE)
15 1 : for (root, dirs, files) in walkdir(DOC_SRC)
16 2 : println("Directories in $root: $dirs")
17 2 : rel_root = relpath(root, DOC_SRC)
18 2 : for dir in dirs
19 1 : stage = joinpath(DOC_STAGE, rel_root, dir)
20 1 : mkpath(stage)
21 1 : end
22 2 : println("Files in $root: $files")
23 2 : for file in files
24 2 : src = joinpath(DOC_SRC, rel_root, file)
25 2 : stage = joinpath(DOC_STAGE, rel_root, file)
26 2 : cp(src, stage)
27 2 : end
28 2 : end
29 :
30 : # Process examples and put them in staging area.
31 : build_examples = true
32 : build_notebooks = true
33 : build_scripts = true
34 : examples = ["Simple Usage" => "simple-usage"]
35 : examples_markdown = []
36 :
37 1 : function update_header(content, pth)
38 1 : links = []
39 1 : if build_notebooks
40 1 : push!(links, "[Jupyter notebook](main.ipynb)")
41 : end
42 1 : if build_scripts
43 1 : push!(links, "[plain script](main.jl)")
44 : end
45 1 : if length(links) == 0
46 0 : return content
47 : end
48 1 : project_link = "[Project.toml](Project.toml)"
49 1 : return """
50 : # # Reproducing example
51 : # The packages for this example are documented in the $project_link.
52 : # # Accessing example
53 : # This can also be accessed as a $(join(links, ", a", ", or a ")).
54 : """ * content
55 : end
56 :
57 : mkpath(joinpath(DOC_STAGE, "examples"))
58 : orig_project = Base.active_project()
59 : for (ex, pth) in examples
60 : in_dir = joinpath(REPO_ROOT, "examples", pth)
61 : in_pth = joinpath(in_dir, "main.jl")
62 : out_dir = joinpath(DOC_STAGE, "examples", pth)
63 : if build_examples
64 : push!(examples_markdown, ex => joinpath("examples", pth, "index.md"))
65 1 : upd(content) = update_header(content, pth)
66 :
67 : # Copy other files over to out_dir.
68 : Base.Filesystem.cptree(in_dir, out_dir)
69 : rm(joinpath(out_dir, "main.jl"))
70 :
71 : if isdir(in_dir)
72 : Pkg.activate(in_dir)
73 : Pkg.develop(; path=joinpath(@__DIR__, ".."))
74 : Pkg.instantiate()
75 : end
76 : try
77 : # Build outputs.
78 : Literate.markdown(in_pth, out_dir; name="index", preprocess=upd, execute=true)
79 : if build_notebooks
80 : Literate.notebook(in_pth, out_dir)
81 : end
82 : if build_scripts
83 : Literate.script(in_pth, out_dir)
84 : end
85 : finally
86 : Pkg.activate(orig_project)
87 : end
88 : end
89 : end
90 :
91 : # Set metadata for doctests.
92 : DocMeta.setdocmeta!(PKGNAME, :DocTestSetup, :(using PKGNAME, Test); recursive=true)
93 : if PKGNAME.HAS_NATIVE_EXTENSIONS
94 : using Random
95 : DocMeta.setdocmeta!(
96 : PKGNAME.get_extension(PKGNAME, :RandomExt),
97 : :DocTestSetup,
98 : :(using PKGNAME, Test);
99 : recursive=true,
100 : )
101 : end
102 : makedocs(;
103 : modules=[PKGNAME, PKGNAME.get_extension(PKGNAME, :RandomExt)],
104 : authors="Grant Bruer gbruer15@gmail.com and contributors",
105 : sitename="PKGNAME.jl",
106 : source=DOC_STAGE,
107 : build=DOC_BUILD,
108 : format=Documenter.HTML(;
109 : repolink="https://github.com/gbruer15/PKGNAME.jl",
110 : canonical="https://gbruer15.github.io/PKGNAME.jl",
111 : edit_link="main",
112 : assets=String[],
113 : size_threshold=2 * 2^20,
114 : ),
115 : repo="github.com/gbruer15/PKGNAME.jl",
116 : pages=[
117 : "Home" => "index.md",
118 : "Examples" => examples_markdown,
119 : "Coverage" => "coverage/index.md",
120 : ],
121 : doctest=false,
122 : )
|