Source file html_fragment_json.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
(* Rendering of HTML fragments together with metadata. For embedding the
   generated documentation in existing websites.
*)

module Html = Tyxml.Html
module Url = Odoc_document.Url

let json_of_breadcrumbs (breadcrumbs : Types.breadcrumb list) : Utils.Json.json
    =
  let breadcrumb (b : Types.breadcrumb) =
    `Object
      [
        ("name", `String b.name);
        ("href", `String b.href);
        ("kind", `String (Url.Path.string_of_kind b.kind));
      ]
  in
  let json_breadcrumbs = breadcrumbs |> List.map breadcrumb in
  `Array json_breadcrumbs

let json_of_toc (toc : Types.toc list) : Utils.Json.json =
  let rec section (s : Types.toc) =
    `Object
      [
        ("title", `String s.title_str);
        ("href", `String s.href);
        ("children", `Array (List.map section s.children));
      ]
  in
  let toc_json_list = toc |> List.map section in
  `Array toc_json_list

let make ~config ~preamble ~url ~breadcrumbs ~toc ~uses_katex ~source_anchor
    content children =
  let filename = Link.Path.as_filename ~is_flat:(Config.flat config) url in
  let filename = Fpath.add_ext ".json" filename in
  let htmlpp = Html.pp_elt ~indent:(Config.indent config) () in
  let json_to_string json = Utils.Json.to_string json in
  let source_anchor =
    match source_anchor with Some url -> `String url | None -> `Null
  in
  let content ppf =
    Format.pp_print_string ppf
      (json_to_string
         (`Object
           [
             ("type", `String "documentation");
             ("uses_katex", `Bool uses_katex);
             ("breadcrumbs", json_of_breadcrumbs breadcrumbs);
             ("toc", json_of_toc toc);
             ("source_anchor", source_anchor);
             ( "preamble",
               `String
                 (String.concat ""
                    (List.map (Format.asprintf "%a" htmlpp) preamble)) );
             ( "content",
               `String
                 (String.concat ""
                    (List.map (Format.asprintf "%a" htmlpp) content)) );
           ]))
  in
  { Odoc_document.Renderer.filename; content; children }

let make_src ~config ~url ~breadcrumbs content =
  let filename = Link.Path.as_filename ~is_flat:(Config.flat config) url in
  let filename = Fpath.add_ext ".json" filename in
  let htmlpp = Html.pp_elt ~indent:(Config.indent config) () in
  let json_to_string json = Utils.Json.to_string json in
  let content ppf =
    Format.pp_print_string ppf
      (json_to_string
         (`Object
           [
             ("type", `String "source");
             ("breadcrumbs", json_of_breadcrumbs breadcrumbs);
             ( "content",
               `String
                 (String.concat ""
                    (List.map (Format.asprintf "%a" htmlpp) content)) );
           ]))
  in
  { Odoc_document.Renderer.filename; content; children = [] }