clean parse_comment

This commit is contained in:
Swrup 2022-02-26 15:28:23 +01:00
parent b863c719c3
commit 1ae5347380

View file

@ -266,70 +266,49 @@ let parse_image image =
-keeps tracks of every post cited in this comment -keeps tracks of every post cited in this comment
- add <br> at each line *) - add <br> at each line *)
let parse_comment comment = let parse_comment comment =
let handle_word w = let citations = ref [] in
let pp_word fmt w =
let trim_w = String.trim w in let trim_w = String.trim w in
(* '>' is '&gt;' after html_escape *) (* '>' is '&gt;' after html_escape *)
if String.starts_with ~prefix:{|&gt;&gt;|} trim_w then if String.starts_with ~prefix:{|&gt;&gt;|} trim_w then
let sub_w = String.sub trim_w 8 (String.length trim_w - 8) in let sub_w = String.sub trim_w 8 (String.length trim_w - 8) in
match Uuidm.of_string sub_w with if Option.is_some (Uuidm.of_string sub_w) then (
| None -> (w, None) citations := sub_w :: !citations;
| Some _ -> Format.fprintf fmt {|<a href="#%s">%s</a>|} sub_w w )
let new_w = Format.sprintf {|<a href="#%s">%s</a>|} sub_w w in else Format.pp_print_string fmt w
(new_w, Some sub_w) else Format.pp_print_string fmt w
else (w, None)
in in
let handle_line l = let pp_line fmt l =
let trim_w = String.trim l in let trim_w = String.trim l in
(*insert quote*) (*insert quote*)
let line = let words = String.split_on_char ' ' l in
match if
String.starts_with ~prefix:{|&gt;|} trim_w String.starts_with ~prefix:{|&gt;|} trim_w
&& not (String.starts_with ~prefix:{|&gt;&gt;|} trim_w) && not (String.starts_with ~prefix:{|&gt;&gt;|} trim_w)
with then
| false -> l Format.fprintf fmt {|<span class="quote">%a</span>|}
| true -> {|<span class="quote">|} ^ l ^ {|</span>|} (Format.pp_print_list ~pp_sep:Format.pp_print_space pp_word)
in words
let words = String.split_on_char ' ' line in else
let words, cited_posts = Format.fprintf fmt "%a"
List.fold_left (Format.pp_print_list ~pp_sep:Format.pp_print_space pp_word)
(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 )
words words
in
(line, cited_posts)
in in
let comment = String.trim comment in let comment = String.trim comment in
let lines = String.split_on_char '\n' 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 <br>*) (*insert <br>*)
let comment = let comment =
Format.asprintf "%a" Format.asprintf "%a"
(Format.pp_print_list (Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "@.<br>") ~pp_sep:(fun fmt () -> Format.fprintf fmt "@\n<br>")
Format.pp_print_string ) pp_line )
lines lines
in in
(* remove duplicate cited_id *) (* remove duplicate cited_id *)
let cited_posts = List.sort_uniq String.compare cited_posts in let citations = List.sort_uniq String.compare !citations in
(comment, cited_posts) (comment, citations)
let upload_post ?image_content post = let upload_post ?image_content post =
let thread_data, reply = let thread_data, reply =