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