diff options
-rw-r--r-- | src/course.ml | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/course.ml b/src/course.ml index 2e5c758..3a0d1fc 100644 --- a/src/course.ml +++ b/src/course.ml @@ -16,13 +16,47 @@ *) open CalendarLib -open Ics module J = Json_encoding let date_format = "%FT%T" let default_date = Calendar.make 1970 1 1 0 0 0 +let separator = Str.regexp_string "\r\n\r\n<br />\r\n\r\n" +let group_prefixes = ["MAT-Agreg Interne "; "3EME ANNEE "; "2EME ANNEE "; + "1ERE ANNEE "; "MAG1 "; "DEUST "; "M2 "; "M1 "; "L3P "; + "L3 "; "L2 "; "L1 "] + +let startswith str prefix = + String.( + let lstr = length str and + lprefix = length prefix in + lstr = lprefix && equal str prefix + || lstr > lprefix && equal (sub str 0 lprefix) prefix) + +let check_groups str = + List.fold_left + (fun res prefix -> + res || startswith str prefix) false group_prefixes + +let location_and_summary str category = + let parts = Str.split separator str in + let _, location, summary = + List.fold_right + (fun str (has_groups, location, summary) -> + if not has_groups then + (check_groups str, "", "") + else if location = "" then + (true, str, "") + else if summary = "" then + (true, str, location) + else + (true, location, summary)) parts (false, "", "") in + if summary = "" then + location, category + else + location, summary ^ " (" ^ category ^ ")" + let date = Printer.Calendar.( J.(conv (sprint date_format) (from_fstring date_format) string)) @@ -31,9 +65,10 @@ let encoding = J.(conv (fun _ -> ("", default_date, default_date, false, "", (), (), (), (), ""), ([], None, (), (), (), (), ())) - (fun ((_id, start, stop, _allDay, summary, (), (), (), (), category), + (fun ((_id, start, stop, _allDay, description, (), (), (), (), category), (_sites, _modules, (), (), (), (), ())) -> - {start; stop; summary; category; location=""}) + let location, summary = location_and_summary description category in + Ics.{start; stop; summary; category; location}) (merge_objs (obj10 (req "id" string) |