From 1d4e60ec55740de93f91ec496075ecb8a7511b3a Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Wed, 12 Jul 2023 19:40:25 -0300 Subject: [PATCH] detect mismatched --- TODO.md | 4 +++- shortcodes.rl | 36 +++++++++++------------------------- tests.c | 9 +++++++++ 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/TODO.md b/TODO.md index 55976f6..4109d76 100644 --- a/TODO.md +++ b/TODO.md @@ -3,8 +3,10 @@ ## Things That May Get Done * Produce decent error structures -* Detect mismatched start/end like {{% foo >}} * Inform tag type (% or <) in result * Handle other kinds of quotes * Handle escaping quote characters * Handle escaping the shortcode itself like \{{% foo %}} (check with hugo docs) +* Report errors on mismatched {{% foo >}} + +* ~~Detect mismatched start/end like {{% foo >}}~~ diff --git a/shortcodes.rl b/shortcodes.rl index 0f5e13e..496a7da 100644 --- a/shortcodes.rl +++ b/shortcodes.rl @@ -49,10 +49,19 @@ start_b = '{{<'; end_b = '>}}'; + content = spc name (sep arg)* spc; + start = start_p | start_b ; end = end_p | end_b ; - shortcode = (start spc name (sep arg)* spc end) + mismatched = ((start_p content end_b) | (start_b content end_p)) + @{ + // Since it's mismatched, remove the name + sc_list[c_sc].name.start = 0; + sc_list[c_sc].name.len=0; + }; + + shortcode = ((start_p content end_p) | (start_b content end_b)) > { sc_list[c_sc].whole.start = p-start-1; } @@ -83,11 +92,6 @@ start + sc_list[c_sc].name.start, sc_list[c_sc-1].name.len) !=0) { - // printf("Mismatched tags!\n"); - // str_copyb(&sc,start + sc_list[c_sc-1].name.start, sc_list[c_sc-1].name.len); - // printf("opened: %s\n", sc.s); - // str_copyb(&sc,start + sc_list[c_sc].name.start, sc_list[c_sc].name.len); - // printf("closed: %s\n", sc.s); return NULL; } // Reuse this shortcode entry for next one @@ -95,7 +99,7 @@ sc_list[c_sc].name.len=0; }; - main := (any* (shortcode | matched_shortcode))*; + main := (any* (shortcode | matched_shortcode | mismatched))*; }%% @@ -121,24 +125,6 @@ shortcode *parse(char *input) { %% write init; %% write exec; - // for (int i=0; sc_list[i].name.start!=0; i++) { - // str_copyb(&sc, start + sc_list[i].name.start, sc_list[i].name.len); - // printf("sc_name: %s (%d)\n", sc.s, sc_list[i].matching ); - // str_copyb(&sc, start + sc_list[i].start, sc_list[i].len); - // printf("full_sc: %s\n", sc.s); - // if (sc_list[i].matching) { - // str_copyb(&sc, start + sc_list[i].data.start, sc_list[i].data.len); - // printf("sc_data: %s\n", sc.s); - // } - - // for (int j=0; j< sc_list[i].argcount; j++) { - // str_copyb(&sc, start + sc_list[i].argnames[j].start, sc_list[i].argnames[j].len); - // printf("argname %d: %s\n", j, sc.s); - // str_copyb(&sc, start + sc_list[i].argvals[j].start, sc_list[i].argvals[j].len); - // printf("argval %d: %s\n", j, sc.s); - // } - // } - // TODO: handle errors return sc_list; } diff --git a/tests.c b/tests.c index 7ee4d7a..a80b3d1 100755 --- a/tests.c +++ b/tests.c @@ -38,6 +38,14 @@ Ensure(parse, simple_shortcode) assert_that(s.s, is_equal_to_string("{{% shortcode %}}")); } +Ensure(parse, mismatched_brackets) +{ + char *input = "foobar {{% shortcode >}}blah"; + result = parse(input); + // No shortcodes + assert_that(result[0].name.len, is_equal_to(0)); +} + Ensure(parse, inner_spaces_optional) { char *input = "foobar {{% shortcode%}}blah"; @@ -164,6 +172,7 @@ int main(int argc, char **argv) str_init(&s); TestSuite *suite = create_test_suite(); add_test_with_context(suite, parse, empty_string); + add_test_with_context(suite, parse, mismatched_brackets); add_test_with_context(suite, parse, simple_shortcode); add_test_with_context(suite, parse, name_can_be_path); add_test_with_context(suite, parse, inner_spaces_optional);