Skip to content

Commit 3ae052a

Browse files
authored
Merge pull request #118 from funkjedi/feature-whitespace-control
Implement whitespace control for variables
2 parents 3bf7447 + 96e189d commit 3ae052a

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

src/Liquid/AbstractBlock.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class AbstractBlock extends AbstractTag
2626
*/
2727
protected $nodelist = array();
2828

29+
/**
30+
* @var bool
31+
*/
32+
protected static $trimWhitespace = false;
33+
2934
/**
3035
* @return array
3136
*/
@@ -45,7 +50,7 @@ public function getNodelist()
4550
public function parse(array &$tokens)
4651
{
4752
$startRegexp = new Regexp('/^' . Liquid::get('TAG_START') . '/');
48-
$tagRegexp = new Regexp('/^' . Liquid::get('TAG_START') . '\s*(\w+)\s*(.*?)' . Liquid::get('TAG_END') . '$/');
53+
$tagRegexp = new Regexp('/^' . Liquid::get('TAG_START') . Liquid::get('WHITESPACE_CONTROL') . '?\s*(\w+)\s*(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('TAG_END') . '$/');
4954
$variableStartRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . '/');
5055

5156
$this->nodelist = array();
@@ -56,6 +61,7 @@ public function parse(array &$tokens)
5661
$token = array_shift($tokens);
5762

5863
if ($startRegexp->match($token)) {
64+
$this->whitespaceHandler($token);
5965
if ($tagRegexp->match($token)) {
6066
// If we found the proper block delimitor just end parsing here and let the outer block proceed
6167
if ($tagRegexp->matches[1] == $this->blockDelimiter()) {
@@ -83,15 +89,38 @@ public function parse(array &$tokens)
8389
throw new ParseException("Tag $token was not properly terminated (won't match $tagRegexp)");
8490
}
8591
} elseif ($variableStartRegexp->match($token)) {
92+
$this->whitespaceHandler($token);
8693
$this->nodelist[] = $this->createVariable($token);
87-
} elseif ($token != '') {
94+
} else {
95+
if (self::$trimWhitespace) {
96+
$token = ltrim($token);
97+
}
98+
99+
self::$trimWhitespace = false;
88100
$this->nodelist[] = $token;
89101
}
90102
}
91103

92104
$this->assertMissingDelimitation();
93105
}
94106

107+
/**
108+
* Handle the whitespace.
109+
*
110+
* @param string $token
111+
*/
112+
protected function whitespaceHandler(&$token)
113+
{
114+
if (mb_substr($token, 2, 1) === Liquid::get('WHITESPACE_CONTROL')) {
115+
$previousToken = end($this->nodelist);
116+
if (is_string($previousToken)) {
117+
$this->nodelist[key($this->nodelist)] = rtrim($previousToken);
118+
}
119+
}
120+
121+
self::$trimWhitespace = (mb_substr($token, -3, 1) === Liquid::get('WHITESPACE_CONTROL'));
122+
}
123+
95124
/**
96125
* Render the block.
97126
*
@@ -214,7 +243,7 @@ private function blockName()
214243
*/
215244
private function createVariable($token)
216245
{
217-
$variableRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . '(.*)' . Liquid::get('VARIABLE_END') . '$/');
246+
$variableRegexp = new Regexp('/^' . Liquid::get('VARIABLE_START') . Liquid::get('WHITESPACE_CONTROL') . '?(.*?)' . Liquid::get('WHITESPACE_CONTROL') . '?' . Liquid::get('VARIABLE_END') . '$/');
218247
if ($variableRegexp->match($token)) {
219248
return new Variable($variableRegexp->matches[1]);
220249
}

src/Liquid/Liquid.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ class Liquid
5252
// Prefix for include files.
5353
'INCLUDE_PREFIX' => '_',
5454

55+
// Whitespace control.
56+
'WHITESPACE_CONTROL' => '-',
57+
5558
// Tag start.
56-
'TAG_START' => '(?:{%|\s*{%-)',
59+
'TAG_START' => '{%',
5760

5861
// Tag end.
59-
'TAG_END' => '(?:%}|-%}\s*)',
62+
'TAG_END' => '%}',
6063

6164
// Variable start.
6265
'VARIABLE_START' => '{{',
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
--
22

33

4-
Wow, John G. Chalmers-Smith, you have a long name!
4+
Wow,
5+
John G. Chalmers-Smith, you have a long name!
56

6-
--Wow, John G. Chalmers-Smith, you have a long name!--
7+
--Wow,John G. Chalmers-Smith, you have a long name!--

tests/Liquid/fixtures/whitespace-control.liquid

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
--
22
{% assign username = "John G. Chalmers-Smith" %}
33
{% if username and username.size > 10 %}
4-
Wow, {{ username }}, you have a long name!
4+
Wow,
5+
{{ username }}, you have a long name!
56
{% else %}
67
Hello there!
78
{% endif %}
89
--
910
{%- assign username = "John G. Chalmers-Smith" -%}
1011
{%- if username and username.size > 10 -%}
11-
Wow, {{ username }}, you have a long name!
12+
Wow,
13+
{{- username -}}, you have a long name!
1214
{%- else -%}
1315
Hello there!
1416
{%- endif -%}

0 commit comments

Comments
 (0)