Source file doc.ml
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
module Map = Map.Make (struct
type t = string
let compare (x : string) (y : string) = compare x y
end)
(** Documentation for plugs. *)
module Plug = struct
type t = {
name : string;
description : string;
mutable items : (string * string) list;
(** an item with given name and description *)
}
let db = ref []
let create ~doc name =
let d = { name; description = doc; items = [] } in
db := d :: !db;
d
let add d ~doc name =
assert (not (List.mem_assoc name d.items));
d.items <- (name, doc) :: d.items
let db () = List.sort compare !db
let print_md print =
List.iter
(fun p ->
Printf.ksprintf print "# %s\n\n%s\n\n" p.name p.description;
List.iter
(fun (name, description) ->
print ("- " ^ name ^ ": " ^ description ^ "\n"))
p.items;
print "\n")
(db ())
let print_string = print_md
end
(** Documentation for protocols. *)
module Protocol = struct
type t = {
name : string;
description : string;
syntax : string;
static : bool;
}
let db = ref []
let add ~name ~doc ~syntax ~static =
let p = { name; description = doc; syntax; static } in
db := p :: !db
let db () = List.sort compare !db
let print_md print =
List.iter
(fun p ->
let static = if p.static then " This protocol is static." else "" in
Printf.ksprintf print "### %s\n\n%s\n\nThe syntax is `%s`.%s\n\n" p.name
p.description p.syntax static)
(db ())
end
(** Documenentation for values. *)
module Value = struct
(** Documentation flags. *)
type flag = [ `Hidden | `Deprecated | `Experimental | `Extra ]
let string_of_flag : flag -> string = function
| `Hidden -> "hidden"
| `Deprecated -> "deprecated"
| `Experimental -> "experimental"
| `Extra -> "extra"
let flag_of_string = function
| "hidden" -> Some `Hidden
| "deprecated" -> Some `Deprecated
| "experimental" -> Some `Experimental
| "extra" -> Some `Extra
| _ -> None
(** Kind of source. *)
type source =
[ `Input
| `Output
| `Conversion
| `FFmpegFilter
| `Track
| `Audio
| `Video
| `MIDI
| `Visualization
| `Synthesis
| `Testing
| `Fade
| `Liquidsoap ]
type category =
[ `Source of source
| `Track of source
| `System
| `File
| `Math
| `String
| `List
| `Bool
| `Getter
| `Time
| `Liquidsoap
| `Metadata
| `Programming
| `Interaction
| `Internet
| `Configuration
| `Settings
| `None ]
let categories : (category * string) list =
[
(`Source `Input, "Source / Input");
(`Source `Output, "Source / Output");
(`Source `Conversion, "Source / Conversion");
(`Source `FFmpegFilter, "Source / FFmpeg filter");
(`Source `Track, "Source / Track processing");
(`Source `Audio, "Source / Audio processing");
(`Source `Video, "Source / Video processing");
(`Source `MIDI, "Source / MIDI processing");
(`Source `Synthesis, "Source / Sound synthesis");
(`Source `Visualization, "Source / Visualization");
(`Source `Liquidsoap, "Source / Liquidsoap");
(`Source `Fade, "Source / Fade");
(`Source `Testing, "Source / Testing");
(`Track `Input, "Track / Input");
(`Track `Output, "Track / Output");
(`Track `Conversion, "Track / Conversion");
(`Track `FFmpegFilter, "Track / FFmpeg filter");
(`Track `Track, "Track / Track processing");
(`Track `Audio, "Track / Audio processing");
(`Track `Video, "Track / Video processing");
(`Track `MIDI, "Track / MIDI processing");
(`Track `Synthesis, "Track / Sound synthesis");
(`Track `Visualization, "Track / Visualization");
(`Track `Liquidsoap, "Track / Liquidsoap");
(`Track `Fade, "Track / Fade");
(`System, "System");
(`Configuration, "Configuration");
(`Settings, "Settings");
(`File, "File");
(`Math, "Math");
(`String, "String");
(`List, "List");
(`Bool, "Bool");
(`Getter, "Getter");
(`Liquidsoap, "Liquidsoap");
(`Metadata, "Metadata");
(`Programming, "Programming");
(`Interaction, "Interaction");
(`Internet, "Internet");
(`Time, "Time");
(`None, "Uncategorized");
]
let string_of_category c = List.assoc c categories
let category_of_string s =
let rec aux = function
| (c, s') :: _ when s = s' -> Some c
| _ :: l -> aux l
| [] -> None
in
aux categories
type argument = {
arg_type : string;
arg_default : string option; (** default value *)
arg_description : string option;
}
type meth = { meth_type : string; meth_description : string option }
(** Documentation for a function. *)
type t = {
typ : string;
category : category;
flags : flag list;
description : string;
examples : string list;
arguments : (string option * argument) list;
methods : (string * meth) list;
}
let db = ref Map.empty
let add (name : string) (doc : t Lazy.t) = db := Map.add name doc !db
let get name = Lazy.force (Map.find name !db)
(** Only print function names. *)
let print_functions print =
Map.iter
(fun f _ ->
print f;
print "\n")
!db
let print_functions_by_category print =
let categories =
categories |> List.map (fun (c, s) -> (s, c)) |> List.sort compare
in
List.iter
(fun (category_name, category) ->
print ("# " ^ category_name ^ "\n\n");
Map.iter
(fun f d ->
let d = Lazy.force d in
if d.category = category && not (List.mem `Hidden d.flags) then
print ("- " ^ f ^ "\n"))
!db;
print "\n")
categories
let colorize = Console.colorize
let title_color = colorize [`bold]
let type_color = colorize [`yellow]
let default_color = colorize [`red; `bold]
let label_color = colorize [`cyan; `bold]
let print name print =
let f = get name in
let reflow ?(indent = 0) ?(cols = 70) s =
let buf = Buffer.create 1024 in
let backtick = ref false in
let protected = ref false in
let n = ref 0 in
let indent () =
for _ = 1 to indent do
Buffer.add_char buf ' '
done
in
let newline () =
Buffer.add_char buf '\n';
indent ();
n := 0
in
let char c =
Buffer.add_char buf c;
incr n
in
let space () = if !n >= cols then newline () else char ' ' in
indent ();
String.iter
(fun c ->
if c = '`' then (
if not !backtick then protected := not !protected;
backtick := true)
else backtick := false;
if (not !protected) && c = ' ' then space ()
else if c = '\n' then newline ()
else char c)
s;
Buffer.contents buf
in
print (title_color f.description);
print "\n\n";
print (title_color "Type: ");
print f.typ;
print "\n\n";
print (title_color "Category: " ^ string_of_category f.category ^ "\n\n");
if f.flags <> [] then (
let flags = f.flags |> List.map string_of_flag |> String.concat "," in
print (title_color "Flags: " ^ flags ^ "\n\n"));
List.iter
(fun e ->
print (title_color "Example:\n\n");
print e;
print "\n\n")
f.examples;
print (title_color "Arguments:\n\n");
List.iter
(fun (l, a) ->
let l = Option.value ~default:"(unlabeled)" l in
let l = label_color l in
let default =
match a.arg_default with
| Some d -> " (default: " ^ default_color d ^ ")"
| None -> ""
in
print (" * " ^ l ^ " : " ^ type_color a.arg_type ^ default ^ "\n");
Option.iter (fun d -> print (reflow ~indent:5 d)) a.arg_description;
print "\n\n")
(List.stable_sort
(fun v v' ->
match (v, v') with
| (None, _), (None, _) -> 0
| (None, _), _ -> 1
| _, (None, _) -> -1
| (l, _), (l', _) -> Stdlib.compare l l')
f.arguments);
if f.methods <> [] then (
print (title_color "Methods:\n\n");
List.iter
(fun (l, m) ->
print (" * " ^ label_color l ^ " : " ^ type_color m.meth_type ^ "\n");
Option.iter (fun d -> print (reflow ~indent:5 d)) m.meth_description;
print "\n\n")
(List.sort compare f.methods))
let to_json () : Json.t =
!db |> Map.to_seq
|> Seq.map (fun (l, f) ->
let f = Lazy.force f in
let arguments =
List.map
(fun (l, a) ->
( Option.value ~default:"" l,
`Assoc
[
("type", `String a.arg_type);
( "default",
Option.fold ~none:`Null
~some:(fun d -> `String d)
a.arg_default );
( "description",
`String (Option.value ~default:"" a.arg_description) );
] ))
f.arguments
in
let arguments = `Assoc arguments in
let methods =
List.map
(fun (l, m) ->
( l,
`Assoc
[
("type", `String m.meth_type);
( "description",
`String (Option.value ~default:"" m.meth_description)
);
] ))
f.methods
in
let methods = `Assoc methods in
( l,
`Assoc
[
("type", `String f.typ);
("category", `String (string_of_category f.category));
( "flags",
`Tuple
(List.map string_of_flag f.flags
|> List.map (fun s -> `String s)) );
("description", `String f.description);
("examples", `Tuple (List.map (fun s -> `String s) f.examples));
("arguments", arguments);
("methods", methods);
] ))
|> List.of_seq
|> fun l -> `Assoc l
let print_functions_md ? ?deprecated print =
let should_show ~category d =
let d = Lazy.force d in
(not (List.mem `Hidden d.flags))
&& ((not (deprecated = Some true)) || List.mem `Deprecated d.flags)
&& (deprecated = Some true || not (List.mem `Deprecated d.flags))
&& d.category = category
&& ((not (extra = Some true)) || List.mem `Extra d.flags)
&& ((not (extra = Some false)) || not (List.mem `Extra d.flags))
in
let categories =
categories
|> List.map (fun (c, s) -> (s, c))
|> List.filter (fun (_, category) ->
Map.exists (fun _ d -> should_show ~category d) !db)
|> List.sort compare
in
List.iter
(fun (category, _) ->
let slug s =
let s = String.lowercase_ascii s in
let r = Str.regexp "[ /]+" in
Str.global_replace r "-" s
in
Printf.ksprintf print "- [%s](#%s)\n" category (slug category))
categories;
print "\n";
List.iter
(fun (category_name, category) ->
print ("## " ^ category_name ^ "\n\n");
Map.iter
(fun f d ->
if should_show ~category d then (
let d = Lazy.force d in
print ("### `" ^ f ^ "`\n\n");
print d.description;
print "\n\n";
print "Type:\n\n```\n";
print d.typ;
print "\n```\n\n";
List.iter
(fun e ->
print "Example:\n\n";
Printf.ksprintf print "```liquidsoap\n%s\n```\n\n" e)
d.examples;
if d.arguments <> [] then (
print "Arguments:\n\n";
List.iter
(fun (l, a) ->
let l = Option.value ~default:"(unlabeled)" l in
let t = a.arg_type in
let d =
match a.arg_default with
| None -> ""
| Some d -> ", which defaults to `" ^ d ^ "`"
in
let s =
match a.arg_description with
| None -> ""
| Some s -> ": " ^ s
in
Printf.ksprintf print "- `%s` (of type `%s`%s)%s\n" l t d s)
d.arguments;
print "\n");
if d.methods <> [] then (
print "Methods:\n\n";
List.iter
(fun (l, m) ->
let t = m.meth_type in
let s =
match m.meth_description with
| None -> ""
| Some s -> ": " ^ s
in
Printf.ksprintf print "- `%s` (of type `%s`)%s\n" l t s)
(List.sort compare d.methods);
print "\n");
if List.mem `Experimental d.flags then
print "This function is experimental.\n\n"))
!db)
categories
let print_emacs_completions print =
print "(defconst liquidsoap-completions '(\n";
Map.iter
(fun name f ->
let f = Lazy.force f in
if not (List.mem `Hidden f.flags || List.mem `Deprecated f.flags) then (
let t = String.map (fun c -> if c = '\n' then ' ' else c) f.typ in
Printf.ksprintf print
"#(\"%s\" 0 1 (:type \"%s\" :description \"%s\"))\n" name t
(String.escaped f.description)))
!db;
print "))\n\n";
print "(provide 'liquidsoap-completions)\n"
end