• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ocaml / odoc / 2999

10 Jul 2025 10:05AM UTC coverage: 73.054% (-0.2%) from 73.263%
2999

push

github

jonludlam
Add explicit transitive dependency to odoc-md.opam

10373 of 14199 relevant lines covered (73.05%)

7148.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.23
/src/document/comment.ml
1
(*
2
 * Copyright (c) 2016, 2017 Thomas Refis <trefis@janestreet.com>
3
 *
4
 * Permission to use, copy, modify, and distribute this software for any
5
 * purpose with or without fee is hereby granted, provided that the above
6
 * copyright notice and this permission notice appear in all copies.
7
 *
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 *)
16

17
open Odoc_utils
18
open Types
19
module Comment = Odoc_model.Comment
20
open Odoc_model.Names
21

22
let default_lang_tag = "ocaml"
23

24
let source_of_code s =
25
  if s = "" then [] else [ Source.Elt [ inline @@ Inline.Text s ] ]
×
26

27
module Reference = struct
28
  open Odoc_model.Paths
29

30
  let rec render_resolved : Reference.Resolved.t -> string =
31
   fun r ->
32
    let open Reference.Resolved in
3,942✔
33
    match r with
34
    | `Identifier id -> Identifier.name id
3,197✔
35
    | `Alias (_, r) -> render_resolved (r :> t)
77✔
36
    | `AliasModuleType (_, r) -> render_resolved (r :> t)
2✔
37
    | `Module (r, s) -> render_resolved (r :> t) ^ "." ^ ModuleName.to_string s
160✔
38
    | `Hidden p -> render_resolved (p :> t)
×
39
    | `ModuleType (r, s) ->
78✔
40
        render_resolved (r :> t) ^ "." ^ ModuleTypeName.to_string s
78✔
41
    | `Type (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
194✔
42
    | `Constructor (r, s) ->
5✔
43
        render_resolved (r :> t) ^ "." ^ ConstructorName.to_string s
5✔
44
    | `PolyConstructor (r, s) ->
8✔
45
        render_resolved (r :> t) ^ ".`" ^ ConstructorName.to_string s
8✔
46
    | `Field (r, s) -> render_resolved (r :> t) ^ "." ^ FieldName.to_string s
×
47
    | `Extension (r, s) ->
3✔
48
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
3✔
49
    | `ExtensionDecl (r, _, s) ->
2✔
50
        render_resolved (r :> t) ^ "." ^ ExtensionName.to_string s
2✔
51
    | `Exception (r, s) ->
126✔
52
        render_resolved (r :> t) ^ "." ^ ExceptionName.to_string s
