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