@@ -8,18 +8,41 @@ module ModuleName = Odoc_model.Names.ModuleName
88
99type t = Entry .t Tree .t
1010
11- let compare_entry (e1 : Entry. t ) (e2 : Entry. t ) =
12- let int_of_kind ( kind : Entry.kind ) =
13- match kind with
14- | Page _ -> - 10
15- | Dir -> 0
11+ let compare_entry (t1 : t ) (t2 : t ) =
12+ let by_kind ( t : t ) =
13+ match t.node. kind with
14+ | Page _ when List. is_empty t.children -> - 10
15+ | Page _ | Dir -> 0
1616 | Module _ -> 10
1717 | Impl -> 20
1818 | _ -> 30
1919 in
20- match Int. compare (int_of_kind e1.kind) (int_of_kind e2.kind) with
21- | 0 -> Astring.String. compare (Id. name e1.id) (Id. name e2.id)
22- | i -> i
20+ (* Heuristic: If a dir contains only pages, place it before. *)
21+ let by_content (t : t ) =
22+ if
23+ List. for_all
24+ (fun x ->
25+ match x.Tree. node.Entry. kind with Page _ -> true | _ -> false )
26+ t.children
27+ then - 10
28+ else 10
29+ in
30+ let by_name (t : t ) =
31+ match t.node.kind with
32+ | Page { short_title = Some title ; _ } -> Comment. to_string title
33+ | _ -> (
34+ match t.node.id.iv with
35+ | `LeafPage (Some parent, name)
36+ when Names.PageName. to_string name = " index" ->
37+ Id. name parent
38+ | _ -> Id. name t.node.id)
39+ in
40+ let try_ comp f fallback =
41+ match comp (f t1) (f t2) with 0 -> fallback () | i -> i
42+ in
43+ try_ (compare : int -> int -> int ) by_kind @@ fun () ->
44+ try_ (compare : int -> int -> int ) by_content @@ fun () ->
45+ try_ Astring.String. compare by_name @@ fun () -> 0
2346
2447let rec t_of_in_progress (dir : In_progress.in_progress ) : t =
2548 let entry_of_page page =
@@ -158,7 +181,7 @@ let rec t_of_in_progress (dir : In_progress.in_progress) : t =
158181 |> List. map snd
159182 in
160183 let unordered =
161- List. sort (fun (_ , x ) (_ , y ) -> compare_entry x. Tree. node y.node ) unordered
184+ List. sort (fun (_ , x ) (_ , y ) -> compare_entry x y ) unordered
162185 in
163186 let contents = ordered @ unordered |> List. map snd in
164187 { Tree. node = index; children = contents }
0 commit comments