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