Skip to content

Commit

Permalink
reimplement whitespace handling for standalone tokens
Browse files Browse the repository at this point in the history
Whitespace handling was previously done by a post-processor on the
token stream produced by the lexer. We reimplement it as
a post-processor on the *output* stream after rendering. This means
that information from the fully-parsed AST can be used to make
rendering decisions.

For example, non-rendered sections (because their corresponding json
data is empty) are now handled as standalone tokens. This gives
nicer output, consider for example:

    {{#foo}}Foo is present.{{/foo}}
    {{^foo}}Foo is absent. {{/foo}}

This used to produce two lines of output, one being empty. Now this
only produces one line of input (if 'foo' is a scalar or object, not
a list).

(Interestingly, such examples are not tested in the Mustache
specification testsuite. Both the previous implementation and the
current implementation pass all the specification tests, which
suggests that the tests are not comprehensive enough.)

Note: The real motivation for this change is not inverted sections, but
rather indentation and whitespace handling for partial parameters --
when implementing template inheritance.
  • Loading branch information
gasche committed Jan 3, 2021
1 parent 750f7f0 commit fba19f7
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 196 deletions.
2 changes: 1 addition & 1 deletion bin/test/errors/parsing-errors.t
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Mismatch between section-start and section-end:
$ echo "{{#bar}} {{#foo}} {{.}} {{/bar}} {{/foo}}" > $PROBLEM
$ mustache foo.json $PROBLEM
Template parse error:
File "wrong-nesting.mustache", lines 1-2, characters 41-0:
File "wrong-nesting.mustache", line 1, characters 32-33:
Section mismatch: {{#foo}} is closed by {{/bar}}.
[3]

Expand Down
1 change: 0 additions & 1 deletion bin/test/errors/render-errors.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ reference.json and reference.mustache work well together, there is no error.

The variable "name" has value "Some Name".


Last line.


Expand Down
Loading

0 comments on commit fba19f7

Please sign in to comment.