Shopify/liquid — pull request #214 by nickpearson (2013-06-21 18:47:46+00:00)
Title: Add ERB-like trim mode for tags and outputs

This change adds support for ERB-like trimming to Liquid. Tags and outputs are supported, though they behave differently. In the descriptions below, "newline" means `\n` and/or `\r` (specifically, `/\n?\r?/`).

**TAG BEHAVIOR**

For any tag starting with `{%-`, as with any ERB segment starting with `<%-`, all tabs and spaces between the beginning of the line and the beginning of the tag will be removed.

For any tag ending with `-%}`, as with any ERB segment ending with `-%>`, the newline character immediately following the tag will be removed.

For example:
```html

{%- if true -%}
hi
{%- endif -%}

```
becomes:
```html

hi

```

Unlike ERB, this implementation for Liquid is more forgiving in that (a) it also removes any tabs and spaces that precede the newline character, and (b) it does not require a newline to be present.

**OUTPUT BEHAVIOR**

Trim mode for output segments behaves a little differently.

For any output starting with `{{-`, all tabs and spaces between the beginning of the line and the beginning of the output _and the newline preceding it_ will be removed. (Tabs and spaces preceding the newline will not be removed.)

For any output ending with `-}}`, all tabs and spaces and the newline character immediately following the output _and all tabs and spaces after the newline_ will be removed.

For example:
```html

{{- 'hi' -}}

```
becomes:
```html

hi

```

**NOTES**

The beginning and ending trim modes can be used independently. For example, `{%- if x %}` or `{{ x -}}`.

The reason tabs and spaces preceding the newline in a `{{-` trim are _not_ removed is so that whitespace between multiple output segments can be controlled. The reason tabs and spaces following a newline in a `-}}` trim _are_ removed is so that indentation can be removed. For example, consider the following, where underscores are shown in the place of spaces:

```html

__{{- 'abc' -}},_
__{{- 'def' -}}

```
becomes:
```html

abc,_def

```
The space after the comma is kept, but the indentation spaces before 'abc' and 'def' are removed.

**BENCHMARKS**

Before:
```
Rehearsal ------------------------------------------------
parse: 12.130000 0.030000 12.160000 ( 12.167833)
parse & run: 28.780000 0.080000 28.860000 ( 28.874338)
-------------------------------------- total: 41.020000sec

user system total real
parse: 12.350000 0.030000 12.380000 ( 12.386125)
parse & run: 28.630000 0.080000 28.710000 ( 28.722666)
```

After:
```
Rehearsal ------------------------------------------------
parse: 16.750000 0.040000 16.790000 ( 16.802643)
parse & run: 33.250000 0.080000 33.330000 ( 33.342917)
-------------------------------------- total: 50.120000sec

user system total real
parse: 16.900000 0.050000 16.950000 ( 16.959132)
parse & run: 33.260000 0.100000 33.360000 ( 33.375376)
```