126✔
53
    | `Value (r, s) -> render_resolved (r :> t) ^ "." ^ ValueName.to_string s
36✔
54
    | `Class (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
×
55
    | `ClassType (r, s) -> render_resolved (r :> t) ^ "." ^ TypeName.to_string s
×
56
    | `Method (r, s) ->
×
57
        (* CR trefis: do we really want to print anything more than [s] here?  *)
58
        render_resolved (r :> t) ^ "." ^ MethodName.to_string s
×
59
    | `InstanceVariable (r, s) ->
×
60
        (* CR trefis: the following makes no sense to me... *)
61
        render_resolved (r :> t) ^ "." ^ InstanceVariableName.to_string s
×
62
    | `Label (_, s) -> LabelName.to_string s
54✔
63

64
  let render_path (tag, cs) =
65
    let tag =
8✔
66
      match tag with
67
      | `TRelativePath -> "./"
8✔
68
      | `TAbsolutePath -> "/"
×
69
      | `TCurrentPackage -> "//"
×
70
    in
71
    tag ^ String.concat ~sep:"/" cs
8✔
72

73
  let rec render_unresolved : Reference.t -> string =
74
    let open Reference in
75
    function
76
    | `Resolved r -> render_resolved r
6✔
77
    | `Root (n, _) -> n
381✔
78
    | `Dot (p, f) -> render_unresolved (p :> t) ^ "." ^ f
505✔
79
    | `Page_path p -> render_path p
×
80
    | `Asset_path p -> render_path p
7✔
81
    | `Module_path p -> render_path p
×
82
    | `Any_path p -> render_path p
1✔
83
    | `Module (p, f) ->
×
84
        render_unresolved (p :> t) ^ "." ^ ModuleName.to_string f
×
85
    | `ModuleType (p, f) ->
×
86
        render_unresolved (p :> t) ^ "." ^ ModuleTypeName.to_string f
×
87
    | `Type (p, f) -> render_unresolved (p :> t) ^ "." ^ TypeName.to_string f
×
88
    | `Constructor (p, f) ->
1✔
89
        render_unresolved (p :> t) ^ "." ^ ConstructorName.to_string f
1✔
90
    | `Field (p, f) -> render_unresolved (p :> t) ^ "." ^ FieldName.to_string f
×
91
    | `Extension (p, f) ->
×
92
        render_unresolved (p :> t) ^ "." ^ ExtensionName.to_string f
×
93
    | `ExtensionDecl (p, f) ->
×
94
        render_unresolved (p :> t) ^ "." ^ ExtensionName.to_string f
×
95
    | `Exception (p, f) ->
×
96
        render_unresolved (p :> t) ^ "." ^ ExceptionName.to_string f
×
97
    | `Value (p, f) -> render_unresolved (p :> t) ^ "." ^ ValueName.to_string f
×
98
    | `Class (p, f) -> render_unresolved (p :> t) ^ "." ^ TypeName.to_string f
×
99
    | `ClassType (p, f) ->
×
100
        render_unresolved (p :> t) ^ "." ^ TypeName.to_string f
×
101
    | `Method (p, f) ->
×
102
        render_unresolved (p :> t) ^ "." ^ MethodName.to_string f
×
103
    | `InstanceVariable (p, f) ->
×
104
        render_unresolved (p :> t) ^ "." ^ InstanceVariableName.to_string f
×
105
    | `Label (p, f) -> render_unresolved (p :> t) ^ "." ^ LabelName.to_string f
×
106

107
  (* This is the entry point. *)
108
  let to_ir : ?text:Inline.t -> Reference.t -> Inline.t =
109
   fun ?text ref ->
110
    match ref with
3,627✔
111
    | `Resolved r ->
3,245✔
112
        (* IDENTIFIER MUST BE RENAMED TO DEFINITION. *)
113
        let id = Reference.Resolved.identifier r in
114
        let rendered = render_resolved r in
3,245✔
115
        let content =
3,245✔
116
          match text with
117
          | None -> [ inline @@ Inline.Source (source_of_code rendered) ]
1,950✔
118
          | Some s -> s
1,295✔
119
        and tooltip =
120
          (* Add a tooltip if the content is not the rendered reference. *)
121
          match text with
122
          | None -> None
1,950✔
123
          | Some _ -> Some rendered
1,295✔
124
        in
125
        let url = Url.from_identifier ~stop_before:false id in
126
        let target = Target.Internal (Resolved url) in
3,245✔
127
        let link = { Link.target; content; tooltip } in
128
        [ inline @@ Inline.Link link ]
3,245✔
129
    | _ -> (
382✔
130
        let s = render_unresolved ref in
131
        match text with
382✔
132
        | None ->
380✔
133
            let s = source_of_code s in
134
            [ inline @@ Inline.Source s ]
380✔
135
        | Some content ->
2✔
136
            let link =
137
              { Link.target = Internal Unresolved; content; tooltip = Some s }
138
            in
139
            [ inline @@ Inline.Link link ])
2✔
140
end
141

142
let leaf_inline_element : Comment.leaf_inline_element -> Inline.one = function
143
  | `Space -> inline @@ Text " "
135,135✔
144
  | `Word s -> inline @@ Text s
145,499✔
145
  | `Code_span s -> inline @@ Source (source_of_code s)
5,495✔
146
  | `Math_span s -> inline @@ Math s
15✔
147
  | `Raw_markup (target, s) -> inline @@ Raw_markup (target, s)
17✔
148

149
let rec non_link_inline_element : Comment.non_link_inline_element -> Inline.one
150
    = function
151
  | #Comment.leaf_inline_element as e -> leaf_inline_element e
2,722✔
152
  | `Styled (style, content) ->
96✔
153
      inline @@ Styled (style, non_link_inline_element_list content)
96✔
154

155
and non_link_inline_element_list : _ -> Inline.t =
156
 fun elements ->
157
  List.map
1,518✔
158
    (fun elt -> non_link_inline_element elt.Odoc_model.Location_.value)
2,818✔
159
    elements
160

161
let link_content = non_link_inline_element_list
162

163
let rec inline_element : Comment.inline_element -> Inline.t = function
164
  | #Comment.leaf_inline_element as e -> [ leaf_inline_element e ]
283,439✔
165
  | `Styled (style, content) ->
653✔
166
      [ inline @@ Styled (style, inline_element_list content) ]
653✔
167
  | `Reference (path, content) ->
3,547✔
168
      (* TODO Rework that ugly function. *)
169
      (* TODO References should be set in code style, if they are to code
170
              elements. *)
171
      let content =
172
        match content with
173
        | [] -> None
2,250✔
174
        | _ -> Some (non_link_inline_element_list content)
1,297✔
175
        (* XXX Span *)
176
      in
177
      Reference.to_ir ?text:content path
178
  | `Link (target, content) ->
129✔
179
      let content =
180
        match content with
181
        | [] -> [ inline @@ Text target ]
34✔
182
        | _ -> non_link_inline_element_list content
95✔
183
      in
184
      [ inline @@ Link { target = External target; content; tooltip = None } ]
129✔
185

186
and inline_element_list elements =
187
  List.concat
14,084✔
188
  @@ List.map
14,084✔
189
       (fun elt -> inline_element elt.Odoc_model.Location_.value)
287,610✔
190
       elements
191

192
let module_references ms =
193
  let module_reference (m : Comment.module_reference) =
40✔
194
    let reference =
80✔
195
      Reference.to_ir (m.module_reference :> Odoc_model.Paths.Reference.t)
80✔
196
    and synopsis =
197
      match m.module_synopsis with
198
      | Some synopsis ->
24✔
199
          [
200
            block ~attr:[ "synopsis" ] @@ Inline (inline_element_list synopsis);
24✔
201
          ]
202
      | None -> []
56✔
203
    in
204
    { Description.attr = []; key = reference; definition = synopsis }
205
  in
206
  let items = List.map module_reference ms in
207
  block ~attr:[ "modules" ] @@ Description items
40✔
208

209
let rec nestable_block_element :
210
    Comment.nestable_block_element -> Block.one list =
211
 fun content ->
212
  match content with
11,336✔
213
  | `Paragraph p -> [ paragraph p ]
10,805✔
214
  | `Code_block (lang_tag, code, outputs) ->
117✔
215
      let lang_tag =
216
        match lang_tag with None -> default_lang_tag | Some t -> t
2✔
217
      in
218
      let rest =
219
        match outputs with
220
        | Some xs -> nestable_block_element_list xs
×
221
        | None -> []
117✔
222
      in
223
      [
224
        block
117✔
225
        @@ Source (lang_tag, source_of_code (Odoc_model.Location_.value code));
117✔
226
      ]
227
      @ rest
228
  | `Math_block s -> [ block @@ Math s ]
9✔
229
  | `Verbatim s -> [ block @@ Verbatim s ]
94✔
230
  | `Modules ms -> [ module_references ms ]
40✔
231
  | `List (kind, items) ->
188✔
232
      let kind =
233
        match kind with
234
        | `Unordered -> Block.Unordered
153✔
235
        | `Ordered -> Block.Ordered
35✔
236
      in
237
      let f = function
238
        | [ { Odoc_model.Location_.value = `Paragraph content; _ } ] ->
791✔
239
            [ block @@ Block.Inline (inline_element_list content) ]
791✔
240
        | item -> nestable_block_element_list item
45✔
241
      in
242
      let items = List.map f items in
243
      [ block @@ Block.List (kind, items) ]
188✔
244
  | `Table { data; align } ->
42✔
245
      let data =
246
        List.map
247
          (List.map (fun (cell, cell_type) ->
42✔
248
               (nestable_block_element_list cell, cell_type)))
268✔
249
          data
250
      in
251
      let generate_align data =
42✔
252
        let max (a : int) b = if a < b then b else a in
25✔
253
        (* Length of the longest line of the table *)
254
        let max_length =
255
          List.fold_left (fun m l -> max m (List.length l)) 0 data
51✔
256
        in
257
        let rec list_init i =
25✔
258
          if i <= 0 then [] else Table.Default :: list_init (i - 1)
25✔
259
        in
260
        list_init max_length
261
      in
262
      let align =
263
        match align with
264
        | None -> generate_align data
25✔
265
        | Some align ->
17✔
266
            List.map
17✔
267
              (function
268
                | None -> Table.Default
18✔
269
                | Some `Right -> Right
16✔
270
                | Some `Left -> Left
16✔
271
                | Some `Center -> Center)
16✔
272
              align
273
        (* We should also check wellness of number of table cells vs alignment,
274
           and raise warnings *)
275
      in
276
      [ block @@ Table { data; align } ]
42✔
277
  | `Media (href, media, content) ->
41✔
278
      let content =
279
        match (content, href) with
280
        | "", `Reference path ->
13✔
281
            Reference.render_unresolved (path :> Comment.Reference.t)
13✔
282
        | "", `Link href -> href
13✔
283
        | _ -> content
15✔
284
      in
285
      let url =
286
        match href with
287
        | `Reference (`Resolved r) -> (
9✔
288
            let id =
289
              Odoc_model.Paths.Reference.Resolved.Asset.(identifier (r :> t))
9✔
290
            in
291
            match Url.from_asset_identifier id with
292
            | url -> Target.Internal (Resolved url))
9✔
293
        | `Reference _ -> Internal Unresolved
10✔
294
        | `Link href -> External href
22✔
295
      in
296
      let i =
297
        match media with
298
        | `Audio -> Block.Audio (url, content)
9✔
299
        | `Video -> Video (url, content)
9✔
300
        | `Image -> Image (url, content)
23✔
301
      in
302
      [ block i ]
41✔
303

304
and paragraph : Comment.paragraph -> Block.one = function
305
  | [ { value = `Raw_markup (target, s); _ } ] ->
9✔
306
      block @@ Block.Raw_markup (target, s)
307
  | p -> block @@ Block.Paragraph (inline_element_list p)
11,545✔
308

309
and nestable_block_element_list :
310
    Comment.nestable_block_element Comment.with_location list -> Block.one list
311
    =
312
 fun elements ->
313
  elements
1,054✔
314
  |> List.map Odoc_model.Location_.value
315
  |> List.map nestable_block_element
1,054✔
316
  |> List.concat
1,054✔
317

318
let tag : Comment.tag -> Description.one =
319
 fun t ->
320
  let sp = inline (Text " ") in
846✔
321
  let item ?value ~tag definition =
846✔
322
    let tag_name = inline ~attr:[ "at-tag" ] (Text tag) in
846✔
323
    let tag_value = match value with None -> [] | Some t -> sp :: t in
189✔
324
    let key = tag_name :: tag_value in
325
    { Description.attr = [ tag ]; key; definition }
326
  in
327
  let mk_value desc = [ inline ~attr:[ "value" ] desc ] in
499✔
328
  let text_def s = [ block (Block.Inline [ inline @@ Text s ]) ] in
49✔
329
  let content_to_inline ?(prefix = []) content =
40✔
330
    match content with
56✔
331
    | None -> []
24✔
332
    | Some content -> prefix @ [ inline @@ Text content ]
32✔
333
  in
334
  match t with
335
  | `Author s -> item ~tag:"author" (text_def s)
16✔
336
  | `Deprecated content ->
59✔
337
      item ~tag:"deprecated" (nestable_block_element_list content)
59✔
338
  | `Param (name, content) ->
24✔
339
      let value = mk_value (Inline.Text name) in
340
      item ~tag:"parameter" ~value (nestable_block_element_list content)
24✔
341
  | `Raise (kind, content) ->
158✔
342
      let value = inline_element (kind :> Comment.inline_element) in
343
      item ~tag:"raises" ~value (nestable_block_element_list content)
158✔
344
  | `Return content -> item ~tag:"returns" (nestable_block_element_list content)
25✔
345
  | `See (kind, target, content) ->
443✔
346
      let value =
347
        match kind with
348
        | `Url ->
403✔
349
            mk_value
403✔
350
              (Inline.Link
351
                 {
352
                   target = External target;
353
                   content = [ inline @@ Text target ];
403✔
354
                   tooltip = None;
355
                 })
356
        | `File -> mk_value (Inline.Source (source_of_code target))
16✔
357
        | `Document -> mk_value (Inline.Text target)
24✔
358
      in
359
      item ~tag:"see" ~value (nestable_block_element_list content)
443✔
360
  | `Since s -> item ~tag:"since" (text_def s)
17✔
361
  | `Before (version, content) ->
32✔
362
      let value = mk_value (Inline.Text version) in
363
      item ~tag:"before" ~value (nestable_block_element_list content)
32✔
364
  | `Version s -> item ~tag:"version" (text_def s)
16✔
365
  | `Alert ("deprecated", content) ->
40✔
366
      let content = content_to_inline content in
367
      item ~tag:"deprecated" [ block (Block.Inline content) ]
40✔
368
  | `Alert (tag, content) ->
16✔
369
      let content = content_to_inline ~prefix:[ sp ] content in
370
      item ~tag:"alert"
16✔
371
        [ block (Block.Inline ([ inline @@ Text tag ] @ content)) ]
16✔
372

373
let attached_block_element : Comment.attached_block_element -> Block.t =
374
  function
375
  | #Comment.nestable_block_element as e -> nestable_block_element e
10,245✔
376
  | `Tag t -> [ block ~attr:[ "at-tags" ] @@ Description [ tag t ] ]
846✔
377

378
(* TODO collaesce tags *)
379

380
let block_element : Comment.block_element -> Block.t = function
381
  | #Comment.attached_block_element as e -> attached_block_element e
8,855✔
382
  | `Heading (_, _, text) ->
202✔
383
      (* We are not supposed to receive Heading in this context.
384
         TODO: Remove heading in attached documentation in the model *)
385
      [ block @@ Paragraph (inline_element_list text) ]
202✔
386

387
let heading_level_to_int = function
388
  | `Title -> 0
70✔
389
  | `Section -> 1
522✔
390
  | `Subsection -> 2
126✔
391
  | `Subsubsection -> 3
124✔
392
  | `Paragraph -> 4
19✔
393
  | `Subparagraph -> 5
8✔
394

395
let heading
396
    (attrs, { Odoc_model.Paths.Identifier.iv = `Label (_, label); _ }, text) =
397
  let label = Odoc_model.Names.LabelName.to_string label in
869✔
398
  let title = inline_element_list text in
869✔
399
  let level = heading_level_to_int attrs.Comment.heading_level in
869✔
400
  let label = Some label in
869✔
401
  let source_anchor = None in
402
  Item.Heading { label; level; title; source_anchor }
403

404
let item_element : Comment.block_element -> Item.t list = function
405
  | #Comment.attached_block_element as e ->
962✔
406
      [ Item.Text (attached_block_element e) ]
962✔
407
  | `Heading h -> [ heading h ]
32✔
408

409
(** The documentation of the expansion is used if there is no comment attached
410
    to the declaration. *)
411
let synopsis ~decl_doc ~expansion_doc =
412
  let ([], Some docs | docs, _) = (decl_doc, expansion_doc) in
3,426✔
413
  match Comment.synopsis docs with Some p -> [ paragraph p ] | None -> []
749✔
414

415
let standalone docs =
416
  List.concat_map item_element
6,572✔
417
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
994✔
418

419
let to_ir (docs : Comment.elements) =
420
  List.concat_map block_element
10,236✔
421
  @@ List.map (fun x -> x.Odoc_model.Location_.value) docs
9,057✔
422

423
let has_doc docs = docs <> []
590✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc