jwerle / mush

Mustache templates for bash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mush unescapes \" character

edi9999 opened this issue · comments

Here is my input.txt :

Hello "{{name}}"
Hello \"{{name}}\"

When running the mush command on this input, it does something strange with the escaped quotes here :

$ export name=John ; mush <input.txt
Hello "John"
Hello ""John""

This seems to come from this line (highlighted with "=======")

      echo "$line" | {
        ## parse undefined variables
        sed -e "s#${LEFT_DELIM}[A-Za-z]*${RIGHT_DELIM}##g" | \
        ## parse comments
        sed -e "s#${LEFT_DELIM}\!.*${RIGHT_DELIM}##g" | \
        ## escaping
        sed -e 's/\\\"/""/g' # ========= This line 
      };

mush/mush.sh

Line 131 in d8ee4db

sed -e 's/\\\"/""/g'

@edi9999 do you see a fix?

Removing the line :

sed -e 's/\\\"/""/g' # ========= This line 

seems to fix the issue for me.

Hello @jwerle ,

This is not the right fix.

The line should be removed completely :

With your current fix, the behavior is like this :

input.txt :

Hello "{{name}}"
Hello \"{{name}}\"

output.txt :

Hello "John"
Hello "John"

But the output should be :

Hello "John"
Hello \"John\"

The right patch is to remove this sed line completely : sed -e 's/\\\"/""/g'

The script should be like this :

  ## read each line
  while IFS= read -r line; do
    printf '%q\n' "${line}" | {
        ## read each ENV variable
        echo "$ENV" | {
          while read -r var; do
            ## split each ENV variable by '='
            ## and parse the line replacing
            ## occurrence of the key with
            ## guarded by the values of
            ## `LEFT_DELIM' and `RIGHT_DELIM'
            ## with the value of the variable
            case "$var" in
              (*"="*)
                key=${var%%"="*}
                val=${var#*"="*}
                ;;

              (*)
                key=$var
                val=
                ;;
            esac

            line="${line//${LEFT_DELIM}$key${RIGHT_DELIM}/$val}"
          done

          if [ "1" = "$ESCAPE" ]; then
            line="${line//&/&amp;}"
            line="${line//\"/&quot;}"
            line="${line//\</&lt;}"
            line="${line//\>/&gt;}"
          fi

          ## output to stdout
          echo "$line" | {
            ## parse undefined variables
            sed -e "s#${LEFT_DELIM}[A-Za-z]*${RIGHT_DELIM}##g" | \
            ## parse comments
            sed -e "s#${LEFT_DELIM}\!.*${RIGHT_DELIM}##g" | \
          };
        }
    };
  done

okay done!

fixed in 0.1.4