PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Templates/Filters/TemplateFilters.php

https://github.com/DocX/nette
PHP | 179 lines | 89 code | 35 blank | 55 comment | 7 complexity | fd49096f6fb798b84b89e4d7c94c8913 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Templates
  17. */
  18. /*namespace Nette\Templates;*/
  19. /**
  20. * Standard template compile-time filters shipped with Nette Framework.
  21. *
  22. * @author David Grudl
  23. * @copyright Copyright (c) 2004, 2009 David Grudl
  24. * @package Nette\Templates
  25. */
  26. final class TemplateFilters
  27. {
  28. /**
  29. * Static class - cannot be instantiated.
  30. */
  31. final public function __construct()
  32. {
  33. throw new /*\*/LogicException("Cannot instantiate static class " . get_class($this));
  34. }
  35. /********************* Filter removePhp ****************d*g**/
  36. /**
  37. * Filters out PHP code.
  38. *
  39. * @param string
  40. * @return string
  41. */
  42. public static function removePhp($s)
  43. {
  44. return preg_replace('#\x01@php:p\d+@\x02#', '<?php ?>', $s); // Template hides PHP code in these snippets
  45. }
  46. /********************* Filter relativeLinks ****************d*g**/
  47. /**
  48. * Filter relativeLinks: prepends root to relative links.
  49. * @param string
  50. * @return string
  51. */
  52. public static function relativeLinks($s)
  53. {
  54. return preg_replace(
  55. '#(src|href|action)\s*=\s*(["\'])(?![a-z]+:|[\x01/\\#])#', // \x01 is PHP snippet
  56. '$1=$2<?php echo \\$baseUri ?>',
  57. $s
  58. );
  59. }
  60. /********************* Filter netteLinks ****************d*g**/
  61. /**
  62. * Filter netteLinks: translates links "nette:...".
  63. * nette:destination?arg
  64. * @param string
  65. * @return string
  66. */
  67. public static function netteLinks($s)
  68. {
  69. return preg_replace_callback(
  70. '#(src|href|action)\s*=\s*(["\'])(nette:.*?)([\#"\'])#',
  71. array(__CLASS__, 'netteLinksCb'),
  72. $s
  73. );
  74. }
  75. /**
  76. * Callback for self::netteLinks.
  77. * Parses a "nette" URI (scheme is 'nette') and converts to real URI
  78. */
  79. private static function netteLinksCb($m)
  80. {
  81. list(, $attr, $quote, $uri, $fragment) = $m;
  82. $parts = parse_url($uri);
  83. if (isset($parts['scheme']) && $parts['scheme'] === 'nette') {
  84. return $attr . '=' . $quote . '<?php echo $template->escape($control->'
  85. . "link('"
  86. . (isset($parts['path']) ? $parts['path'] : 'this!')
  87. . (isset($parts['query']) ? '?' . $parts['query'] : '')
  88. . '\'))?>'
  89. . $fragment;
  90. } else {
  91. return $m[0];
  92. }
  93. }
  94. /********************* Filter texyElements ****************d*g**/
  95. /** @var Texy */
  96. public static $texy;
  97. /**
  98. * Process <texy>...</texy> elements.
  99. * @param string
  100. * @return string
  101. */
  102. public static function texyElements($s)
  103. {
  104. return preg_replace_callback(
  105. '#<texy([^>]*)>(.*?)</texy>#s',
  106. array(__CLASS__, 'texyCb'),
  107. $s
  108. );
  109. }
  110. /**
  111. * Callback for self::texyBlocks.
  112. */
  113. private static function texyCb($m)
  114. {
  115. list(, $mAttrs, $mContent) = $m;
  116. // parse attributes
  117. $attrs = array();
  118. if ($mAttrs) {
  119. preg_match_all(
  120. '#([a-z0-9:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
  121. $mAttrs,
  122. $arr,
  123. PREG_SET_ORDER
  124. );
  125. foreach ($arr 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 self::$texy->process($m[2]);
  134. }
  135. }