aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2021-09-16 22:11:59 +0200
committerAlban Gruin2021-09-19 12:36:21 +0200
commitcb2f075c326f40c27b4c6d9dde6fd51b78a43f9c (patch)
tree422e65ea254e8c663406b2cf4d776f31f23cf3da
parenta5e7ab75ed9ff253df73cf951140748ce41503c4 (diff)
course, ics: read, store, and dump groups in a course
In the future, we want to filter courses by its groups. To allow this, we must parse and store the groups from celcat. This adds this feature. We also dump them in the `COMMENT' field of the iCalendar files. Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
-rw-r--r--src/course.ml52
-rw-r--r--src/ics.ml8
-rw-r--r--src/ics.mli4
3 files changed, 39 insertions, 25 deletions
diff --git a/src/course.ml b/src/course.ml
index 52aa062..4b9c914 100644
--- a/src/course.ml
+++ b/src/course.ml
@@ -1,5 +1,5 @@
(*
- * Copyright (C) 2020 Alban Gruin
+ * Copyright (C) 2020, 2021 Alban Gruin
*
* ucs is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
@@ -48,18 +48,31 @@ let replace_entities str =
type loc_and_sum =
| Nothing
- | Groups
- | Location of string
- | Summary of string * string
-
-let loc_and_sum_to_str category = function
- | Nothing | Groups -> "", category
- | Location location -> location, category
- | Summary (location, summary) ->
- let summary = match Astring.String.cut ~sep:" - " summary with
- | None -> summary
- | Some (_, str) -> str in
- location, summary ^ " (" ^ category ^ ")"
+ | Groups of string list
+ | Location of string list * string
+ | Summary of string list * string * string
+
+let loc_and_sum_to_groups = function
+ | Nothing -> []
+ | Groups groups | Location (groups, _) | Summary (groups, _, _) -> groups
+
+let loc_and_sum_to_location = function
+ | Nothing | Groups _ -> ""
+ | Location (_, location) | Summary (_, location, _) -> location
+
+let loc_and_sum_to_summary category = function
+ | Nothing | Groups _ | Location _ -> category
+ | Summary (_, _, summary) ->
+ let summary = match Astring.String.cut ~sep:" - " summary with
+ | None -> summary
+ | Some (_, str) -> str in
+ summary ^ " (" ^ category ^ ")"
+
+let loc_and_sum_to_event start stop category loc_and_sum =
+ let groups = loc_and_sum_to_groups loc_and_sum and
+ location = loc_and_sum_to_location loc_and_sum and
+ summary = loc_and_sum_to_summary category loc_and_sum in
+ Some (Ics.Event.make start stop summary location groups)
let check_groups str =
let group_affixes = ["L1 "; "L2 "; "L3 "; "L3P "; "M1 "; "M2 "; "DEUST ";
@@ -68,20 +81,19 @@ let check_groups str =
if List.fold_left
(fun res affix ->
res || Astring.String.is_prefix ~affix str) false group_affixes then
- Groups
+ Groups (Astring.String.cuts ~empty:false ~sep:"<br />" @@ replace_entities str)
else
Nothing
-let location_and_summary str category =
+let location_and_summary str =
let sep = "\r\n\r\n<br />\r\n\r\n" in
let parts = Astring.String.cuts ~empty:false ~sep str in
List.fold_right (fun str -> function
| Nothing -> check_groups str
- | Groups -> Location (replace_entities str)
- | Location summary -> Summary (replace_entities str, summary)
+ | Groups groups -> Location (groups, replace_entities str)
+ | Location (groups, summary) -> Summary (groups, replace_entities str, summary)
| Summary _ as res -> res)
parts Nothing
- |> loc_and_sum_to_str category
let date =
let date_format = "%FT%T" in
@@ -92,10 +104,10 @@ let encoding =
J.(conv
(fun _ -> (None, None, "", ""), ())
(fun ((start, stop, description, category), ()) ->
- let location, summary = location_and_summary description category in
match start, stop with
| Some start, Some stop ->
- Some (Ics.Event.make start stop summary location)
+ let loc_and_sum = location_and_summary description in
+ loc_and_sum_to_event start stop category loc_and_sum
| _, _ -> None)
(merge_objs
(obj4
diff --git a/src/ics.ml b/src/ics.ml
index d1bb808..f1b6cc5 100644
--- a/src/ics.ml
+++ b/src/ics.ml
@@ -1,5 +1,5 @@
(*
- * Copyright (C) 2020 Alban Gruin
+ * Copyright (C) 2020, 2021 Alban Gruin
*
* ucs is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
@@ -39,10 +39,11 @@ module Event = struct
stop: Calendar.t;
summary: string;
location: string;
+ groups: string list;
}
- let make start stop summary location =
- {start; stop; summary; location}
+ let make start stop summary location groups =
+ {start; stop; summary; location; groups}
let to_string date event id =
["BEGIN:VEVENT";
@@ -51,6 +52,7 @@ module Event = struct
"DTEND:" ^ to_date event.stop;
"DTSTAMP:" ^ date;
"SUMMARY:" ^ event.summary;
+ "COMMENT:" ^ String.concat ", " event.groups;
"LOCATION:" ^ event.location;
"END:VEVENT\n"]
|> List.map ics_split_line
diff --git a/src/ics.mli b/src/ics.mli
index a4cbbd6..ca09406 100644
--- a/src/ics.mli
+++ b/src/ics.mli
@@ -1,5 +1,5 @@
(*
- * Copyright (C) 2020 Alban Gruin
+ * Copyright (C) 2020, 2021 Alban Gruin
*
* ucs is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
@@ -19,7 +19,7 @@ module Event : sig
type t
val make : CalendarLib.Calendar.t -> CalendarLib.Calendar.t ->
- string -> string -> t
+ string -> string -> string list -> t
val to_string : string -> t -> int -> string
end