blob: b478265de0fea21dca1af9195ccbd04dfcfb1906 (
plain) (
blame)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
(* emit_csv.ml *)
module T = Template
let escape_field s =
let needs =
String.exists (fun c -> c = ',' || c = '"' || c = '\n' || c = '\r') s
in
if not needs then s
else begin
let b = Buffer.create (String.length s + 8) in
Buffer.add_char b '"';
String.iter (fun c -> if c = '"' then Buffer.add_string b "\"\"" else Buffer.add_char b c) s;
Buffer.add_char b '"';
Buffer.contents b
end
let get v k = match v with T.Obj kvs -> List.assoc_opt k kvs | _ -> None
let s v k = match get v k with Some (T.Str x) -> x | _ -> ""
let nested v a b = match get v a with Some inner -> s inner b | None -> ""
let columns =
[ "date"; "rite"; "season"; "week"; "slug"; "rank"; "colour"; "subject";
"name_la"; "name_en"; "first"; "gospel"; "comms" ]
let row ~rite d =
let comms =
match get d "comms" with
| Some (T.List l) -> String.concat " " (List.map (fun c -> s c "slug") l)
| _ -> ""
in
String.concat ","
(List.map escape_field
[ s d "iso"; rite; s d "season"; s d "week"; s d "slug"; s d "rank";
s d "colour"; s d "subject"; nested d "name" "la"; nested d "name" "en";
s d "first"; s d "gospel"; comms ])
let year v =
let rite = s v "rite" in
let days = match get v "days" with Some (T.List l) -> l | _ -> [] in
let b = Buffer.create (64 * 400) in
Buffer.add_string b (String.concat "," columns);
Buffer.add_char b '\n';
List.iter (fun d -> Buffer.add_string b (row ~rite d); Buffer.add_char b '\n') days;
Buffer.contents b
|