package kkmarkdown

  1. Overview
  2. Docs

Source file BlockRuleHeader.ml

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
module Sharp = struct
  let is_safe = true
  let first_char = FirstChar.One '#'
  let force_construct = true

  type state = { level : int }

  let re = Str.regexp "\\(#+\\) "

  let start line : state BlockRule.res =
    if Str.string_match re line 0 then
      let level = String.length (Str.matched_group 1 line) in
      if level <= 6 then Stop { state = { level }; handle_line = `Keep }
      else Die
    else Die

  let continue state line = assert false

  let remove_trailing_sharps line =
    Utils.remove_trailing_spaces line
    |> Utils.remove_trailing_sharps |> Utils.remove_trailing_spaces

  let construct { BlockRule.trans_spans } { level } lines : Typ.block =
    let title =
      match lines with
      | [ line ] ->
          Str.string_after line (level + 1)
          |> remove_trailing_sharps |> trans_spans
      | _ -> assert false
    in
    match level with
    | 1 -> H1 title
    | 2 -> H2 title
    | 3 -> H3 title
    | 4 -> H4 title
    | 5 -> H5 title
    | 6 -> H6 title
    | _ -> assert false
end

module Make (M : sig
  val c : char
  val construct : Typ.span list -> Typ.block
end) =
struct
  let is_safe = true
  let first_char = FirstChar.Any
  let force_construct = false

  type state = unit

  let start _ = BlockRule.Go { state = (); handle_line = `Keep }
  let re = Str.regexp (String.make 3 M.c ^ "+[ \t]*$")

  let continue () line : state BlockRule.res =
    if Str.string_match re line 0 then
      Stop { state = (); handle_line = `Discard }
    else Die

  let construct { BlockRule.trans_spans } () lines =
    match lines with
    | [ title ] -> M.construct (trans_spans title)
    | _ -> assert false
end

module H1 = Make (struct
  let c = '='
  let construct spans = Typ.H1 spans
end)

module H2 = Make (struct
  let c = '-'
  let construct spans = Typ.H2 spans
end)
OCaml

Innovation. Community. Security.