Skip to content

Commit 730eb48

Browse files
authored
Merge pull request #86 from JuliaComputing/as/flexible_column_components
Create an interface for column components other than MultiDocRef
2 parents 0caa723 + cfdeff5 commit 730eb48

File tree

4 files changed

+61
-17
lines changed

4 files changed

+61
-17
lines changed

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ docs = [
8787
name = "JuliaInterpreter",
8888
giturl = "https://github.com/JuliaDebug/JuliaInterpreter.jl.git",
8989
),
90+
MultiDocumenter.Link("Lux", "https://github.com/avik-pal/Lux.jl"),
9091
],
9192
),
9293
MultiDocumenter.Column(

src/MultiDocumenter.jl

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,25 @@ Base.@kwdef mutable struct SearchConfig
2626
end
2727

2828
"""
29-
struct MultiDocRef
29+
abstract type DropdownComponent
30+
31+
The supertype for any component that can be put in a dropdown column and
32+
rendered using `MultiDocumenter.render(::YourComponent, thispagepath, dir, prettyurls)`.
33+
34+
All `DropdownComponent`s go in [`Column`](@ref)s, which go in [`MegaDropdownNav`](@ref).
35+
36+
Any subtype of `DropdownComponent` must implement that `render` method.
37+
38+
The main subtype is [`MultiDocRef`](@ref), which refers to external documentation
39+
and adds it to the search index. However, there are others like [`Link`](@ref)
40+
which is used to link to external sites without making them searchable, and
41+
users can implement their own custom components.
42+
"""
43+
abstract type DropdownComponent end
44+
45+
"""
46+
struct MultiDocRef <: DropdownComponent
47+
MultiDocRef(; upstream, name, path, giturl = "", branch = "gh-pages", fix_canonical_url = true)
3048
3149
Represents one set of docs that will get an entry in the MultiDocumenter navigation.
3250
@@ -44,7 +62,7 @@ Represents one set of docs that will get an entry in the MultiDocumenter navigat
4462
* `fix_canonical_url`: this can be set to `false` to disable the canonical URL fixing
4563
for this `MultiDocRef` (see also `canonical_domain` for [`make`](@ref)).
4664
"""
47-
struct MultiDocRef
65+
struct MultiDocRef <: DropdownComponent
4866
upstream::String
4967
path::String
5068
name::Any
@@ -64,14 +82,28 @@ function MultiDocRef(;
6482
MultiDocRef(upstream, path, name, fix_canonical_url, giturl, branch)
6583
end
6684

85+
"""
86+
Link([text::String], link::String, [isexternal::Bool]) <: DropdownComponent
87+
88+
Represents a link to an external site.
89+
"""
90+
struct Link <: MultiDocumenter.DropdownComponent
91+
text::String
92+
link::String
93+
isexternal::Bool
94+
end
95+
96+
Link(link::String) = Link(link, link)
97+
Link(text::String, link::String) = Link(text, link, contains(link, "//"))
98+
6799
struct DropdownNav
68100
name::String
69-
children::Vector{MultiDocRef}
101+
children::Vector{DropdownComponent}
70102
end
71103

72104
struct Column
73105
name::Any
74-
children::Vector{MultiDocRef}
106+
children::Vector{DropdownComponent}
75107
end
76108

77109
struct MegaDropdownNav
@@ -84,8 +116,8 @@ struct BrandImage
84116
imagepath::String
85117
end
86118

87-
function walk_outputs(f, root, docs::Vector{MultiDocRef}, dirs::Vector{String})
88-
for ref in docs
119+
function walk_outputs(f, root, docs::Vector, dirs::Vector{String})
120+
for ref in filter(x -> x isa MultiDocRef, docs)
89121
p = joinpath(root, ref.path)
90122
for dir in dirs
91123
dirpath = joinpath(p, dir)
@@ -199,10 +231,10 @@ function make(
199231
end
200232
site_root_url = string(canonical_domain, rstrip(rootpath, '/'))
201233

202-
maybe_clone(flatten_multidocrefs(docs))
234+
maybe_clone(flatten_dropdowncomponents(docs))
203235

204236
dir = make_output_structure(
205-
flatten_multidocrefs(docs),
237+
flatten_dropdowncomponents(docs),
206238
prettyurls,
207239
hide_previews;
208240
canonical = site_root_url,
@@ -235,7 +267,7 @@ function make(
235267
if search_engine != false
236268
search_engine.engine.build_search_index(
237269
dir,
238-
flatten_multidocrefs(docs),
270+
flatten_dropdowncomponents(docs),
239271
search_engine,
240272
rootpath,
241273
)
@@ -255,10 +287,10 @@ function make(
255287
return outdir
256288
end
257289

258-
function flatten_multidocrefs(docs::Vector)
259-
out = MultiDocRef[]
290+
function flatten_dropdowncomponents(docs::Vector)
291+
out = DropdownComponent[]
260292
for doc in docs
261-
if doc isa MultiDocRef
293+
if doc isa DropdownComponent
262294
push!(out, doc)
263295
elseif doc isa MegaDropdownNav
264296
for col in doc.columns
@@ -272,11 +304,11 @@ function flatten_multidocrefs(docs::Vector)
272304
end
273305
end
274306
end
275-
out
307+
return out
276308
end
277309

278-
function maybe_clone(docs::Vector{MultiDocRef})
279-
for doc in docs
310+
function maybe_clone(docs::Vector)
311+
for doc in filter(x -> x isa MultiDocRef, docs)
280312
if !isdir(doc.upstream)
281313
if isempty(doc.giturl)
282314
error(
@@ -311,17 +343,18 @@ function maybe_clone(docs::Vector{MultiDocRef})
311343
end
312344
end
313345
end
346+
return nothing
314347
end
315348

316349
function make_output_structure(
317-
docs::Vector{MultiDocRef},
350+
docs::Vector{DropdownComponent},
318351
prettyurls,
319352
hide_previews;
320353
canonical::Union{AbstractString,Nothing},
321354
)
322355
dir = mktempdir()
323356

324-
for doc in docs
357+
for doc in Iterators.filter(x -> x isa MultiDocRef, docs)
325358
outpath = joinpath(dir, doc.path)
326359

327360
mkpath(dirname(outpath))

src/renderers.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ function render(doc::MultiDocRef, dir, thispagepath, prettyurls)
3636
"""
3737
end
3838

39+
function render(c::Link, doc, thispage, prettyurls)
40+
# class nav-link nav-item makes the formatting correct
41+
# target="_blank" opens the link in a new tab
42+
# TODO: add "external link" icon after, either chain or arrow exiting box.
43+
return @htl """
44+
<a href=$(c.link) class="nav-link nav-item" target=$(c.isexternal ? "_blank" : "_self")>$(c.text)</a>
45+
"""
46+
end
47+
3948
function render(doc::DropdownNav, dir, thispagepath, prettyurls)
4049
return @htl """
4150
<div class="nav-dropdown">

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ docs = [
5454
name = "Lux",
5555
giturl = "https://github.com/avik-pal/Lux.jl",
5656
),
57+
MultiDocumenter.Link("JuliaHub", "https://juliahub.com"),
5758
],
5859
),
5960
MultiDocumenter.Column(

0 commit comments

Comments
 (0)