1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19:
20: final class TemplateFilters
21: {
22:
23: 24: 25:
26: final public function __construct()
27: {
28: throw new LogicException("Cannot instantiate static class " . get_class($this));
29: }
30:
31:
32:
33:
34:
35:
36:
37: 38: 39: 40: 41:
42: public static function removePhp($s)
43: {
44: return String::replace($s, '#\x01@php:p\d+@\x02#', '<?php ?>'); 45: }
46:
47:
48:
49:
50:
51:
52:
53: 54: 55: 56: 57:
58: public static function relativeLinks($s)
59: {
60: return String::replace(
61: $s,
62: '#(src|href|action)\s*=\s*(["\'])(?![a-z]+:|[\x01/\\#])#', 63: '$1=$2<?php echo \\$baseUri ?>'
64: );
65: }
66:
67:
68:
69:
70:
71:
72:
73: 74: 75: 76: 77: 78:
79: public static function netteLinks($s)
80: {
81: return String::replace($s, '#(src|href|action)\s*=\s*(["\'])(nette:.*?)([\#"\'])#', callback(create_function('$m', '
82: list(, $attr, $quote, $uri, $fragment) = $m;
83: $parts = parse_url($uri);
84: if (isset($parts[\'scheme\']) && $parts[\'scheme\'] === \'nette\') {
85: return $attr . \'=\' . $quote . \'<?php echo $template->escape($control->\'
86: . "link(\'"
87: . (isset($parts[\'path\']) ? $parts[\'path\'] : \'this!\')
88: . (isset($parts[\'query\']) ? \'?\' . $parts[\'query\'] : \'\')
89: . \'\\\'))?>\'
90: . $fragment;
91: } else {
92: return $m[0];
93: }
94: ')));
95: }
96:
97:
98:
99:
100:
101:
102:
103:
104: public static $texy;
105:
106:
107:
108: 109: 110: 111: 112:
113: public static function texyElements($s)
114: {
115: return String::replace($s, '#<texy([^>]*)>(.*?)</texy>#s', callback(create_function('$m', '
116: list(, $mAttrs, $mContent) = $m;
117: // parse attributes
118: $attrs = array();
119: if ($mAttrs) {
120: foreach (String::matchAll($mAttrs, \'#([a-z0-9:-]+)\\s*(?:=\\s*(\\\'[^\\\']*\\\'|"[^"]*"|[^\\\'"\\s]+))?()#isu\') as $m) {
121: $key = strtolower($m[1]);
122: $val = $m[2];
123: if ($val == NULL) $attrs[$key] = TRUE;
124: elseif ($val{0} === \'\\\'\' || $val{0} === \'"\') $attrs[$key] = html_entity_decode(substr($val, 1, -1), ENT_QUOTES, \'UTF-8\');
125: else $attrs[$key] = html_entity_decode($val, ENT_QUOTES, \'UTF-8\');
126: }
127: }
128: return TemplateFilters::$texy->process($m[2]);
129: ')));
130: }
131:
132: }
133: