1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Latte;
9:
10: use Nette;
11: use Nette\Utils\Strings;
12:
13:
14: 15: 16: 17: 18:
19: class Parser extends Nette\Object
20: {
21:
22: const RE_STRING = '\'(?:\\\\.|[^\'\\\\])*\'|"(?:\\\\.|[^"\\\\])*"';
23:
24:
25: const N_PREFIX = 'n:';
26:
27:
28: public $defaultSyntax = 'latte';
29:
30:
31: public $shortNoEscape = TRUE;
32:
33:
34: public $syntaxes = array(
35: 'latte' => array('\\{(?![\\s\'"{}])', '\\}'),
36: 'double' => array('\\{\\{(?![\\s\'"{}])', '\\}\\}'),
37: 'asp' => array('<%\s*', '\s*%>'),
38: 'python' => array('\\{[{%]\s*', '\s*[%}]\\}'),
39: 'off' => array('[^\x00-\xFF]', ''),
40: );
41:
42:
43: private $macroRe;
44:
45:
46: private $input;
47:
48:
49: private $output;
50:
51:
52: private $offset;
53:
54:
55: private $context;
56:
57:
58: private $lastHtmlTag;
59:
60:
61: private $syntaxEndTag;
62:
63:
64: private $xmlMode;
65:
66:
67: const CONTEXT_HTML_TEXT = 'htmlText',
68: CONTEXT_CDATA = 'cdata',
69: CONTEXT_HTML_TAG = 'htmlTag',
70: CONTEXT_HTML_ATTRIBUTE = 'htmlAttribute',
71: CONTEXT_RAW = 'raw',
72: = 'htmlComment';
73:
74:
75: 76: 77: 78: 79:
80: public function parse($input)
81: {
82: $this->offset = 0;
83:
84: if (substr($input, 0, 3) === "\xEF\xBB\xBF") {
85: $input = substr($input, 3);
86: }
87: if (!Strings::checkEncoding($input)) {
88: throw new Nette\InvalidArgumentException('Template is not valid UTF-8 stream.');
89: }
90: $input = str_replace("\r\n", "\n", $input);
91: $this->input = $input;
92: $this->output = array();
93:
94: $this->setSyntax($this->defaultSyntax);
95: $this->setContext(self::CONTEXT_HTML_TEXT);
96: $this->lastHtmlTag = $this->syntaxEndTag = NULL;
97:
98: while ($this->offset < strlen($input)) {
99: $matches = $this->{"context".$this->context[0]}();
100:
101: if (!$matches) {
102: break;
103:
104: } elseif (!empty($matches['comment'])) {
105: $this->addToken(Token::COMMENT, $matches[0]);
106:
107: } elseif (!empty($matches['macro'])) {
108: $token = $this->addToken(Token::MACRO_TAG, $matches[0]);
109: list($token->name, $token->value, $token->modifiers, $token->empty) = $this->parseMacroTag($matches['macro']);
110: }
111:
112: $this->filter();
113: }
114:
115: if ($this->offset < strlen($input)) {
116: $this->addToken(Token::TEXT, substr($this->input, $this->offset));
117: }
118: return $this->output;
119: }
120:
121:
122: 123: 124:
125: private function contextHtmlText()
126: {
127: $matches = $this->match('~
128: (?:(?<=\n|^)[ \t]*)?<(?P<closing>/?)(?P<tag>[a-z0-9:]+)| ## begin of HTML tag <tag </tag - ignores <!DOCTYPE
129: <(?P<htmlcomment>!--(?!>))| ## begin of HTML comment <!--, but not <!-->
130: '.$this->macroRe.' ## macro tag
131: ~xsi');
132:
133: if (!empty($matches['htmlcomment'])) {
134: $this->addToken(Token::HTML_TAG_BEGIN, $matches[0]);
135: $this->setContext(self::CONTEXT_HTML_COMMENT);
136:
137: } elseif (!empty($matches['tag'])) {
138: $token = $this->addToken(Token::HTML_TAG_BEGIN, $matches[0]);
139: $token->name = $matches['tag'];
140: $token->closing = (bool) $matches['closing'];
141: $this->lastHtmlTag = $matches['closing'] . strtolower($matches['tag']);
142: $this->setContext(self::CONTEXT_HTML_TAG);
143: }
144: return $matches;
145: }
146:
147:
148: 149: 150:
151: private function contextCData()
152: {
153: $matches = $this->match('~
154: </(?P<tag>'.$this->lastHtmlTag.')(?![a-z0-9:])| ## end HTML tag </tag
155: '.$this->macroRe.' ## macro tag
156: ~xsi');
157:
158: if (!empty($matches['tag'])) {
159: $token = $this->addToken(Token::HTML_TAG_BEGIN, $matches[0]);
160: $token->name = $this->lastHtmlTag;
161: $token->closing = TRUE;
162: $this->lastHtmlTag = '/' . $this->lastHtmlTag;
163: $this->setContext(self::CONTEXT_HTML_TAG);
164: }
165: return $matches;
166: }
167:
168:
169: 170: 171:
172: private function contextHtmlTag()
173: {
174: $matches = $this->match('~
175: (?P<end>\ ?/?>)([ \t]*\n)?| ## end of HTML tag
176: '.$this->macroRe.'| ## macro tag
177: \s*(?P<attr>[^\s/>={]+)(?:\s*=\s*(?P<value>["\']|[^\s/>{]+))? ## beginning of HTML attribute
178: ~xsi');
179:
180: if (!empty($matches['end'])) {
181: $this->addToken(Token::HTML_TAG_END, $matches[0]);
182: $this->setContext(!$this->xmlMode && in_array($this->lastHtmlTag, array('script', 'style'), TRUE) ? self::CONTEXT_CDATA : self::CONTEXT_HTML_TEXT);
183:
184: } elseif (isset($matches['attr']) && $matches['attr'] !== '') {
185: $token = $this->addToken(Token::HTML_ATTRIBUTE, $matches[0]);
186: $token->name = $matches['attr'];
187: $token->value = isset($matches['value']) ? $matches['value'] : '';
188:
189: if ($token->value === '"' || $token->value === "'") {
190: if (Strings::startsWith($token->name, self::N_PREFIX)) {
191: $token->value = '';
192: if ($m = $this->match('~(.*?)' . $matches['value'] . '~xsi')) {
193: $token->value = $m[1];
194: $token->text .= $m[0];
195: }
196: } else {
197: $this->setContext(self::CONTEXT_HTML_ATTRIBUTE, $matches['value']);
198: }
199: }
200: }
201: return $matches;
202: }
203:
204:
205: 206: 207:
208: private function contextHtmlAttribute()
209: {
210: $matches = $this->match('~
211: (?P<quote>'.$this->context[1].')| ## end of HTML attribute
212: '.$this->macroRe.' ## macro tag
213: ~xsi');
214:
215: if (!empty($matches['quote'])) {
216: $this->addToken(Token::TEXT, $matches[0]);
217: $this->setContext(self::CONTEXT_HTML_TAG);
218: }
219: return $matches;
220: }
221:
222:
223: 224: 225:
226: private function ()
227: {
228: $matches = $this->match('~
229: (?P<htmlcomment>-->)| ## end of HTML comment
230: '.$this->macroRe.' ## macro tag
231: ~xsi');
232:
233: if (!empty($matches['htmlcomment'])) {
234: $this->addToken(Token::HTML_TAG_END, $matches[0]);
235: $this->setContext(self::CONTEXT_HTML_TEXT);
236: }
237: return $matches;
238: }
239:
240:
241: 242: 243:
244: private function ()
245: {
246: $matches = $this->match('~
247: '.$this->macroRe.' ## macro tag
248: ~xsi');
249: return $matches;
250: }
251:
252:
253: 254: 255: 256: 257:
258: private function match($re)
259: {
260: if ($matches = Strings::match($this->input, $re, PREG_OFFSET_CAPTURE, $this->offset)) {
261: $value = substr($this->input, $this->offset, $matches[0][1] - $this->offset);
262: if ($value !== '') {
263: $this->addToken(Token::TEXT, $value);
264: }
265: $this->offset = $matches[0][1] + strlen($matches[0][0]);
266: foreach ($matches as $k => $v) {
267: $matches[$k] = $v[0];
268: }
269: }
270: return $matches;
271: }
272:
273:
274: 275: 276:
277: public function setContext($context, $quote = NULL)
278: {
279: $this->context = array($context, $quote);
280: return $this;
281: }
282:
283:
284: 285: 286: 287: 288:
289: public function setSyntax($type)
290: {
291: $type = $type ?: $this->defaultSyntax;
292: if (isset($this->syntaxes[$type])) {
293: $this->setDelimiters($this->syntaxes[$type][0], $this->syntaxes[$type][1]);
294: } else {
295: throw new Nette\InvalidArgumentException("Unknown syntax '$type'");
296: }
297: return $this;
298: }
299:
300:
301: 302: 303: 304: 305: 306:
307: public function setDelimiters($left, $right)
308: {
309: $this->macroRe = '
310: (?P<comment>' . $left . '\\*.*?\\*' . $right . '\n{0,2})|
311: ' . $left . '
312: (?P<macro>(?:
313: ' . self::RE_STRING . '|
314: \{(?:' . self::RE_STRING . '|[^\'"{}])*+\}|
315: [^\'"{}]
316: )+?)
317: ' . $right . '
318: (?P<rmargin>[ \t]*(?=\n))?
319: ';
320: return $this;
321: }
322:
323:
324: 325: 326: 327: 328:
329: public function parseMacroTag($tag)
330: {
331: $match = Strings::match($tag, '~^
332: (
333: (?P<name>\?|/?[a-z]\w*+(?:[.:]\w+)*+(?!::|\(|\\\\))| ## ?, name, /name, but not function ( or class:: or namespace\
334: (?P<noescape>!?)(?P<shortname>/?[=\~#%^&_]?) ## !expression, !=expression, ...
335: )(?P<args>.*?)
336: (?P<modifiers>\|[a-z](?:'.Parser::RE_STRING.'|[^\'"])*(?<!/))?
337: (?P<empty>/?\z)
338: ()\z~isx');
339:
340: if (!$match) {
341: return FALSE;
342: }
343: if ($match['name'] === '') {
344: $match['name'] = $match['shortname'] ?: '=';
345: if ($match['noescape']) {
346: if (!$this->shortNoEscape) {
347: trigger_error("The noescape shortcut {!...} is deprecated, use {...|noescape} modifier on line {$this->getLine()}.", E_USER_DEPRECATED);
348: }
349: $match['modifiers'] .= '|noescape';
350: }
351: }
352: return array($match['name'], trim($match['args']), $match['modifiers'], (bool) $match['empty']);
353: }
354:
355:
356: private function addToken($type, $text)
357: {
358: $this->output[] = $token = new Token;
359: $token->type = $type;
360: $token->text = $text;
361: $token->line = $this->getLine();
362: return $token;
363: }
364:
365:
366: private function getLine()
367: {
368: return $this->offset
369: ? substr_count(substr($this->input, 0, $this->offset - 1), "\n") + 1
370: : 0;
371: }
372:
373:
374: 375: 376:
377: protected function filter()
378: {
379: $token = end($this->output);
380: if ($token->type === Token::MACRO_TAG && $token->name === '/syntax') {
381: $this->setSyntax($this->defaultSyntax);
382: $token->type = Token::COMMENT;
383:
384: } elseif ($token->type === Token::MACRO_TAG && $token->name === 'syntax') {
385: $this->setSyntax($token->value);
386: $token->type = Token::COMMENT;
387:
388: } elseif ($token->type === Token::HTML_ATTRIBUTE && $token->name === 'n:syntax') {
389: $this->setSyntax($token->value);
390: $this->syntaxEndTag = '/' . $this->lastHtmlTag;
391: $token->type = Token::COMMENT;
392:
393: } elseif ($token->type === Token::HTML_TAG_END && $this->lastHtmlTag === $this->syntaxEndTag) {
394: $this->setSyntax($this->defaultSyntax);
395:
396: } elseif ($token->type === Token::MACRO_TAG && $token->name === 'contentType') {
397: if (preg_match('#html|xml#', $token->value, $m)) {
398: $this->xmlMode = $m[0] === 'xml';
399: $this->setContext(self::CONTEXT_HTML_TEXT);
400: } else {
401: $this->setContext(self::CONTEXT_RAW);
402: }
403: }
404: }
405:
406: }
407: