summaryrefslogtreecommitdiff
path: root/bs5/server/rsc/Markdown.ml
blob: e8c89426deac6c04e8aa7180a00d91d07c1c2254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
module List = struct
  include List
  let rec take lst n =
    match (lst, n) with
    | ([], _) -> []
    | (_, 0) -> []
    | (x :: xs, n) -> x :: take xs (n - 1)
end

let convert_headings text =
  text
  |> Str.global_replace (Str.regexp "^#### \\(.*\\)$") "<h4>\\1</h4>"
  |> Str.global_replace (Str.regexp "^### \\(.*\\)$") "<h3>\\1</h3>"
  |> Str.global_replace (Str.regexp "^## \\(.*\\)$") "<h2>\\1</h2>"
  |> Str.global_replace (Str.regexp "^# \\(.*\\)$") "<h1>\\1</h1>"

let convert_emphasis text =
  text
  |> Str.global_replace
       (Str.regexp "\\*\\*\\([^*]*\\)\\*\\*")
       "<strong>\\1</strong>"
  |> Str.global_replace
       (Str.regexp "__\\([^_]*\\)__")
       "<strong>\\1</strong>"
  |> Str.global_replace (Str.regexp "\\*\\([^*]*\\)\\*") "<em>\\1</em>"
  |> Str.global_replace (Str.regexp "_\\([^_]*\\)_") "<em>\\1</em>"

let convert_code text =
  text
  |> Str.global_replace
       (Str.regexp "```\\([^`]*\\)```")
       "<pre><code>\\1</code></pre>"
  |> Str.global_replace (Str.regexp "`\\([^`]*\\)`") "<code>\\1</code>"

let convert_links text =
  text
  |> Str.global_replace
       (Str.regexp "\\[\\([^]]*\\)\\](\\([^)]*\\))")
       "<a href=\"\\2\">\\1</a>"

let convert_lists text =
  let lines = String.split_on_char '\n' text in

  let process_line line =
    match line with
    | line when Str.string_match (Str.regexp "^-\\s*\\(.*\\)$") line 0 ->
      "<li>" ^ Str.matched_group 1 line ^ "</li>"
    | line when Str.string_match (Str.regexp "^\\+\\s*\\(.*\\)$") line 0 ->
      "<li>" ^ Str.matched_group 1 line ^ "</li>"
    | line when Str.string_match (Str.regexp "^\\*\\s*\\(.*\\)$") line 0 ->
      "<li>" ^ Str.matched_group 1 line ^ "</li>"
    | line
        when Str.string_match (Str.regexp "^\\d+\\.\\s*\\(.*\\)$") line 0 ->
      "<li>" ^ Str.matched_group 1 line ^ "</li>"
    | _ -> line
  in

  let wrap_consecutive_items lines =
    let rec aux acc current_list lines =
      match (current_list, lines) with
      | ([], []) -> List.rev acc
      | (hd :: tl, []) ->
        List.rev [
          "<ul>" ^ String.concat "\n" (List.rev (hd :: tl)) ^ "</ul>";
        ] @ acc
      | ([], line :: rest) ->
        if Str.string_match (Str.regexp "^<li>") line 0 then
          aux acc [line] rest
        else
          aux (line :: acc) [] rest
      | (items, line :: rest) ->
        if Str.string_match (Str.regexp "^<li>") line 0 then
          aux acc (line :: current_list) rest
        else
          aux
            (line :: ("<ul>" ^ String.concat "\n" (List.rev items) ^ "</ul>") :: acc)
            []
            rest
    in
    aux [] [] lines
  in

  lines
  |> List.map process_line
  |> wrap_consecutive_items
  |> String.concat "\n"

let wrap_lists text =
  text
  |> Str.global_replace
       (Str.regexp "<li>.*</li>\\(\n<li>.*</li>\\)*")
       "<ul>\\0</ul>"

