shawnbot / meta-template

:sparkles: Automagically convert Nunjucks templates into a variety of other formats!
52 stars 9 forks source link

Escape template delimiters in "raw" template data #4

Open shawnbot opened 7 years ago

shawnbot commented 7 years ago

Currently, Nunjucks' raw filter doesn't produce an AST node that we can use to easily map to the equivalent escape tags in other templating systems:

$ echo '{% raw %}{{hi}}{% endraw %}' | ./bin/parse.js -ct

produces:

{
  "children": [
    {
      "children": [
        {
          "value": "{{hi}}",
          "type": "TemplateData"
        }
      ],
      "type": "Output"
    }
  ],
  "type": "Root"
}

It looks like what we have to do is check if each TemplateData node's value contains any of the format's O_OPEN, O_CLOSE, C_OPEN, or C_CLOSE strings, and escape it accordingly. This actually makes more sense because the delimiters will vary by engine, so ERBs can escape only the values they need to.