@@ -141,6 +141,13 @@ module Output_format = struct
141
141
| Html -> Alias. make Alias0. doc ~dir
142
142
| Json -> Alias. make Alias0. doc_json ~dir
143
143
;;
144
+
145
+ let toplevel_index_path format ctx =
146
+ let base = Paths. toplevel_index ctx in
147
+ match format with
148
+ | Html -> base
149
+ | Json -> Path.Build. extend_basename base ~suffix: " .json"
150
+ ;;
144
151
end
145
152
146
153
module Dep : sig
@@ -501,14 +508,46 @@ module Toplevel_index = struct
501
508
Paths. odoc_support_dirname
502
509
(html_list_items t)
503
510
;;
511
+
512
+ let string_to_json s = `String s
513
+ let list_to_json ~f l = `List (List. map ~f l)
514
+
515
+ let option_to_json ~f = function
516
+ | None -> `Null
517
+ | Some x -> f x
518
+ ;;
519
+
520
+ let item_to_json { name; version; link } =
521
+ `Assoc
522
+ [ " name" , string_to_json name
523
+ ; " version" , option_to_json ~f: string_to_json version
524
+ ; " link" , string_to_json link
525
+ ]
526
+ ;;
527
+
528
+ (* * This format is public API. *)
529
+ let to_json items = `Assoc [ " packages" , list_to_json items ~f: item_to_json ]
530
+
531
+ let json t = Dune_stats.Json. to_string (to_json t)
532
+
533
+ let content (output : Output_format.t ) t =
534
+ match output with
535
+ | Html -> html t
536
+ | Json -> json t
537
+ ;;
504
538
end
505
539
506
- let setup_toplevel_index_rule sctx =
540
+ let setup_toplevel_index_rule sctx output =
507
541
let * packages = Only_packages. get () in
508
542
let index = Toplevel_index. of_packages packages in
509
- let html = Toplevel_index. html index in
543
+ let content = Toplevel_index. content output index in
510
544
let ctx = Super_context. context sctx in
511
- add_rule sctx (Action_builder. write_file (Paths. toplevel_index ctx) html)
545
+ let path = Output_format. toplevel_index_path output ctx in
546
+ add_rule sctx (Action_builder. write_file path content)
547
+ ;;
548
+
549
+ let setup_toplevel_index_rules sctx =
550
+ Output_format. iter ~f: (setup_toplevel_index_rule sctx)
512
551
;;
513
552
514
553
let libs_of_pkg ctx ~pkg =
@@ -568,11 +607,6 @@ let create_odoc ctx ~target odoc_file =
568
607
{ odoc_file; odocl_file; html_file = file Html ; json_file = file Json }
569
608
;;
570
609
571
- let static_html ctx =
572
- let open Paths in
573
- [ odoc_support ctx; toplevel_index ctx ]
574
- ;;
575
-
576
610
let check_mlds_no_dupes ~pkg ~mlds =
577
611
match
578
612
List. rev_map mlds ~f: (fun mld ->
@@ -702,12 +736,13 @@ let out_file (output : Output_format.t) odoc =
702
736
let out_files ctx (output : Output_format.t ) odocs =
703
737
let extra_files =
704
738
match output with
705
- | Html -> List. map ~f: Path. build (static_html ctx)
739
+ | Html -> [ Path. build (Paths. odoc_support ctx) ]
706
740
| Json -> []
707
741
in
708
- List. rev_append
709
- extra_files
710
- (List. map odocs ~f: (fun odoc -> Path. build (out_file output odoc)))
742
+ Path. build (Output_format. toplevel_index_path output ctx)
743
+ :: List. rev_append
744
+ extra_files
745
+ (List. map odocs ~f: (fun odoc -> Path. build (out_file output odoc)))
711
746
;;
712
747
713
748
let setup_lib_html_rules_def =
@@ -911,7 +946,7 @@ let gen_rules sctx ~dir rest =
911
946
| [ " _html" ] ->
912
947
let ctx = Super_context. context sctx in
913
948
let directory_targets = Path.Build.Map. singleton (Paths. odoc_support ctx) Loc. none in
914
- has_rules ~directory_targets (setup_css_rule sctx >>> setup_toplevel_index_rule sctx)
949
+ has_rules ~directory_targets (setup_css_rule sctx >>> setup_toplevel_index_rules sctx)
915
950
| [ " _mlds" ; pkg ] ->
916
951
with_package pkg ~f: (fun pkg ->
917
952
let * _mlds, rules = package_mlds sctx ~pkg in
0 commit comments