1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Latte;
9:
10:
11: 12: 13: 14: 15:
16: class Compiler extends Object
17: {
18:
19: private $tokens;
20:
21:
22: private $output;
23:
24:
25: private $position;
26:
27:
28: private $macros;
29:
30:
31: private $macroHandlers;
32:
33:
34: private $htmlNode;
35:
36:
37: private $macroNode;
38:
39:
40: private $attrCodes = array();
41:
42:
43: private $contentType;
44:
45:
46: private $context;
47:
48:
49: private $templateId;
50:
51:
52: const CONTENT_HTML = 'html',
53: CONTENT_XHTML = 'xhtml',
54: CONTENT_XML = 'xml',
55: CONTENT_JS = 'js',
56: CONTENT_CSS = 'css',
57: CONTENT_URL = 'url',
58: CONTENT_ICAL = 'ical',
59: CONTENT_TEXT = 'text';
60:
61:
62: const = 'comment',
63: CONTEXT_SINGLE_QUOTED_ATTR = "'",
64: CONTEXT_DOUBLE_QUOTED_ATTR = '"',
65: CONTEXT_UNQUOTED_ATTR = '=';
66:
67:
68: public function __construct()
69: {
70: $this->macroHandlers = new \SplObjectStorage;
71: }
72:
73:
74: 75: 76: 77: 78:
79: public function addMacro($name, IMacro $macro)
80: {
81: $this->macros[$name][] = $macro;
82: $this->macroHandlers->attach($macro);
83: return $this;
84: }
85:
86:
87: 88: 89: 90: 91:
92: public function compile(array $tokens)
93: {
94: $this->templateId = substr(lcg_value(), 2, 10);
95: $this->tokens = $tokens;
96: $output = '';
97: $this->output = & $output;
98: $this->htmlNode = $this->macroNode = $this->context = NULL;
99:
100: foreach ($this->macroHandlers as $handler) {
101: $handler->initialize($this);
102: }
103:
104: foreach ($tokens as $this->position => $token) {
105: $this->{"process$token->type"}($token);
106: }
107:
108: while ($this->htmlNode) {
109: if (!empty($this->htmlNode->macroAttrs)) {
110: throw new CompileException('Missing ' . self::printEndTag($this->macroNode));
111: }
112: $this->htmlNode = $this->htmlNode->parentNode;
113: }
114:
115: $prologs = $epilogs = '';
116: foreach ($this->macroHandlers as $handler) {
117: $res = $handler->finalize();
118: $handlerName = get_class($handler);
119: $prologs .= empty($res[0]) ? '' : "<?php\n// prolog $handlerName\n$res[0]\n?>";
120: $epilogs = (empty($res[1]) ? '' : "<?php\n// epilog $handlerName\n$res[1]\n?>") . $epilogs;
121: }
122: $output = ($prologs ? $prologs . "<?php\n//\n// main template\n//\n?>\n" : '') . $output . $epilogs;
123:
124: if ($this->macroNode) {
125: throw new CompileException('Missing ' . self::printEndTag($this->macroNode));
126: }
127:
128: $output = $this->expandTokens($output);
129: return $output;
130: }
131:
132:
133: 134: 135:
136: public function setContentType($type)
137: {
138: $this->contentType = $type;
139: $this->context = NULL;
140: return $this;
141: }
142:
143:
144: 145: 146:
147: public function getContentType()
148: {
149: return $this->contentType;
150: }
151:
152:
153: 154: 155:
156: public function setContext($context, $sub = NULL)
157: {
158: $this->context = array($context, $sub);
159: return $this;
160: }
161:
162:
163: 164: 165:
166: public function getContext()
167: {
168: return $this->context;
169: }
170:
171:
172: 173: 174:
175: public function getTemplateId()
176: {
177: return $this->templateId;
178: }
179:
180:
181: 182: 183:
184: public function getMacroNode()
185: {
186: return $this->macroNode;
187: }
188:
189:
190: 191: 192: 193:
194: public function getLine()
195: {
196: return $this->tokens ? $this->tokens[$this->position]->line : NULL;
197: }
198:
199:
200:
201: public function expandTokens($s)
202: {
203: return strtr($s, $this->attrCodes);
204: }
205:
206:
207: private function processText(Token $token)
208: {
209: if (in_array($this->context[0], array(self::CONTEXT_SINGLE_QUOTED_ATTR, self::CONTEXT_DOUBLE_QUOTED_ATTR), TRUE)
210: && $token->text === $this->context[0]
211: ) {
212: $this->setContext(self::CONTEXT_UNQUOTED_ATTR);
213: }
214: $this->output .= $token->text;
215: }
216:
217:
218: private function processMacroTag(Token $token)
219: {
220: $isRightmost = !isset($this->tokens[$this->position + 1])
221: || substr($this->tokens[$this->position + 1]->text, 0, 1) === "\n";
222:
223: if ($token->name[0] === '/') {
224: $this->closeMacro((string) substr($token->name, 1), $token->value, $token->modifiers, $isRightmost);
225: } else {
226: $this->openMacro($token->name, $token->value, $token->modifiers, $isRightmost && !$token->empty);
227: if ($token->empty) {
228: $this->closeMacro($token->name, NULL, NULL, $isRightmost);
229: }
230: }
231: }
232:
233:
234: private function processHtmlTagBegin(Token $token)
235: {
236: if ($token->closing) {
237: while ($this->htmlNode) {
238: if (strcasecmp($this->htmlNode->name, $token->name) === 0) {
239: break;
240: }
241: if ($this->htmlNode->macroAttrs) {
242: throw new CompileException("Unexpected </$token->name>, expecting " . self::printEndTag($this->macroNode));
243: }
244: $this->htmlNode = $this->htmlNode->parentNode;
245: }
246: if (!$this->htmlNode) {
247: $this->htmlNode = new HtmlNode($token->name);
248: }
249: $this->htmlNode->closing = TRUE;
250: $this->htmlNode->offset = strlen($this->output);
251: $this->setContext(NULL);
252:
253: } elseif ($token->text === '<!--') {
254: $this->setContext(self::CONTEXT_COMMENT);
255:
256: } else {
257: $this->htmlNode = new HtmlNode($token->name, $this->htmlNode);
258: $this->htmlNode->isEmpty = in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML), TRUE)
259: && isset(Helpers::$emptyElements[strtolower($token->name)]);
260: $this->htmlNode->offset = strlen($this->output);
261: $this->setContext(self::CONTEXT_UNQUOTED_ATTR);
262: }
263: $this->output .= $token->text;
264: }
265:
266:
267: private function processHtmlTagEnd(Token $token)
268: {
269: if ($token->text === '-->') {
270: $this->output .= $token->text;
271: $this->setContext(NULL);
272: return;
273: }
274:
275: $htmlNode = $this->htmlNode;
276: $isEmpty = !$htmlNode->closing && (strpos($token->text, '/') !== FALSE || $htmlNode->isEmpty);
277: $end = '';
278:
279: if ($isEmpty && in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML), TRUE)) {
280: $token->text = preg_replace('#^.*>#', $htmlNode->isEmpty && $this->contentType === self::CONTENT_XHTML ? ' />' : '>', $token->text);
281: if (!$htmlNode->isEmpty) {
282: $end = "</$htmlNode->name>";
283: }
284: }
285:
286: if (empty($htmlNode->macroAttrs)) {
287: $this->output .= $token->text . $end;
288: } else {
289: $code = substr($this->output, $htmlNode->offset) . $token->text;
290: $this->output = substr($this->output, 0, $htmlNode->offset);
291: $this->writeAttrsMacro($code);
292: if ($isEmpty) {
293: $htmlNode->closing = TRUE;
294: $this->writeAttrsMacro($end);
295: }
296: }
297:
298: if ($isEmpty) {
299: $htmlNode->closing = TRUE;
300: }
301:
302: $lower = strtolower($htmlNode->name);
303: if (!$htmlNode->closing && ($lower === 'script' || $lower === 'style')) {
304: $this->setContext($lower === 'script' ? self::CONTENT_JS : self::CONTENT_CSS);
305: } else {
306: $this->setContext(NULL);
307: if ($htmlNode->closing) {
308: $this->htmlNode = $this->htmlNode->parentNode;
309: }
310: }
311: }
312:
313:
314: private function processHtmlAttribute(Token $token)
315: {
316: if (strncmp($token->name, Parser::N_PREFIX, strlen(Parser::N_PREFIX)) === 0) {
317: $name = substr($token->name, strlen(Parser::N_PREFIX));
318: if (isset($this->htmlNode->macroAttrs[$name])) {
319: throw new CompileException("Found multiple attributes $token->name.");
320:
321: } elseif ($this->macroNode && $this->macroNode->htmlNode === $this->htmlNode) {
322: throw new CompileException("n:attributes must not appear inside macro; found $token->name inside {{$this->macroNode->name}}.");
323: }
324: $this->htmlNode->macroAttrs[$name] = $token->value;
325: return;
326: }
327:
328: $this->htmlNode->attrs[$token->name] = TRUE;
329: $this->output .= $token->text;
330:
331: $contextMain = in_array($token->value, array(self::CONTEXT_SINGLE_QUOTED_ATTR, self::CONTEXT_DOUBLE_QUOTED_ATTR), TRUE)
332: ? $token->value
333: : self::CONTEXT_UNQUOTED_ATTR;
334:
335: $context = NULL;
336: if (in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML), TRUE)) {
337: $lower = strtolower($token->name);
338: if (substr($lower, 0, 2) === 'on') {
339: $context = self::CONTENT_JS;
340: } elseif ($lower === 'style') {
341: $context = self::CONTENT_CSS;
342: } elseif (in_array($lower, array('href', 'src', 'action', 'formaction'), TRUE)
343: || ($lower === 'data' && strtolower($this->htmlNode->name) === 'object')
344: ) {
345: $context = self::CONTENT_URL;
346: }
347: }
348:
349: $this->setContext($contextMain, $context);
350: }
351:
352:
353: private function (Token $token)
354: {
355: $isLeftmost = trim(substr($this->output, strrpos("\n$this->output", "\n"))) === '';
356: if (!$isLeftmost) {
357: $this->output .= substr($token->text, strlen(rtrim($token->text, "\n")));
358: }
359: }
360:
361:
362:
363:
364:
365: 366: 367: 368: 369: 370: 371: 372: 373:
374: public function openMacro($name, $args = NULL, $modifiers = NULL, $isRightmost = FALSE, $nPrefix = NULL)
375: {
376: $node = $this->expandMacro($name, $args, $modifiers, $nPrefix);
377: if ($node->isEmpty) {
378: $this->writeCode($node->openingCode, $this->output, $node->replaced, $isRightmost);
379: } else {
380: $this->macroNode = $node;
381: $node->saved = array(& $this->output, $isRightmost);
382: $this->output = & $node->content;
383: }
384: return $node;
385: }
386:
387:
388: 389: 390: 391: 392: 393: 394: 395: 396:
397: public function closeMacro($name, $args = NULL, $modifiers = NULL, $isRightmost = FALSE, $nPrefix = NULL)
398: {
399: $node = $this->macroNode;
400:
401: if (!$node || ($node->name !== $name && '' !== $name) || $modifiers
402: || ($args && $node->args && strncmp("$node->args ", "$args ", strlen($args) + 1))
403: || $nPrefix !== $node->prefix
404: ) {
405: $name = $nPrefix
406: ? "</{$this->htmlNode->name}> for " . Parser::N_PREFIX . implode(' and ' . Parser::N_PREFIX, array_keys($this->htmlNode->macroAttrs))
407: : '{/' . $name . ($args ? ' ' . $args : '') . $modifiers . '}';
408: throw new CompileException("Unexpected $name" . ($node ? ', expecting ' . self::printEndTag($node) : ''));
409: }
410:
411: $this->macroNode = $node->parentNode;
412: if (!$node->args) {
413: $node->setArgs($args);
414: }
415:
416: $isLeftmost = $node->content ? trim(substr($this->output, strrpos("\n$this->output", "\n"))) === '' : FALSE;
417:
418: $node->closing = TRUE;
419: $node->macro->nodeClosed($node);
420:
421: $this->output = & $node->saved[0];
422: $this->writeCode($node->openingCode, $this->output, $node->replaced, $node->saved[1]);
423: $this->writeCode($node->closingCode, $node->content, $node->replaced, $isRightmost, $isLeftmost);
424: $this->output .= $node->content;
425: return $node;
426: }
427:
428:
429: private function writeCode($code, & $output, $replaced, $isRightmost, $isLeftmost = NULL)
430: {
431: if ($isRightmost) {
432: $leftOfs = strrpos("\n$output", "\n");
433: if ($isLeftmost === NULL) {
434: $isLeftmost = trim(substr($output, $leftOfs)) === '';
435: }
436: if ($replaced === NULL) {
437: $replaced = preg_match('#<\?php.*\secho\s#As', $code);
438: }
439: if ($isLeftmost && !$replaced) {
440: $output = substr($output, 0, $leftOfs);
441: } elseif (substr($code, -2) === '?>') {
442: $code .= "\n";
443: }
444: }
445: $output .= $code;
446: }
447:
448:
449: 450: 451: 452: 453: 454:
455: public function writeAttrsMacro($code)
456: {
457: $attrs = $this->htmlNode->macroAttrs;
458: $left = $right = array();
459:
460: foreach ($this->macros as $name => $foo) {
461: $attrName = MacroNode::PREFIX_INNER . "-$name";
462: if (isset($attrs[$attrName])) {
463: if ($this->htmlNode->closing) {
464: $left[] = array('closeMacro', $name, '', MacroNode::PREFIX_INNER);
465: } else {
466: array_unshift($right, array('openMacro', $name, $attrs[$attrName], MacroNode::PREFIX_INNER));
467: }
468: unset($attrs[$attrName]);
469: }
470: }
471:
472: foreach (array_reverse($this->macros) as $name => $foo) {
473: $attrName = MacroNode::PREFIX_TAG . "-$name";
474: if (isset($attrs[$attrName])) {
475: $left[] = array('openMacro', $name, $attrs[$attrName], MacroNode::PREFIX_TAG);
476: array_unshift($right, array('closeMacro', $name, '', MacroNode::PREFIX_TAG));
477: unset($attrs[$attrName]);
478: }
479: }
480:
481: foreach ($this->macros as $name => $foo) {
482: if (isset($attrs[$name])) {
483: if ($this->htmlNode->closing) {
484: $right[] = array('closeMacro', $name, '', MacroNode::PREFIX_NONE);
485: } else {
486: array_unshift($left, array('openMacro', $name, $attrs[$name], MacroNode::PREFIX_NONE));
487: }
488: unset($attrs[$name]);
489: }
490: }
491:
492: if ($attrs) {
493: throw new CompileException('Unknown attribute ' . Parser::N_PREFIX
494: . implode(' and ' . Parser::N_PREFIX, array_keys($attrs)));
495: }
496:
497: if (!$this->htmlNode->closing) {
498: $this->htmlNode->attrCode = & $this->attrCodes[$uniq = ' n:' . substr(lcg_value(), 2, 10)];
499: $code = substr_replace($code, $uniq, strrpos($code, '/>') ?: strrpos($code, '>'), 0);
500: }
501:
502: foreach ($left as $item) {
503: $node = $this->{$item[0]}($item[1], $item[2], NULL, NULL, $item[3]);
504: if ($node->closing || $node->isEmpty) {
505: $this->htmlNode->attrCode .= $node->attrCode;
506: if ($node->isEmpty) {
507: unset($this->htmlNode->macroAttrs[$node->name]);
508: }
509: }
510: }
511:
512: $this->output .= $code;
513:
514: foreach ($right as $item) {
515: $node = $this->{$item[0]}($item[1], $item[2], NULL, NULL, $item[3]);
516: if ($node->closing) {
517: $this->htmlNode->attrCode .= $node->attrCode;
518: }
519: }
520:
521: if ($right && substr($this->output, -2) === '?>') {
522: $this->output .= "\n";
523: }
524: }
525:
526:
527: 528: 529: 530: 531: 532: 533: 534:
535: public function expandMacro($name, $args, $modifiers = NULL, $nPrefix = NULL)
536: {
537: $inScript = in_array($this->context[0], array(self::CONTENT_JS, self::CONTENT_CSS), TRUE);
538:
539: if (empty($this->macros[$name])) {
540: throw new CompileException("Unknown macro {{$name}}" . ($inScript ? ' (in JavaScript or CSS, try to put a space after bracket.)' : ''));
541: }
542:
543: if ($this->context[1] === self::CONTENT_URL) {
544: $modifiers = preg_replace('#\|nosafeurl\s?(?=\||\z)#i', '', $modifiers, -1, $found);
545: if (!$found && !preg_match('#\|datastream(?=\s|\||\z)#i', $modifiers)) {
546: $modifiers .= '|safeurl';
547: }
548: }
549:
550: $modifiers = preg_replace('#\|noescape\s?(?=\||\z)#i', '', $modifiers, -1, $found);
551: if (!$found && strpbrk($name, '=~%^&_')) {
552: $modifiers .= '|escape';
553: }
554:
555: if (!$found && $inScript && $name === '=' && preg_match('#["\'] *\z#', $this->tokens[$this->position - 1]->text)) {
556: throw new CompileException("Do not place {$this->tokens[$this->position]->text} inside quotes.");
557: }
558:
559: foreach (array_reverse($this->macros[$name]) as $macro) {
560: $node = new MacroNode($macro, $name, $args, $modifiers, $this->macroNode, $this->htmlNode, $nPrefix);
561: if ($macro->nodeOpened($node) !== FALSE) {
562: return $node;
563: }
564: }
565:
566: throw new CompileException('Unknown ' . ($nPrefix
567: ? 'attribute ' . Parser::N_PREFIX . ($nPrefix === MacroNode::PREFIX_NONE ? '' : "$nPrefix-") . $name
568: : 'macro {' . $name . ($args ? " $args" : '') . '}'
569: ));
570: }
571:
572:
573: private static function printEndTag(MacroNode $node)
574: {
575: if ($node->prefix) {
576: return "</{$node->htmlNode->name}> for " . Parser::N_PREFIX
577: . implode(' and ' . Parser::N_PREFIX, array_keys($node->htmlNode->macroAttrs));
578: } else {
579: return "{/$node->name}";
580: }
581: }
582:
583: }
584: