summaryrefslogtreecommitdiff
path: root/lib/model/content.ml
blob: 2760715a168da4533114105fc9bdca19c29eb24b (plain)
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
open StdLabels

type 'a t = {
  id : string;
  sections : 'a Section.t list;
  subcontents : 'a t list;
  parent : 'a t Weak.t;
}

let v : string -> 'a Section.t list -> 'a t list -> 'a t =
 fun id sections subcontents ->
  let parent = Weak.create 1 in
  let content = { id; sections; subcontents; parent } in
  (* Fill the parent for all existing subcontent declared in the type
     There is no need to repeat the same for the subcontents as they all are
     already created in the same way.
   *)
  List.iter content.subcontents ~f:(fun v -> Weak.set v.parent 0 (Some content));
  content

let id t = t.id
let sections t = t.sections
let subcontents t = t.subcontents
let parent t = Weak.get t.parent 0
let rec root t = match parent t with None -> t | Some t -> root t

let fields : 'a t -> 'a Field.t Seq.t =
 fun t ->
  List.to_seq t.sections
  |> Seq.flat_map (fun section -> List.to_seq section.Section.fields)