aboutsummaryrefslogtreecommitdiff
path: root/src/gemtext/preformatted_block.gleam
blob: 5e94fef65b7e58673a51ebf5b805c86c1c09f337 (plain) (blame)
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
import gleam/list
import gleam/string

pub fn group_adjacent_preformatted_block_lines(
  lines: List(String),
) -> List(String) {
  case lines {
    ["```", desc, ..rest] -> {
      let #(body, rest) = do_group_adjacent_preformatted_block_lines(rest, [])
      list.append(
        ["```" <> desc <> "\n" <> body <> "\n```"],
        group_adjacent_preformatted_block_lines(rest),
      )
    }
    [line, ..rest] ->
      list.append([line], group_adjacent_preformatted_block_lines(rest))
    [] -> []
  }
}

fn do_group_adjacent_preformatted_block_lines(
  lines: List(String),
  accumulator: List(String),
) {
  case lines {
    ["```", ..rest] -> #(string.join(accumulator, "\n"), rest)
    [line, ..rest] ->
      do_group_adjacent_preformatted_block_lines(
        rest,
        list.append(accumulator, [line]),
      )
    [] -> #(string.join(accumulator, "\n"), [])
  }
}