|
| 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 | + ] |
0 commit comments