From 2ffcea6c3d960c5da99103db96d4ceb409370b97 Mon Sep 17 00:00:00 2001 From: fabien Date: Sat, 28 Nov 2009 12:51:56 +0000 Subject: [PATCH] removed the LINE_DELIMITER const and made it possible to insert newlines in tag and variable blocks git-svn-id: http://svn.twig-project.org/trunk@147 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- CHANGELOG | 1 + lib/Twig/Lexer.php | 30 ++++++++++++------------------ 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 01e8629..6c3f7f3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ If you have custom loaders, you MUST upgrade them for this release: The Twig_Loader base class has been removed, and the Twig_LoaderInterface has also been changed (see the source code for more information or the documentation). + * made it possible to insert newlines in tag and variable blocks * fixed a bug when a literal '\n' were present in a template text * refactored loaders diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index fa6b082..8fc6f78 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -29,8 +29,6 @@ class Twig_Lexer implements Twig_LexerInterface protected $env; protected $options; - const LINE_DELIMITER = ''; - const POSITION_DATA = 0; const POSITION_BLOCK = 1; const POSITION_VAR = 2; @@ -61,7 +59,7 @@ class Twig_Lexer implements Twig_LexerInterface */ public function tokenize($code, $filename = 'n/a') { - $this->code = preg_replace('/(\r\n|\r|\n)/', self::LINE_DELIMITER, $code); + $this->code = preg_replace('/(\r\n|\r|\n)/', "\n", $code); $this->filename = $filename; $this->cursor = 0; $this->lineno = 1; @@ -153,9 +151,9 @@ class Twig_Lexer implements Twig_LexerInterface // if no matches are left we return the rest of the template // as simple text token - if (!preg_match('/(.*?)('.preg_quote($this->options['tag_comment'][0], '/').'|'.preg_quote($this->options['tag_variable'][0], '/').'|'.preg_quote($this->options['tag_block'][0], '/').')/A', $this->code, $match, null, $this->cursor)) + if (!preg_match('/(.*?)('.preg_quote($this->options['tag_comment'][0], '/').'|'.preg_quote($this->options['tag_variable'][0], '/').'|'.preg_quote($this->options['tag_block'][0], '/').')/As', $this->code, $match, null, $this->cursor)) { - $rv = new Twig_Token(Twig_Token::TEXT_TYPE, $this->fixNewLines(substr($this->code, $this->cursor)), $this->lineno); + $rv = new Twig_Token(Twig_Token::TEXT_TYPE, substr($this->code, $this->cursor), $this->lineno); $this->cursor = $this->end; return $rv; @@ -174,15 +172,15 @@ class Twig_Lexer implements Twig_LexerInterface $text = $match[1]; if (!empty($text)) { - $result[] = new Twig_Token(Twig_Token::TEXT_TYPE, $this->fixNewLines($text), $lineno); - $lineno += substr_count($text, self::LINE_DELIMITER); + $result[] = new Twig_Token(Twig_Token::TEXT_TYPE, $text, $lineno); + $lineno += substr_count($text, "\n"); } $token = $match[2]; switch ($token) { case $this->options['tag_comment'][0]: - if (!preg_match('/(.*?)'.preg_quote($this->options['tag_comment'][1], '/').'/A', $this->code, $match, null, $this->cursor)) + if (!preg_match('/(.*?)'.preg_quote($this->options['tag_comment'][1], '/').'/As', $this->code, $match, null, $this->cursor)) { throw new Twig_SyntaxError('unclosed comment', $this->lineno, $this->filename); } @@ -192,9 +190,9 @@ class Twig_Lexer implements Twig_LexerInterface case $this->options['tag_block'][0]: // raw data? - if (preg_match('/\s*raw\s*'.preg_quote($this->options['tag_block'][1], '/').'(.*?)'.preg_quote($this->options['tag_block'][0], '/').'\s*endraw\s*'.preg_quote($this->options['tag_block'][1], '/').'/A', $this->code, $match, null, $this->cursor)) + if (preg_match('/\s*raw\s*'.preg_quote($this->options['tag_block'][1], '/').'(.*?)'.preg_quote($this->options['tag_block'][0], '/').'\s*endraw\s*'.preg_quote($this->options['tag_block'][1], '/').'/As', $this->code, $match, null, $this->cursor)) { - $result[] = new Twig_Token(Twig_Token::TEXT_TYPE, $this->fixNewLines($match[1]), $lineno); + $result[] = new Twig_Token(Twig_Token::TEXT_TYPE, $match[1], $lineno); $this->moveCursor($match[0]); $this->moveLineNo($match[0]); $this->position = self::POSITION_DATA; @@ -217,7 +215,7 @@ class Twig_Lexer implements Twig_LexerInterface protected function lexBlock() { - if (preg_match('/\s*'.preg_quote($this->options['tag_block'][1], '/').'/A', $this->code, $match, null, $this->cursor)) + if (preg_match('/\s*'.preg_quote($this->options['tag_block'][1], '/').'/As', $this->code, $match, null, $this->cursor)) { $lineno = $this->lineno; $this->moveCursor($match[0]); @@ -232,7 +230,7 @@ class Twig_Lexer implements Twig_LexerInterface protected function lexVar() { - if (preg_match('/\s*'.preg_quote($this->options['tag_variable'][1], '/').'/A', $this->code, $match, null, $this->cursor)) + if (preg_match('/\s*'.preg_quote($this->options['tag_variable'][1], '/').'/As', $this->code, $match, null, $this->cursor)) { $lineno = $this->lineno; $this->moveCursor($match[0]); @@ -250,7 +248,7 @@ class Twig_Lexer implements Twig_LexerInterface $match = null; // whitespace - while (preg_match('/\s+/A', $this->code, $match, null, $this->cursor)) + while (preg_match('/\s+/As', $this->code, $match, null, $this->cursor)) { $this->moveCursor($match[0]); $this->moveLineNo($match[0]); @@ -304,7 +302,7 @@ class Twig_Lexer implements Twig_LexerInterface protected function moveLineNo($text) { - $this->lineno += substr_count($text, self::LINE_DELIMITER); + $this->lineno += substr_count($text, "\n"); } protected function moveCursor($text) @@ -312,8 +310,4 @@ class Twig_Lexer implements Twig_LexerInterface $this->cursor += strlen($text); } - protected function fixNewLines($text) - { - return str_replace(self::LINE_DELIMITER, "\n", $text); - } } -- 1.7.2.5