diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-03-27 08:48:05 +0100 |
| commit | 830e7c5d27d161b91402414e6ecd49f1ed818bfb (patch) | |
| tree | 448864635d29b1ea66424cbb9e0816fbcf1e1907 /lib/model/content.ml | |
Initial commit
Diffstat (limited to 'lib/model/content.ml')
| -rw-r--r-- | lib/model/content.ml | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/model/content.ml b/lib/model/content.ml new file mode 100644 index 0000000..2760715 --- /dev/null +++ b/lib/model/content.ml @@ -0,0 +1,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) |
