Skip to content

Commit 7d438cc

Browse files
committed
Fixes and cleanup
1 parent f404ebf commit 7d438cc

File tree

5 files changed

+93
-71
lines changed

5 files changed

+93
-71
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ version = "0.1.0"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
8-
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
98
Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a"
109
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1110
NodeJS = "2bd173c7-0d6d-553b-b6af-13a54713934c"
1211
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1312

1413
[compat]
1514
AbstractTrees = "0.4"
16-
Documenter = "0.27"
1715
Gumbo = "0.8"
1816
JSON = "0.20,0.21"
1917
NodeJS = "1"

README.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@ Example usage:
44
```julia
55
using MultiDocumenter
66

7+
clonedir = mktempdir()
8+
79
docs = [
8-
MultiDocumenter.MultiDocRef(
9-
upstream = "/home/pfitzseb/.julia/dev/Documenter/docs/build",
10-
path = "documenter",
11-
name = "Home"
10+
("JuliaDocs/Documenter.jl.git", "gh-pages") => MultiDocumenter.MultiDocRef(
11+
upstream = joinpath(clonedir, "Documenter"),
12+
path = "doc",
13+
name = "Documenter"
1214
),
13-
MultiDocumenter.MultiDocRef(
14-
upstream = "/home/pfitzseb/.julia/dev/Infiltrator/docs/build",
15-
path = "infil",
15+
("JuliaDebug/Infiltrator.jl.git", "gh-pages") => MultiDocumenter.MultiDocRef(
16+
upstream = joinpath(clonedir, "Infiltrator"),
17+
path = "inf",
1618
name = "Infiltrator"
17-
)
19+
),
1820
]
1921

22+
for ((remote, branch), docref) in docs
23+
run(`git clone --depth 1 [email protected]:$remote --branch $branch --single-branch $(docref.upstream)`)
24+
end
25+
26+
outpath = joinpath(@__DIR__, "out")
27+
2028
MultiDocumenter.make(
21-
joinpath(@__DIR__, "out"),
22-
docs;
23-
assets_dir = joinpath(@__DIR__, "assets"),
24-
brand_image = MultiDocumenter.BrandImage(
25-
"documenter/index.html",
26-
"assets/brandimg.svg"
27-
),
28-
custom_stylesheets = [
29-
"assets/custom.css",
30-
"assets/fonts/fonts.css",
31-
]
29+
outpath,
30+
collect(last.(docs));
31+
search_engine = MultiDocumenter.SearchConfig(
32+
index_versions = ["stable"],
33+
engine = MultiDocumenter.FlexSearch
34+
)
3235
)
3336
```
3437

src/MultiDocumenter.jl

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module MultiDocumenter
22

3-
import Documenter, Gumbo, AbstractTrees
4-
5-
include("search/flexsearch.jl")
6-
include("search/stork.jl")
3+
import Gumbo, AbstractTrees
74

85
"""
96
SearchConfig(index_versions = ["stable"], engine = MultiDocumenter.FlexSearch)
107
8+
`index_versions` is a vector of relative paths used for generating the search index.
9+
`engine` may be `MultiDocumenter.FlexSearch`, `MultiDocumenter.Stork`, or a module that conforms
10+
to the expected API.
1111
"""
1212
Base.@kwdef mutable struct SearchConfig
1313
index_versions = ["stable"]
@@ -30,6 +30,28 @@ function MultiDocRef(; upstream, name, path)
3030
MultiDocRef(upstream, path, name)
3131
end
3232

