/** * Parse next commend token. * * @return CommentNode */ protected function parseComment() { $token = $this->expectTokenType('comment'); $node = new CommentNode(preg_replace('/^ +| +$/', '', $token->value), $token->buffer, $this->lexer->getCurrentLine()); // Skip newlines while ($this->lexer->predictToken()->type === 'newline') { $this->lexer->getAdvancedToken(); } if ($this->lexer->predictToken()->type === 'indent') { $node->setBlock($this->parseBlock()); } return $node; }
/** * Dump comment node. * * @param CommentNode $node comment node * @param integer $level indentation level * * @return string */ protected function dumpComment(CommentNode $node, $level = 0) { if ($node->isBuffered()) { $html = str_repeat(' ', $level); if ($node->getBlock()) { $string = $node->getString(); $beg = "<!--\n"; $end = "\n" . str_repeat(' ', $level) . '-->'; if (preg_match('/^ *if/', $string)) { $beg = '<!--[' . $string . "]>\n"; $end = "\n" . str_repeat(' ', $level) . '<![endif]-->'; $string = ''; } $html .= $beg; if ($string !== '') { $html .= str_repeat(' ', $level + 1) . $string . "\n"; } $html .= $this->dumpBlock($node->getBlock(), $level + 1); $html .= $end; } else { $html = str_repeat(' ', $level) . '<!-- ' . $node->getString() . ' -->'; } return $html; } else { return ''; } }