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: public function expandTokens($s)
201: {
202: return strtr($s, $this->attrCodes);
203: }
204:
205:
206: private function processText(Token $token)
207: {
208: if (in_array($this->context[0], array(self::CONTEXT_SINGLE_QUOTED_ATTR, self::CONTEXT_DOUBLE_QUOTED_ATTR), TRUE)
209: && $token->text === $this->context[0]
210: ) {
211: $this->setContext(self::CONTEXT_UNQUOTED_ATTR);
212: }
213: $this->output .= $token->text;
214: }
215:
216:
217: private function processMacroTag(Token $token)
218: {
219: $isRightmost = !isset($this->tokens[$this->position + 1])
220: || substr($this->tokens[$this->position + 1]->text, 0, 1) === "\n";
221:
222: if ($token->name[0] === '/') {
223: $this->closeMacro((string) substr($token->name, 1), $token->value, $token->modifiers, $isRightmost);
224: } else {
225: $this->openMacro($token->name, $token->value, $token->modifiers, $isRightmost && !$token->empty);
226: if ($token->empty) {
227: $this->closeMacro($token->name, NULL, NULL, $isRightmost);
228: }
229: }
230: }
231:
232:
233: private function processHtmlTagBegin(Token $token)
234: {
235: if ($token->closing) {
236: while ($this->htmlNode) {
237: if (strcasecmp($this->htmlNode->name, $token->name) === 0) {
238: break;
239: }
240: if ($this->htmlNode->macroAttrs) {
241: throw new CompileException("Unexpected </$token->name>, expecting " . self::printEndTag($this->macroNode));
242: }
243: $this->htmlNode = $this->htmlNode->parentNode;
244: }
245: if (!$this->htmlNode) {
246: $this->htmlNode = new HtmlNode($token->name);
247: }
248: $this->htmlNode->closing = TRUE;
249: $this->htmlNode->offset = strlen($this->output);
250: $this->setContext(NULL);
251:
252: } elseif ($token->text === '<!--') {
253: $this->setContext(self::CONTEXT_COMMENT);
254:
255: } else {
256: $this->htmlNode = new HtmlNode($token->name, $this->htmlNode);
257: $this->htmlNode->isEmpty = in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML), TRUE)
258: && isset(Helpers::$emptyElements[strtolower($token->name)]);
259: $this->htmlNode->offset = strlen($this->output);
260: $this->setContext(self::CONTEXT_UNQUOTED_ATTR);
261: }
262: $this->output .= $token->text;
263: }
264:
265:
266: private function processHtmlTagEnd(Token $token)
267: {
268: if ($token->text === '-->') {
269: $this->output .= $token->text;
270: $this->setContext(NULL);
271: return;
272: }
273:
274: $htmlNode = $this->htmlNode;
275: $isEmpty = !$htmlNode->closing && (strpos($token->text, '/') !== FALSE || $htmlNode->isEmpty);
276: $end = '';
277:
278: if ($isEmpty && in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML), TRUE)) {
279: $token->text = preg_replace('#^.*>#', $htmlNode->isEmpty && $this->contentType === self::CONTENT_XHTML ? ' />' : '>', $token->text);
280: if (!$htmlNode->isEmpty) {
281: $end = "</$htmlNode->name>";
282: }
283: }
284:
285: if (empty($htmlNode->macroAttrs)) {
286: $this->output .= $token->text . $end;
287: } else {
288: $code = substr($this->output, $htmlNode->offset) . $token->text;
289: $this->output = substr($this->output, 0, $htmlNode->offset);
290: $this->writeAttrsMacro($code);
291: if ($isEmpty) {
292: $htmlNode->closing = TRUE;
293: $this->writeAttrsMacro($end);
294: }
295: }
296:
297: if ($isEmpty) {
298: $htmlNode->closing = TRUE;
299: }
300:
301: $lower = strtolower($htmlNode->name);
302: if (!$htmlNode->closing && ($lower === 'script' || $lower === 'style')) {
303: $this->setContext($lower === 'script' ? self::CONTENT_JS : self::CONTENT_CSS);
304: } else {
305: $this->setContext(NULL);
306: if ($htmlNode->closing) {
307: $this->htmlNode = $this->htmlNode->parentNode;
308: }
309: }
310: }
311:
312:
313: private function processHtmlAttribute(Token $token)
314: {
315: if (strncmp($token->name, Parser::N_PREFIX, strlen(Parser::N_PREFIX)) === 0) {
316: $name = substr($token->name, strlen(Parser::N_PREFIX));
317: if (isset($this->htmlNode->macroAttrs[$name])) {
318: throw new CompileException("Found multiple attributes $token->name.");
319:
320: } elseif ($this->macroNode && $this->macroNode->htmlNode === $this->htmlNode) {
321: throw new CompileException("n:attributes must not appear inside macro; found $token->name inside {{$this->macroNode->name}}.");
322: }
323: $this->htmlNode->macroAttrs[$name] = $token->value;
324: return;
325: }
326:
327: $this->htmlNode->attrs[$token->name] = TRUE;
328: $this->output .= $token->text;
329:
330: $contextMain = in_array($token->value, array(self::CONTEXT_SINGLE_QUOTED_ATTR, self::CONTEXT_DOUBLE_QUOTED_ATTR), TRUE)
331: ? $token->value
332: : self::CONTEXT_UNQUOTED_ATTR;
333:
334: $context = NULL;
335: if (in_array($this->contentType, array(self::CONTENT_HTML, self::CONTENT_XHTML), TRUE)) {
336: $lower = strtolower($token->name);
337: if (substr($lower, 0, 2) === 'on') {
338: $context = self::CONTENT_JS;
339: } elseif ($lower === 'style') {
340: $context = self::CONTENT_CSS;
341: } elseif (in_array($lower, array('href', 'src', 'action', 'formaction'), TRUE)
342: || ($lower === 'data' && strtolower($this->htmlNode->name) === 'object')
343: ) {
344: $context = self::CONTENT_URL;
345: }
346: }
347:
348: $this->setContext($contextMain, $context);
349: }
350:
351:
352: private function (Token $token)
353: {
354: $isLeftmost = trim(substr($this->output, strrpos("\n$this->output", "\n"))) === '';
355: if (!$isLeftmost) {
356: $this->output .= substr($token->text, strlen(rtrim($token->text, "\n")));
357: }
358: }
359:
360:
361:
362:
363:
364: 365: 366: 367: 368: 369: 370: 371:
372: public function openMacro($name, $args = NULL, $modifiers = NULL, $isRightmost = FALSE, $nPrefix = NULL)
373: {
374: $node = $this->expandMacro($name, $args, $modifiers, $nPrefix);
375: if ($node->isEmpty) {
376: $this->writeCode($node->openingCode, $this->output, $isRightmost);
377: } else {
378: $this->macroNode = $node;
379: $node->saved = array(& $this->output, $isRightmost);
380: $this->output = & $node->content;
381: }
382: return $node;
383: }
384:
385:
386: 387: 388: 389: 390: 391: 392: 393:
394: public function closeMacro($name, $args = NULL, $modifiers = NULL, $isRightmost = FALSE, $nPrefix = NULL)
395: {
396: $node = $this->macroNode;
397:
398: if (!$node || ($node->name !== $name && '' !== $name) || $modifiers
399: || ($args && $node->args && strncmp("$node->args ", "$args ", strlen($args) + 1))
400: || $nPrefix !== $node->prefix
401: ) {
402: $name = $nPrefix
403: ? "</{$this->htmlNode->name}> for " . Parser::N_PREFIX . implode(' and ' . Parser::N_PREFIX, array_keys($this->htmlNode->macroAttrs))
404: : '{/' . $name . ($args ? ' ' . $args : '') . $modifiers . '}';
405: throw new CompileException("Unexpected $name" . ($node ? ', expecting ' . self::printEndTag($node) : ''));
406: }
407:
408: $this->macroNode = $node->parentNode;
409: if (!$node->args) {
410: $node->setArgs($args);
411: }
412:
413: $isLeftmost = $node->content ? trim(substr($this->output, strrpos("\n$this->output", "\n"))) === '' : FALSE;
414:
415: $node->closing = TRUE;
416: $node->macro->nodeClosed($node);
417:
418: $this->output = & $node->saved[0];
419: $this->writeCode($node->openingCode, $this->output, $node->saved[1]);
420: $this->writeCode($node->closingCode, $node->content, $isRightmost, $isLeftmost);
421: $this->output .= $node->content;
422: return $node;
423: }
424:
425:
426: private function writeCode($code, & $output, $isRightmost, $isLeftmost = NULL)
427: {
428: if ($isRightmost) {
429: $leftOfs = strrpos("\n$output", "\n");
430: $isLeftmost = $isLeftmost === NULL ? trim(substr($output, $leftOfs)) === '' : $isLeftmost;
431: if ($isLeftmost && substr($code, 0, 11) !== '<?php echo ') {
432: $output = substr($output, 0, $leftOfs);
433: } elseif (substr($code, -2) === '?>') {
434: $code .= "\n";
435: }
436: }
437: $output .= $code;
438: }
439:
440:
441: 442: 443: 444: 445:
446: public function writeAttrsMacro($code)
447: {
448: $attrs = $this->htmlNode->macroAttrs;
449: $left = $right = array();
450:
451: foreach ($this->macros as $name => $foo) {
452: $attrName = MacroNode::PREFIX_INNER . "-$name";
453: if (isset($attrs[$attrName])) {
454: if ($this->htmlNode->closing) {
455: $left[] = array('closeMacro', $name, '', MacroNode::PREFIX_INNER);
456: } else {
457: array_unshift($right, array('openMacro', $name, $attrs[$attrName], MacroNode::PREFIX_INNER));
458: }
459: unset($attrs[$attrName]);
460: }
461: }
462:
463: foreach (array_reverse($this->macros) as $name => $foo) {
464: $attrName = MacroNode::PREFIX_TAG . "-$name";
465: if (isset($attrs[$attrName])) {
466: $left[] = array('openMacro', $name, $attrs[$attrName], MacroNode::PREFIX_TAG);
467: array_unshift($right, array('closeMacro', $name, '', MacroNode::PREFIX_TAG));
468: unset($attrs[$attrName]);
469: }
470: }
471:
472: foreach ($this->macros as $name => $foo) {
473: if (isset($attrs[$name])) {
474: if ($this->htmlNode->closing) {
475: $right[] = array('closeMacro', $name, '', MacroNode::PREFIX_NONE);
476: } else {
477: array_unshift($left, array('openMacro', $name, $attrs[$name], MacroNode::PREFIX_NONE));
478: }
479: unset($attrs[$name]);
480: }
481: }
482:
483: if ($attrs) {
484: throw new CompileException('Unknown attribute ' . Parser::N_PREFIX
485: . implode(' and ' . Parser::N_PREFIX, array_keys($attrs)));
486: }
487:
488: if (!$this->htmlNode->closing) {
489: $this->htmlNode->attrCode = & $this->attrCodes[$uniq = ' n:' . substr(lcg_value(), 2, 10)];
490: $code = substr_replace($code, $uniq, strrpos($code, '/>') ?: strrpos($code, '>'), 0);
491: }
492:
493: foreach ($left as $item) {
494: $node = $this->{$item[0]}($item[1], $item[2], NULL, NULL, $item[3]);
495: if ($node->closing || $node->isEmpty) {
496: $this->htmlNode->attrCode .= $node->attrCode;
497: if ($node->isEmpty) {
498: unset($this->htmlNode->macroAttrs[$node->name]);
499: }
500: }
501: }
502:
503: $this->output .= $code;
504:
505: foreach ($right as $item) {
506: $node = $this->{$item[0]}($item[1], $item[2], NULL, NULL, $item[3]);
507: if ($node->closing) {
508: $this->htmlNode->attrCode .= $node->attrCode;
509: }
510: }
511:
512: if ($right && substr($this->output, -2) === '?>') {
513: $this->output .= "\n";
514: }
515: }
516:
517:
518: 519: 520: 521: 522: 523: 524:
525: public function expandMacro($name, $args, $modifiers = NULL, $nPrefix = NULL)
526: {
527: $inScript = in_array($this->context[0], array(self::CONTENT_JS, self::CONTENT_CSS), TRUE);
528:
529: if (empty($this->macros[$name])) {
530: throw new CompileException("Unknown macro {{$name}}" . ($inScript ? ' (in JavaScript or CSS, try to put a space after bracket.)' : ''));
531: }
532:
533: if ($this->context[1] === self::CONTENT_URL) {
534: $modifiers = preg_replace('#\|nosafeurl\s?(?=\||\z)#i', '', $modifiers, -1, $found);
535: if (!$found && !preg_match('#\|datastream(?=\s|\||\z)#i', $modifiers)) {
536: $modifiers .= '|safeurl';
537: }
538: }
539:
540: $modifiers = preg_replace('#\|noescape\s?(?=\||\z)#i', '', $modifiers, -1, $found);
541: if (!$found && strpbrk($name, '=~%^&_')) {
542: $modifiers .= '|escape';
543: }
544:
545: if (!$found && $inScript && $name === '=' && preg_match('#["\'] *\z#', $this->tokens[$this->position - 1]->text)) {
546: throw new CompileException("Do not place {$this->tokens[$this->position]->text} inside quotes.");
547: }
548:
549: foreach (array_reverse($this->macros[$name]) as $macro) {
550: $node = new MacroNode($macro, $name, $args, $modifiers, $this->macroNode, $this->htmlNode, $nPrefix);
551: if ($macro->nodeOpened($node) !== FALSE) {
552: return $node;
553: }
554: }
555:
556: throw new CompileException('Unknown ' . ($nPrefix
557: ? 'attribute ' . Parser::N_PREFIX . ($nPrefix === MacroNode::PREFIX_NONE ? '' : "$nPrefix-") . $name
558: : 'macro {' . $name . ($args ? " $args" : '') . '}'
559: ));
560: }
561:
562:
563: private static function printEndTag(MacroNode $node)
564: {
565: if ($node->prefix) {
566: return "</{$node->htmlNode->name}> for " . Parser::N_PREFIX
567: . implode(' and ' . Parser::N_PREFIX, array_keys($node->htmlNode->macroAttrs));
568: } else {
569: return "{/$node->name}";
570: }
571: }
572:
573: }
574: