From 9e89b92de0d55ea40c5b27e90ce7f97984bdc2ad Mon Sep 17 00:00:00 2001 From: Swrup Date: Sat, 26 Feb 2022 15:28:23 +0100 Subject: [PATCH] clean parse_comment --- src/babillard.ml | 69 +++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/src/babillard.ml b/src/babillard.ml index 5d5e7b4..80bba1c 100644 --- a/src/babillard.ml +++ b/src/babillard.ml @@ -266,70 +266,49 @@ let parse_image image = -keeps tracks of every post cited in this comment - add
at each line *) let parse_comment comment = - let handle_word w = + let citations = ref [] in + + let pp_word fmt w = let trim_w = String.trim w in (* '>' is '>' after html_escape *) if String.starts_with ~prefix:{|>>|} trim_w then let sub_w = String.sub trim_w 8 (String.length trim_w - 8) in - match Uuidm.of_string sub_w with - | None -> (w, None) - | Some _ -> - let new_w = Format.sprintf {|%s|} sub_w w in - (new_w, Some sub_w) - else (w, None) + if Option.is_some (Uuidm.of_string sub_w) then ( + citations := sub_w :: !citations; + Format.fprintf fmt {|%s|} sub_w w ) + else Format.pp_print_string fmt w + else Format.pp_print_string fmt w in - let handle_line l = + let pp_line fmt l = let trim_w = String.trim l in (*insert quote*) - let line = - match - String.starts_with ~prefix:{|>|} trim_w - && not (String.starts_with ~prefix:{|>>|} trim_w) - with - | false -> l - | true -> {||} ^ l ^ {||} - in - let words = String.split_on_char ' ' line in - let words, cited_posts = - List.fold_left - (fun (acc_words, acc_cited_posts) w -> - match handle_word w with - | w, Some cited_id -> (w :: acc_words, cited_id :: acc_cited_posts) - | w, None -> (w :: acc_words, acc_cited_posts) ) - ([], []) words - in - let words = List.rev words in - let line = - Format.asprintf "%a" - (Format.pp_print_list - ~pp_sep:(fun fmt () -> Format.fprintf fmt " ") - Format.pp_print_string ) + let words = String.split_on_char ' ' l in + if + String.starts_with ~prefix:{|>|} trim_w + && not (String.starts_with ~prefix:{|>>|} trim_w) + then + Format.fprintf fmt {|%a|} + (Format.pp_print_list ~pp_sep:Format.pp_print_space pp_word) + words + else + Format.fprintf fmt "%a" + (Format.pp_print_list ~pp_sep:Format.pp_print_space pp_word) words - in - (line, cited_posts) in let comment = String.trim comment in let lines = String.split_on_char '\n' comment in - let lines, cited_posts = - List.fold_left - (fun (acc_lines, acc_cited_posts) l -> - let line, cited_posts = handle_line l in - (line :: acc_lines, cited_posts @ acc_cited_posts) ) - ([], []) lines - in - let lines = List.rev lines in (*insert
*) let comment = Format.asprintf "%a" (Format.pp_print_list - ~pp_sep:(fun fmt () -> Format.fprintf fmt "@.
") - Format.pp_print_string ) + ~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n
") + pp_line ) lines in (* remove duplicate cited_id *) - let cited_posts = List.sort_uniq String.compare cited_posts in - (comment, cited_posts) + let citations = List.sort_uniq String.compare !citations in + (comment, citations) let upload_post ?image_content post = let thread_data, reply =