forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.jl
361 lines (285 loc) · 7.48 KB
/
docs.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
module Docs
import Base.Markdown: @doc_str, @doc_mstr, MD
export doc, @doc
# Basic API / Storage
const modules = Module[]
meta() = current_module().META
macro init ()
META = esc(:META)
quote
if !isdefined(:META)
const $META = ObjectIdDict()
doc($META, doc"Documentation metadata for $(string(current_module())).")
push!(modules, current_module())
nothing
end
end
end
function doc(obj, data)
meta()[obj] = data
end
function doc(obj)
for mod in modules
haskey(mod.META, obj) && return mod.META[obj]
end
end
function doc(obj::Union(Symbol, AbstractString))
doc(current_module().(symbol(obj)))
end
# Function / Method support
function newmethod(defs)
keylen = -1
key = nothing
for def in defs
length(def.sig) > keylen && (keylen = length(def.sig); key = def)
end
return key
end
function newmethod(funcs, f)
applicable = Method[]
for def in methods(f)
(!haskey(funcs, def) || funcs[def] != def.func) && push!(applicable, def)
end
return newmethod(applicable)
end
function trackmethod(def)
name = uncurly(unblock(def).args[1].args[1])
f = esc(name)
quote
if $(isexpr(name, Symbol)) && isdefined($(Expr(:quote, name))) && isgeneric($f)
funcs = [def => def.func for def in methods($f)]
$(esc(def))
$f, newmethod(funcs, $f)
else
$(esc(def))
$f, newmethod(methods($f))
end
end
end
type FuncDoc
order::Vector{Method}
meta::Dict{Method, Any}
source::Dict{Method, Any}
end
FuncDoc() = FuncDoc(Method[], Dict(), Dict())
getset(coll, key, default) = coll[key] = get(coll, key, default)
function doc(f::Function, m::Method, data, source)
fd = getset(meta(), f, FuncDoc())
isa(fd, FuncDoc) || error("Can't document a method when the function already has metadata")
!haskey(fd.meta, m) && push!(fd.order, m)
fd.meta[m] = data
fd.source[m] = source
end
function doc(f::Function)
docs = []
for mod in modules
if haskey(mod.META, f)
fd = mod.META[f]
if isa(fd, FuncDoc)
for m in fd.order
push!(docs, fd.meta[m])
end
elseif length(docs) == 0
return fd
end
end
end
return catdoc(docs...)
end
function doc(f::Function, m::Method)
for mod in modules
haskey(mod.META, f) && isa(mod.META[f], FuncDoc) && haskey(mod.META[f].meta, m) &&
return mod.META[f].meta[m]
end
end
catdoc() = nothing
catdoc(xs...) = [xs...]
# Macros
isexpr(x::Expr, ts...) = x.head in ts
isexpr{T}(x::T, ts...) = T in ts
function unblock(ex)
isexpr(ex, :block) || return ex
exs = filter(ex->!isexpr(ex, :line), ex.args)
length(exs) == 1 || return ex
return exs[1]
end
uncurly(ex) = isexpr(ex, :curly) ? ex.args[1] : ex
namify(ex::Expr) = namify(ex.args[1])
namify(sy::Symbol) = sy
function mdify(ex)
if isa(ex, AbstractString)
:(@doc_str $(esc(ex)))
elseif isexpr(ex, :macrocall) && namify(ex) == symbol("@mstr")
:(@doc_mstr $(esc(ex.args[2])))
else
esc(ex)
end
end
function namedoc(meta, def, name)
quote
@init
$(esc(def))
doc($(esc(name)), $(mdify(meta)))
nothing
end
end
function funcdoc(meta, def)
quote
@init
f, m = $(trackmethod(def))
doc(f, m, $(mdify(meta)), $(esc(Expr(:quote, def))))
f
end
end
function objdoc(meta, def)
quote
@init
f = $(esc(def))
doc(f, $(mdify(meta)))
f
end
end
fexpr(ex) = isexpr(ex, :function) || (isexpr(ex, :(=)) && isexpr(ex.args[1], :call))
function docm(meta, def)
def′ = unblock(def)
isexpr(def′, :macro) && return namedoc(meta, def, symbol("@", namify(def′)))
isexpr(def′, :type) && return namedoc(meta, def, namify(def′.args[2]))
isexpr(def′, :abstract) && return namedoc(meta, def, namify(def′))
fexpr(def′) && return funcdoc(meta, def)
isexpr(def, :macrocall) && (def = namify(def))
return objdoc(meta, def)
end
function docm(ex)
isexpr(ex, :->) && return docm(ex.args...)
isexpr(ex, :call) && return :(doc($(esc(ex.args[1])), @which $(esc(ex))))
isexpr(ex, :macrocall) && (ex = namify(ex))
:(doc($(esc(ex))))
end
macro doc (args...)
docm(args...)
end
# Metametadata
@doc """
# Documentation
The `@doc` macro can be used to both set and retrieve documentation /
metadata. By default, documentation is written as Markdown, but any
object can be placed before the arrow. For example:
@doc \"""
# The Foo Function
`foo(x)`: Foo the living hell out of `x`.
\""" ->
function foo() ...
The `->` is not required if the object is on the same line, e.g.
@doc "foo" foo
# Retrieving Documentation
You can retrieve docs for functions, macros and other objects as
follows:
@doc foo
@doc @time
@doc md""
# Functions & Methods
Placing documentation before a method definition (e.g. `function foo()
...` or `foo() = ...`) will cause that specific method to be
documented, as opposed to the whole function. Method docs are
concatenated together in the order they were defined to provide docs
for the function.
""" @doc
@doc "`doc(obj)`: Get the doc metadata for `obj`." doc
@doc """
`catdoc(xs...)`: Combine the documentation metadata `xs` into a single meta object.
""" catdoc
# Text / HTML objects
import Base: print, writemime
export HTML, @html_str, @html_mstr
export HTML, Text
@doc """
`HTML(s)`: Create an object that renders `s` as html.
HTML("<div>foo</div>")
You can also use a stream for large amounts of data:
HTML() do io
println(io, "<div>foo</div>")
end
""" ->
type HTML{T}
content::T
end
function HTML(xs...)
HTML() do io
for x in xs
writemime(io, MIME"text/html"(), x)
end
end
end
writemime(io::IO, ::MIME"text/html", h::HTML) = print(io, h.content)
writemime(io::IO, ::MIME"text/html", h::HTML{Function}) = h.content(io)
@doc "Create an `HTML` object from a literal string." ->
macro html_str (s)
:(HTML($s))
end
@doc (@doc html"") ->
macro html_mstr (s)
:(HTML($(Base.triplequoted(s))))
end
function catdoc(xs::HTML...)
HTML() do io
for x in xs
writemime(io, MIME"text/html"(), x)
end
end
end
export Text, @text_str, @text_mstr
# @doc """
# `Text(s)`: Create an object that renders `s` as plain text.
# HTML("foo")
# You can also use a stream for large amounts of data:
# Text() do io
# println(io, "foo")
# end
# """ ->
type Text{T}
content::T
end
print(io::IO, t::Text) = print(io, t.content)
print(io::IO, t::Text{Function}) = t.content(io)
writemime(io::IO, ::MIME"text/plain", t::Text) = print(io, t)
@doc "Create a `Text` object from a literal string." ->
macro text_str (s)
:(Text($s))
end
@doc (@doc text"") ->
macro text_mstr (s)
:(Text($(Base.triplequoted(s))))
end
function catdoc(xs::Text...)
Text() do io
for x in xs
writemime(io, MIME"text/plain"(), x)
end
end
end
# MD support
catdoc(md::MD...) = MD(md...)
# REPL help
const intro = doc"""
**Welcome to Julia $(string(VERSION)).** The full manual is available at
http:https://docs.julialang.org/
as well many great tutorials and learning resources:
http:https://julialang.org/learning/
For help on a specific function or macro, type `?` followed
by its name, e.g. `?fft`, `?@time` or `?html""`, and press
enter.
You can also use `apropos("...")` to search the documentation.
"""
function replhelp(ex)
if ex === :? || ex === :help
return intro
else
quote
# Backwards-compatible with the previous help system, for now
let doc = @doc $(esc(ex))
doc ≠ nothing ? doc : Base.Help.@help_ $(esc(ex))
end
end
end
end
end