Skip to content

Commit 0bd3042

Browse files
authored
Use doc environment if requested (#87)
* experimental doc_env keyword and skip_dirs for servedocs
1 parent 084756d commit 0bd3042

File tree

5 files changed

+54
-10
lines changed

5 files changed

+54
-10
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
name = "LiveServer"
22
uuid = "16fef848-5104-11e9-1b77-fb7a48bbb589"
33
authors = ["Jonas Asprion <jonas.asprion@gmx.ch", "Thibaut Lienart <tlienart@me.com>"]
4-
version = "0.3.3"
4+
version = "0.3.4"
55

66
[deps]
77
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
88
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
99
FileWatching = "7b1f6079-737a-58dc-b8bc-7a2ca5c1b5ee"
1010
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
11+
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1112
Sockets = "6462fe0b-24de-5631-8697-dd941f90decc"
1213
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1314

docs/src/man/functionalities.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,21 @@ Upon modifying a `.md` file (e.g. updating `docs/src/index.md`), the `make.jl` w
7979
The first pass collects all information in the code (i.e. docstrings), while
8080
subsequent passes only consider changes in the markdown (`.md`) files. This
8181
restriction is necessary to achieve a fast update behavior.
82+
83+
### Additional keywords
84+
85+
The `servedocs` function now takes two extra keywords which may, in some cases, make your life easier:
86+
87+
* `doc_env=false`, if set to true, the `Project.toml` available in `docs/` will be activated (note 1),
88+
* `skip_dir=""`, indicates a directory to skip when looking at the docs folder for change, this can be useful when using packages like Literate or Weave that may generate files inside your `src` folder.
89+
90+
Note that in both cases these keywords are there for your convenience but would be best not used. See also the discussion in [this issue](https://github.com/asprionj/LiveServer.jl/issues/85).
91+
In the first case, doing
92+
93+
```
94+
julia --project=docs -e 'using LiveServer; servedocs()'
95+
```
96+
97+
is more robust.
98+
99+
In the second case, it would be best if you made sure that all generated files are saved in `docs/build/...`.

src/LiveServer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module LiveServer
22

33
# from stdlib
4-
using Sockets
4+
using Sockets, Pkg
55
# the only dependency (see the patch in http_patch.jl)
66
using HTTP
77

src/utils.jl

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,25 @@ triggered to regenerate the documents, subsequently the LiveServer will render t
1010
in `docs/build`.
1111
"""
1212
function servedocs_callback!(dw::SimpleWatcher, fp::AbstractString, makejl::AbstractString,
13-
literate::String="")
13+
literate::String="", skip_dirs::Vector{String}=String[])
14+
# ignore things happening in build (generated files etc)
15+
startswith(fp, joinpath("docs", "build")) && return nothing
16+
if !isempty(skip_dirs)
17+
for dir in skip_dirs
18+
startswith(fp, dir) && return nothing
19+
end
20+
end
1421
# if the file that was changed is the `make.jl` file, assume that maybe new files are # referenced and so refresh the vector of watched files as a result.
1522
if fp == makejl
1623
# it's easier to start from scratch (takes negligible time)
1724
empty!(dw.watchedfiles)
1825
scan_docs!(dw, literate)
1926
end
2027
fext = splitext(fp)[2]
21-
P1 = fext (".md", ".jl")
28+
P1 = fext (".md", ".jl", ".css")
2229
# the second condition is for CSS files, we want to track it but not the output
2330
# if we track the output then there's an infinite loop being triggered (see docstring)
24-
if P1 || (fext == ".css" && !occursin(joinpath("docs", "build", "assets"), fp))
31+
if P1
2532
Main.include(makejl)
2633
file_changed_callback(fp)
2734
end
@@ -97,31 +104,47 @@ end
97104

98105

99106
"""
100-
servedocs(; verbose=false, literate="")
107+
servedocs(; verbose=false, literate="", doc_env=false)
101108
102109
Can be used when developing a package to run the `docs/make.jl` file from Documenter.jl and
103110
then serve the `docs/build` folder with LiveServer.jl. This function assumes you are in the
104111
directory `[MyPackage].jl` with a subfolder `docs`.
105112
106-
* `verbose` is a boolean switch to make the server print information about file changes and
113+
* `verbose=false` is a boolean switch to make the server print information about file changes and
107114
connections.
108-
* `literate` is the path to the folder containing the literate scripts, if left empty, it will be
115+
* `literate=""` is the path to the folder containing the literate scripts, if left empty, it will be
109116
assumed that they are in `docs/src`.
117+
* `doc_env=false` is a boolean switch to make the server start by activating the doc environment or not (i.e. the `Project.toml` in `docs/`).
118+
* `skip_dir=""` is a subpath of `docs/` where modifications should not trigger the generation of the docs, this is useful for instance if you're using Weave and Weave generates some files in `docs/src/examples` in which case you should give `skip_dir=joinpath("docs","src","examples")`.
119+
* `skip_dirs=[]` same as `skip_dir` but for a vector of such dirs. Takes precedence over `skip_dir`.
110120
"""
111-
function servedocs(; verbose::Bool=false, literate::String="")
121+
function servedocs(; verbose::Bool=false, literate::String="", doc_env::Bool=false,
122+
skip_dir::String="", skip_dirs::Vector{String}=String[])
112123
# Custom file watcher: it's the standard `SimpleWatcher` but with a custom callback.
113124
docwatcher = SimpleWatcher()
114-
set_callback!(docwatcher, fp->servedocs_callback!(docwatcher, fp, makejl, literate))
125+
126+
if isempty(skip_dirs) && !isempty(skip_dir)
127+
skip_dirs = [skip_dir]
128+
end
129+
130+
set_callback!(docwatcher,
131+
fp->servedocs_callback!(docwatcher, fp, makejl, literate, skip_dirs))
115132

116133
# Retrieve files to watch
117134
makejl = scan_docs!(docwatcher, literate)
118135

136+
if doc_env
137+
Pkg.activate("docs/Project.toml")
138+
end
119139
# trigger a first pass of Documenter (& possibly Literate)
120140
Main.include(makejl)
121141

122142
# note the `docs/build` exists here given that if we're here it means the documenter
123143
# pass did not error and therefore that a docs/build has been generated.
124144
serve(docwatcher, dir=joinpath("docs", "build"), verbose=verbose)
145+
if doc_env
146+
Pkg.activate()
147+
end
125148
return nothing
126149
end
127150

test/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ end
5858
dw = LS.SimpleWatcher()
5959

6060
# error if there's no docs/ folder
61+
cray = Crayon(foreground=:cyan, bold=true)
62+
println(cray, "\n⚠ Deliberately causing an error to be displayed and handled...\n")
6163
@test_throws SystemError LS.scan_docs!(dw, "docs")
6264

6365
empty!(dw.watchedfiles)

0 commit comments

Comments
 (0)