aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlban Gruin2020-09-20 11:59:19 +0200
committerAlban Gruin2020-09-20 11:59:19 +0200
commit98c4919dc592f45265ee3a8e1784df7127e3ad78 (patch)
tree360ee8265b1051f4445a8fac9339080b0e986db7
parent6fc1084cf13ec0578f7774fd41aee2dd2ace1649 (diff)
course: use a proper type to iterate over a course description
Currently, to find a course's summary and location, there is a pattern matching over a ad-hoc 3-uple of a boolean and two strings. To make it clear, this badly defined structure is replaced by an actual ADT, `loc_and_sum', containing 4 cases: - `Nothing', when the groups has not been found yet; - `Groups', when the groups has been found by `check_groups'. This means that the next line should be the course's location; - `Location', when the location has been found. If there is no new line to read, the string is the location, otherwise it's the summary, and the next line should be the actual location; - `Summary', when the location and summary has been both found. All subsequent lines will be ignored. A function is also added to convert a `loc_and_sum' to a couple of string containing the location and the summary. Signed-off-by: Alban Gruin <alban at pa1ch dot fr>
-rw-r--r--src/course.ml46
1 files changed, 28 insertions, 18 deletions
diff --git a/src/course.ml b/src/course.ml
index 6109a3e..aede83e 100644
--- a/src/course.ml
+++ b/src/course.ml
@@ -46,32 +46,42 @@ let replace_entities str =
| _ -> None)
|> Stringext.replace_all_assoc 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 ^ ")"
+
let check_groups str =
let group_affixes = ["L1 "; "L2 "; "L3 "; "L3P "; "M1 "; "M2 "; "DEUST ";
"MAG1 "; "1ERE ANNEE "; "2EME ANNEE "; "3EME ANNEE ";
"MAT-Agreg Interne "] in
- List.fold_left
- (fun res affix ->
- res || Astring.String.is_prefix ~affix str) false group_affixes
+ if List.fold_left
+ (fun res affix ->
+ res || Astring.String.is_prefix ~affix str) false group_affixes then
+ Groups
+ else
+ Nothing
let location_and_summary str category =
let sep = "\r\n\r\n<br />\r\n\r\n" in
let parts = Astring.String.cuts ~empty:false ~sep str in
- let _, location, summary =
- List.fold_right
- (fun str -> function
- | false, _, _ -> check_groups str, "", ""
- | true, "", _ -> true, replace_entities str, ""
- | true, summary, "" -> true, replace_entities str, summary
- | true, location, summary -> true, location, summary)
- parts (false, "", "") in
- if summary = "" then
- location, category
- else
- let summary = match Astring.String.cut ~sep:" - " summary with
- | None -> summary
- | Some (_, str) -> str in
- location, summary ^ " (" ^ category ^ ")"
+ List.fold_right (fun str -> function
+ | Nothing -> check_groups str
+ | Groups -> Location (replace_entities str)
+ | Location summary -> Summary (replace_entities str, summary)
+ | Summary _ as res -> res)
+ parts Nothing
+ |> loc_and_sum_to_str category
let date =
let date_format = "%FT%T" in