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