Skip to content

Commit 7ddb120

Browse files
authored
Merge pull request #27 from shizejin/add_docs
WIP: Add docs using Documenter.jl
2 parents 8ad9d8f + 2c87517 commit 7ddb120

File tree

6 files changed

+229
-1
lines changed

6 files changed

+229
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.jl.cov
22
*.jl.*.cov
33
*.jl.mem
4+
docs/build/
5+
docs/site/

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ os:
44
- linux
55
- osx
66
julia:
7-
- release
7+
- 0.5
88
- nightly
99
matrix:
1010
allow_failures:
@@ -18,3 +18,6 @@ notifications:
1818
#script:
1919
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
2020
# - julia -e 'Pkg.clone(pwd()); Pkg.build("Games"); Pkg.test("Games"; coverage=true)'
21+
after_success:
22+
- julia -e 'Pkg.add("Documenter")'
23+
- julia -e 'cd(Pkg.dir("Games")); include(joinpath("docs", "make.jl"))'

docs/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Generate docs automatically
2+
3+
Run
4+
```
5+
$julia make.jl
6+
```
7+
8+
The generated docs can be accessed in `build/index.html`.
9+
10+
You may modify `Structure` to arrange the structure of the website to be generated. For example:
11+
12+
```
13+
Order: normal_form_game, Computing Nash equilibria, Repeated Game
14+
Computing Nash equilibria: pure_nash, support_enumeration
15+
Repeated Game: repeated_game_util, repeated_game
16+
```
17+
18+
The first line sets the order of pages. You can add a file name if you want it to generate a single page, or the name of section you want to create. In the following you need to declare which files are included in each section. Note that you shouldn't put a comma or dot at the end of each line.
19+
20+
It is not necessary to declare structure for every file included in the Module. Files not mentioned in `Structure` will simply generate a single page automatically.

docs/Structure

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Order: normal_form_game, Computing Nash Equilibria, Repeated Game
2+
Computing Nash Equilibria: pure_nash
3+
Repeated Game: repeated_game_util, repeated_game

docs/auto_doc_gen.jl

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#=
2+
Generate markdown files automatically, for generating
3+
docs using `Documenter.jl`.
4+
5+
@author: Zejin Shi
6+
7+
=#
8+
9+
path = Pkg.dir("Games")
10+
11+
# read the basic structures
12+
order = SubString{String}[]
13+
sections_names = SubString{String}[]
14+
sections = Dict{SubString{String}, Vector{SubString{String}}}()
15+
re = r"(.*): (.*)\n"
16+
open(joinpath(path, "docs/Structure")) do f
17+
for match in eachmatch(re, readstring(f))
18+
list = map(i -> strip(i), split(match.captures[2], ","))
19+
if list ==[""]
20+
continue
21+
end
22+
23+
if match.captures[1] == "Order"
24+
order = list
25+
global order
26+
else
27+
section_name = match.captures[1]
28+
push!(sections_names, section_name)
29+
sections[section_name] = list
30+
end
31+
end
32+
end
33+
34+
# find all files used in Games.jl
35+
re = r"include\(\"(.*)\.jl\"\)"
36+
files = String[]
37+
38+
open(joinpath(path, "src/Games.jl")) do f
39+
for match in eachmatch(re, readstring(f))
40+
push!(files, match.captures[1])
41+
end
42+
end
43+
44+
files_names = Dict{String, String}(
45+
file => replace(join(map(ucfirst, split(file, "_")), " "),
46+
"Util",
47+
"Utilities")
48+
for file in files
49+
)
50+
51+
# generate paths
52+
if !ispath(joinpath(path, "docs/src/lib"))
53+
mkpath(joinpath(path, "docs/src/lib"))
54+
end
55+
56+
# write .md for files not in section
57+
for file in files
58+
if file in vcat(values(sections)...)
59+
continue
60+
end
61+
62+
if !(file in order)
63+
push!(order, file)
64+
end
65+
file_name = files_names[file]
66+
file_page = """
67+
# [$file_name](@id $file)
68+
69+
This is documentation for `$file.jl`.
70+
71+
## Exported
72+
73+
```@autodocs
74+
Modules = [Games]
75+
Pages = ["$file.jl"]
76+
Private = false
77+
```
78+
79+
## Internal
80+
81+
```@autodocs
82+
Modules = [Games]
83+
Pages = ["$file.jl"]
84+
Public = false
85+
```
86+
"""
87+
open(joinpath(path, "docs/src/lib/$file.md"), "w") do f
88+
write(f, file_page)
89+
end
90+
end
91+
92+
# write .md for sections
93+
for section_name in sections_names
94+
if !(section_name in order)
95+
push!(order, section_name)
96+
end
97+
98+
section_files = sections[section_name]
99+
section_name_lower = replace(lowercase(section_name), " ", "_")
100+
section_page = """
101+
# $section_name
102+
103+
This is documentation for $section_name.
104+
"""
105+
open(joinpath(path, "docs/src/lib/$section_name_lower.md"), "w") do f
106+
write(f, section_page)
107+
for file in section_files
108+
file_name = files_names[file]
109+
section_file_page = """
110+
111+
## [$file_name](@id $file)
112+
113+
Documentation for `$file.jl`.
114+
115+
### Exported
116+
```@autodocs
117+
Modules = [Games]
118+
Pages = ["$file.jl"]
119+
Private = false
120+
```
121+
122+
### Internal
123+
```@autodocs
124+
Modules = [Games]
125+
Pages = ["$file.jl"]
126+
Public = false
127+
```
128+
"""
129+
write(f, section_file_page)
130+
end
131+
end
132+
end
133+
134+
# write index page for Liabrary
135+
index = """
136+
# Index
137+
138+
```@index
139+
modules = [Games]
140+
```
141+
"""
142+
143+
open(joinpath(path, "docs/src/lib/index.md"), "w") do f
144+
write(f, index)
145+
end
146+
147+
# write index.md as Homepage
148+
home_page = """
149+
# Games.jl
150+
151+
"""
152+
153+
open(joinpath(path, "docs/src/index.md"), "w") do f
154+
write(f, home_page)
155+
for page in order
156+
if page in keys(sections)
157+
write(f, "$page\n")
158+
for file in sections[page]
159+
file_name = files_names[file]
160+
write(f, "* [$file_name](@ref $file)\n")
161+
end
162+
write(f, "\n")
163+
else
164+
file = page
165+
file_name = files_names[file]
166+
write(f, "[$file_name](@ref $file)\n\n")
167+
end
168+
end
169+
end
170+
171+
pages_names = map(i -> replace(lowercase(i), " ", "_"), order)
172+
173+
PAGES = ["Home" => "index.md",
174+
"Library" => push!(
175+
["lib/$page_name.md"
176+
for page_name in pages_names
177+
],
178+
"lib/index.md"
179+
)
180+
]

docs/make.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Documenter, Games
2+
3+
include("auto_doc_gen.jl")
4+
5+
makedocs(
6+
modules = [Games],
7+
format = :html,
8+
sitename = "Games.jl",
9+
pages = PAGES,
10+
)
11+
12+
deploydocs(
13+
repo = "github.com/QuantEcon/Games.jl.git",
14+
branch = "gh-pages",
15+
target = "build",
16+
osname = "linux",
17+
julia = "0.5",
18+
deps = nothing,
19+
make = nothing,
20+
)

0 commit comments

Comments
 (0)