This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
documentation.jl
232 lines (197 loc) · 6.64 KB
/
documentation.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
# ==========================================================
# Help functions
# help function defaults to STDOUT output when io is not specified
"""
help(func[; extended = false])
Welcome to the main help function of `Makie.jl` / `AbstractPlotting.jl`.
For help on a specific function's arguments, type `help_arguments(function_name)`.
For help on a specific function's attributes, type `help_attributes(plot_Type)`.
Use the optional `extended = true` keyword argument to see more details.
"""
help(func; kw_args...) = help(stdout, func; kw_args...) #defaults to STDOUT
function help(io::IO, input::Type{T}; extended = false) where T <: AbstractPlot
buffer = IOBuffer()
_help(buffer, input; extended = extended)
Markdown.parse(String(take!(buffer)))
end
function help(io::IO, input::Function; extended = false)
buffer = IOBuffer()
_help(buffer, to_type(input); extended = extended)
Markdown.parse(String(take!(buffer)))
end
# Internal help functions
function _help(io::IO, input::Type{T}; extended = false) where T <: AbstractPlot
func = to_func(input)
str = to_string(input)
# Print docstrings
println(io, Base.Docs.doc(func))
# Arguments
help_arguments(io, func)
if extended
println(io, "Please refer to [`convert_arguments`](@ref) to find the full list of accepted arguments\n")
end
# Keyword arguments
help_attributes(io, input; extended = extended)
if extended
println(io, "You can see usage examples of `$func` by running:\n")
println(io, "`example_database($func)`\n")
end
end
function _help(io::IO, input::Function; extended = false)
_help(io, to_type(input); extended = extended)
end
"""
help_arguments([io], func)
Returns a list of signatures for function `func`.
"""
help_arguments(x) = help_arguments(stdout, x)
# Other help functions
function help_arguments(io::IO, x::Function)
@warn("argument help isn't currently implemented correctly")
#TODO: will need to parse what arguments from convert_arguments apply to x
println(io, "`$x` has the following function signatures: \n")
println(io, "```")
println(io, " ", "(Vector, Vector)")
println(io, " ", "(Vector, Vector, Vector)")
println(io, " ", "(Matrix)")
println(io, "```")
end
"""
help_attributes([io], Union{PlotType, PlotFunction}; extended = false)
Returns a list of attributes for the plot type `Typ`.
The attributes returned extend those attributes found in the `default_theme`.
Use the optional keyword argument `extended` (default = `false`) to show
in addition the default values of each attribute.
usage:
```example
>help_attributes(scatter)
alpha
color
colormap
colorrange
distancefield
glowcolor
glowwidth
linewidth
marker
marker_offset
markersize
overdraw
rotations
strokecolor
strokewidth
transform_marker
transparency
uv_offset_width
visible
```
"""
help_attributes(x; kw...) = help_attributes(stdout, x; kw...)
function help_attributes(io::IO, Typ::Type{T}; extended = false) where T <: AbstractPlot
# get and sort list of attributes from function (using Scatter as an example)
# this is a symbolic dictionary, with symbols as the keys
attributes = default_theme(nothing, Typ)
# manually filter out some attributes
filter_keys = Symbol.([:fxaa, :model, :transformation])
# count the character length of the longest key
longest = 0
allkeys = sort(collect(keys(attributes)))
for k in allkeys
currentlength = length(string(k))
if currentlength > longest
longest = currentlength
end
end
extra_padding = 2
# increase verbosity if extended kwarg is on
if extended
println(io, "Available attributes and their defaults for `$Typ` are: \n")
else
println(io, "Available attributes for `$Typ` are: \n")
end
println(io, "```")
if extended
for attribute in allkeys
value = attributes[attribute]
if !(attribute in filter_keys)
padding = longest - length(string(attribute)) + extra_padding
print(io, " ", attribute, " "^padding)
show(io, isnothing(AbstractPlotting.to_value(value)) ? "nothing" : to_value(value))
print(io, "\n")
end
end
else
for attribute in allkeys
value = attributes[attribute]
if !(attribute in filter_keys)
println(io, " ", attribute)
end
end
end
println(io, "```")
end
function help_attributes(io::IO, func::Function; extended = false)
help_attributes(io, to_type(func); extended = extended)
end
function help_attributes(io::IO, Typ::Type{T}; extended = false) where T <: Union{Axis2D, Axis3D}
if extended
println(io, "Axis attributes and their defaults for `$Typ` are: \n")
else
println(io, "Axis attributes for `$Typ` are: \n")
end
attributes = default_theme(nothing, Typ)
println(io, "```")
print_rec(io, attributes, 1; extended = extended)
println(io, "```")
end
# ==========================================================
# Supporting functions for the help functions
"""
to_func(Typ)
Maps the input of a Type name to its cooresponding function.
"""
function to_func(Typ::Type{<: AbstractPlot{F}}) where F
F
end
to_func(func::Function) = func
"""
to_type(func)
Maps the input of a function name to its cooresponding Type.
"""
to_type(func::Function) = Combined{func}
to_type(Typ::Type{T}) where T <: AbstractPlot = Typ
"""
to_string(func)
Turns the input of a function name or plot Type into a string.
"""
function to_string(func::Function)
str = string(typeof(func).name.mt.name)
end
to_string(Typ::Type{T}) where T <: AbstractPlot = to_string(to_func(Typ))
to_string(s::Symbol) = string(s)
to_string(s::String) = s
"""
print_rec(io::IO, dict, indent::Int = 1[; extended = false])
Traverses a dictionary `dict` and recursively print out its keys and values
in a nicely-indented format.
Use the optional `extended = true` keyword argument to see more details.
"""
function print_rec(io::IO, dict, indent::Int = 1; extended = false)
for (k, v) in dict
print(io, " "^(indent*4), k)
if isa(to_value(v), AbstractPlotting.Attributes)
print(io, ": ")
println(io)
print_rec(io, v.attributes, indent + 1; extended = extended)
elseif isa(v, Node)
if extended
print(io, ": ")
println(io, isnothing(to_value(v)) ? "nothing" : to_value(v))
else
println(io)
end
else
println(io, v)
end
end
end