Source for file TemplateFilters.php

Documentation is available at TemplateFilters.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Templates
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Standard template compile-time filters shipped with Nette Framework.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Templates
  20. 20:  */
  21. 21: final class TemplateFilters
  22. 22: {
  23. 23:  
  24. 24:     /**
  25. 25:      * Static class - cannot be instantiated.
  26. 26:      */
  27. 27:     final public function __construct()
  28. 28:     {
  29. 29:         throw new LogicException("Cannot instantiate static class " get_class($this));
  30. 30:     }
  31. 31:  
  32. 32:  
  33. 33:  
  34. 34:     /********************* Filter removePhp ****************d*g**/
  35. 35:  
  36. 36:  
  37. 37:  
  38. 38:     /**
  39. 39:      * Filters out PHP code.
  40. 40:      *
  41. 41:      * @param  string 
  42. 42:      * @return string 
  43. 43:      */
  44. 44:     public static function removePhp($s)
  45. 45:     {
  46. 46:         return preg_replace('#\x01@php:p\d+@\x02#''<?php ?>'$s)// Template hides PHP code in these snippets
  47. 47:     }
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /********************* Filter relativeLinks ****************d*g**/
  52. 52:  
  53. 53:  
  54. 54:  
  55. 55:     /**
  56. 56:      * Filter relativeLinks: prepends root to relative links.
  57. 57:      * @param  string 
  58. 58:      * @return string 
  59. 59:      */
  60. 60:     public static function relativeLinks($s)
  61. 61:     {
  62. 62:         return preg_replace(
  63. 63:             '#(src|href|action)\s*=\s*(["\'])(?![a-z]+:|[\x01/\\#])#'// \x01 is PHP snippet
  64. 64:             '$1=$2<?php echo \\$baseUri ?>',
  65. 65:             $s
  66. 66:         );
  67. 67:     }
  68. 68:  
  69. 69:  
  70. 70:  
  71. 71:     /********************* Filter netteLinks ****************d*g**/
  72. 72:  
  73. 73:  
  74. 74:  
  75. 75:     /**
  76. 76:      * Filter netteLinks: translates links "nette:...".
  77. 77:      *   nette:destination?arg
  78. 78:      * @param  string 
  79. 79:      * @return string 
  80. 80:      */
  81. 81:     public static function netteLinks($s)
  82. 82:     {
  83. 83:         return preg_replace_callback(
  84. 84:             '#(src|href|action)\s*=\s*(["\'])(nette:.*?)([\#"\'])#',
  85. 85:             array(__CLASS__'netteLinksCb'),
  86. 86:             $s
  87. 87:         );
  88. 88:     }
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     /**
  93. 93:      * Callback for self::netteLinks.
  94. 94:      * Parses a "nette" URI (scheme is 'nette') and converts to real URI
  95. 95:      */
  96. 96:     private static function netteLinksCb($m)
  97. 97:     {
  98. 98:         list($attr$quote$uri$fragment$m;
  99. 99:  
  100. 100:         $parts parse_url($uri);
  101. 101:         if (isset($parts['scheme']&& $parts['scheme'=== 'nette'{
  102. 102:             return $attr '=' $quote '<?php echo $template->escape($control->'
  103. 103:                 . "link('"
  104. 104:                 . (isset($parts['path']$parts['path''this!')
  105. 105:                 . (isset($parts['query']'?' $parts['query''')
  106. 106:                 . '\'))?>'
  107. 107:                 . $fragment;
  108. 108:         else {
  109. 109:             return $m[0];
  110. 110:         }
  111. 111:     }
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /********************* Filter texyElements ****************d*g**/
  116. 116:  
  117. 117:  
  118. 118:  
  119. 119:     /** @var Texy */
  120. 120:     public static $texy;
  121. 121:  
  122. 122:  
  123. 123:  
  124. 124:     /**
  125. 125:      * Process <texy>...</texy> elements.
  126. 126:      * @param  string 
  127. 127:      * @return string 
  128. 128:      */
  129. 129:     public static function texyElements($s)
  130. 130:     {
  131. 131:         return preg_replace_callback(
  132. 132:             '#<texy([^>]*)>(.*?)</texy>#s',
  133. 133:             array(__CLASS__'texyCb'),
  134. 134:             $s
  135. 135:         );
  136. 136:     }
  137. 137:  
  138. 138:  
  139. 139:  
  140. 140:     /**
  141. 141:      * Callback for self::texyBlocks.
  142. 142:      */
  143. 143:     private static function texyCb($m)
  144. 144:     {
  145. 145:         list($mAttrs$mContent$m;
  146. 146:  
  147. 147:         // parse attributes
  148. 148:         $attrs array();
  149. 149:         if ($mAttrs{
  150. 150:             preg_match_all(
  151. 151:                 '#([a-z0-9:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
  152. 152:                 $mAttrs,
  153. 153:                 $arr,
  154. 154:                 PREG_SET_ORDER
  155. 155:             );
  156. 156:  
  157. 157:             foreach ($arr as $m{
  158. 158:                 $key strtolower($m[1]);
  159. 159:                 $val $m[2];
  160. 160:                 if ($val == NULL$attrs[$keyTRUE;
  161. 161:                 elseif ($val{0=== '\'' || $val{0=== '"'$attrs[$keyhtml_entity_decode(substr($val1-1)ENT_QUOTES'UTF-8');
  162. 162:                 else $attrs[$keyhtml_entity_decode($valENT_QUOTES'UTF-8');
  163. 163:             }
  164. 164:         }
  165. 165:  
  166. 166:         return self::$texy->process($m[2]);
  167. 167:     }
  168. 168: