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
open Odoc_model.Paths
open Odoc_model.Names
module Root = Odoc_model.Root
let render_path : Path.t -> string =
let rec render_resolved : Path.Resolved.t -> string =
let open Path.Resolved in
function
| `Identifier id -> Identifier.name id
| `CoreType n -> TypeName.to_string n
| `OpaqueModule p -> render_resolved (p :> t)
| `OpaqueModuleType p -> render_resolved (p :> t)
| `Subst (_, p) -> render_resolved (p :> t)
| `SubstT (_, p) -> render_resolved (p :> t)
| `Alias (dest, `Resolved src) ->
if Path.Resolved.(is_hidden (src :> t)) then render_resolved (dest :> t)
else render_resolved (src :> t)
| `Alias (dest, src) ->
if Path.is_hidden (src :> Path.t) then render_resolved (dest :> t)
else render_path (src :> Path.t)
| `AliasModuleType (p1, p2) ->
if Path.Resolved.(is_hidden (p2 :> t)) then render_resolved (p1 :> t)
else render_resolved (p2 :> t)
| `Hidden p -> render_resolved (p :> t)
| `Module (p, s) -> render_resolved (p :> t) ^ "." ^ ModuleName.to_string s
| `Canonical (_, `Resolved p) -> render_resolved (p :> t)
| `Canonical (p, _) -> render_resolved (p :> t)
| `CanonicalModuleType (_, `Resolved p) -> render_resolved (p :> t)
| `CanonicalModuleType (p, _) -> render_resolved (p :> t)
| `CanonicalType (_, `Resolved p) -> render_resolved (p :> t)
| `CanonicalType (p, _) -> render_resolved (p :> t)
| `Substituted c -> render_resolved (c :> t)
| `SubstitutedMT c -> render_resolved (c :> t)
| `SubstitutedT c -> render_resolved (c :> t)
| `SubstitutedCT c -> render_resolved (c :> t)
| `Apply (rp, p) ->
render_resolved (rp :> t)
^ "("
^ render_resolved (p :> Path.Resolved.t)
^ ")"
| `ModuleType (p, s) ->
render_resolved (p :> t) ^ "." ^ ModuleTypeName.to_string s
| `Type (p, s) -> render_resolved (p :> t) ^ "." ^ TypeName.to_string s
| `Value (p, s) -> render_resolved (p :> t) ^ "." ^ ValueName.to_string s
| `Class (p, s) -> render_resolved (p :> t) ^ "." ^ TypeName.to_string s
| `ClassType (p, s) -> render_resolved (p :> t) ^ "." ^ TypeName.to_string s
and dot p s = render_path (p : Path.Module.t :> Path.t) ^ "." ^ s
and render_path : Path.t -> string =
fun x ->
match x with
| `Identifier (id, _) -> Identifier.name id
| `Root root -> ModuleName.to_string root
| `Forward root -> root
| `Dot (p, s) -> dot p (ModuleName.to_string s)
| `DotT (p, s) -> dot p (TypeName.to_string s)
| `DotMT (p, s) -> dot p (ModuleTypeName.to_string s)
| `DotV (p, s) -> dot p (ValueName.to_string s)
| `Apply (p1, p2) ->
render_path (p1 :> Path.t) ^ "(" ^ render_path (p2 :> Path.t) ^ ")"
| `Resolved rp -> render_resolved rp
| `Substituted m -> render_path (m :> Path.t)
| `SubstitutedMT m -> render_path (m :> Path.t)
| `SubstitutedT m -> render_path (m :> Path.t)
| `SubstitutedCT m -> render_path (m :> Path.t)
in
render_path
module Path = struct
type nonsrc_pv =
[ Identifier.Page.t_pv
| Identifier.Signature.t_pv
| Identifier.ClassSignature.t_pv ]
type any_pv =
[ nonsrc_pv | Identifier.SourcePage.t_pv | Identifier.AssetFile.t_pv ]
and any = any_pv Identifier.id
type kind =
[ `Module
| `Page
| `LeafPage
| `ModuleType
| `Parameter of int
| `Class
| `ClassType
| `File
| `SourcePage ]
let string_of_kind : kind -> string = function
| `Page -> "page"
| `Module -> "module"
| `LeafPage -> "leaf-page"
| `ModuleType -> "module-type"
| `Parameter arg_num -> Printf.sprintf "argument-%d" arg_num
| `Class -> "class"
| `ClassType -> "class-type"
| `File -> "file"
| `SourcePage -> "source"
let pp_kind fmt kind = Format.fprintf fmt "%s" (string_of_kind kind)
let pp_disambiguating_prefix fmt = function
| `Module | `Page | `LeafPage | `File | `SourcePage -> ()
| kind -> Format.fprintf fmt "%s-" (string_of_kind kind)
type t = { kind : kind; parent : t option; name : string }
let mk ?parent kind name = { kind; parent; name }
let rec from_identifier : any -> t =
fun x ->
match x with
| { iv = `Root (parent, unit_name); _ } ->
let parent =
match parent with
| Some p -> Some (from_identifier (p :> any))
| None -> None
in
let kind = `Module in
let name = ModuleName.to_string unit_name in
mk ?parent kind name
| { iv = `Page (parent, page_name); _ } ->
let parent =
match parent with
| Some p -> Some (from_identifier (p :> any))
| None -> None
in
let kind = `Page in
let name = PageName.to_string page_name in
mk ?parent kind name
| { iv = `LeafPage (parent, page_name); _ } ->
let parent =
match parent with
| Some p -> Some (from_identifier (p :> any))
| None -> None
in
let kind = `LeafPage in
let name = PageName.to_string page_name in
mk ?parent kind name
| { iv = `Module (parent, mod_name); _ } ->
let parent = from_identifier (parent :> any) in
let kind = `Module in
let name = ModuleName.to_string mod_name in
mk ~parent kind name
| { iv = `Parameter (functor_id, arg_name); _ } as p ->
let parent = from_identifier (functor_id :> any) in
let arg_num = Identifier.FunctorParameter.functor_arg_pos p in
let kind = `Parameter arg_num in
let name = ModuleName.to_string arg_name in
mk ~parent kind name
| { iv = `ModuleType (parent, modt_name); _ } ->
let parent = from_identifier (parent :> any) in
let kind = `ModuleType in
let name = ModuleTypeName.to_string modt_name in
mk ~parent kind name
| { iv = `Class (parent, name); _ } ->
let parent = from_identifier (parent :> any) in
let kind = `Class in
let name = TypeName.to_string name in
mk ~parent kind name
| { iv = `ClassType (parent, name); _ } ->
let parent = from_identifier (parent :> any) in
let kind = `ClassType in
let name = TypeName.to_string name in
mk ~parent kind name
| { iv = `Result p; _ } -> from_identifier (p :> any)
| { iv = `SourcePage (parent, name); _ } ->
let parent = from_identifier (parent :> any) in
let kind = `SourcePage in
mk ~parent kind name
| { iv = `AssetFile (parent, name); _ } ->
let parent = from_identifier (parent :> any) in
let kind = `File in
let name = AssetName.to_string name in
mk ~parent kind name
let from_identifier p = from_identifier (p : [< any_pv ] Identifier.id :> any)
let to_list url =
let rec loop acc { parent; name; kind } =
match parent with
| None -> (kind, name) :: acc
| Some p -> loop ((kind, name) :: acc) p
in
loop [] url
let of_list l =
let rec inner parent = function
| [] -> parent
| (kind, name) :: xs -> inner (Some { parent; name; kind }) xs
in
inner None l
let split :
is_dir:(kind -> [ `Always | `Never | `IfNotLast ]) ->
(kind * string) list ->
(kind * string) list * (kind * string) list =
fun ~is_dir l ->
let rec inner dirs = function
| [ ((kind, _) as x) ] when is_dir kind = `IfNotLast ->
(List.rev dirs, [ x ])
| ((kind, _) as x) :: xs when is_dir kind <> `Never ->
inner (x :: dirs) xs
| xs -> (List.rev dirs, xs)
in
inner [] l
let rec is_prefix (url1 : t) (url2 : t) =
match url1 with
| { kind = `LeafPage; parent = None; name = "index" } -> true
| { kind = `LeafPage; parent = Some p; name = "index" } -> is_prefix p url2
| _ -> (
if url1 = url2 then true
else
match url2 with
| { parent = Some parent; _ } -> is_prefix url1 parent
| { parent = None; _ } -> false)
end
module Anchor = struct
type kind =
[ Path.kind
| `Section
| `Type
| `Extension
| `ExtensionDecl
| `Exception
| `Method
| `Val
| `Constructor
| `Field
| `SourceAnchor ]
let string_of_kind : kind -> string = function
| #Path.kind as k -> Path.string_of_kind k
| `Section -> "section"
| `Type -> "type"
| `Extension -> "extension"
| `ExtensionDecl -> "extension-decl"
| `Exception -> "exception"
| `Method -> "method"
| `Val -> "val"
| `Constructor -> "constructor"
| `Field -> "field"
| `SourceAnchor -> "source-anchor"
let pp_kind fmt kind = Format.fprintf fmt "%s" (string_of_kind kind)
type t = { page : Path.t; anchor : string; kind : kind }
let anchorify_path { Path.parent; name; kind } =
match parent with
| None -> assert false
| Some page ->
let anchor = Printf.sprintf "%s-%s" (Path.string_of_kind kind) name in
{ page; anchor; kind = (kind :> kind) }
let add_suffix ~kind { page; anchor; _ } suffix =
{ page; anchor = anchor ^ "." ^ suffix; kind }
let mk ~kind parent str_name =
let page = Path.from_identifier parent in
{ page; anchor = str_name; kind }
let suffix_for_constructor x = x
let rec from_identifier : Identifier.t -> t = function
| { iv = `Module (parent, mod_name); _ } ->
let parent = Path.from_identifier (parent :> Path.any) in
let kind = `Module in
let anchor =
Printf.sprintf "%s-%s" (Path.string_of_kind kind)
(ModuleName.to_string mod_name)
in
{ page = parent; anchor; kind }
| { iv = `Root _; _ } as p ->
let page = Path.from_identifier (p :> Path.any) in
{ page; kind = `Module; anchor = "" }
| { iv = `Page _; _ } as p ->
let page = Path.from_identifier (p :> Path.any) in
{ page; kind = `Page; anchor = "" }
| { iv = `LeafPage _; _ } as p ->
let page = Path.from_identifier (p :> Path.any) in
{ page; kind = `LeafPage; anchor = "" }
| {
iv = `Parameter _ | `Result _ | `ModuleType _ | `Class _ | `ClassType _;
_;
} as p ->
anchorify_path @@ Path.from_identifier p
| { iv = `Type (parent, type_name); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
let kind = `Type in
{
page;
anchor =
Format.asprintf "%a-%s" pp_kind kind (TypeName.to_string type_name);
kind;
}
| { iv = `Extension (parent, name); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
let kind = `Extension in
{
page;
anchor =
Format.asprintf "%a-%s" pp_kind kind (ExtensionName.to_string name);
kind;
}
| { iv = `ExtensionDecl (parent, name, _); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
let kind = `ExtensionDecl in
{
page;
anchor =
Format.asprintf "%a-%s" pp_kind kind (ExtensionName.to_string name);
kind;
}
| { iv = `Exception (parent, name); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
let kind = `Exception in
{
page;
anchor =
Format.asprintf "%a-%s" pp_kind kind (ExceptionName.to_string name);
kind;
}
| { iv = `Value (parent, name); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
let kind = `Val in
{
page;
anchor =
Format.asprintf "%a-%s" pp_kind kind (ValueName.to_string name);
kind;
}
| { iv = `Method (parent, name); _ } ->
let str_name = MethodName.to_string name in
let page = Path.from_identifier (parent :> Path.any) in
let kind = `Method in
{ page; anchor = Format.asprintf "%a-%s" pp_kind kind str_name; kind }
| { iv = `InstanceVariable (parent, name); _ } ->
let str_name = InstanceVariableName.to_string name in
let page = Path.from_identifier (parent :> Path.any) in
let kind = `Val in
{ page; anchor = Format.asprintf "%a-%s" pp_kind kind str_name; kind }
| { iv = `Constructor (parent, name); _ } ->
let page = from_identifier (parent :> Identifier.t) in
let kind = `Constructor in
let suffix = suffix_for_constructor (ConstructorName.to_string name) in
add_suffix ~kind page suffix
| { iv = `Field (parent, name); _ } ->
let page = from_identifier (parent :> Identifier.t) in
let kind = `Field in
let suffix = FieldName.to_string name in
add_suffix ~kind page suffix
| { iv = `Label (parent, anchor); _ } -> (
let str_name = LabelName.to_string anchor in
match parent with
| { iv = `Type (gp, _); _ } -> mk ~kind:`Section gp str_name
| { iv = #Path.nonsrc_pv; _ } as p ->
mk ~kind:`Section (p :> Path.any) str_name)
| { iv = `SourceLocation (parent, loc); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
{ page; kind = `SourceAnchor; anchor = DefName.to_string loc }
| { iv = `SourceLocationInternal (parent, loc); _ } ->
let page = Path.from_identifier (parent :> Path.any) in
{ page; kind = `SourceAnchor; anchor = LocalName.to_string loc }
| { iv = `SourceLocationMod parent; _ } ->
let page = Path.from_identifier (parent :> Path.any) in
{ page; kind = `SourceAnchor; anchor = "" }
| { iv = `SourcePage _; _ } as p ->
let page = Path.from_identifier (p :> Path.any) in
{ page; kind = `Page; anchor = "" }
| { iv = `AssetFile _; _ } as p ->
let page = Path.from_identifier p in
{ page; kind = `File; anchor = "" }
let polymorphic_variant ~type_ident elt =
let name_of_type_constr te =
match te with
| Odoc_model.Lang.TypeExpr.Constr (path, _) ->
render_path (path :> Odoc_model.Paths.Path.t)
| _ ->
invalid_arg
"DocOckHtml.Url.Polymorphic_variant_decl.name_of_type_constr"
in
let url = from_identifier type_ident in
match elt with
| Odoc_model.Lang.TypeExpr.Polymorphic_variant.Type te ->
let kind = `Type in
let suffix = name_of_type_constr te in
add_suffix ~kind url suffix
| Constructor { name; _ } ->
let kind = `Constructor in
let suffix = suffix_for_constructor name in
add_suffix ~kind url suffix
(** The anchor looks like
[extension-decl-"Path.target_type"-FirstConstructor]. *)
let extension_decl (decl : Odoc_model.Lang.Extension.t) =
let page = Path.from_identifier (decl.parent :> Path.any) in
let kind = `ExtensionDecl in
let first_cons = Identifier.name (List.hd decl.constructors).id in
let anchor = Format.asprintf "%a-%s" pp_kind kind first_cons in
{ page; kind; anchor }
let source_anchor path anchor = { page = path; anchor; kind = `SourceAnchor }
end
type kind = Anchor.kind
type t = Anchor.t
let from_path page =
{ Anchor.page; anchor = ""; kind = (page.kind :> Anchor.kind) }
let from_identifier ~stop_before x =
match x with
| { Identifier.iv = #Path.any_pv; _ } as p when not stop_before ->
from_path @@ Path.from_identifier p
| p -> Anchor.from_identifier p
let from_asset_identifier p = from_path @@ Path.from_identifier p
let kind id =
let { Anchor.kind; _ } = Anchor.from_identifier id in
kind