/
Documents.jl
545 lines (482 loc) · 18.1 KB
/
Documents.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
"""
Defines [`Document`](@ref) and its supporting types
- [`Page`](@ref)
- [`User`](@ref)
- [`Internal`](@ref)
- [`Globals`](@ref)
"""
module Documents
import ..Documenter:
Documenter,
Anchors,
Utilities,
Plugin,
Writer
import ..Documenter.Utilities.Markdown2
using DocStringExtensions
import Markdown
using Unicode
# Pages.
# ------
"""
[`Page`](@ref)-local values such as current module that are shared between nodes in a page.
"""
mutable struct Globals
mod :: Module
meta :: Dict{Symbol, Any}
end
Globals() = Globals(Main, Dict())
"""
Represents a single markdown file.
"""
struct Page
source :: String
build :: String
workdir :: Union{Symbol,String}
"""
Ordered list of raw toplevel markdown nodes from the parsed page contents. This vector
should be considered immutable.
"""
elements :: Vector
"""
Each element in `.elements` maps to an "expanded" element. This may be itself if the
element does not need expanding or some other object, such as a `DocsNode` in the case
of `@docs` code blocks.
"""
mapping :: IdDict{Any,Any}
globals :: Globals
md2ast :: Markdown2.MD
end
function Page(source::AbstractString, build::AbstractString, workdir::AbstractString)
mdpage = Markdown.parse(read(source, String))
md2ast = try
Markdown2.convert(Markdown2.MD, mdpage)
catch err
@error """
Markdown2 conversion error on $(source).
This is a bug — please report this on the Documenter issue tracker
"""
rethrow(err)
end
Page(source, build, workdir, mdpage.content, IdDict{Any,Any}(), Globals(), md2ast)
end
# FIXME -- special overload for Utilities.parseblock
Utilities.parseblock(code::AbstractString, doc, page::Documents.Page; kwargs...) = Utilities.parseblock(code, doc, page.source; kwargs...)
# Document blueprints.
# --------------------
# Should contain all the information that is necessary to build a document.
# Currently has enough information to just run doctests.
struct DocumentBlueprint
pages :: Dict{String, Page} # Markdown files only.
modules :: Set{Module} # Which modules to check for missing docs?
end
# Document Nodes.
# ---------------
## IndexNode.
struct IndexNode
pages :: Vector{String} # Which pages to include in the index? Set by user.
modules :: Vector{Module} # Which modules to include? Set by user.
order :: Vector{Symbol} # What order should docs be listed in? Set by user.
build :: String # Path to the file where this index will appear.
source :: String # Path to the file where this index was written.
elements :: Vector # (object, doc, page, mod, cat)-tuple for constructing links.
function IndexNode(;
# TODO: Fix difference between uppercase and lowercase naming of keys.
# Perhaps deprecate the uppercase versions? Same with `ContentsNode`.
Pages = [],
Modules = [],
Order = [:module, :constant, :type, :function, :macro],
build = error("missing value for `build` in `IndexNode`."),
source = error("missing value for `source` in `IndexNode`."),
others...
)
new(Pages, Modules, Order, build, source, [])
end
end
## ContentsNode.
struct ContentsNode
pages :: Vector{String} # Which pages should be included in contents? Set by user.
depth :: Int # Down to which level should headers be displayed? Set by user.
build :: String # Same as for `IndexNode`s.
source :: String # Same as for `IndexNode`s.
elements :: Vector # (order, page, anchor)-tuple for constructing links.
function ContentsNode(;
Pages = [],
Depth = 2,
build = error("missing value for `build` in `ContentsNode`."),
source = error("missing value for `source` in `ContentsNode`."),
others...
)
new(Pages, Depth, build, source, [])
end
end
## Other nodes
struct MetaNode
dict :: Dict{Symbol, Any}
end
struct MethodNode
method :: Method
visible :: Bool
end
struct DocsNode
docstr :: Any
anchor :: Anchors.Anchor
object :: Utilities.Object
page :: Documents.Page
end
struct DocsNodes
nodes :: Vector{Union{DocsNode,Markdown.Admonition}}
end
struct EvalNode
code :: Markdown.Code
result :: Any
end
struct RawHTML
code::String
end
struct RawNode
name::Symbol
text::String
end
struct MultiOutput
content::Vector
end
# Navigation
# ----------------------
"""
Element in the navigation tree of a document, containing navigation references
to other page, reference to the [`Page`](@ref) object etc.
"""
mutable struct NavNode
"""
`nothing` if the `NavNode` is a non-page node of the navigation tree, otherwise
the string should be a valid key in `doc.blueprint.pages`
"""
page :: Union{String, Nothing}
"""
If not `nothing`, specifies the text that should be displayed in navigation
links etc. instead of the automatically determined text.
"""
title_override :: Union{String, Nothing}
parent :: Union{NavNode, Nothing}
children :: Vector{NavNode}
visible :: Bool
prev :: Union{NavNode, Nothing}
next :: Union{NavNode, Nothing}
end
NavNode(page, title_override, parent) = NavNode(page, title_override, parent, [], true, nothing, nothing)
"""
Constructs a list of the ancestors of the `navnode` (inclding the `navnode` itself),
ordered so that the root of the navigation tree is the first and `navnode` itself
is the last item.
"""
navpath(navnode::NavNode) = navnode.parent === nothing ? [navnode] :
push!(navpath(navnode.parent), navnode)
# Inner Document Fields.
# ----------------------
"""
User-specified values used to control the generation process.
"""
struct User
root :: String # An absolute path to the root directory of the document.
source :: String # Parent directory is `.root`. Where files are read from.
build :: String # Parent directory is also `.root`. Where files are written to.
workdir :: Union{Symbol,String} # Parent directory is also `.root`. Where code is executed from.
format :: Vector{Writer} # What format to render the final document with?
clean :: Bool # Empty the `build` directory before starting a new build?
doctest :: Union{Bool,Symbol} # Run doctests?
linkcheck::Bool # Check external links..
linkcheck_ignore::Vector{Union{String,Regex}} # ..and then ignore (some of) them.
linkcheck_timeout::Real # ..but only wait this many seconds for each one.
checkdocs::Symbol # Check objects missing from `@docs` blocks. `:none`, `:exports`, or `:all`.
doctestfilters::Vector{Regex} # Filtering for doctests
strict::Bool # Throw an exception when any warnings are encountered.
pages :: Vector{Any} # Ordering of document pages specified by the user.
expandfirst::Vector{String} # List of pages that get "expanded" before others
repo :: String # Template for URL to source code repo
sitename:: String
authors :: String
version :: String # version string used in the version selector by default
highlightsig::Bool # assume leading unlabeled code blocks in docstrings to be Julia.
end
"""
Private state used to control the generation process.
"""
struct Internal
assets :: String # Path where asset files will be copied to.
remote :: String # The remote repo on github where this package is hosted.
navtree :: Vector{NavNode} # A vector of top-level navigation items.
navlist :: Vector{NavNode} # An ordered list of `NavNode`s that point to actual pages
headers :: Anchors.AnchorMap # See `modules/Anchors.jl`. Tracks `Markdown.Header` objects.
docs :: Anchors.AnchorMap # See `modules/Anchors.jl`. Tracks `@docs` docstrings.
bindings:: IdDict{Any,Any} # Tracks insertion order of object per-binding.
objects :: IdDict{Any,Any} # Tracks which `Utilities.Objects` are included in the `Document`.
contentsnodes :: Vector{ContentsNode}
indexnodes :: Vector{IndexNode}
locallinks :: Dict{Markdown.Link, String}
errors::Set{Symbol}
end
# Document.
# ---------
"""
Represents an entire document.
"""
struct Document
user :: User # Set by the user via `makedocs`.
internal :: Internal # Computed values.
plugins :: Dict{DataType, Plugin}
blueprint :: DocumentBlueprint
end
function Document(plugins = nothing;
root :: AbstractString = Utilities.currentdir(),
source :: AbstractString = "src",
build :: AbstractString = "build",
workdir :: Union{Symbol, AbstractString} = :build,
format :: Any = Documenter.HTML(),
clean :: Bool = true,
doctest :: Union{Bool,Symbol} = true,
linkcheck:: Bool = false,
linkcheck_ignore :: Vector = [],
linkcheck_timeout :: Real = 10,
checkdocs::Symbol = :all,
doctestfilters::Vector{Regex}= Regex[],
strict::Bool = false,
modules :: Utilities.ModVec = Module[],
pages :: Vector = Any[],
expandfirst :: Vector = String[],
repo :: AbstractString = "",
sitename :: AbstractString = "",
authors :: AbstractString = "",
version :: AbstractString = "",
highlightsig::Bool = true,
others...
)
Utilities.check_kwargs(others)
if !isa(format, AbstractVector)
format = Writer[format]
end
if version == "git-commit"
version = "git:$(Utilities.get_commit_short(root))"
end
user = User(
root,
source,
build,
workdir,
format,
clean,
doctest,
linkcheck,
linkcheck_ignore,
linkcheck_timeout,
checkdocs,
doctestfilters,
strict,
pages,
expandfirst,
repo,
sitename,
authors,
version,
highlightsig
)
internal = Internal(
Utilities.assetsdir(),
Utilities.getremote(root),
[],
[],
Anchors.AnchorMap(),
Anchors.AnchorMap(),
IdDict{Any,Any}(),
IdDict{Any,Any}(),
[],
[],
Dict{Markdown.Link, String}(),
Set{Symbol}()
)
plugin_dict = Dict{DataType, Plugin}()
if plugins !== nothing
for plugin in plugins
plugin isa Plugin ||
throw(ArgumentError("$(typeof(plugin)) is not a subtype of `Documenter.Plugin`."))
haskey(plugin_dict, typeof(plugin)) &&
throw(ArgumentError("only one copy of $(typeof(plugin)) may be passed."))
plugin_dict[typeof(plugin)] = plugin
end
end
blueprint = DocumentBlueprint(
Dict{String, Page}(),
Utilities.submodules(modules),
)
Document(user, internal, plugin_dict, blueprint)
end
"""
getplugin(doc::Document, T)
Retrieves the [`Plugin`](@ref Documenter.Plugin) type for `T` stored in `doc`. If `T` was passed to
[`makedocs`](@ref Documenter.makedocs), the passed type will be returned. Otherwise, a new `T` object
will be created using the default constructor `T()`.
"""
function getplugin(doc::Document, plugin_type::Type{T}) where T <: Plugin
if !haskey(doc.plugins, plugin_type)
doc.plugins[plugin_type] = plugin_type()
end
doc.plugins[plugin_type]
end
## Methods
function addpage!(doc::Document, src::AbstractString, dst::AbstractString, wd::AbstractString)
page = Page(src, dst, wd)
# page's identifier is the path relative to the `doc.user.source` directory
name = normpath(relpath(src, doc.user.source))
doc.blueprint.pages[name] = page
end
"""
$(SIGNATURES)
Populates the `ContentsNode`s and `IndexNode`s of the `document` with links.
This can only be done after all the blocks have been expanded (and nodes constructed),
because the items have to exist before we can gather the links to those items.
"""
function populate!(document::Document)
for node in document.internal.contentsnodes
populate!(node, document)
end
for node in document.internal.indexnodes
populate!(node, document)
end
end
function populate!(index::IndexNode, document::Document)
# Filtering valid index links.
for (object, doc) in document.internal.objects
page = relpath(doc.page.build, dirname(index.build))
mod = object.binding.mod
# Include *all* signatures, whether they are `Union{}` or not.
cat = Symbol(lowercase(Utilities.doccat(object.binding, Union{})))
if _isvalid(page, index.pages) && _isvalid(mod, index.modules) && _isvalid(cat, index.order)
push!(index.elements, (object, doc, page, mod, cat))
end
end
# Sorting index links.
pagesmap = precedence(index.pages)
modulesmap = precedence(index.modules)
ordermap = precedence(index.order)
comparison = function(a, b)
(x = _compare(pagesmap, 3, a, b)) == 0 || return x < 0 # page
(x = _compare(modulesmap, 4, a, b)) == 0 || return x < 0 # module
(x = _compare(ordermap, 5, a, b)) == 0 || return x < 0 # category
string(a[1].binding) < string(b[1].binding) # object name
end
sort!(index.elements, lt = comparison)
return index
end
function populate!(contents::ContentsNode, document::Document)
# Filtering valid contents links.
for (id, filedict) in document.internal.headers.map
for (file, anchors) in filedict
for anchor in anchors
page = relpath(anchor.file, dirname(contents.build))
if _isvalid(page, contents.pages) && Utilities.header_level(anchor.object) ≤ contents.depth
push!(contents.elements, (anchor.order, page, anchor))
end
end
end
end
# Sorting contents links.
pagesmap = precedence(contents.pages)
comparison = function(a, b)
(x = _compare(pagesmap, 2, a, b)) == 0 || return x < 0 # page
a[1] < b[1] # anchor order
end
sort!(contents.elements, lt = comparison)
return contents
end
# some replacements for jldoctest blocks
function doctest_replace!(doc::Documents.Document)
for (src, page) in doc.blueprint.pages
empty!(page.globals.meta)
for element in page.elements
page.globals.meta[:CurrentFile] = page.source
walk(page.globals.meta, page.mapping[element]) do block
doctest_replace!(block)
end
end
end
end
function doctest_replace!(block::Markdown.Code)
startswith(block.language, "jldoctest") || return false
# suppress output for `#output`-style doctests with `output=false` kwarg
if occursin(r"^# output$"m, block.code) && occursin(r";.*output\h*=\h*false", block.language)
input = first(split(block.code, "# output\n", limit = 2))
block.code = rstrip(input)
end
# correct the language field
block.language = occursin(r"^julia> "m, block.code) ? "julia-repl" : "julia"
return false
end
doctest_replace!(block) = true
## Utilities.
function buildnode(T::Type, block, doc, page)
mod = get(page.globals.meta, :CurrentModule, Main)
dict = Dict{Symbol, Any}(:source => page.source, :build => page.build)
for (ex, str) in Utilities.parseblock(block.code, doc, page)
if Utilities.isassign(ex)
cd(dirname(page.source)) do
dict[ex.args[1]] = Core.eval(mod, ex.args[2])
end
end
end
T(; dict...)
end
function _compare(col, ind, a, b)
x, y = a[ind], b[ind]
haskey(col, x) && haskey(col, y) ? _compare(col[x], col[y]) : 0
end
_compare(a, b) = a < b ? -1 : a == b ? 0 : 1
_isvalid(x, xs) = isempty(xs) || x in xs
precedence(vec) = Dict(zip(vec, 1:length(vec)))
##############################################
# walk (previously in the Walkers submodule) #
##############################################
"""
$(SIGNATURES)
Calls `f` on `element` and any of its child elements. `meta` is a `Dict` containing metadata
such as current module.
"""
walk(f, meta, element) = (f(element); nothing)
# Change to the docstring's defining module if it has one. Change back afterwards.
function walk(f, meta, block::Markdown.MD)
tmp = get(meta, :CurrentModule, nothing)
mod = get(block.meta, :module, nothing)
mod ≡ nothing || (meta[:CurrentModule] = mod)
f(block) && walk(f, meta, block.content)
tmp ≡ nothing ? delete!(meta, :CurrentModule) : (meta[:CurrentModule] = tmp)
nothing
end
function walk(f, meta, block::Vector)
for each in block
walk(f, meta, each)
end
end
const MDContentElements = Union{
Markdown.BlockQuote,
Markdown.Paragraph,
Markdown.MD,
}
walk(f, meta, block::MDContentElements) = f(block) ? walk(f, meta, block.content) : nothing
const MDTextElements = Union{
Markdown.Bold,
Markdown.Header,
Markdown.Italic,
}
walk(f, meta, block::MDTextElements) = f(block) ? walk(f, meta, block.text) : nothing
walk(f, meta, block::Markdown.Footnote) = f(block) ? walk(f, meta, block.text) : nothing
walk(f, meta, block::Markdown.Admonition) = f(block) ? walk(f, meta, block.content) : nothing
walk(f, meta, block::Markdown.Image) = f(block) ? walk(f, meta, block.alt) : nothing
walk(f, meta, block::Markdown.Table) = f(block) ? walk(f, meta, block.rows) : nothing
walk(f, meta, block::Markdown.List) = f(block) ? walk(f, meta, block.items) : nothing
walk(f, meta, block::Markdown.Link) = f(block) ? walk(f, meta, block.text) : nothing
walk(f, meta, block::RawHTML) = nothing
walk(f, meta, block::DocsNodes) = walk(f, meta, block.nodes)
walk(f, meta, block::DocsNode) = walk(f, meta, block.docstr)
walk(f, meta, block::EvalNode) = walk(f, meta, block.result)
walk(f, meta, block::MetaNode) = (merge!(meta, block.dict); nothing)
walk(f, meta, block::Anchors.Anchor) = walk(f, meta, block.object)
end