/
Documenter.jl
299 lines (249 loc) · 7.03 KB
/
Documenter.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
### A Nbdev script file ###
### Autogenerated file. Don't modify. ###
module Documenter
#export
using Base.Docs
#export
using TOML
#export
using Markdown
#export
using Distributed
#export
using ProgressMeter
#export
include("../src/Export.jl")
#export
include("../src/CodeRunner.jl")
#export
include("../src/Common.jl")
#export
import Pluto: Notebook, Cell, load_notebook_nobackup
#export
begin
"""
> struct Section--> This is like the section of a page and is made up of one or multiple lines.
> * Fields:
> * line--> String which makes up a section
"""
Base.@kwdef mutable struct Section
line::String=""
end
"""
> Section(line)--> Helper function to call the constructor of the type Section
"""
Section(line)=Section(line=line)
"""
> line(section::Section)--> Getter for accessing the underlying field line.
"""
line(section::Section)=section.line
end
#export
begin
"""
> Page--> The type that represents a page in a document.
> * Fields:
> * sections--> Array of Section type.
> * name--> Name of the module being documented.
"""
struct Page
sections::Array{Section,1}
name::AbstractString
end
"""
> Page--> Helper function to create constructer for Page type.
"""
Page(sections, path)=Page(sections=sections, path=path)
"""
> sections--> Getter for accessing the underlying field sections of Page.
"""
sections(p::Page)=p.sections
"""
> name--> Getter for accessing the underlying field name of Page.
"""
name(p::Page)=p.name
end
#export
"""
> runandupdatenb(fn::AbstractString)--> Run the notebook in the supplied path and update the notebook with the output of each cell.
"""
function runandupdatenb(fn::AbstractString)
nb=load_notebook_nobackup(fn)
return CodeRunner.executecode(nb)
end
#export
begin
"""
> struct Functiondocs--> Stores the document of different objects.
> * funcdocs--> Array of strings.
"""
mutable struct Functiondocs
funcdocs::Array{String, 1}
end
"""
> Functiondocs(funcdocs)--> Helper for accessing the FunctionDocs constructer.
"""
Functiondocs(funcdocs)=Functiondocs(funcdocs)
end
#export
begin
"""
> stitchcode(cell::Cell)--> Stitches the code in a Pluto notebook cell with the output of that code. The output is a code block.
"""
function stitchcode(cell::AbstractArray)
op=' '
if length(cell) > 1
op=values(cell[2])
end
string("```","\n$(cell[1])\n","------\nOutput\n------\n","$(op)\n", "```\n")
end
"""
> stitchcode(cellop::AbstractString)--> Removes the quotes from a string and creates a code block with that string inside the newely formed code block
"""
function stitchcode(cellop::AbstractString)
cleanedop=Export.strip(Export.strip(cellop,"\""), "\"")
string("```","\n$cleanedop\n","```\n")
end
"""
> stitchcode(fdocs::Functiondocs)--> When supplied with a FunctionDocs type, stitchCode appends together the object docstrings and generates documentation for that particular object
"""
function stitchcode(fdocs::Functiondocs)
funcdocs=""
for fdoc in fdocs.funcdocs
funcdocs=string(funcdocs, "$(fdoc)\n\n")
end
funcdocs
end
end
#export
"""
> collectfuncdocs(obj)--> Collects objects (functions, methods, macro structs etc.) and creates an array of documents (generated from the docstrings). Creates aFunctionDocs type from these documents.
"""
function collectfuncdocs(obj)
docs=doc(obj)
searchurl = "https://github-link.vercel.app/api?ghUrl="
giturl = Common.getsettings(joinpath("..", "Project.toml"), ["settings", "github_url"])
pattern = r"\\(.+\\)*(.+)(\.jl)"
fn = uppercasefirst(Export.strip(basename(join(match(pattern, string(first(methods(obj)))).captures)), r"[0-9_]"))
searchurl = "$searchurl$giturl/blob/master/src/$fn&q=$(string(nameof(obj)))"
fdocs=["$(docs.meta[:results][i].object) [source]($searchurl)" for i=1:length(docs.meta[:results])]
Functiondocs(fdocs)
end
#export
begin
"""
> showdoc(o)--> Takes an object and builds markdown documentation.
"""
function showdoc(o)
docs=collectfuncdocs(o)
stitchcode(docs)
end
end
#export
begin
function maintainheading(str:: AbstractString)
res= nothing
if startswith(str, """<div class="markdown"><h2>""")
res = Export.strip(Export.strip(Export.strip(str, """<div class="markdown"><h2>"""), """</h2>"""), """</div>""")
res = string("## ", res)
else
res = str
end
res
end
"""
> CreatePage--> Creates the Page type from the markdown and example code cells of the supplied notebook. The filename is the name of the notebook which is parsed.
"""
function createpage(fn::AbstractString, nb::Notebook)
sections=Section[]
res=nothing
for cell in nb.cells
if cell.errored
error("Build stopped. Seems like the code $(cell.code) has an error")
break
end
if startswith(cell.code, "md")
cleanop = maintainheading(cell.output.body)
push!(sections, Section(cleanop))
elseif !startswith(cell.code, "#export") && !startswith(cell.code, "#hide")
if occursin( "showdoc", cell.code)
cleanedop=Export.strip(cell.output.body, "\"")
cleanedop=replace(cleanedop, "\\n"=>"\n")
push!(sections, Section(cleanedop))
elseif occursin( "#noop", cell.code)
stitchedcode=stitchcode([cell.code])
push!(sections, Section(stitchedcode))
else
stitchedcode=stitchcode([cell.code, cell.output.body])
push!(sections, Section(stitchedcode))
end
end
end
if !isempty(sections)
res=Page(sections, basename(fn))
end
return res
end
end
#export
begin
"""
> savepage(io, page::Page)--> Take the contents from a Page type and write to the io
"""
function savepage(io, page::Page)
pageheading=uppercasefirst(Export.strip(Export.strip(page.name, r"[0-9_]"), r".jl"))
println(io, "<h1>$pageheading</h1>")
for section in page.sections
println(io, section.line)
end
end
"""
> savepage(page::Page, path::String)--> Given a Page type and the required path, creates the related markdwon file in the specified path. The name of the resulting markdown file is same as the nameof the notebook for which the document is generated
"""
function savepage(page::Page, path::String)
fn=lowercase(Export.strip(Export.strip(page.name, r"[0-9_]"), r".jl"))
open(joinpath(path, fn*".md"), "w") do io
savepage(io, page)
end
end
end
#export
begin
"""
> export2md(fn::String, path::String)--> Generate document for a file in the given path
"""
function export2md(fn::String, path::String)
nb=runandupdatenb(fn)
page=createpage(fn, nb)
if page != nothing
savepage(page, path)
end
end
"""
> export2md(fns::AbstractVector, path::String)--> Map the `export2md(file, path)` to a given vector of file.
"""
function export2md(fns::AbstractVector, path::String)
n = length(fns)
p = Progress(n, 1)
for fn in fns
next!(p)
export2md(fn, path)
end
end
"""
> export2md()--> Higher level API to generate documents for all the valid notebooks
"""
export2md(nbsdir)=export2md(Export.readfilenames(joinpath(pwd(), nbsdir)), "docs")
end
#export
export showdoc, export2md
#export
begin
"""
> export2readme()--> create readme from the contents of Index notebook
"""
function export2readme()
cp(normpath(joinpath(".", "docs/index.md")), normpath(joinpath(".", "README.md")), force=true)
end
end
end