let convert_blockquotes text =
  let lines = String.split_on_char '\n' text in

  let rec process_lines acc in_quote lines =
    match lines with
    | [] when in_quote -> List.rev ("</blockquote>" :: acc)
    | [] -> List.rev acc
    | line :: rest ->
      let trimmed = String.trim line in
      if Str.string_match (Str.regexp "^>\\s*\\(.*\\)$") trimmed 0 then
        let content = Str.matched_group 1 trimmed in
        if in_quote then
          process_lines (content :: acc) true rest
        else
          process_lines (content :: "<blockquote>" :: acc) true rest
      else if trimmed = "" then
        if in_quote then
          process_lines ("</blockquote>" :: acc) false rest
        else
          process_lines (line :: acc) false rest
      else if in_quote then
        process_lines (line :: acc) true rest
      else
        process_lines (line :: acc) false rest
  in

  lines |> process_lines [] false |> String.concat "\n"

let convert_paragraphs text =
  let lines = String.split_on_char '\n' text in

  let is_block_element line =
    Str.string_match
      (Str.regexp "^<\\(h[1-6]\\|ul\\|ol\\|blockquote\\|pre\\)>")
      line
      0
  in

  let wrap_paragraphs lines =
    let rec aux acc current_p lines =
      match lines with
      | [] when current_p <> "" ->
        List.rev (("<p>" ^ current_p ^ "</p>") :: acc)
      | [] -> List.rev acc
      | line :: rest when is_block_element line ->
        if current_p <> "" then
          aux (line :: ("<p>" ^ current_p ^ "</p>") :: acc) "" rest
        else
          aux (line :: acc) "" rest
      | line :: rest when String.trim line = "" ->
        if current_p <> "" then
          aux (("<p>" ^ current_p ^ "</p>") :: acc) "" rest
        else
          aux acc "" rest
      | line :: rest ->
        let sep =
          if current_p = "" then
            ""
          else
            " "
        in
        aux acc (current_p ^ sep ^ String.trim line) rest
    in
    aux [] "" lines
  in

  lines |> wrap_paragraphs |> String.concat "\n"

let to_html markdown =
  markdown
  |> convert_headings
  |> convert_emphasis
  |> convert_code
  |> convert_links
  |> convert_lists
  |> wrap_lists
  |> convert_blockquotes
  |> convert_paragraphs
  |> String.trim

let extract_text markdown =
  markdown
  |> Str.global_replace (Str.regexp "\\[([^]]*)\\]\\([^)]*\\)") "\\1"
  |> Str.global_replace (Str.regexp "\\*\\*\\([^*]*\\)\\*\\*") "\\1"
  |> Str.global_replace (Str.regexp "\\*\\([^*]*\\)\\*") "\\1"
  |> Str.global_replace (Str.regexp "__\\([^_]*\\)__") "\\1"
  |> Str.global_replace (Str.regexp "_\\([^_]*\\)_") "\\1"
  |> Str.global_replace (Str.regexp "~~\\([^~]*\\)~~") "\\1"
  |> Str.global_replace (Str.regexp "`\\([^`]*\\)`") "\\1"
  |> Str.global_replace (Str.regexp "```[^`]*```") ""
  |> Str.global_replace (Str.regexp "^#+ .*$") "\n"
  |> Str.global_replace (Str.regexp "^#* .*$") "\n"
  |> Str.global_replace (Str.regexp "> \\|>") ""
  |> Str.global_replace (Str.regexp "\\[\\|\\]\\|\\(\\|\\)") ""
  |> Str.global_replace (Str.regexp "-\\|\\+\\|\\*\\s+") ""
  |> Str.global_replace (Str.regexp "^\\d+\\.\\s+") ""
  |> Str.global_replace (Str.regexp "\\\\") ""
  |> String.trim

let summarize text ~words:n =
  let words = Str.split (Str.regexp "[ \n\r\t]+") text in
  let truncated = List.take words n in
  let dots =
    if List.length words > n then
      "..."
    else
      ""
  in
  String.concat " " truncated ^ dots