33+
function walk_outputs(f, root, docs::Vector{MultiDocRef}, dirs::Vector{String})
34+
for ref in docs
35+
p = joinpath(root, ref.path)
36+
for dir in dirs
37+
dirpath = joinpath(p, dir)
38+
isdir(dirpath) || continue
39+
for (r, _, files) in walkdir(dirpath)
40+
for file in files
41+
file == "index.html" || continue
42+
43+
f(chop(r, head = length(root), tail = 0), joinpath(r, file))
44+
end
45+
end
46+
end
47+
end
48+
end
49+
50+
include("search/flexsearch.jl")
51+
include("search/stork.jl")
52+
53+
const DEFAULT_ENGINE = SearchConfig(index_versions = ["stable"], engine = FlexSearch)
54+
3355
"""
3456
make(
3557
outdir,
@@ -49,7 +71,7 @@ Aggregates multiple Documenter.jl-based documentation pages `docs` into `outdir`
4971
item in the global navigation
5072
- `custom_stylesheets` is a `Vector{String}` of stylesheets injected into each page.
5173
- `custom_scripts` is a `Vector{String}` of scripts injected into each page.
52-
- `search_engine` inserts a global search bar. See [`SearchConfig`](@ref) for more details.
74+
- `search_engine` inserts a global search bar if not `false`. See [`SearchConfig`](@ref) for more details.
5375
- `prettyurls` removes all `index.html` suffixes from links in the global navigation.
5476
"""
5577
function make(
@@ -59,7 +81,7 @@ function make(
5981
brand_image::Union{Nothing,BrandImage} = nothing,
6082
custom_stylesheets = [],
6183
custom_scripts = [],
62-
search_engine = SearchConfig(index_versions = ["stable"], engine = FlexSearch),
84+
search_engine = DEFAULT_ENGINE,
6385
prettyurls = true
6486
)
6587

@@ -71,6 +93,13 @@ function make(
7193
isdir(out_assets) || mkpath(out_assets)
7294
cp(joinpath(@__DIR__, "..", "assets", "__default"), joinpath(out_assets, "__default"))
7395

96+
if search_engine != false
97+
if search_engine.engine == Stork && !Stork.has_stork()
98+
@warn "stork binary not found. Falling back to flexsearch as search_engine."
99+
search_engine = DEFAULT_ENGINE
100+
end
101+
end
102+
74103
inject_styles_and_global_navigation(
75104
dir,
76105
docs,
@@ -82,7 +111,7 @@ function make(
82111
)
83112

84113
if search_engine != false
85-
search_engine.engine.build_search_index(dir, search_engine)
114+
search_engine.engine.build_search_index(dir, docs, search_engine)
86115
end
87116

88117
cp(dir, outdir; force = true)
@@ -215,10 +244,11 @@ function js_injector()
215244
return read(joinpath(@__DIR__, "..", "assets", "multidoc_injector.js"), String)
216245
end
217246

247+
218248
function inject_styles_and_global_navigation(
219249
dir,
220250
docs::Vector{MultiDocRef},
221-
brand_image::BrandImage,
251+
brand_image,
222252
custom_stylesheets,
223253
custom_scripts,
224254
search_engine,
@@ -273,16 +303,15 @@ function inject_styles_and_global_navigation(
273303
documenter_div = first(el.children)
274304
if documenter_div isa Gumbo.HTMLElement &&
275305
Gumbo.getattr(documenter_div, "id", "") == "documenter"
276-
# inject global navigation as first element in body
277-
278-
global_nav =
279-
make_global_nav(dir, docs, root, brand_image, search_engine, prettyurls)
280-
global_nav.parent = el
281-
pushfirst!(el.children, global_nav)
282-
injected += 1
283-
else
284-
@warn "Could not inject global nav into $path."
306+
@debug "Could not detect Documenter page layout in $path. This may be due to an old version of Documenter."
285307
end
308+
# inject global navigation as first element in body
309+
310+
global_nav =
311+
make_global_nav(dir, docs, root, brand_image, search_engine, prettyurls)
312+
global_nav.parent = el
313+
pushfirst!(el.children, global_nav)
314+
injected += 1
286315
end
287316
end
288317
end

src/search/flexsearch.jl

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module FlexSearch
22
import Gumbo, JSON, AbstractTrees, NodeJS
3+
import ..walk_outputs
34

45
const ID = Ref(0)
56

@@ -61,7 +62,7 @@ function add_fragment(doc, el)
6162
end
6263
end
6364

64-
function add_to_index(index, ref, file)
65+
function add_to_index!(index, ref, file)
6566
content = read(file, String)
6667
html = Gumbo.parsehtml(content)
6768

@@ -82,22 +83,12 @@ function add_to_index(index, ref, file)
8283
push!(index.documents, doc)
8384
end
8485

85-
function generate_index(root, config)
86+
function generate_index(root, docs, config)
8687
search_index = SearchIndex()
87-
for (r, _, files) in walkdir(root)
88-
pathparts = splitpath(relpath(r, root))
89-
if length(pathparts) >= 2 && pathparts[2] in config.index_versions
90-
for file in files
91-
if file == "index.html"
92-
add_to_index(
93-
search_index,
94-
chop(r, head = length(root), tail = 0),
95-
joinpath(r, file),
96-
)
97-
end
98-
end
99-
end
88+
walk_outputs(root, docs, config.index_versions) do path, file
89+
add_to_index!(search_index, path, file)
10090
end
91+
10192
return search_index
10293
end
10394

@@ -160,9 +151,9 @@ function to_json_index(index::SearchIndex, file)
160151
end
161152
end
162153

163-
function build_search_index(root, config)
154+
function build_search_index(root, docs, config)
164155
ID[] = 0
165-
idx = generate_index(root, config)
156+
idx = generate_index(root, docs, config)
166157
to_json_index(idx, joinpath(root, "index.json"))
167158
println("Writing flexsearch index:")
168159
cd(root) do

src/search/stork.jl

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
module Stork
22
import Pkg.TOML
33
import Gumbo, AbstractTrees
4+
import ..walk_outputs
45

5-
function build_search_index(root, config)
6-
config = make_stork_config(root, config)
6+
function has_stork()
7+
has_stork = false
8+
try
9+
has_stork = success(`stork -V`)
10+
catch
11+
has_stork = false
12+
end
13+
return has_stork
14+
end
15+
16+
function build_search_index(root, docs, config)
17+
config = make_stork_config(root, docs, config)
718
config_path = joinpath(root, "stork.config.toml")
819
index_path = joinpath(root, "stork.st")
920
open(config_path, "w") do io
@@ -13,7 +24,7 @@ function build_search_index(root, config)
1324
return index_path
1425
end
1526

16-
function make_stork_config(root, config)
27+
function make_stork_config(root, docs, config)
1728
files = []
1829
stork_config = Dict(
1930
"input" => Dict(
@@ -23,19 +34,9 @@ function make_stork_config(root, config)
2334
),
2435
"output" => Dict("save_nearest_html_id" => true),
2536
)
26-
for (r, _, fs) in walkdir(root)
27-
pathparts = splitpath(relpath(r, root))
28-
if length(pathparts) >= 2 && pathparts[2] in config.index_versions
29-
for file in fs
30-
if file == "index.html"
31-
add_to_index!(
32-
files,
33-
chop(r, head = length(root), tail = 0),
34-
joinpath(r, file),
35-
)
36-
end
37-
end
38-
end
37+
38+
walk_outputs(root, docs, config.index_versions) do path, file
39+
add_to_index!(files, path, file)
3940
end
4041

4142
return stork_config

0 commit comments

Comments
 (0)