0

WE have a markdown validator that authors use to validate their md files, One check in that is the liquid tag validation where we check for invalid liquid tags in the md files. My requirement is to add a validation to check whether the flower braces are used properly and are properly terminated. eg if an author opens 5 { , there should be equally 5 closing }. But we also have to exclude code blocks from this validation. any brackets inside a code block should not be validated. code blocks may be a 4 space indentation, 3 backticks or a single backtick.

bracecountopen=$(grep -o -i '{' $file | wc -l); bracecountclose=$(grep -o -i '}' $file | wc -l); if [ "$bracecountopen" != "$bracecountclose" ]; then throw error fi 

for excluding code blocks

var+=$(grep '^\ \ \ \ [^>]' $file); while IFS= read -n1 char; do if [[ "$char" == "\`" ]]; then if [ $flag = 1 ]; then flag=0; continue fi flag=1; fi if [ $flag = 1 ]; then # var+=$char; echo "" fi done <$file codebraceopen=$(echo "$var" | grep -o -i '{' | wc -l) codebraceclose=$(echo "$var" | grep -o -i '}' | wc -l) let "bracecountopen=bracecountopen - codebraceopen" let "bracecountclose=bracecountclose - codebraceclose" 

Is this approach correct? Is there any better logic to achieve this? The problem here is some author add a tab or 4 spaces before starting their code block with back ticks which makes the content appear twice in the 'var' , it is counted in the 4 space as well as the backticks check. How to fix this?

5
  • 1
    Counting braces will not properly find bad cases like }}} {{{. It would be best if you used some external Markdown validation tool to do this job. Unfortunately, I'm unaware of what's available for doing this. Also note that double and triple backtick is used for different types of code, not just single backticks. Commented Dec 15, 2022 at 7:30
  • Just to address @Kusalananda 's first comment on brace sequence errors ({}is legit, }{ is not) when counting opening and closing braces, OP's first code block needs to keep track of the order in which braces appear in parsed file. If you check for syntactic correctness then the simplest test that come to mind as you parse your file is that the nbr of opening braces should be >= to the nbr of closing braces at all time during parsing and counting, excluding code blocks as noted. Commented Dec 15, 2022 at 10:13
  • Its okay if we dont check for braces like }} {{ , but they should be equal number and we need to exclude those which are occuring inside a code block Commented Dec 15, 2022 at 10:23
  • how to fix the issue of counting the same occurence twice with the backticks as well as the 4space indentation? Commented Dec 15, 2022 at 10:26
  • What if you have {{ new line } new line }? This is what my answer resolve! Commented Dec 15, 2022 at 12:23

1 Answer 1

0

This is not full answer but here is a code in awk you can use to count open and close curly brackets ({}):

codebraceopen=$(echo "$var" | awk '{n+=gsub("\{","",$0)} END {print n}' ) codebraceclose=$(echo "$var" | awk '{n+=gsub("\}","",$0)} END {print n}') 

This will help you work properly with construction like }}} {{{ (as mentioned in comment).

Same technique can be used to count backticks, backslashes, etc.

6
  • 1
    Sorry, how does this helps deal with atypical sequence order }{ in the context of Markdown ? Commented Dec 15, 2022 at 10:12
  • how to fix the issue of counting the same occurence twice with the backticks as well as the 4space indentation? Commented Dec 15, 2022 at 10:26
  • @Cbhihe, did you read the text on the begin of my answer? This is not full answer Commented Dec 15, 2022 at 12:31
  • I am already counting the braces in my code. I think you didn't read full question, The issue mentioned is different with that. Commented Dec 15, 2022 at 13:35
  • Do you have a way to count just the braces that are inside a code block? i.e in a single , triple backtick or 4 space indentation Commented Dec 15, 2022 at 13:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.