package rpclib-html

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

Source file htmlgen.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
open Rpc.Types
open Idl
open Codegen
open Cow.Html

(* Printable string of type *)
let rec html_of_t : type a. a typ -> string list =
  let of_basic : type b. b basic -> string = function
    | Int -> "int"
    | Int32 -> "int32"
    | Int64 -> "int64"
    | Bool -> "bool"
    | Float -> "float"
    | String -> "string"
    | Char -> "char"
  in
  let print txt = [ txt ] in
  function
  | Basic b -> print (of_basic b)
  | DateTime -> print (of_basic String)
  | Base64 -> print (of_basic String)
  | Struct _ -> print "struct  { ... }"
  | Variant _ -> print "variant { ... }"
  | Array t -> html_of_t t @ print " list"
  | List t -> html_of_t t @ print " list"
  | Dict (key, v) ->
    print (Printf.sprintf "(%s * " (of_basic key)) @ html_of_t v @ print ") list"
  | Unit -> print "unit"
  | Option x -> html_of_t x @ print " option"
  | Tuple (a, b) -> html_of_t a @ print " * " @ html_of_t b
  | Tuple3 (a, b, c) ->
    html_of_t a @ print " * " @ html_of_t b @ print " * " @ html_of_t c
  | Tuple4 (a, b, c, d) ->
    html_of_t a
    @ print " * "
    @ html_of_t b
    @ print " * "
    @ html_of_t c
    @ print " * "
    @ html_of_t d
  | Abstract _ -> print "<abstract>"


(* Function inputs and outputs in a table *)
let of_args args =
  let row_of_arg (is_in, Param.Boxed arg) =
    let name = arg.Param.name in
    let direction = if is_in then "in" else "out" in
    let ty = html_of_t arg.Param.typedef.ty |> List.map string in
    let description = arg.Param.description in
    tag
      "tr"
      (list
         [ tag
             "td"
             (tag
                "code"
                (string
                   (match name with
                   | Some s -> s
                   | None -> "unnamed")))
         ; tag "td" (string direction)
         ; tag "td" (tag "code" (list ty))
         ; tag "td" (string (String.concat " " description))
         ])
  in
  tag
    "table"
    ~attrs:[ "width", "100%" ]
    (list
       [ tag
           "thead"
           (tag
              "tr"
              (list
                 [ tag "th" (string "Name")
                 ; tag "th" (string "Direction")
                 ; tag "th" (string "Type")
                 ; tag "th" (string "Description")
                 ]))
       ; tag "tbody" (list (List.map row_of_arg args))
       ])


let sidebar x =
  let of_typedef (BoxedDef t) =
    let target = Printf.sprintf "#a-%s" t.name in
    let name = t.name in
    tag "li" (a ~href:(Uri.of_string target) (string name))
  in
  let of_method iname (BoxedFunction m) =
    let target = Printf.sprintf "#a-%s-%s" iname m.Method.name in
    let name = m.Method.name in
    tag "li" (a ~href:(Uri.of_string target) (string name))
  in
  let of_interface i =
    let name = i.Interface.details.Idl.Interface.name in
    [ tag "li" ~cls:"docs-nav-title" (string name) ]
    @ List.map (of_method i.Interface.details.Idl.Interface.name) i.Interface.methods
  in
  div
    ~cls:"large-3 medium-3 columns"
    (tag
       "ul"
       ~cls:"menu vertical"
       (list
          ([ tag "li" ~cls:"docs-nav-title" (string "types") ]
          @ List.map of_typedef x.Interfaces.type_decls
          @ List.concat (List.map of_interface x.Interfaces.interfaces))))


let of_struct_fields : 'a boxed_field list -> Cow.Html.t =
 fun all ->
  let of_row (BoxedField f) =
    let ty = html_of_t f.field in
    tag
      "tr"
      (list
         [ tag "td" (tag "pre" (string f.fname))
         ; tag "td" (tag "pre" (string (String.concat "" ty)))
         ; tag "td" (string (String.concat " " f.fdescription))
         ])
  in
  tag
    "table"
    ~attrs:[ "width", "100%" ]
    (list
       [ tag
           "thead"
           (tag
              "tr"
              (list
                 [ tag "th" (string "Name")
                 ; tag "th" (string "Type")
                 ; tag "th" (string "Description")
                 ]))
       ; tag "tbody" (list (List.map of_row all))
       ])


(* TODO: Unify with the above! *)
let of_variant_tags : 'a boxed_tag list -> Cow.Html.t =
 fun all ->
  let of_row (BoxedTag t) =
    let ty = html_of_t t.tcontents in
    tag
      "tr"
      (list
         [ tag "td" (tag "pre" (string t.tname))
         ; tag "td" (tag "pre" (string (String.concat "" ty)))
         ; tag "td" (string (String.concat " " t.tdescription))
         ])
  in
  tag
    "table"
    ~attrs:[ "width", "100%" ]
    (list
       [ tag
           "thead"
           (tag
              "tr"
              (list
                 [ tag "th" (string "Name")
                 ; tag "th" (string "Type")
                 ; tag "th" (string "Description")
                 ]))
       ; tag "tbody" (list (List.map of_row all))
       ])


