• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ocaml / odoc / 2403

02 Oct 2024 03:15PM UTC coverage: 72.971% (+0.1%) from 72.848%
2403

Pull #1193

github

web-flow
Merge 93aa604a2 into 0ba1fdbe6
Pull Request #1193: Specify children order in frontmatter

140 of 154 new or added lines in 10 files covered. (90.91%)

60 existing lines in 5 files now uncovered.

10248 of 14044 relevant lines covered (72.97%)

2965.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.75
/src/model/sidebar.ml
1
open Odoc_utils
2
open Paths.Identifier
3

4
module CPH = Hashtbl.ContainerPage
5
module LPH = Hashtbl.LeafPage
6

7
type page = Page.t
8
type leaf_page = LeafPage.t
9
type container_page = ContainerPage.t
10

11
module PageToc = struct
12
  type title = Comment.link_content
13
  type children_order = Frontmatter.child list Location_.with_location
14

15
  type payload = { title : title; children_order : children_order option }
16

17
  type dir_content = { leafs : payload LPH.t; dirs : in_progress CPH.t }
18
  and in_progress = container_page option * dir_content
19

20
  let empty_t dir_id = (dir_id, { leafs = LPH.create 10; dirs = CPH.create 10 })
15✔
21

22
  let get_parent id : container_page option =
23
    let id :> page = id in
35✔
24
    match id.iv with
25
    | `Page (Some parent, _) -> Some parent
15✔
26
    | `LeafPage (Some parent, _) -> Some parent
10✔
NEW
27
    | `Page (None, _) | `LeafPage (None, _) -> None
×
28

29
  let find_leaf ((_, dir_content) : in_progress) leaf_page =
30
    try Some (LPH.find dir_content.leafs leaf_page) with Not_found -> None
4✔
31

32
  let leafs (_, dir_content) =
33
    LPH.fold
15✔
34
      (fun id { title = payload; _ } acc ->
35
        if Astring.String.equal "index" (Paths.Identifier.name id) then acc
4✔
36
        else (id, payload) :: acc)
6✔
37
      dir_content.leafs []
38

39
  let dirs (_, dir_content) =
40
    CPH.fold (fun id payload acc -> (id, payload) :: acc) dir_content.dirs []
6✔
41

42
  let rec get_or_create (dir : in_progress) (id : container_page) : in_progress
43
      =
44
    let _, { dirs = parent_dirs; _ } =
25✔
45
      match get_parent id with
46
      | Some parent -> get_or_create dir parent
15✔
47
      | None -> dir
10✔
48
    in
49
    let current_item =
50
      try Some (CPH.find parent_dirs id) with Not_found -> None
6✔
51
    in
52
    match current_item with
53
    | Some item -> item
19✔
54
    | None ->
6✔
55
        let new_ = empty_t (Some id) in
56
        CPH.add parent_dirs id new_;
6✔
57
        new_
6✔
58

59
  let add (dir : in_progress) ((id : leaf_page), title, children_order) =
60
    let _, dir_content =
10✔
61
      match get_parent id with
62
      | Some parent -> get_or_create dir parent
10✔
NEW
63
      | None -> dir
×
64
    in
65
    LPH.replace dir_content.leafs id { title; children_order }
66

67
  let dir_index ((parent_id, _) as dir) =
68
    let index_id =
15✔
69
      Paths.Identifier.Mk.leaf_page (parent_id, Names.PageName.make_std "index")
15✔
70
    in
71
    match find_leaf dir index_id with
15✔
72
    | Some payload -> Some (payload, index_id, payload.title)
4✔
73
    | None -> None
11✔
74

75
  type index = Page.t * title
76
  type t = (Page.t * content) list * index option
77
  and content = Entry of title | Dir of t
78

79
  let rec t_of_in_progress (dir : in_progress) =
80
    let children_order, index =
15✔
81
      match dir_index dir with
82
      | Some ({ children_order; _ }, index_id, index_title) ->
4✔
83
          (children_order, Some (index_id, index_title))
84
      | None -> (None, None)
11✔
85
    in
86
    let ordered, unordered =
87
      let contents =
88
        let leafs =
89
          leafs dir
90
          |> List.map (fun (id, payload) -> ((id :> Page.t), Entry payload))
6✔
91
        in
92
        let dirs =
15✔
93
          dirs dir
94
          |> List.map (fun (id, payload) ->
15✔
95
                 ((id :> Page.t), Dir (t_of_in_progress payload)))
6✔
96
        in
97
        leafs @ dirs
15✔
98
      in
99
      match children_order with
100
      | None -> ([], contents)
14✔
101
      | Some children_order ->
1✔
102
          List.partition_map
1✔
103
            (fun (((id : Page.t), _) as entry) ->
104
              match
3✔
105
                List.find_index
106
                  (fun ch ->
107
                    match (ch, id.iv) with
6✔
108
                    | Frontmatter.Dir c, `Page (_, name) ->
1✔
109
                        String.equal (Names.PageName.to_string name) c
1✔
110
                    | Page c, `LeafPage (_, name) ->
3✔
111
                        String.equal (Names.PageName.to_string name) c
3✔
112
                    | _ -> false)
2✔
113
                  children_order.value
114
              with
115
              | Some i -> `Left (i, entry)
2✔
116
              | None -> `Right entry)
1✔
117
            contents
118
    in
119
    let () =
120
      match (children_order, unordered) with
121
      | Some x, (_ :: _ as l) ->
1✔
122
          let pp fmt (id, _) =
123
            match id.iv with
1✔
124
            | `LeafPage (_, name) ->
1✔
125
                Format.fprintf fmt "'%s'" (Names.PageName.to_string name)
1✔
NEW
126
            | `Page (_, name) ->
×
NEW
127
                Format.fprintf fmt "'%s/'" (Names.PageName.to_string name)
×
128
          in
129
          Error.raise_warning
1✔
130
            (Error.make "(children) doesn't include %a."
1✔
131
               (Format.pp_print_list pp) l (Location_.location x))
1✔
132
      | _ -> ()
14✔
133
    in
134
    let ordered =
135
      ordered
136
      |> List.sort (fun (i, _) (j, _) -> Int.compare i j)
1✔
137
      |> List.map snd
15✔
138
    in
139
    let unordered =
15✔
140
      List.sort
141
        (fun (x, _) (y, _) ->
142
          String.compare (Paths.Identifier.name x) (Paths.Identifier.name y))
2✔
143
        unordered
144
    in
145
    let contents = ordered @ unordered in
15✔
146
    (contents, index)
147

148
  let of_list l =
149
    let dir = empty_t None in
9✔
150
    List.iter (add dir) l;
9✔
151
    t_of_in_progress dir
9✔
152
end
153

154
type toc = PageToc.t
155

156
type library = { name : string; units : Paths.Identifier.RootModule.t list }
157

158
type page_hierarchy = { hierarchy_name : string; pages : toc }
159

160
type t = { pages : page_hierarchy list; libraries : library list }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc