-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTo_html_tree.fold
1615 lines (1441 loc) · 52.4 KB
/
To_html_tree.fold
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright (c) 2016 Thomas Refis <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)
module Location = Model.Location_
module Paths = Model.Paths
module Comment = Model.Comment
module Lang = Model.Lang
module Html = Tyxml.Html
open Utils
type rendered_item = list (Html.elt Html_types.div_content)
(* [rendered_item] should really be [dt_content], but that is bugged in TyXML
until https://github.com/ocsigen/tyxml/pull/193 is released. *)
type text 'inner 'outer = {>
| `PCDATA
| `Span
| `A ({> `PCDATA } as 'inner)
} as 'outer
val a_href = Html_tree.Relative_link.to_sub_element
val functor_arg_pos { Model.Lang.FunctorArgument.id; _ } =
match id with
| Paths.Identifier.Argument (_, nb, _) -> nb
| _ -> failwith "TODO"
end
(* let id = string_of_sexp <| Identifier.sexp_of_t id in
invalid_arg (Printf.sprintf "functor_arg_pos: %s" id) *)
module Type_expression : {
val type_expr
: needs_parentheses: bool?
-> Lang.TypeExpr.t
-> list (Html.elt (text 'inner 'outer))
val format_type_path
: delim: { `parens | `brackets }
-> Lang.TypeExpr.t list ->
-> list (Html.elt (text 'inner 'outer))
-> list (Html.elt (text 'inner 'outer))
} = {
val* te_variant t : forall 'inner 'outer. Model.Lang.TypeExpr.Variant -> list (Html.elt (text 'inner 'outer)) =
let elements =
list_concat_map t.elements sep: (Markup.keyword " | ") f: {
| Model.Lang.TypeExpr.Variant.Type te -> type_expr te
| Constructor (name, _bool, args) ->
let constr = "`" ++ name in
match args {
| [] -> [ Html.pcdata constr ]
| _ ->
let args =
list_concat_map args sep: Html.pcdata " * " f: type_expr
in
[Html.pcdata (constr ^ " of ") & args]
}
}
in
match t.kind with
| Fixed -> List.append (Html.pcdata "[ " & elements) (Html.pcdata " ]")
| Open -> List.append (Html.pcdata "[> " & elements) (Html.pcdata " ]")
| Closed [] -> List.append (Html.pcdata "[< " & elements) (Html.pcdata " ]")
| Closed xs -> List.append (Html.pcdata "[< " & elements) (Html.pcdata (" " ++ constrs ++ " ]"))
where constrs = String.concat " " xs
and te_object
: 'inner 'outer. Model.Lang.TypeExpr.Object.t ->
('inner, 'outer) text Html.elt list
= fun (t : Model.Lang.TypeExpr.Object.t) ->
let fields =
list_concat_map t.fields fn:
| Model.Lang.TypeExpr.Object.Method { name; type_ } ->
Html.pcdata (name ^ " : ") & type_expr type_ @ [Html.pcdata "; "]
| Inherit type_ ->
type_expr type_ @ [Html.pcdata "; "]
end
in
Html.pcdata "< " &
fields @ [Html.pcdata ((if t.open_ then ".. " else "") ++ ">")]
and format_type_path
: 'inner 'outer. delim:[ `parens | `brackets ]
-> Model.Lang.TypeExpr.t list -> ('inner, 'outer) text Html.elt list
-> ('inner, 'outer) text Html.elt list
= fun ~delim params path ->
match params with
| [] -> path
| [param] ->
type_expr needs_parentheses: true, param @ Html.pcdata " " :: path
| params ->
let params =
list_concat_map params,
sep: Html.pcdata ",\194\160",
f: type_expr
match delim with
| `parens -> Html.pcdata "(" & params @ Html.pcdata ")\194\160" & path
| `brackets -> Html.pcdata "[" & params @ Html.pcdata "]\194\160" & path
and type_expr
: 'inner 'outer. ?needs_parentheses:bool
-> Model.Lang.TypeExpr.t -> ('inner, 'outer) text Html.elt list
= fun needs_parentheses: false, t ->
match t with
| Var s -> [Markup.ML.Type.var ("'" ^ s)]
| Any -> [Markup.ML.Type.var "_"]
| Alias (te, alias) ->
type_expr needs_parentheses: true, te ++
Markup.keyword " as " & [Html.pcdata alias]
| Arrow (None, src, dst) ->
let res =
List.append
(type_expr needs_parentheses: true, src)
(Html.pcdata " " & Markup.ML.arrow & Html.pcdata " " & type_expr dst)
in
if not needs_parentheses then
res
else
Html.pcdata "(" :: res @ [Html.pcdata ")"]
| Arrow (Some lbl, src, dst) ->
let res =
Markup.ML.label lbl @ Html.pcdata ":" ::
type_expr ~needs_parentheses:true src @
Html.pcdata " " :: Markup.ML.arrow :: Html.pcdata " " :: type_expr dst
in
if not needs_parentheses then
res
else
Html.pcdata "(" :: res @ [Html.pcdata ")"]
| Tuple lst ->
let res =
list_concat_map lst ~sep:(Markup.keyword " * ")
~f:(type_expr ~needs_parentheses:true)
in
if not needs_parentheses then
res
else
Html.pcdata "(" :: res @ [Html.pcdata ")"]
| Constr (path, args) ->
let link = Html_tree.Relative_link.of_path ~stop_before:false path in
format_type_path ~delim:(`parens) args link
| Variant v -> te_variant v
| Object o -> te_object o
| Class (path, args) ->
format_type_path ~delim:(`brackets) args
(Html_tree.Relative_link.of_path ~stop_before:false path)
| Poly (polyvars, t) ->
Html.pcdata (String.concat " " polyvars ^ ". ") :: type_expr t
| Package pkg ->
Html.pcdata "(" :: Markup.keyword "module " ::
Html_tree.Relative_link.of_path ~stop_before:false pkg.path @
begin match pkg.substitutions with
| [] -> []
| lst ->
Html.pcdata " " :: Markup.keyword "with" :: Html.pcdata " " ::
list_concat_map ~sep:(Markup.keyword " and ") lst
~f:(package_subst pkg.path)
end
@ [Html.pcdata ")"]
and package_subst
: 'inner 'outer.
Paths.Path.module_type -> Paths.Fragment.type_ * Model.Lang.TypeExpr.t
-> ('inner, 'outer) text Html.elt list
= fun pkg_path (frag_typ, te) ->
Markup.keyword "type " ::
(match pkg_path with
| Paths.Path.Resolved rp ->
let base =
Paths.Identifier.signature_of_module_type
(Paths.Path.Resolved.identifier rp)
in
Html_tree.Relative_link.of_fragment ~base
(Paths.Fragment.any_sort frag_typ)
| _ ->
[Html.pcdata
(Html_tree.render_fragment (Paths.Fragment.any_sort frag_typ))]) @
Html.pcdata " " :: Markup.keyword "=" :: Html.pcdata " " ::
type_expr te
end
open Type_expression
(* Also handles constructor declarations for exceptions and extensible
variants, and exposes a few helpers used in formatting classes and signature
constraints. *)
module Type_declaration :
sig
val type_decl : Lang.TypeDecl.t -> rendered_item * Comment.docs
val extension : Lang.Extension.t -> rendered_item * Comment.docs
val exn : Lang.Exception.t -> rendered_item * Comment.docs
val format_params :
?delim:[ `parens | `brackets ] ->
Lang.TypeDecl.param list ->
[> `PCDATA ] Html.elt
val format_manifest :
?compact_variants:bool ->
Lang.TypeDecl.Equation.t ->
((('inner, 'outer) text Html.elt) list) * bool
val format_constraints :
(Lang.TypeExpr.t * Lang.TypeExpr.t) list ->
(('inner, 'outer) text Html.elt) list
end = {
let record fields =
let field mutable_ id typ =
match Url.from_identifier ~stop_before:true id with
| Error e -> failwith (Url.Error.to_string e)
| Ok { anchor; kind; _ } ->
let name = Paths.Identifier.name id in
let cell =
Html.td ~a:[ Html.a_class ["def"; kind ] ]
[Html.a ~a:[Html.a_href ("#" ^ anchor); Html.a_class ["anchor"]] []
; Html.code (
(if mutable_ then Markup.keyword "mutable " else Html.pcdata "")
:: (Html.pcdata name)
:: (Html.pcdata " : ")
:: (type_expr typ)
@ [Html.pcdata ";"]
)
]
in
anchor, cell
in
let rows =
fields |> List.map (fun fld ->
let open Model.Lang.TypeDecl.Field in
let anchor, lhs = field fld.mutable_ fld.id fld.type_ in
let rhs = Documentation.to_html fld.doc in
let rhs = (rhs :> (Html_types.td_content Html.elt) list) in
Html.tr ~a:[ Html.a_id anchor; Html.a_class ["anchored"] ] (
lhs ::
if not (Documentation.has_doc fld.doc) then [] else [
Html.td ~a:[ Html.a_class ["doc"] ] rhs
]
)
)
in
[ Html.code [Html.pcdata "{"]
; Html.table ~a:[ Html.a_class ["record"] ] rows
; Html.code [Html.pcdata "}"]]
let constructor
: 'b. 'b Paths.Identifier.t -> Model.Lang.TypeDecl.Constructor.argument
-> Model.Lang.TypeExpr.t option
-> [> `Code | `PCDATA | `Table ] Html.elt list
= fun id args ret_type ->
let name = Paths.Identifier.name id in
let cstr =
Html.span
~a:[Html.a_class [Url.kind_of_id_exn id]] [Html.pcdata name]
in
let is_gadt, ret_type =
match ret_type with
| None -> false, []
| Some te ->
let constant =
match args with
| Tuple [] -> true
| _ -> false
in
let ret_type =
Html.pcdata " " ::
(if constant then Markup.keyword ":" else Markup.ML.arrow) ::
Html.pcdata " " ::
type_expr te
in
true, ret_type
in
match args with
| Tuple [] -> [ Html.code (cstr :: ret_type) ]
| Tuple lst ->
[ Html.code (
cstr ::
Markup.keyword (if is_gadt then " : " else " of ") ::
list_concat_map lst ~sep:(Markup.keyword " * ")
~f:(type_expr ~needs_parentheses:is_gadt)
@ ret_type
)
]
| Record fields ->
Html.code [ cstr; Markup.keyword (if is_gadt then " : " else " of ") ]
:: record fields
@ [ Html.code ret_type ]
let variant cstrs : [> Html_types.table ] Html.elt =
let constructor id args res =
match Url.from_identifier ~stop_before:true id with
| Error e -> failwith (Url.Error.to_string e)
| Ok { anchor; kind; _ } ->
let cell =
Html.td ~a:[ Html.a_class ["def"; kind ] ] (
Html.a ~a:[Html.a_href ("#" ^ anchor); Html.a_class ["anchor"]]
[] ::
Html.code [Markup.keyword "| " ] ::
constructor id args res
)
in
anchor, cell
in
let rows =
cstrs |> List.map (fun cstr ->
let open Model.Lang.TypeDecl.Constructor in
let anchor, lhs = constructor cstr.id cstr.args cstr.res in
let rhs = Documentation.to_html cstr.doc in
let rhs = (rhs :> (Html_types.td_content Html.elt) list) in
Html.tr ~a:[ Html.a_id anchor; Html.a_class ["anchored"] ] (
lhs ::
if not (Documentation.has_doc cstr.doc) then [] else [
Html.td ~a:[ Html.a_class ["doc"] ] rhs
]
)
)
in
Html.table ~a:[ Html.a_class ["variant"] ] rows
let extension_constructor (t : Model.Lang.Extension.Constructor.t) =
(* TODO doc *)
constructor t.id t.args t.res
let extension (t : Model.Lang.Extension.t) =
let extension =
Html.code (
Markup.keyword "type " ::
Html_tree.Relative_link.of_path ~stop_before:false t.type_path @
[ Markup.keyword " += " ]
) ::
list_concat_map t.constructors ~sep:(Html.code [Markup.keyword " | "])
~f:extension_constructor
in
extension, t.doc
let exn (t : Model.Lang.Exception.t) =
let cstr = constructor t.id t.args t.res in
let exn = Html.code [ Markup.keyword "exception " ] :: cstr in
exn, t.doc
let polymorphic_variant ~type_ident (t : Model.Lang.TypeExpr.Variant.t) =
let row item =
let kind_approx, cstr =
match item with
| Model.Lang.TypeExpr.Variant.Type te ->
"unknown", [Html.code (type_expr te)]
| Constructor (name, _bool, args) ->
let cstr = "`" ^ name in
"constructor",
match args with
| [] -> [Html.code [ Html.pcdata cstr ]]
| _ ->
[ Html.code (
Html.pcdata cstr ::
Markup.keyword " of " ::
list_concat_map args ~sep:(Markup.keyword " * ")
~f:type_expr
)
]
in
try
let { Url.Anchor. name = anchor; kind } =
Url.Anchor.Polymorphic_variant_decl.from_element ~type_ident item
in
Html.tr ~a:[ Html.a_id anchor; Html.a_class ["anchored"] ] [
Html.td ~a:[ Html.a_class ["def"; kind] ] (
Html.a ~a:[
Tyxml.Html.a_href ("#" ^ anchor); Html.a_class ["anchor"] ] [] ::
Html.code [Markup.keyword "| " ] ::
cstr
);
(* TODO: retrieve doc comments. *)
]
with Failure s ->
Printf.eprintf "ERROR: %s\n%!" s;
Html.tr [
Html.td ~a:[ Html.a_class ["def"; kind_approx] ] (
Html.code [Markup.keyword "| " ] ::
cstr
);
(* TODO: retrieve doc comments. *)
]
in
let table =
Html.table ~a:[Html.a_class ["variant"]] (List.map row t.elements) in
match t.kind with
| Fixed ->
Html.code [Html.pcdata "[ "] :: table :: [Html.code [Html.pcdata " ]"]]
| Open ->
Html.code [Html.pcdata "[> "] :: table :: [Html.code [Html.pcdata " ]"]]
| Closed [] ->
Html.code [Html.pcdata "[< "] :: table :: [Html.code [Html.pcdata " ]"]]
| Closed lst ->
let constrs = String.concat " " lst in
Html.code [Html.pcdata "[< "] :: table ::
[Html.code [Html.pcdata (" " ^ constrs ^ " ]")]]
let format_params
: 'row. ?delim:[`parens | `brackets] -> Model.Lang.TypeDecl.param list
-> ([> `PCDATA ] as 'row) Html.elt
= fun ?(delim=`parens) params ->
let format_param (desc, variance_opt) =
let param_desc =
match desc with
| Model.Lang.TypeDecl.Any -> "_"
| Var s -> "'" ^ s
in
match variance_opt with
| None -> param_desc
| Some Model.Lang.TypeDecl.Pos -> "+" ^ param_desc
| Some Model.Lang.TypeDecl.Neg -> "-" ^ param_desc
in
Html.pcdata (
match params with
| [] -> ""
| [x] -> format_param x ^ " "
| lst ->
let params = String.concat ", " (List.map format_param lst) in
(match delim with `parens -> "(" | `brackets -> "[")
^ params ^
(match delim with `parens -> ") " | `brackets -> "] ")
)
let format_constraints
: 'inner_row 'outer_row. (_ * _) list ->
([> `PCDATA | `Span
| `A of ([> `PCDATA ] as 'inner_row) ] as 'outer_row) Html.elt list
= function
| [] -> []
| lst ->
Markup.keyword " constraint " ::
list_concat_map lst ~sep:(Markup.keyword " and ") ~f:(fun (t1, t2) ->
type_expr t1 @ Html.pcdata " = " :: type_expr t2
)
let format_manifest
: 'inner_row 'outer_row. ?compact_variants:bool
-> Model.Lang.TypeDecl.Equation.t
-> ('inner_row, 'outer_row) text Html.elt list * bool
= fun ?(compact_variants=true) equation ->
let _ = compact_variants in (* TODO *)
let private_ = equation.private_ in
match equation.manifest with
| None -> [], private_
| Some t ->
let manifest =
Markup.keyword " = " ::
(if private_ then Markup.keyword "private " else Html.pcdata "") ::
type_expr t
in
manifest, false
let type_decl (t : Lang.TypeDecl.t) =
let tyname = Paths.Identifier.name t.id in
let params = format_params t.equation.params in
let constraints = format_constraints t.equation.constraints in
let manifest, need_private =
match t.equation.manifest with
| Some (Model.Lang.TypeExpr.Variant variant) ->
let manifest =
Markup.keyword " = " ::
(if t.equation.private_ then
Markup.keyword "private "
else
Html.pcdata "") ::
polymorphic_variant ~type_ident:t.id variant
in
manifest, false
| _ ->
let manifest, need_private = format_manifest t.equation in
[Html.code manifest], need_private
in
let representation =
match t.representation with
| None -> []
| Some repr ->
Html.code [
Markup.keyword " = ";
if need_private then Markup.keyword "private " else Html.pcdata ""
] ::
match repr with
| Extensible -> [Html.code [Markup.keyword ".."]]
| Variant cstrs -> [variant cstrs]
| Record fields -> record fields
in
let tdecl_def =
Html.code [
Markup.keyword "type ";
params;
Html.pcdata tyname;
] ::
manifest @
representation @
[Html.code constraints]
in
tdecl_def, t.doc
end
open Type_declaration
module Value :
sig
val value : Lang.Value.t -> rendered_item * Comment.docs
val external_ : Lang.External.t -> rendered_item * Comment.docs
end =
struct
let value (t : Model.Lang.Value.t) =
let name = Paths.Identifier.name t.id in
let value =
Markup.keyword "val " ::
Html.pcdata name ::
Html.pcdata " : " ::
type_expr t.type_
in
[Html.code value], t.doc
let external_ (t : Model.Lang.External.t) =
let name = Paths.Identifier.name t.id in
let external_ =
Markup.keyword "external " ::
Html.pcdata name ::
Html.pcdata " : " ::
type_expr t.type_ @
Html.pcdata " = " ::
List.map (fun p -> Html.pcdata ("\"" ^ p ^ "\" ")) t.primitives
in
[Html.code external_], t.doc
end
open Value
(* This chunk of code is responsible for laying out signatures and class
signatures: the parts of OCaml that contain other parts as nested items.
Each item is either
- a leaf, like a type declaration or a value,
- something that has a nested signature/class signature, or
- a comment.
Comments can contain section headings, and the top-level markup code is also
responsible for generating a table of contents. As a result, it must compute
the nesting of sections.
This is also a good opportunity to properly nest everything in <section>
tags. Even though that is not strictly required by HTML, we carry out the
computation for it anyway when computing nesting for the table of
contents.
Leaf items are set in <dl> tags – the name and any definition in <dt>, and
documentation in <dd>. Multiple adjacent undocumented leaf items of the same
kind are set as sibling <dt>s in one <dl>, until one of them has
documentation. This indicates groups like:
{[
type sync
type async
(** Documentation for both types. *)
]}
Nested signatures are currently marked up with <article> tags. The top-level
layout code is eventually indirectly triggered recursively for laying them
out, as well. *)
type ('item_kind, 'item) tagged_item = [
| `Leaf_item of 'item_kind * 'item
| `Nested_article of 'item
| `Comment of Comment.docs_or_stop
]
type section = {
anchor : string;
text : Comment.link_content;
children : section list;
}
type toc = section list
module Top_level_markup :
sig
val lay_out :
item_to_id:('item -> string option) ->
item_to_spec:('item -> string option) ->
render_leaf_item:('item -> rendered_item * Comment.docs) ->
render_nested_article:('item -> rendered_item * 'root Html_tree.t list) ->
((_, 'item) tagged_item) list ->
rendered_item * toc * 'root Html_tree.t list
val render_toc :
toc -> ([> Html_types.flow5_without_header_footer ] Html.elt) list
end =
struct
(* Just some type abbreviations. *)
type html = Html_types.flow5 Html.elt
type comment_html = Html_types.flow5_without_header_footer Html.elt
let add_anchor item_to_id item html =
match item_to_id item with
| None ->
html,
[]
| Some anchor_text ->
let anchor =
Html.a
~a:[Html.a_href ("#" ^ anchor_text); Html.a_class ["anchor"]]
[]
in
anchor::html,
[Html.a_id anchor_text]
(* Adds spec class to the list of existing item attributes. *)
let add_spec item_to_spec item a =
match item_to_spec item with
| Some spec -> Html.a_class ["spec " ^ spec] :: a
| None -> a
(* "Consumes" adjacent leaf items of the same kind, until one is found with
documentation. Then, joins all their definitions, and the documentation of
the last item (if any), into a <dl> element. The rendered <dl> element is
paired with the list of unconsumed items remaining in the input. *)
let leaf_item_group
item_to_id item_to_spec render_leaf_item first_item_kind items
: html * 'item list =
let rec consume_leaf_items_until_one_is_documented =
fun items acc ->
match items with
| (`Leaf_item (this_item_kind, item))::items
when this_item_kind = first_item_kind ->
let html, maybe_docs = render_leaf_item item in
(* Temporary coercion until https://github.com/ocsigen/tyxml/pull/193
is released in TyXML; see also type [rendered_item]. *)
let html = List.map Html.Unsafe.coerce_elt html in
let html, maybe_id = add_anchor item_to_id item html in
let a = add_spec item_to_spec item maybe_id in
let html = Html.dt ~a html in
let acc = html::acc in
begin match maybe_docs with
| [] ->
consume_leaf_items_until_one_is_documented items acc
| docs ->
let docs = Documentation.to_html docs in
let docs = (docs :> (Html_types.dd_content Html.elt) list) in
let docs = Html.dd docs in
List.rev (docs::acc), items
end
| _ ->
List.rev acc, items
in
let rendered_item_group, remaining_items =
consume_leaf_items_until_one_is_documented items [] in
Html.dl rendered_item_group, remaining_items
(* When we encounter a stop comment, [(**/**)], we read everything until the
next stop comment, if there is one, and discard it. The returned item list
is the signature items following the next stop comment, if there are
any. *)
let rec skip_everything_until_next_stop_comment : 'item list -> 'item list =
function
| [] -> []
| item::items ->
match item with
| `Comment `Stop -> items
| _ -> skip_everything_until_next_stop_comment items
(* Reads comment content until the next heading, or the end of comment, and
renders it as HTML. The returned HTML is paired with the remainder of the
comment, which will either start with the next section heading in the
comment, or be empty if there are no section headings. *)
let render_comment_until_heading_or_end
: Comment.docs -> comment_html list * Comment.docs = fun docs ->
let rec scan_comment acc docs =
match docs with
| [] -> List.rev acc, docs
| block::rest ->
match block.Location.value with
| `Heading _ -> List.rev acc, docs
| _ -> scan_comment (block::acc) rest
in
let included, remaining = scan_comment [] docs in
let docs = Documentation.to_html included in
docs, remaining
(* The sectioning functions take several arguments, and return "modified"
instances of them as results. So, it is convenient to group them into a
record type. This is most useful for the return type, as otherwise there is
no way to give names to its components.
The components themselves are:
- The current comment being read. When non-empty, this is progressively
replaced with its tail until it is exhausted.
- The general signature items to be read. These are read one at a time when
there is no current comment. Upon encountering a comment, it becomes the
"current comment," and the sectioning functions read it one block element
at a time, scanning for section headings.
- A reversed accumulator of the rendered signature items.
- A reversed accumulator of the table of contents.
- An accumulator of the subpages generated for nested signatures.
The record is also convenient for passing around the two item-rendering
functions. *)
type ('root, 'kind, 'item) sectioning_state = {
input_items : (('kind, 'item) tagged_item) list;
input_comment : Comment.docs;
acc_html : html list;
acc_toc : toc;
acc_subpages : 'root Html_tree.t list;
item_to_id : 'item -> string option;
item_to_spec : 'item -> string option;
render_leaf_item : 'item -> rendered_item * Comment.docs;
render_nested_article : 'item -> rendered_item * 'root Html_tree.t list;
}
let finish_section state =
{state with
acc_html = List.rev state.acc_html;
acc_toc = List.rev state.acc_toc;
}
let is_deeper_section_level =
let level_to_int = function
| `Title -> 1
| `Section -> 2
| `Subsection -> 3
| `Subsubsection -> 4
in
fun other_level ~than ->
level_to_int other_level > level_to_int than
let rec section_items section_level state =
match state.input_items with
| [] ->
finish_section state
| tagged_item::input_items ->
match tagged_item with
| `Leaf_item (kind, _) ->
let html, input_items =
leaf_item_group
state.item_to_id
state.item_to_spec
state.render_leaf_item
kind
state.input_items
in
section_items section_level {state with
input_items;
acc_html = html::state.acc_html;
}
| `Nested_article item ->
let html, subpages = state.render_nested_article item in
let html, maybe_id = add_anchor state.item_to_id item html in
let a = add_spec state.item_to_spec item maybe_id in
section_items section_level {state with
input_items;
acc_html = (Html.article ~a html)::state.acc_html;
acc_subpages = state.acc_subpages @ subpages;
}
| `Comment `Stop ->
let input_items = skip_everything_until_next_stop_comment input_items in
section_items section_level {state with
input_items;
}
| `Comment (`Docs input_comment) ->
section_comment section_level {state with
input_items;
input_comment;
}
and section_comment section_level state =
match state.input_comment with
| [] ->
section_items section_level state
| element::input_comment ->
match element.Location.value with
| `Heading (level, label, content) ->
if not (is_deeper_section_level level ~than:section_level) then
finish_section state
else
(* We have a deeper section heading in a comment within this section.
Parse it recursively. We start the nested HTML by parsing the
section heading itself, and anything that follows it in the current
comment, up to the next section heading, if any. All of this
comment matter goes into a <header> element. The nested HTML will
then be extended recursively by parsing more structure items,
including, perhaps, additional comments in <aside> elements. *)
let heading_html = Documentation.to_html [element] in
let more_comment_html, input_comment =
render_comment_until_heading_or_end input_comment in
let html = Html.header (heading_html @ more_comment_html) in
let nested_section_state =
{state with
input_comment = input_comment;
acc_html = [html];
acc_toc = [];
}
in
let nested_section_state =
section_comment level nested_section_state in
(* Wrap the nested section in a <section> element, and extend the
table of contents. *)
let html = Html.section nested_section_state.acc_html in
let Paths.Identifier.Label (_, label) = label in
let toc_entry =
{
anchor = label;
text = content;
children = nested_section_state.acc_toc;
}
in
(* Continue parsing after the nested section. In practice, we have
either run out of items, or the first thing in the input will be
another section heading – the nested section will have consumed
everything else. *)
section_comment section_level {nested_section_state with
acc_html = html::state.acc_html;
acc_toc = toc_entry::state.acc_toc;
}
| _ ->
let html, input_comment =
render_comment_until_heading_or_end state.input_comment in
let html = (html :> (Html_types.aside_content Html.elt) list) in
section_comment section_level {state with
input_comment;
acc_html = (Html.aside html)::state.acc_html;
}
let lay_out ~item_to_id ~item_to_spec ~render_leaf_item ~render_nested_article items =
let initial_state =
{
input_items = items;
input_comment = [];
acc_html = [];
acc_toc = [];
acc_subpages = [];
item_to_id;
item_to_spec;
render_leaf_item;
render_nested_article;
}
in
let state = section_items `Title initial_state in
state.acc_html, state.acc_toc, state.acc_subpages
let render_toc toc =
let rec section the_section : Html_types.li_content Html.elt list =
let text = Documentation.link_content_to_html the_section.text in
let text =
(text
: Html_types.phrasing_without_interactive Html.elt list
:> (Html_types.flow5_without_interactive Html.elt) list)
in
let link =
Html.a
~a:[Html.a_href ("#" ^ the_section.anchor)] text
in
match the_section.children with
| [] -> [link]
| _ -> [link; sections the_section.children]
and sections the_sections =
the_sections
|> List.map (fun the_section -> Html.li (section the_section))
|> Html.ul
in
match toc with
| [] -> []
| _ -> [Html.nav ~a:[Html.a_class ["toc"]] [sections toc]]
end
(* TODO Figure out when this function would fail. It is currently pasted from
[make_def], but the [make_spec] version doesn't have a [failwith]. *)
let path_to_id path =
match Url.from_identifier ~stop_before:true path with
| Error _ ->
None
| Ok {anchor; _} ->
Some anchor
module Class :
sig
val class_ :
?theme_uri:Html_tree.uri -> root:'a Html_tree.root -> Lang.Class.t ->
((Html_types.article_content Html.elt) list) * ('a Html_tree.t list)
val class_type :
?theme_uri:Html_tree.uri -> root:'a Html_tree.root -> Lang.ClassType.t ->
((Html_types.article_content Html.elt) list) * ('a Html_tree.t list)
end =
struct
let class_signature_item_to_id : Lang.ClassSignature.item -> _ = function
| Method {id; _} -> path_to_id id
| InstanceVariable {id; _} -> path_to_id id
| Constraint _
| Inherit _
| Comment _ -> None
let class_signature_item_to_spec : Lang.ClassSignature.item -> _ = function
| Method _ -> Some "method"
| InstanceVariable _ -> Some "instance-variable"
| Constraint _
| Inherit _
| Comment _ -> None
let tag_class_signature_item : Lang.ClassSignature.item -> _ = fun item ->
match item with
| Method _ -> `Leaf_item (`Method, item)
| InstanceVariable _ -> `Leaf_item (`Variable, item)
| Constraint _ -> `Leaf_item (`Constraint, item)
| Inherit _ -> `Leaf_item (`Inherit, item)
| Comment comment -> `Comment comment
let rec render_class_signature_item : Lang.ClassSignature.item -> _ = function
| Method m -> method_ m
| InstanceVariable v -> instance_variable v
| Constraint (t1, t2) -> format_constraints [(t1, t2)], []
| Inherit (Signature _) -> assert false (* Bold. *)
| Inherit class_type_expression ->
(Markup.keyword "inherit " ::