Skip to content

Commit af80d3a

Browse files
authored
Add a convenience function to generate a servedocs+literate example (#89)
1 parent 0bd3042 commit af80d3a

File tree

4 files changed

+88
-77
lines changed

4 files changed

+88
-77
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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.4"
4+
version = "0.3.5"
55

66
[deps]
77
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
@@ -14,6 +14,6 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1414

1515
[compat]
1616
Crayons = "^4.0.0"
17-
Documenter = "^0.23.0"
17+
Documenter = "^0.23, ^0.24"
1818
HTTP = "^0.8"
1919
julia = "^1.0.0"

docs/src/man/ls+lit.md

Lines changed: 7 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -83,88 +83,20 @@ What the for loop does is simple: it loops over the files in the folder where it
8383

8484
## Complete example
8585

86-
Here's a step-by-step example to get started which should help put all the pieces together.
87-
88-
Let's start by creating a dummy repo
89-
90-
```julia-repl
91-
pkg> generate testlit
92-
julia> cd("testlit")
93-
pkg> activate testlit
94-
pkg> add Documenter Literate LiveServer
95-
pkg> dev .
96-
```
97-
98-
add a `docs/` folder with the appropriate structure so that the `testlit` folder ends up like
99-
100-
```
101-
.
102-
├── Manifest.toml
103-
├── Project.toml
104-
├── docs
105-
│   ├── literate
106-
│   │   └── man
107-
│   │   └── pg1.jl
108-
│   ├── make.jl
109-
│   └── src
110-
│   ├── index.md
111-
│   └── man
112-
└── src
113-
└── testlit.jl
114-
```
115-
116-
where the file `pg1.jl` contains
117-
118-
```julia
119-
# # Test literate
120-
121-
# We can include some code like so:
122-
123-
f(x) = x^5
124-
f(5)
125-
```
126-
127-
the file `index.md` contains
128-
129-
```
130-
# Test
131-
132-
A link to the [other page](/man/pg1.md)
133-
```
134-
135-
and the file `make.jl` contains
136-
137-
```julia
138-
using Documenter, Literate
139-
140-
src = joinpath(@__DIR__, "src")
141-
lit = joinpath(@__DIR__, "literate")
142-
143-
for (root, _, files) walkdir(lit), file files
144-
splitext(file)[2] == ".jl" || continue
145-
ipath = joinpath(root, file)
146-
opath = splitdir(replace(ipath, lit=>src))[1]
147-
Literate.markdown(ipath, opath)
148-
end
149-
150-
makedocs(
151-
sitename = "testlit",
152-
modules = [testlit],
153-
pages = ["Home" => "index.md",
154-
"Other page" => "man/pg1.md"]
155-
)
156-
```
157-
158-
Now `cd("testlit/")` and do
86+
The function `LiveServer.servedocs_literate_example` generates a directory which has the right structure that you can copy for your package.
87+
To experiment, do:
15988

16089
```julia-repl
90+
julia> using LiveServer
91+
julia> LiveServer.servedocs_literate_example("test_dir")
92+
julia> cd("test_dir")
16193
julia> servedocs(literate=joinpath("docs", "literate"))
16294
```
16395

164-
if you navigate to `localhost:8000` you should end up with
96+
if you then navigate to `localhost:8000` you should end up with
16597

16698
![](../assets/testlit.png)
16799

168-
if you modify `testlit/docs/literate/man/pg1.jl` for instance writing `f(4)` it will be applied directly:
100+
if you modify `test_dir/docs/literate/man/pg1.jl` for instance writing `f(4)` it will be applied directly:
169101

170102
![](../assets/testlit2.png)

src/utils.jl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,68 @@ function example(; basedir="")
179179
isempty(basedir) && (basedir = pwd())
180180
cp(joinpath(dirname(dirname(pathof(LiveServer))), "example"), joinpath(basedir, "example"))
181181
end
182+
183+
#
184+
# Generate example repo for servedocs
185+
#
186+
187+
const INDEX_MD = raw"""
188+
# Test
189+
A link to the [other page](/man/pg1/)
190+
"""
191+
const PG1_JL = raw"""
192+
# # Test literate
193+
# We can include some code like so:
194+
f(x) = x^5
195+
f(5)
196+
"""
197+
const MAKE_JL = raw"""
198+
using Documenter, Literate
199+
src = joinpath(@__DIR__, "src")
200+
lit = joinpath(@__DIR__, "literate")
201+
for (root, _, files) ∈ walkdir(lit), file ∈ files
202+
splitext(file)[2] == ".jl" || continue
203+
ipath = joinpath(root, file)
204+
opath = splitdir(replace(ipath, lit=>src))[1]
205+
Literate.markdown(ipath, opath)
206+
end
207+
makedocs(
208+
sitename = "testlit",
209+
pages = ["Home" => "index.md",
210+
"Other page" => "man/pg1.md"]
211+
)
212+
"""
213+
214+
"""
215+
servedocs_literate_example(dir="servedocs_literate_example")
216+
217+
Generates a folder with the right structure for servedocs+literate example.
218+
You can then `cd` to that folder and use servedocs:
219+
220+
```
221+
julia> using LiveServer
222+
julia> LiveServer.servedocs_literate_example()
223+
julia> cd("servedocs_literate_example")
224+
julia> servedocs(literate=joinpath("docs","literate"))
225+
```
226+
"""
227+
function servedocs_literate_example(dirname="servedocs_literate_example")
228+
isdir(dirname) && rm(dirname, recursive=true)
229+
mkdir(dirname)
230+
# folder structure
231+
src = joinpath(dirname, "src")
232+
mkdir(src)
233+
write(joinpath(src, "$dirname.jl"), "module $dirname\n foo()=1\n end")
234+
docs = joinpath(dirname, "docs")
235+
mkdir(docs)
236+
src = joinpath(docs, "src")
237+
lit = joinpath(docs, "literate")
238+
man = joinpath(lit, "man")
239+
mkdir(src)
240+
mkdir(lit)
241+
mkdir(man)
242+
write(joinpath(src, "index.md"), INDEX_MD)
243+
write(joinpath(man, "pg1.jl"), PG1_JL)
244+
write(joinpath(docs, "make.jl"), MAKE_JL)
245+
return
246+
end

test/utils.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ end
9898
@test isfile("example/index.html")
9999
cd(bk)
100100
end
101+
102+
103+
@testset "utils/servedocs_literate " begin
104+
bk = pwd()
105+
tdir = mktempdir()
106+
cd(tdir)
107+
LiveServer.servedocs_literate_example("test")
108+
@test isdir("test")
109+
@test isfile(joinpath("test", "docs", "literate", "man", "pg1.jl"))
110+
@test isfile(joinpath("test", "docs", "src", "index.md"))
111+
@test isfile(joinpath("test", "src", "test.jl"))
112+
cd(bk)
113+
rm(tdir, recursive=true)
114+
end

0 commit comments

Comments
 (0)