@@ -72,6 +72,7 @@ type odoc_artefact =
72
72
{ odoc_file : Path.Build .t
73
73
; odocl_file : Path.Build .t
74
74
; html_file : Path.Build .t
75
+ ; json_file : Path.Build .t
75
76
}
76
77
77
78
let add_rule sctx =
@@ -112,11 +113,40 @@ module Paths = struct
112
113
let toplevel_index ctx = html_root ctx ++ " index.html"
113
114
end
114
115
116
+ module Format = struct
117
+ type t =
118
+ | Html
119
+ | Json
120
+
121
+ let all = [ Html ; Json ]
122
+
123
+ let extension = function
124
+ | Html -> " .html"
125
+ | Json -> " .html.json"
126
+
127
+ let args = function
128
+ | Html -> Command.Args. empty
129
+ | Json -> A " --as-json"
130
+
131
+ let target format odoc_file =
132
+ match format with
133
+ | Html -> odoc_file.html_file
134
+ | Json -> odoc_file.json_file
135
+
136
+ let alias format ~dir =
137
+ match format with
138
+ | Html -> Alias. doc ~dir
139
+ | Json -> Alias. doc_json ~dir
140
+ end
141
+
115
142
module Dep : sig
116
143
(* * [html_alias ctx target] returns the alias that depends on all html targets
117
144
produced by odoc for [target] *)
118
145
val html_alias : Context .t -> target -> Alias .t
119
146
147
+ (* * XXX doc *)
148
+ val format_alias : Format .t -> Context .t -> target -> Alias .t
149
+
120
150
(* * [deps ctx pkg libraries] returns all odoc dependencies of [libraries]. If
121
151
[libraries] are all part of a package [pkg], then the odoc dependencies of
122
152
the package are also returned*)
@@ -130,7 +160,9 @@ module Dep : sig
130
160
These dependencies may be used using the [deps] function *)
131
161
val setup_deps : Context .t -> target -> Path.Set .t -> unit Memo .t
132
162
end = struct
133
- let html_alias ctx m = Alias. doc ~dir: (Paths. html ctx m)
163
+ let format_alias f ctx m = Format. alias f ~dir: (Paths. html ctx m)
164
+
165
+ let html_alias = format_alias Html
134
166
135
167
let alias = Alias. make (Alias.Name. of_string " .odoc-all" )
136
168
@@ -335,7 +367,7 @@ let setup_library_odoc_rules cctx (local_lib : Lib.Local.t) =
335
367
Dep. setup_deps ctx (Lib local_lib)
336
368
(Path.Set. of_list_map modules_and_odoc_files ~f: (fun (_ , p ) -> Path. build p))
337
369
338
- let setup_html sctx (odoc_file : odoc_artefact ) =
370
+ let setup_generate sctx (odoc_file : odoc_artefact ) format =
339
371
let ctx = Super_context. context sctx in
340
372
let open Memo.O in
341
373
let odoc_support_path = Paths. odoc_support ctx in
@@ -350,11 +382,15 @@ let setup_html sctx (odoc_file : odoc_artefact) =
350
382
; A " --theme-uri"
351
383
; Path (Path. build odoc_support_path)
352
384
; Dep (Path. build odoc_file.odocl_file)
353
- ; Hidden_targets [ odoc_file.html_file ]
385
+ ; Format. args format
386
+ ; Hidden_targets [ Format. target format odoc_file ]
354
387
]
355
388
in
356
389
add_rule sctx run_odoc
357
390
391
+ let setup_generate_all sctx odoc_file =
392
+ Memo. parallel_iter Format. all ~f: (setup_generate sctx odoc_file)
393
+
358
394
let setup_css_rule sctx =
359
395
let open Memo.O in
360
396
let ctx = Super_context. context sctx in
@@ -453,15 +489,18 @@ let create_odoc ctx ~target odoc_file =
453
489
match target with
454
490
| Lib _ ->
455
491
let html_dir = html_base ++ Stdune.String. capitalize basename in
456
- { odoc_file; odocl_file; html_file = html_dir ++ " index.html" }
492
+ let file format =
493
+ html_dir ++ " index"
494
+ |> Path.Build. extend_basename ~suffix: (Format. extension format)
495
+ in
496
+ { odoc_file; odocl_file; html_file = file Html ; json_file = file Json }
457
497
| Pkg _ ->
458
- { odoc_file
459
- ; odocl_file
460
- ; html_file =
461
- html_base
462
- ++ sprintf " %s.html"
463
- (basename |> String. drop_prefix ~prefix: " page-" |> Option. value_exn)
464
- }
498
+ let file format =
499
+ html_base
500
+ ++ (basename |> String. drop_prefix ~prefix: " page-" |> Option. value_exn)
501
+ |> Path.Build. extend_basename ~suffix: (Format. extension format)
502
+ in
503
+ { odoc_file; odocl_file; html_file = file Html ; json_file = file Json }
465
504
466
505
let static_html ctx =
467
506
let open Paths in
@@ -580,6 +619,20 @@ let setup_pkg_odocl_rules_def =
580
619
let setup_pkg_odocl_rules sctx ~pkg : unit Memo. t =
581
620
Memo.With_implicit_output. exec setup_pkg_odocl_rules_def (sctx, pkg)
582
621
622
+ let out_file (format : Format.t ) odoc =
623
+ match format with
624
+ | Html -> odoc.html_file
625
+ | Json -> odoc.json_file
626
+
627
+ let out_files ctx (format : Format.t ) odocs =
628
+ let extra_files =
629
+ match format with
630
+ | Html -> List. map ~f: Path. build (static_html ctx)
631
+ | Json -> []
632
+ in
633
+ List. rev_append extra_files
634
+ (List. map odocs ~f: (fun odoc -> Path. build (out_file format odoc)))
635
+
583
636
let setup_lib_html_rules_def =
584
637
let module Input = struct
585
638
module Super_context = Super_context. As_memo_key
@@ -596,12 +649,14 @@ let setup_lib_html_rules_def =
596
649
let f (sctx , lib ) =
597
650
let ctx = Super_context. context sctx in
598
651
let * odocs = odoc_artefacts sctx (Lib lib) in
599
- let * () = Memo. parallel_iter odocs ~f: (fun odoc -> setup_html sctx odoc) in
600
- let html_files = List. map ~f: (fun o -> Path. build o.html_file) odocs in
601
- let static_html = List. map ~f: Path. build (static_html ctx) in
602
- Rules.Produce.Alias. add_deps
603
- (Dep. html_alias ctx (Lib lib))
604
- (Action_builder. paths (List. rev_append static_html html_files))
652
+ let * () =
653
+ Memo. parallel_iter odocs ~f: (fun odoc -> setup_generate_all sctx odoc)
654
+ in
655
+ Memo. parallel_iter Format. all ~f: (fun format ->
656
+ let paths = out_files ctx format odocs in
657
+ Rules.Produce.Alias. add_deps
658
+ (Dep. format_alias format ctx (Lib lib))
659
+ (Action_builder. paths paths))
605
660
in
606
661
Memo.With_implicit_output. create " setup-library-html-rules"
607
662
~implicit_output: Rules. implicit_output
@@ -618,40 +673,41 @@ let setup_pkg_html_rules_def =
618
673
let * () = Memo. parallel_iter libs ~f: (setup_lib_html_rules sctx)
619
674
and * pkg_odocs =
620
675
let * pkg_odocs = odoc_artefacts sctx (Pkg pkg) in
621
- let + () = Memo. parallel_iter pkg_odocs ~f: (fun o -> setup_html sctx o ) in
676
+ let + () = Memo. parallel_iter pkg_odocs ~f: (setup_generate_all sctx) in
622
677
pkg_odocs
623
678
and * lib_odocs =
624
679
Memo. parallel_map libs ~f: (fun lib -> odoc_artefacts sctx (Lib lib))
625
680
in
626
681
let odocs = List. concat (pkg_odocs :: lib_odocs) in
627
- let html_files = List. map ~f: (fun o -> Path. build o.html_file) odocs in
628
- let static_html = List. map ~f: Path. build (static_html ctx) in
629
- Rules.Produce.Alias. add_deps
630
- (Dep. html_alias ctx (Pkg pkg))
631
- (Action_builder. paths ( List. rev_append static_html html_files ))
682
+ Memo. parallel_iter Format. all ~f: (fun format ->
683
+ let paths = out_files ctx format odocs in
684
+ Rules.Produce.Alias. add_deps
685
+ (Dep. format_alias format ctx (Pkg pkg))
686
+ (Action_builder. paths paths ))
632
687
in
633
688
setup_pkg_rules_def " setup-package-html-rules" f
634
689
635
690
let setup_pkg_html_rules sctx ~pkg : unit Memo. t =
636
691
Memo.With_implicit_output. exec setup_pkg_html_rules_def (sctx, pkg)
637
692
638
- let setup_package_aliases sctx (pkg : Package.t ) =
693
+ let setup_package_aliases_format sctx (pkg : Package.t ) ( format : Format .t ) =
639
694
let ctx = Super_context. context sctx in
640
695
let name = Package. name pkg in
641
696
let alias =
642
697
let pkg_dir = Package. dir pkg in
643
698
let dir = Path.Build. append_source ctx.build_dir pkg_dir in
644
- Alias. doc ~dir
645
- in
646
- let * libs =
647
- libs_of_pkg ctx ~pkg: name
648
- >> | List. map ~f: (fun lib -> Dep. html_alias ctx (Lib lib))
699
+ Format. alias format ~dir
649
700
in
650
- Dep. html_alias ctx (Pkg name) :: libs
701
+ let * libs = libs_of_pkg ctx ~pkg: name >> | List. map ~f: (fun lib -> Lib lib) in
702
+ Pkg name :: libs
703
+ |> List. map ~f: (Dep. format_alias format ctx)
651
704
|> Dune_engine.Dep.Set. of_list_map ~f: (fun f -> Dune_engine.Dep. alias f)
652
705
|> Action_builder. deps
653
706
|> Rules.Produce.Alias. add_deps alias
654
707
708
+ let setup_package_aliases sctx (pkg : Package.t ) =
709
+ Memo. parallel_iter Format. all ~f: (setup_package_aliases_format sctx pkg)
710
+
655
711
let default_index ~pkg entry_modules =
656
712
let b = Buffer. create 512 in
657
713
Printf. bprintf b " {0 %s index}\n " (Package.Name. to_string pkg);
0 commit comments