Skip to content

Commit 9437513

Browse files
authored
Add include_dirs to servedocs (#154)
This patch add a new keyword argument `include_dirs` to `servedocs` which make it possible to add additional source directories (in addition to `docs/src`) that should be considered for the file watching. This does, for example, make it possible to watch the source files of your package, and call `Revise.revise()` in `docs/make.jl` in order to automatically update docstrings.
1 parent 292901b commit 9437513

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ authors = [
44
"Jonas Asprion <jonas.asprion@gmx.ch",
55
"Thibaut Lienart <tlienart@me.com>"
66
]
7-
version = "1.0.3"
7+
version = "1.1.0"
88

99
[deps]
1010
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"

src/utils.jl

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function servedocs_callback!(
2323
literate::Union{Nothing,String},
2424
skip_dirs::Vector{String},
2525
skip_files::Vector{String},
26+
include_dirs::Vector{String},
2627
foldername::String,
2728
buildfoldername::String)
2829
# ignore things happening in the build folder (generated files etc)
@@ -40,7 +41,7 @@ function servedocs_callback!(
4041
if fp == path2makejl
4142
# it's easier to start from scratch (takes negligible time)
4243
empty!(dw.watchedfiles)
43-
scan_docs!(dw, foldername, path2makejl, literate)
44+
scan_docs!(dw, foldername, path2makejl, literate, include_dirs)
4445
end
4546

4647
# Run a Documenter pass
@@ -62,7 +63,8 @@ See the docs of the parent function `servedocs`.
6263
function scan_docs!(dw::SimpleWatcher,
6364
foldername::String,
6465
path2makejl::String,
65-
literate::Union{Nothing,String}
66+
literate::Union{Nothing,String},
67+
include_dirs::Vector{String},
6668
)::Nothing
6769
# Typical expected structure:
6870
# docs
@@ -75,16 +77,25 @@ function scan_docs!(dw::SimpleWatcher,
7577
if !isdir(foldername) || !isdir(src)
7678
error("I didn't find a $foldername/ or $foldername/src/ folder.")
7779
end
78-
# watch the make.jl file as well as
7980

81+
# watch the make.jl file as well as
8082
push!(dw.watchedfiles, WatchedFile(path2makejl))
83+
84+
# include all files in the docs/src directory
8185
if isdir(foldername)
8286
# add all files in `docs/src` to watched files
8387
for (root, _, files) walkdir(joinpath(foldername, "src")), file files
8488
push!(dw.watchedfiles, WatchedFile(joinpath(root, file)))
8589
end
8690
end
8791

92+
# include all files in user-specified include directories
93+
for idir in filter(isdir, include_dirs)
94+
for (root, _, files) in walkdir(idir), file in files
95+
push!(dw.watchedfiles, WatchedFile(joinpath(root, file)))
96+
end
97+
end
98+
8899
# if the user is not using Literate, return early
89100
literate === nothing && return
90101

@@ -172,6 +183,8 @@ subfolder `docs`.
172183
* `skip_dirs=[]`: same as `skip_dir` but for a list of such dirs. Takes
173184
precedence over `skip_dir`.
174185
* `skip_files=[]`: a vector of files that should not trigger regeneration.
186+
* `include_dirs=[]`: extra source directories to watch
187+
(in addition to `joinpath(foldername, "src")`).
175188
* `foldername="docs"`: specify a different path for the content.
176189
* `buildfoldername="build"`: specify a different path for the build.
177190
* `makejl="make.jl"`: path of the script generating the documentation relative
@@ -189,6 +202,7 @@ function servedocs(;
189202
skip_dir::String="",
190203
skip_dirs::Vector{String}=String[],
191204
skip_files::Vector{String}=String[],
205+
include_dirs::Vector{String}=String[],
192206
foldername::String="docs",
193207
buildfoldername::String="build",
194208
makejl::String="make.jl",
@@ -202,6 +216,7 @@ function servedocs(;
202216
end
203217
skip_dirs = abspath.(skip_dirs)
204218
skip_files = abspath.(skip_files)
219+
include_dirs = abspath.(include_dirs)
205220

206221
path2makejl = joinpath(foldername, makejl)
207222

@@ -212,12 +227,12 @@ function servedocs(;
212227
docwatcher,
213228
fp -> servedocs_callback!(
214229
docwatcher, fp, path2makejl,
215-
literate, skip_dirs, skip_files, foldername, buildfoldername
230+
literate, skip_dirs, skip_files, include_dirs, foldername, buildfoldername
216231
)
217232
)
218233

219234
# Scan the folder and update the list of files to watch
220-
scan_docs!(docwatcher, foldername, path2makejl, literate)
235+
scan_docs!(docwatcher, foldername, path2makejl, literate, include_dirs)
221236

222237
# activate the doc environment if required
223238
doc_env && Pkg.activate(joinpath(foldername, "Project.toml"))

test/utils.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
include(abspath(makejl))
2424
@test readmake() == 1
2525

26-
def = (nothing, String[], String[], "docs", "build")
26+
def = (nothing, String[], String[], String[], "docs", "build")
2727

2828
# callback function
2929
dw = LS.SimpleWatcher()
@@ -60,7 +60,7 @@ end
6060
# error if there's no docs/ folder
6161
cray = Crayon(foreground=:cyan, bold=true)
6262
println(cray, "\n⚠ Deliberately causing an error to be displayed and handled...\n")
63-
@test_throws ErrorException LS.scan_docs!(dw, "docs", "", "")
63+
@test_throws ErrorException LS.scan_docs!(dw, "docs", "", "", String[])
6464

6565
empty!(dw.watchedfiles)
6666

@@ -70,15 +70,19 @@ end
7070
write(joinpath("docs", "src", "index2.md"), "Random file")
7171
write(joinpath("docs", "make.jl"), "1+1")
7272

73+
mkdir("extrasrc")
74+
write(joinpath("extrasrc", "extra.md"), "Extra source file")
75+
7376
mkdir(joinpath("docs", "lit"))
7477
write(joinpath("docs", "lit", "index.jl"), "1+1")
7578

76-
LS.scan_docs!(dw, "docs", "docs/make.jl", joinpath("docs", "lit"))
79+
LS.scan_docs!(dw, "docs", "docs/make.jl", joinpath("docs", "lit"), [abspath("extrasrc")])
7780

78-
@test length(dw.watchedfiles) == 3 # index.jl, index2.md, make.jl
81+
@test length(dw.watchedfiles) == 4 # index.jl, index2.md, make.jl, extra.md
7982
@test endswith(dw.watchedfiles[1].path, "make.jl")
8083
@test endswith(dw.watchedfiles[2].path, "index2.md")
81-
@test endswith(dw.watchedfiles[3].path, "index.jl")
84+
@test endswith(dw.watchedfiles[3].path, "extra.md")
85+
@test endswith(dw.watchedfiles[4].path, "index.jl")
8286

8387
cd(bk)
8488
end
@@ -107,7 +111,7 @@ end
107111
# callback function
108112
dw = LS.SimpleWatcher()
109113

110-
LS.servedocs_callback!(dw, makejl, makejl, "", String[], String[], "site", "build")
114+
LS.servedocs_callback!(dw, makejl, makejl, "", String[], String[], String[], "site", "build")
111115

112116
@test length(dw.watchedfiles) == 3
113117
@test dw.watchedfiles[1].path == joinpath("site", "make.jl")
@@ -118,14 +122,14 @@ end
118122

119123
# let's now remove `index2.md`
120124
rm(joinpath("site", "src", "index2.md"))
121-
LS.servedocs_callback!(dw, makejl, makejl, "", String[], String[], "site", "build")
125+
LS.servedocs_callback!(dw, makejl, makejl, "", String[], String[], String[], "site", "build")
122126

123127
# the file has been removed
124128
@test length(dw.watchedfiles) == 2
125129
@test readmake() == 3
126130

127131
# let's check there's an appropriate trigger for index
128-
LS.servedocs_callback!(dw, joinpath("site", "src", "index.md"), makejl, "", String[], String[], "site", "build")
132+
LS.servedocs_callback!(dw, joinpath("site", "src", "index.md"), makejl, "", String[], String[], String[], "site", "build")
129133
@test length(dw.watchedfiles) == 2
130134
@test readmake() == 4
131135

@@ -140,7 +144,7 @@ end
140144
# error if there's no docs/ folder
141145
cray = Crayon(foreground=:cyan, bold=true)
142146
println(cray, "\n⚠ Deliberately causing an error to be displayed and handled...\n")
143-
@test_throws ErrorException LS.scan_docs!(dw, "site", "site", "")
147+
@test_throws ErrorException LS.scan_docs!(dw, "site", "site", "", String[])
144148

145149
empty!(dw.watchedfiles)
146150

@@ -153,7 +157,7 @@ end
153157
mkdir(joinpath("site", "lit"))
154158
write(joinpath("site", "lit", "index.jl"), "1+1")
155159

156-
LS.scan_docs!(dw, "site", "site/make.jl", joinpath("site", "lit"))
160+
LS.scan_docs!(dw, "site", "site/make.jl", joinpath("site", "lit"), String[])
157161

158162
@test length(dw.watchedfiles) == 3 # index.jl, index2.md, make.jl
159163
@test endswith(dw.watchedfiles[1].path, "make.jl")

0 commit comments

Comments
 (0)