fix parse_comment: insert <br> in comment, insert quote, fix error in sort_uniq
This commit is contained in:
parent
bd99462e0a
commit
90bf5ee169
3 changed files with 56 additions and 19 deletions
|
|
@ -266,27 +266,61 @@ let () =
|
||||||
then
|
then
|
||||||
Dream.warning (fun log -> log "can't create table")
|
Dream.warning (fun log -> log "can't create table")
|
||||||
|
|
||||||
(* TODO should I escape html or smthing ?*)
|
(* TODO: Is this safe? *)
|
||||||
(*TODO fix bad link if post in other thread*)
|
(*TODO fix bad link if post in other thread*)
|
||||||
let parse_comment comment =
|
let parse_comment comment =
|
||||||
let words = String.split_on_char ' ' comment in
|
let handle_word w =
|
||||||
let cited_posts, words =
|
let trim_w = String.trim w in
|
||||||
List.fold_left
|
(* '>' is '>' after html_escape *)
|
||||||
(fun (acc_cited, acc_posts) w ->
|
match String.starts_with ~prefix:{|>>|} trim_w with
|
||||||
match String.starts_with ~prefix:">>" w with
|
| false -> (w, None)
|
||||||
| false -> (acc_cited, acc_posts @ [ w ])
|
|
||||||
| true -> (
|
| true -> (
|
||||||
let sub_w = String.sub w 2 (String.length w - 2) in
|
let sub_w = String.sub trim_w 8 (String.length trim_w - 8) in
|
||||||
match Uuidm.of_string sub_w with
|
match Uuidm.of_string sub_w with
|
||||||
| None -> (acc_cited, acc_posts @ [ w ])
|
| None -> (w, None)
|
||||||
| Some _ ->
|
| Some _ ->
|
||||||
let new_w = Format.sprintf {|<a href="#%s">%s</a>|} sub_w w in
|
let new_w = Format.sprintf {|<a href="#%s">%s</a>|} sub_w w in
|
||||||
(acc_cited @ [ sub_w ], acc_posts @ [ new_w ]) ) )
|
(new_w, Some sub_w) )
|
||||||
|
in
|
||||||
|
let handle_line 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 -> {|<span class="quote">|} ^ l ^ {|</span>|}
|
||||||
|
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 ->
|
||||||
|
(acc_words @ [ w ], acc_cited_posts @ [ cited_id ])
|
||||||
|
| w, None -> (acc_words @ [ w ], acc_cited_posts) )
|
||||||
([], []) words
|
([], []) words
|
||||||
in
|
in
|
||||||
let comment = String.concat (String.make 1 ' ') words in
|
let line = String.concat (String.make 1 ' ') words in
|
||||||
(* remove duplicate *)
|
(line, cited_posts)
|
||||||
let cited_posts = List.sort_uniq (fun _ _ -> 0) cited_posts in
|
in
|
||||||
|
|
||||||
|
let comment = String.trim comment in
|
||||||
|
let comment = Dream.html_escape 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
|
||||||
|
(acc_lines @ [ line ], acc_cited_posts @ cited_posts) )
|
||||||
|
([], []) lines
|
||||||
|
in
|
||||||
|
(*insert <br>*)
|
||||||
|
let comment = String.concat "\n<br>" lines in
|
||||||
|
(* remove duplicate cited_id *)
|
||||||
|
let cited_posts = List.sort_uniq (fun _ _ -> 1) cited_posts in
|
||||||
(comment, cited_posts)
|
(comment, cited_posts)
|
||||||
|
|
||||||
let view_post post_id =
|
let view_post post_id =
|
||||||
|
|
|
||||||
|
|
@ -49,3 +49,6 @@ blockquote.blockquote {
|
||||||
padding: 5px 5px 5px 5px;
|
padding: 5px 5px 5px 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.quote {
|
||||||
|
color: green;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue