@@ -28,6 +28,11 @@ module Part = struct
2828
2929end
3030
31+ (* * Remove empty strings at the beginning of a list *)
32+ let rec remove_empty_heads = function
33+ | "" :: tl -> remove_empty_heads tl
34+ | l -> l
35+
3136module Parse_parts =
3237struct
3338
@@ -41,12 +46,7 @@ struct
4146 ]
4247
4348 let make_part ~name ~lines =
44- (* Remove empty lines at the end of the part *)
45- let rec remove_empty = function
46- | "" :: tl -> remove_empty tl
47- | ls -> ls
48- in
49- let body = String. concat " \n " (List. rev (remove_empty lines)) in
49+ let body = String. concat " \n " (List. rev (remove_empty_heads lines)) in
5050 Part. v ~name ~body
5151
5252 let rec parse_parts input name lines =
@@ -66,25 +66,17 @@ struct
6666
6767end
6868
69- type file =
70- | Parts of Part .t list
71- | Body of (exn * string )
72-
73- let read file =
74- Parts (Parse_parts. of_file file)
69+ type file = Part .t list
7570
76- let err_parse_error (e , _ ) =
77- Fmt. failwith " Parse error: %a" Fmt. exn e
71+ let read file = Parse_parts. of_file file
7872
79- let find file ~part = match file, part with
80- | Body (_ , s ), None -> Some [s]
81- | Body b , _ -> err_parse_error b
82- | Parts parts , Some part ->
83- (match List. find_opt (fun p -> String. equal (Part. name p) part) parts with
73+ let find file ~part = match part with
74+ | Some part ->
75+ (match List. find_opt (fun p -> String. equal (Part. name p) part) file with
8476 | Some p -> Some [Part. body p]
8577 | None -> None )
86- | Parts parts , None ->
87- List. fold_left (fun acc p -> Part. body p :: [" " ] @ acc) [] parts
78+ | None ->
79+ List. fold_left (fun acc p -> Part. body p :: [" " ] @ acc) [] file
8880 |> List. rev
8981 |> fun x -> Some x
9082
@@ -96,25 +88,19 @@ let rec replace_or_append part_name body = function
9688 | [] ->
9789 [{ name = part_name; body }]
9890
99- let replace file ~part ~lines = match file, part with
100- | Body (e , _ ), None -> Body (e, String. concat " \n " lines)
101- | Body b , _ -> err_parse_error b
102- | Parts parts , _ ->
103- let part = match part with None -> " " | Some p -> p in
104- let parts = replace_or_append part (String. concat " \n " lines) parts in
105- Parts parts
106-
107- let contents = function
108- | Body (_ , s ) -> String. trim s ^ " \n "
109- | Parts parts ->
110- let lines =
111- List. fold_left (fun acc p ->
112- let body = Part. body p in
113- match Part. name p with
114- | "" -> body :: acc
115- | n -> body :: (" \n [@@@part \" " ^ n ^ " \" ] ;;\n " ) :: acc
116- ) [] parts
117- in
118- let lines = List. rev lines in
119- let lines = String. concat " \n " lines in
120- String. trim lines ^ " \n "
91+ let replace file ~part ~lines =
92+ let part = match part with None -> " " | Some p -> p in
93+ replace_or_append part (String. concat " \n " lines) file
94+
95+ let contents file =
96+ let lines =
97+ List. fold_left (fun acc p ->
98+ let body = Part. body p in
99+ match Part. name p with
100+ | "" -> body :: acc
101+ | n -> body :: (" \n [@@@part \" " ^ n ^ " \" ] ;;\n " ) :: acc
102+ ) [] file
103+ in
104+ let lines = List. rev lines in
105+ let lines = String. concat " \n " lines in
106+ String. trim lines ^ " \n "
0 commit comments