Browse Source

Fixed bug with escaping special sequence.

Fixes #8.
Chris Konstad 4 years ago
parent
commit
a293ef5dba
4 changed files with 23 additions and 6 deletions
  1. 12 5
      lib/parser.ml
  2. 1 1
      test.ml
  3. 1 0
      test/test4.md
  4. 9 0
      test/test4.mr

+ 12 - 5
lib/parser.ml

@ -20,10 +20,17 @@ let rec parse text =
20 20
    let var_start = Batteries.String.find text var_start_string in
21 21
    let var_end = String.find text var_end_string in
22 22
    if var_start != 0 && text.[var_start-1] = '\\' then (
23
      (* Escaped *)
24
      let next_ind = String.find_from text (var_start + 1) var_start_string in
25
      Text(String.sub text 0 next_ind) ::
26
      parse (String.sub text next_ind ((String.length text) - next_ind))
23
      (* Escaped, also consumes escape char *)
24
      try
25
        let next_ind = String.find_from text (var_start + 1) var_start_string in
26
        Text(String.sub text 0 (var_start-1)) ::
27
        Text(String.sub text var_start (next_ind-var_start)) ::
28
        parse (String.sub text next_ind ((String.length text) - next_ind))
29
      with
30
      | Not_found ->
31
        Text(String.sub text 0 (var_start-1)) ::
32
        Text(String.sub text (var_start) ((String.length text) - var_start)) ::
33
        []
27 34
    ) else
28 35
      let var_length = var_end - var_start + (String.length var_end_string) in
29 36
      Text(String.sub text 0 var_start) ::
@ -44,7 +51,7 @@ let rec parse text =
44 51
        Variable(contents)::next
45 52
  with
46 53
  (* No variables found *)
47
  | _ -> Text(text)::[]
54
  | x -> Text(text)::[]
48 55

													
49 56

													
50 57
(** Remove empty text segments *)

+ 1 - 1
test.ml

@ -10,7 +10,7 @@ let test_basic test_ctxt = assert_equal
10 10
       "./test")
11 11

													
12 12
let test_basic_escaped test_ctxt = assert_equal
13
    "I like \{{lang}}!"
13
    "I like {{lang}}!"
14 14
    (markright
15 15
       false
16 16
       "{{ {\"mappings\": { \"lang\": \"OCaml\"} } }}I like \{{lang}}!"

+ 1 - 0
test/test4.md

@ -0,0 +1 @@
1
I like {{lang}}! But OCaml isn't bad either.  Almost as good as {{lang}}.

+ 9 - 0
test/test4.mr

@ -0,0 +1,9 @@
1
{{
2
{
3
  "mappings":
4
  {
5
    "lang": "OCaml"
6
  }
7
}
8
}}
9
I like \{{lang}}! But {{lang}} isn't bad either.  Almost as good as \{{lang}}.