Skip to content

Commit f7fbd75

Browse files
authored
Add include_files option to servedocs (#163)
* Add `include_files` option to `servedocs` This patch adds a new argument, `include_files::Vector{String}`, which allows users to explicitly track individual files. In particular, this option can be used to track individual files that would otherwise be excluded due to being inside a `skipped_dirs` directory. * Set version to 1.2.0.
1 parent e5006f7 commit f7fbd75

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
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.1.3"
7+
version = "1.2.0"
88

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

src/utils.jl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,28 @@ function servedocs_callback!(
2424
skip_dirs::Vector{String},
2525
skip_files::Vector{String},
2626
include_dirs::Vector{String},
27+
include_files::Vector{String},
2728
foldername::String,
2829
buildfoldername::String)
2930
# ignore things happening in the build folder (generated files etc)
3031
startswith(fp, joinpath(foldername, buildfoldername)) && return nothing
31-
# ignore files skip_dirs and skip_files
32-
for dir in skip_dirs
33-
startswith(fp, dir) && return nothing
34-
end
35-
for file in skip_files
36-
fp == file && return nothing
37-
end
32+
33+
# ignore files in skip_dirs and skip_files (unless the file is in include_files)
34+
if !(fp in include_files)
35+
for dir in skip_dirs
36+
startswith(fp, dir) && return nothing
37+
end
38+
for file in skip_files
39+
fp == file && return nothing
40+
end
41+
end
3842

3943
# if the file that was changed is the `make.jl` file, assume that maybe
4044
# new files have been generated and so refresh the vector of watched files
4145
if fp == path2makejl
4246
# it's easier to start from scratch (takes negligible time)
4347
empty!(dw.watchedfiles)
44-
scan_docs!(dw, foldername, path2makejl, literate, include_dirs)
48+
scan_docs!(dw, foldername, path2makejl, literate, include_dirs, include_files)
4549
end
4650

4751
# Run a Documenter pass
@@ -65,6 +69,7 @@ function scan_docs!(dw::SimpleWatcher,
6569
path2makejl::String,
6670
literate::Union{Nothing,String},
6771
include_dirs::Vector{String},
72+
include_files::Vector{String},
6873
)::Nothing
6974
# Typical expected structure:
7075
# docs
@@ -96,6 +101,11 @@ function scan_docs!(dw::SimpleWatcher,
96101
end
97102
end
98103

104+
# include all user-specified files
105+
for f in filter(isfile, include_files)
106+
push!(dw.watchedfiles, WatchedFile(f))
107+
end
108+
99109
# if the user is not using Literate, return early
100110
literate === nothing && return
101111

@@ -185,6 +195,9 @@ subfolder `docs`.
185195
* `skip_files=[]`: a vector of files that should not trigger regeneration.
186196
* `include_dirs=[]`: extra source directories to watch
187197
(in addition to `joinpath(foldername, "src")`).
198+
* `include_files=[]`: extra source files to watch. Takes precedence over
199+
`skip_dirs` so can e.g. be used to track individual
200+
files in an otherwise skipped directory.
188201
* `foldername="docs"`: specify a different path for the content.
189202
* `buildfoldername="build"`: specify a different path for the build.
190203
* `makejl="make.jl"`: path of the script generating the documentation relative
@@ -203,6 +216,7 @@ function servedocs(;
203216
skip_dirs::Vector{String}=String[],
204217
skip_files::Vector{String}=String[],
205218
include_dirs::Vector{String}=String[],
219+
include_files::Vector{String}=String[],
206220
foldername::String="docs",
207221
buildfoldername::String="build",
208222
makejl::String="make.jl",
@@ -217,6 +231,7 @@ function servedocs(;
217231
skip_dirs = abspath.(skip_dirs)
218232
skip_files = abspath.(skip_files)
219233
include_dirs = abspath.(include_dirs)
234+
include_files = abspath.(include_files)
220235

221236
path2makejl = joinpath(foldername, makejl)
222237

@@ -227,12 +242,12 @@ function servedocs(;
227242
docwatcher,
228243
fp -> servedocs_callback!(
229244
docwatcher, fp, path2makejl,
230-
literate, skip_dirs, skip_files, include_dirs, foldername, buildfoldername
245+
literate, skip_dirs, skip_files, include_dirs, include_files, foldername, buildfoldername
231246
)
232247
)
233248

234249
# Scan the folder and update the list of files to watch
235-
scan_docs!(docwatcher, foldername, path2makejl, literate, include_dirs)
250+
scan_docs!(docwatcher, foldername, path2makejl, literate, include_dirs, include_files)
236251

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

test/utils.jl

Lines changed: 16 additions & 12 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[], String[], "docs", "build")
26+
def = (nothing, String[], 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", "", "", String[])
63+
@test_throws ErrorException LS.scan_docs!(dw, "docs", "", "", String[], String[])
6464

6565
empty!(dw.watchedfiles)
6666

@@ -70,19 +70,23 @@ 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")
73+
mkdir("extradir")
74+
write(joinpath("extradir", "extra.md"), "Extra source file")
75+
76+
mkdir("extradir2")
77+
write(joinpath("extradir2", "extra2.md"), "Extra source file 2")
7578

7679
mkdir(joinpath("docs", "lit"))
7780
write(joinpath("docs", "lit", "index.jl"), "1+1")
7881

79-
LS.scan_docs!(dw, "docs", "docs/make.jl", joinpath("docs", "lit"), [abspath("extrasrc")])
82+
LS.scan_docs!(dw, "docs", "docs/make.jl", joinpath("docs", "lit"), [abspath("extradir")], [abspath("extradir2", "extra2.md")])
8083

81-
@test length(dw.watchedfiles) == 4 # index.jl, index2.md, make.jl, extra.md
84+
@test length(dw.watchedfiles) == 5 # index.jl, index2.md, make.jl, extra.md, extra2.md
8285
@test endswith(dw.watchedfiles[1].path, "make.jl")
8386
@test endswith(dw.watchedfiles[2].path, "index2.md")
8487
@test endswith(dw.watchedfiles[3].path, "extra.md")
85-
@test endswith(dw.watchedfiles[4].path, "index.jl")
88+
@test endswith(dw.watchedfiles[4].path, "extra2.md")
89+
@test endswith(dw.watchedfiles[5].path, "index.jl")
8690

8791
cd(bk)
8892
end
@@ -111,7 +115,7 @@ end
111115
# callback function
112116
dw = LS.SimpleWatcher()
113117

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

116120
@test length(dw.watchedfiles) == 3
117121
@test dw.watchedfiles[1].path == joinpath("site", "make.jl")
@@ -122,14 +126,14 @@ end
122126

123127
# let's now remove `index2.md`
124128
rm(joinpath("site", "src", "index2.md"))
125-
LS.servedocs_callback!(dw, makejl, makejl, "", String[], String[], String[], "site", "build")
129+
LS.servedocs_callback!(dw, makejl, makejl, "", String[], String[], String[], String[], "site", "build")
126130

127131
# the file has been removed
128132
@test length(dw.watchedfiles) == 2
129133
@test readmake() == 3
130134

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

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

149153
empty!(dw.watchedfiles)
150154

@@ -157,7 +161,7 @@ end
157161
mkdir(joinpath("site", "lit"))
158162
write(joinpath("site", "lit", "index.jl"), "1+1")
159163

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

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

0 commit comments

Comments
 (0)