package odoc

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file tree.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
(*
 * Copyright (c) 2016 Thomas Refis <trefis@janestreet.com>
 *
 * 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 Html = Tyxml.Html
module Paths = Odoc_model.Paths

open Odoc_model.Names

type syntax = OCaml | Reason

type kind = [ `Arg | `Mod | `Mty | `Class | `Cty | `Page ]

let string_of_syntax = function
  | OCaml -> "ml"
  | Reason -> "re"

type uri =
  | Absolute of string
  | Relative of string

type t = {
  name : string;
  content : [ `Html ] Html.elt;
  children : t list
}

let path = Stack.create ()

let stack_to_list s =
  let acc = ref [] in
  Stack.iter (fun x -> acc := x :: !acc) s;
  !acc

let enter ?kind name = Stack.push (name, kind) path
let leave () = ignore @@ Stack.pop path

(* FIXME: reuse [Url.kind] *)
let stack_elt_to_path_fragment = function
  | (name, None)
  | (name, Some `Page) -> name (* fixme? *)
  | (name, Some `Mod) -> name
  | (name, Some `Mty) -> "module-type-" ^ name
  | (name, Some `Arg) -> "argument-" ^ name
  | (name, Some `Class) -> "class-" ^ name
  | (name, Some `Cty) -> "class-type-" ^ name

module Relative_link = struct
  open Odoc_model.Paths

  let semantic_uris = ref false

  module Id : sig
    exception Not_linkable
    exception Can't_stop_before

    val href : ?xref_base_uri:string -> stop_before:bool -> Identifier.t -> string
  end = struct
    exception Not_linkable

    let rec drop_shared_prefix l1 l2 =
      match l1, l2 with
      | l1 :: l1s, l2 :: l2s when l1 = l2 ->
        drop_shared_prefix l1s l2s
      | _, _ -> l1, l2

    exception Can't_stop_before

    let href ?xref_base_uri ~stop_before id =
      match xref_base_uri, Url.from_identifier ~stop_before id with
      (* If xref_base_uri is defined, do not perform relative URI resolution. *)
      | Some xref_base_uri, Ok { Url. page; anchor; kind } ->
        let absolute_target =
          List.rev (
            if !semantic_uris || kind = "page" then
              page
            else
              "index.html" :: page
          )
        in
        let page = xref_base_uri ^ String.concat "/" absolute_target in
        begin match anchor with
        | "" -> page
        | anchor -> page ^ "#" ^ anchor
        end
      | None, Ok { Url. page; anchor; kind } ->
        let target =
          List.rev (
            if !semantic_uris || kind = "page" then
              page
            else
              "index.html" :: page
          )
        in
        let current_loc =
          let path =
            match Stack.top path with
            | (_, Some `Page) ->
              (* Sadness. *)
              let s = Stack.copy path in
              ignore (Stack.pop s);
              s
            | _ -> path
          in
          List.map stack_elt_to_path_fragment (stack_to_list path)
        in
        let current_from_common_ancestor, target_from_common_ancestor =
          drop_shared_prefix current_loc target
        in
        let relative_target =
          List.map (fun _ -> "..") current_from_common_ancestor
          @ target_from_common_ancestor
        in
        let page = String.concat "/" relative_target in
        begin match anchor with
        | "" -> page
        | anchor -> page ^ "#" ^ anchor
        end
      | _, Error e ->
        (* TODO: handle errors better, perhaps by returning a [result] *)
        match e with
        | Not_linkable _ -> raise Not_linkable
        | otherwise ->
          Printf.eprintf "%s\n%!" (Url.Error.to_string otherwise);
          exit 1
  end

  module Of_path = struct
    let rec to_html : stop_before:bool -> Path.t -> _ =
      fun ~stop_before path ->
        match path with
        | `Root root -> [ Html.txt root ]
        | `Forward root -> [ Html.txt root ] (* FIXME *)
        | `Dot (prefix, suffix) ->
          let link = to_html ~stop_before:true (prefix :> Path.t) in
          link @ [ Html.txt ("." ^ suffix) ]
        | `Apply (p1, p2) ->
          let link1 = to_html ~stop_before (p1 :> Path.t) in
          let link2 = to_html ~stop_before (p2 :> Path.t) in
          link1 @ Html.txt "(":: link2 @ [ Html.txt ")" ]
        | `Resolved rp ->
          let id = Path.Resolved.identifier rp in
          let txt = Url.render_path path in
          begin match Id.href ~stop_before id with
          | href -> [ Html.a ~a:[ Html.a_href href ] [ Html.txt txt ] ]
          | exception Id.Not_linkable -> [ Html.txt txt ]
          | exception exn ->
            Printf.eprintf "Id.href failed: %S\n%!" (Printexc.to_string exn);
            [ Html.txt txt ]
          end
  end

  module Of_fragment = struct
    let dot prefix suffix =
      match prefix with
      | "" -> suffix
      | _  -> prefix ^ "." ^ suffix

    let rec render_raw : Fragment.t -> string =
      fun fragment ->
        match fragment with
        | `Resolved rr -> render_resolved rr
        | `Dot (prefix, suffix) -> dot (render_raw (prefix :> Fragment.t)) suffix

    and render_resolved : Fragment.Resolved.t -> string =
      let open Fragment.Resolved in
      fun fragment ->
        match fragment with
        | `Root -> ""
        | `Subst (_, rr) -> render_resolved (rr :> t)
        | `SubstAlias (_, rr) -> render_resolved (rr :> t)
        | `Module (rr, s) -> dot (render_resolved (rr :> t)) (ModuleName.to_string s)
        | `Type (rr, s) -> dot (render_resolved (rr :> t)) (TypeName.to_string s)
        | `Class (rr, s) -> dot (render_resolved ( rr :> t)) (ClassName.to_string s)
        | `ClassType (rr, s) -> dot (render_resolved (rr :> t)) (ClassTypeName.to_string s)

    let rec to_html : stop_before:bool ->
      Identifier.Signature.t -> Fragment.t -> _ =
      fun ~stop_before id fragment ->
        let open Fragment in
        match fragment with
        | `Resolved `Root ->
          begin match Id.href ~stop_before:true (id :> Identifier.t) with
          | href ->
            [Html.a ~a:[Html.a_href href] [Html.txt (Identifier.name id)]]
          | exception Id.Not_linkable -> [ Html.txt (Identifier.name id) ]
          | exception exn ->
            Printf.eprintf "[FRAG] Id.href failed: %S\n%!" (Printexc.to_string exn);
            [ Html.txt (Identifier.name id) ]
          end
        | `Resolved rr ->
          let id = Resolved.identifier id (rr :> Resolved.t) in
          let txt = render_resolved rr in
          begin match Id.href ~stop_before id with
          | href ->
            [ Html.a ~a:[ Html.a_href href ] [ Html.txt txt ] ]
          | exception Id.Not_linkable -> [ Html.txt txt ]
          | exception exn ->
            Printf.eprintf "[FRAG] Id.href failed: %S\n%!" (Printexc.to_string exn);
            [ Html.txt txt ]
          end
        | `Dot (prefix, suffix) ->
          let link = to_html ~stop_before:true id (prefix :> Fragment.t) in
          link @ [ Html.txt ("." ^ suffix) ]
  end

  let of_path ~stop_before p =
    Of_path.to_html ~stop_before p

  let of_fragment ~base frag =
    Of_fragment.to_html ~stop_before:false base frag

  let to_sub_element ~kind name =
    (* FIXME: Reuse [Url]. *)
    let prefix =
      match kind with
      | `Mod   -> ""
      | `Mty   -> "module-type-"
      | `Arg   -> "argument-"
      | `Class -> "class-"
      | `Cty   -> "class-type-"
      | `Page  -> assert false
    in
    Html.a_href (prefix ^ name ^ (if !semantic_uris then "" else "/index.html"))
end

let render_fragment = Relative_link.Of_fragment.render_raw

let page_creator ?kind ?(theme_uri = Relative "./") ~path header_docs content =
  let rec add_dotdot ~n acc =
    if n <= 0 then
      acc
    else
      add_dotdot ~n:(n - 1) ("../" ^ acc)
  in
  let resolve_relative_uri uri =
    (* Remove the first "dot segment". *)
    let uri =
      if String.length uri >= 2 && String.sub uri 0 2 = "./" then
        String.sub uri 2 (String.length uri - 2)
      else uri
    in
    (* How deep is this page? *)
    let n =
      List.length path - (
        (* This is just horrible. *)
        match kind with
        | Some `Page -> 1
        | _ -> 0)
    in
    add_dotdot uri ~n
  in

  let name = List.hd @@ List.rev path in
  let head : Html_types.head Html.elt =
    let title_string = Printf.sprintf "%s (%s)" name (String.concat "." path) in

    let theme_uri =
      match theme_uri with
      | Absolute uri -> uri
      | Relative uri -> resolve_relative_uri uri
    in

    let support_files_uri = resolve_relative_uri "./" in

    let odoc_css_uri = theme_uri ^ "odoc.css" in
    let highlight_js_uri = support_files_uri ^ "highlight.pack.js" in

    Html.head (Html.title (Html.txt title_string)) [
      Html.link ~rel:[`Stylesheet] ~href:odoc_css_uri () ;
      Html.meta ~a:[ Html.a_charset "utf-8" ] () ;
      Html.meta ~a:[ Html.a_name "generator";
                     Html.a_content "odoc 1.5.3" ] ();
      Html.meta ~a:[ Html.a_name "viewport";
                  Html.a_content "width=device-width,initial-scale=1.0"; ] ();
      Html.script ~a:[Html.a_src highlight_js_uri] (Html.txt "");
      Html.script (Html.txt "hljs.initHighlightingOnLoad();");
    ]
  in

  let wrapped_content : (Html_types.div_content Html.elt) list =
    let title_prefix =
      match kind with
      | None
      | Some `Mod -> Some "Module"
      | Some `Arg -> Some "Parameter"
      | Some `Mty -> Some "Module type"
      | Some `Cty -> Some "Class type"
      | Some `Class -> Some "Class"
      | Some `Page -> None
    in

    let header_docs =
      match title_prefix with
      | None ->
        header_docs
      | Some prefix ->
        let title_heading =
          Html.h1 [
            Html.txt @@ prefix ^ " ";
            Html.code [
              (* Shorten path to at most 2 levels *)
              match List.tl path |> List.rev with
              | y :: x :: _ -> Html.txt @@ x ^ "." ^ y
              | x :: _ -> Html.txt x
              | _ -> Html.txt "" (* error *)
            ]
          ]
        in
        title_heading::header_docs
    in

    let header_content =
      let dot = if !Relative_link.semantic_uris then "" else "index.html" in
      let dotdot = add_dotdot ~n:1 dot in
      let up_href = match kind with
      | Some `Page when name <> "index" -> dot
      | _ -> dotdot
      in
      let has_parent = List.length path > 1 in
      if has_parent then
        let nav =
          Html.nav @@ [
            Html.a ~a:[Html.a_href up_href] [
              Html.txt "Up"
            ];
            Html.txt " – "
          ] @
            (* Create breadcrumbs *)
            let space = Html.txt " " in
            let breadcrumb_spec = match kind with
            | Some `Page -> (fun n x -> n, dot, x)
            | _ -> (fun n x -> n, add_dotdot ~n dot, x)
            in
            let rev_path = match kind with
            | Some `Page when name = "index" -> List.tl (List.rev path)
            | _ -> List.rev path
            in
            rev_path |>
            List.mapi breadcrumb_spec |>
            List.rev |>
            Utils.list_concat_map ?sep:(Some([space; Html.entity "#x00BB"; space]))
              ~f:(fun (n, addr, lbl) ->
                if n > 0 then
                  [[Html.a ~a:[Html.a_href addr] [Html.txt lbl]]]
                else
                  [[Html.txt lbl]]
                ) |>
            List.flatten
        in
        nav::header_docs
      else
        header_docs
    in

    let header = Html.header header_content in

    [Html.div ~a:[Html.a_class ["content"]] (header::content)]
  in

  let html : [ `Html ] Html.elt = Html.html head (Html.body wrapped_content) in

  html

let make ?(header_docs = []) ?theme_uri content children =
  assert (not (Stack.is_empty path));
  let name    = stack_elt_to_path_fragment (Stack.top path) in
  let kind    = snd (Stack.top path) in
  let path    = List.map fst (stack_to_list path) in
  let content = page_creator ?kind ?theme_uri ~path header_docs content in
  { name; content; children }

let traverse ~f t =
  let rec aux parents node =
    f ~parents node.name node.content;
    List.iter (aux (node.name :: parents)) node.children
  in
  aux [] t

let open_details = ref true
OCaml

Innovation. Community. Security.