diff --git a/h2o/datatype.php b/h2o/datatype.php index da174af..2870299 100644 --- a/h2o/datatype.php +++ b/h2o/datatype.php @@ -65,11 +65,12 @@ static function exec($args, $context) { * $type of token, Block | Variable */ class H2o_Token { - function __construct ($type, $content, $position) { + function __construct ($type, $content, $position, $raw) { $this->type = $type; $this->content = $content; $this->result=''; $this->position = $position; + $this->raw = $raw; } function write($content){ @@ -98,10 +99,10 @@ function pop() { return array_pop($this->stream); } - function feed($type, $contents, $position) { + function feed($type, $contents, $position, $raw) { if ($this->closed) throw new Exception('cannot feed closed stream'); - $this->stream[] = new H2o_Token($type, $contents, $position); + $this->stream[] = new H2o_Token($type, $contents, $position, $raw); } function push($token) { diff --git a/h2o/parser.php b/h2o/parser.php index 15b819a..67bb65c 100644 --- a/h2o/parser.php +++ b/h2o/parser.php @@ -22,18 +22,20 @@ function tokenize($source) { foreach ($matches as $match) { if ($match[1]) - $result->feed('text', $match[1], $pos); - $tagpos = $pos + strlen($match[1]); + $result->feed('text', $match[1], $pos, $match[1]); + $pretext_len = strlen($match[1]); + $tagpos = $pos + $pretext_len; + $raw_text = substr($match[0], $pretext_len); if ($match[2]) - $result->feed('block', trim($match[2]), $tagpos); + $result->feed('block', trim($match[2]), $tagpos, $raw_text); elseif ($match[3]) - $result->feed('variable', trim($match[3]), $tagpos); + $result->feed('variable', trim($match[3]), $tagpos, $raw_text); elseif ($match[4]) - $result->feed('comment', trim($match[4]), $tagpos); + $result->feed('comment', trim($match[4]), $tagpos, $raw_text); $pos += strlen($match[0]); } if ($pos < strlen($source)){ - $result->feed('text', substr($source, $pos), $pos); + $result->feed('text', substr($source, $pos), $pos, substr($source, $pos)); } $result->close(); return $result; @@ -65,7 +67,7 @@ function __construct($source, $filename, $runtime, $options) { function &parse() { $until = func_get_args(); $nodelist = new NodeList($this); - while($token = $this->tokenstream->next()) { + while($token = $this->tokenstream->next()) { //$token = $this->tokenstream->current(); switch($token->type) { case 'text' : @@ -100,6 +102,31 @@ function &parse() { return $nodelist; } + function &parseRaw() { + $until = func_get_args(); + $nodelist = new NodeList($this); + while($token = $this->tokenstream->next()) { + //$token = $this->tokenstream->current(); + switch ($token->type) { + case 'block': + if (in_array($token->content, $until)) { + $this->token = $token; + return $nodelist; + } + default: + $node = new TextNode($token->raw, $token->position); + } + $this->searching = join(',',$until); + $this->first = false; + $nodelist->append($node); + } + + if ($until) { + throw new TemplateSyntaxError('Unclose tag, expecting '. $until[0]); + } + return $nodelist; + } + function skipTo($until) { $this->parse($until); return null; diff --git a/h2o/tags.php b/h2o/tags.php index c207ce2..8ba6207 100644 --- a/h2o/tags.php +++ b/h2o/tags.php @@ -486,5 +486,15 @@ function render($context, $stream) { } } -H2o::addTag(array('block', 'extends', 'include', 'if', 'ifchanged', 'for', 'with', 'cycle', 'load', 'debug', 'comment', 'now', 'autoescape', 'csrf_token')); +class Raw_Tag extends H2o_Node { + private $body; + function __construct($argstring, $parser, $pos=0){ + $this->body = $parser->parseRaw('endraw'); + } + function render($context, $stream){ + $this->body->render($context, $stream); + } +} + +H2o::addTag(array('block', 'extends', 'include', 'if', 'ifchanged', 'for', 'with', 'cycle', 'load', 'debug', 'comment', 'now', 'autoescape', 'csrf_token', 'raw')); ?> \ No newline at end of file