let of_type_decl _ (BoxedDef t) =
  let anchor = Printf.sprintf "a-%s" t.name in
  let name = t.name in
  let defn = String.concat "" (html_of_t t.ty) in
  let description = t.description in
  let common =
    [ h4 ~id:anchor (string (Printf.sprintf "type %s = %s" name defn))
    ; p (string (String.concat " " description))
    ]
  in
  let rest =
    match t.ty with
    | Struct structure -> [ p (string "Members:"); of_struct_fields structure.fields ]
    | Variant variant -> [ p (string "Constructors:"); of_variant_tags variant.variants ]
    | _ -> []
  in
  common @ rest


let tabs_of _ i m =
  let mname = m.Method.name in
  let hash_defn = "#defn-" ^ mname in
  let hash_ocaml = "#ocaml-" ^ mname in
  let hash_python = "#python-" ^ mname in
  let id_tab = "tab-" ^ mname in
  let id_defn = "defn-" ^ mname in
  let id_ocaml = "ocaml-" ^ mname in
  let id_python = "python-" ^ mname in
  [ ul
      ~cls:"tabs"
      ~id:id_tab
      ~attrs:[ "data-tabs", "" ]
      [ li
          ~cls:"tabs-title is-active"
          (tag
             "a"
             ~attrs:[ "href", hash_defn; "aria-selected", "true" ]
             (string "Definition"))
      ; li ~cls:"tabs-title" (a ~href:(Uri.of_string hash_ocaml) (string "OCaml example"))
      ; li
          ~cls:"tabs-title"
          (a ~href:(Uri.of_string hash_python) (string "Python example"))
      ]
  ; div
      ~cls:"tabs-content"
      ~attrs:[ "data-tabs-content", id_tab ]
      (list
         [ div
             ~cls:"tabs-panel is-active"
             ~id:id_defn
             (of_args
                (List.map (fun p -> true, p) Method.(find_inputs m.ty)
                @ [ (false, Method.(find_output m.ty)) ]))
         ; div
             ~cls:"tabs-panel"
             ~id:id_ocaml
             (list [ h4 (string "Client:"); h4 (string "Server:") ])
         ; div
             ~cls:"tabs-panel"
             ~id:id_python
             (list
                [ h4 (string "Client:")
                ; tag
                    "pre"
                    ~cls:"prettyprint lang-py"
                    (string
                       (Pythongen.example_stub_user i (BoxedFunction m)
                       |> Pythongen.string_of_ts))
                ; h4 (string "Server:")
                ; tag
                    "pre"
                    ~cls:"prettyprint lang-py"
                    (string
                       (Pythongen.example_skeleton_user i (BoxedFunction m)
                       |> Pythongen.string_of_ts))
                ])
         ])
  ]


let of_method is i (Codegen.BoxedFunction m) =
  let anchor =
    Printf.sprintf "a-%s-%s" i.Interface.details.Idl.Interface.name m.Method.name
  in
  let name = m.Method.name in
  let description = m.Method.description in
  [ h3 ~id:anchor (string name); p (string (String.concat " " description)) ]
  @ tabs_of is i m


let of_interface is i =
  let name = i.Interface.details.Idl.Interface.name in
  let anchor = "a-" ^ name in
  let description = i.Interface.details.Idl.Interface.description in
  [ h2 ~id:anchor (string name); p (string (String.concat " " description)) ]
  @ List.concat (List.map (of_method is i) i.Interface.methods)


(*
let of_exception ts =
  let row_of t =
    let ident = ident_of_type_decl t in
    let name = [ `Data (String.concat "/" ident.Ident.name) ] in
    let ty = [ `Data (Type.ocaml_of_t ident.Ident.original_ty) ] in
    let description = [ `Data ident.Ident.description ] in
    <:html<
      <tr>
        <td><pre>$name$</pre></td>
        <td><pre>$ty$</pre></td>
        <td>$description$</td>
      </tr>
    >> in
  <:html<
    <h3 id="a-exceptions">exceptions</h3>
    <table width="100%">
      <thead>
        <tr>
          <th>Name</th>
          <th>Type</th>
          <th>Description</th>
        </tr>
     </thead>
     <tbody>
       $List.concat (List.map row_of ts)$
     </tbody>
    </table>
  >>
*)

let of_interfaces x =
  let name = x.Interfaces.name in
  let description = x.Interfaces.description in
  div
    ~cls:"row"
    (list
       [ sidebar x
       ; div
           ~cls:"large-9 medium-9 columns"
           (list
              ([ h1 (string name); p (string (String.concat " " description)) ]
              @ List.concat (List.map (of_type_decl None) x.Interfaces.type_decls)
              @ List.concat (List.map (of_interface x) x.Interfaces.interfaces)))
       ])


let to_string x = Cow.Html.to_string (of_interfaces x)