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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
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
(** 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)
let count () = Map.cardinal !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
type doc_type = [ `Full | `Argsof of string list ]
let parse_doc ~pos doc =
let doc = String.split_on_char '\n' doc in
let doc =
List.map
(fun x ->
Re.replace ~all:true ~f:(fun _ -> "") (Re.Pcre.regexp "^\\s*#\\s?") x)
doc
in
if doc = [] then None
else (
let rec parse_doc (main, special, params, methods) = function
| [] -> (main, special, params, methods)
| line :: lines -> (
try
let sub =
Re.Pcre.exec
~rex:
(Re.Pcre.regexp
"^\\s*@(category|docof|flag|param|method|argsof)\\s*(.*)$")
line
in
let s = Re.Pcre.get_substring sub 2 in
match Re.Pcre.get_substring sub 1 with
| "docof" ->
let doc = Value.get s in
let main =
if doc.description <> "" then doc.description :: main
else main
in
let params =
List.filter_map
(fun (l, a) ->
match a.Value.arg_description with
| Some d -> Some (l, d)
| None -> None)
doc.arguments
@ params
in
let doc_specials =
`Category (Value.string_of_category doc.category)
:: List.map
(fun f -> `Flag (Value.string_of_flag f))
doc.flags
in
parse_doc
(main, doc_specials @ special, params, methods)
lines
| "argsof" ->
let s, only, except =
try
let sub =
Re.Pcre.exec
~rex:
(Re.Pcre.regexp "^\\s*([^\\[]+)\\[([^\\]]+)\\]\\s*$")
s
in
let s = Re.Pcre.get_substring sub 1 in
let args =
List.filter
(fun s -> s <> "")
(List.map String.trim
(String.split_on_char ','
(Re.Pcre.get_substring sub 2)))
in
let only, except =
List.fold_left
(fun (only, except) v ->
if String.length v > 0 && v.[0] = '!' then
( only,
String.sub v 1 (String.length v - 1) :: except
)
else (v :: only, except))
([], []) args
in
(s, only, except)
with Not_found -> (s, [], [])
in
let doc = Value.get s in
let args =
List.filter
(fun (n, _) ->
match n with
| None -> false
| Some n -> (
match (only, except) with
| [], except -> not (List.mem n except)
| only, except ->
List.mem n only && not (List.mem n except)))
doc.arguments
in
let args =
List.filter_map
(fun (n, a) ->
Option.map (fun d -> (n, d)) a.Value.arg_description)
args
in
parse_doc (main, special, args @ params, methods) lines
| "category" ->
parse_doc
(main, `Category s :: special, params, methods)
lines
| "flag" ->
parse_doc (main, `Flag s :: special, params, methods) lines
| "param" ->
let sub =
Re.Pcre.exec
~rex:(Re.Pcre.regexp "^(~?[a-zA-Z0-9_.]+)\\s*(.*)$")
s
in
let label = Re.Pcre.get_substring sub 1 in
let descr = Re.Pcre.get_substring sub 2 in
let label =
if label.[0] = '~' then
Some (String.sub label 1 (String.length label - 1))
else None
in
let rec parse_descr descr lines =
match lines with
| [] -> raise Not_found
| line :: lines ->
let line =
Re.replace ~all:true
~f:(fun _ -> "")
(Re.Pcre.regexp "^ *") line
in
let n = String.length line - 1 in
if line.[n] = '\\' then (
let descr = String.sub line 0 n :: descr in
parse_descr descr lines)
else (
let descr = List.rev (line :: descr) in
(String.concat "" descr, lines))
in
let descr, lines = parse_descr [] (descr :: lines) in
parse_doc
(main, special, (label, descr) :: params, methods)
lines
| "method" ->
let sub =
Re.Pcre.exec
~rex:(Re.Pcre.regexp "^(~?[a-zA-Z0-9_.]+)\\s*(.*)$")
s
in
let label = Re.Pcre.get_substring sub 1 in
let descr = Re.Pcre.get_substring sub 2 in
parse_doc
(main, special, params, (label, descr) :: methods)
lines
| d -> failwith ("Unknown documentation item: " ^ d)
with Not_found ->
parse_doc (line :: main, special, params, methods) lines)
in
let main, special, params, methods = parse_doc ([], [], [], []) doc in
let main = List.rev main in
let params =
List.map
(fun (l, d) ->
( l,
Value.
{ arg_type = "???"; arg_default = None; arg_description = Some d }
))
(List.rev params)
in
let methods =
List.map
(fun (l, d) ->
(l, Value.{ meth_type = "???"; meth_description = Some d }))
(List.rev methods)
in
let main = String.concat "\n" main in
let main = Lang_string.unbreak_md main in
let category, flags =
List.fold_left
(fun (c, f) s ->
match s with `Category c -> (c, f) | `Flag flag -> (c, flag :: f))
("Uncategorized", []) special
in
let category = String.trim category in
let category =
match Value.category_of_string category with
| Some c -> c
| None ->
failwith
(Printf.sprintf "Unknown category: %s (%s)." category
(Pos.to_string pos))
in
let flags =
let f f =
match Value.flag_of_string f with
| Some f -> f
| None ->
failwith
(Printf.sprintf "Unknown flag: %s (%s)." f (Pos.to_string pos))
in
List.map f flags
in
Some
Value.
{
typ = "???";
category;
flags;
description = main;
examples = [];
arguments = params;
methods;
})