PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/include/Savant/Savant2/Savant2_Filter_trimwhitespace.php

https://github.com/radicaldesigns/amp
PHP | 104 lines | 40 code | 14 blank | 50 comment | 3 complexity | b636d5a1f4ba0a9b5324ffb3d10cbdb9 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * Base filter class.
  4. */
  5. require_once 'Savant2/Filter.php';
  6. /**
  7. *
  8. * Remove extra white space within the text.
  9. *
  10. * $Id: Savant2_Filter_trimwhitespace.php,v 1.2 2004/10/28 20:51:27 pmjones Exp $
  11. *
  12. * @author Monte Ohrt <monte@ispi.net>
  13. *
  14. * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
  15. *
  16. * @author Converted to a Savant2 filter by Paul M. Jones
  17. * <pmjones@ciaweb.net>
  18. *
  19. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  20. *
  21. * This program is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU Lesser General Public License as
  23. * published by the Free Software Foundation; either version 2.1 of the
  24. * License, or (at your option) any later version.
  25. *
  26. * This program is distributed in the hope that it will be useful, but
  27. * WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. */
  32. class Savant2_Filter_trimwhitespace extends Savant2_Filter {
  33. /**
  34. *
  35. * Removes extra white space within the text.
  36. *
  37. * Trim leading white space and blank lines from template source after it
  38. * gets interpreted, cleaning up code and saving bandwidth. Does not
  39. * affect <PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br>
  40. *
  41. * @access public
  42. *
  43. * @param string &$source The source text to be filtered.
  44. *
  45. */
  46. function filter(&$source)
  47. {
  48. // Pull out the script blocks
  49. preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
  50. $_script_blocks = $match[0];
  51. $source = preg_replace("!<script[^>]+>.*?</script>!is",
  52. '@@@SAVANT:TRIM:SCRIPT@@@', $source);
  53. // Pull out the pre blocks
  54. preg_match_all("!<pre[^>]+>.*?</pre>!is", $source, $match);
  55. $_pre_blocks = $match[0];
  56. $source = preg_replace("!<pre[^>]+>.*?</pre>!is",
  57. '@@@SAVANT:TRIM:PRE@@@', $source);
  58. // Pull out the textarea blocks
  59. preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
  60. $_textarea_blocks = $match[0];
  61. $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is",
  62. '@@@SAVANT:TRIM:TEXTAREA@@@', $source);
  63. // remove all leading spaces, tabs and carriage returns NOT
  64. // preceeded by a php close tag.
  65. $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
  66. // replace script blocks
  67. Savant2_Filter_trimwhitespace::_replace(
  68. "@@@SAVANT:TRIM:SCRIPT@@@",$_script_blocks, $source);
  69. // replace pre blocks
  70. Savant2_Filter_trimwhitespace::_replace(
  71. "@@@SAVANT:TRIM:PRE@@@",$_pre_blocks, $source);
  72. // replace textarea blocks
  73. Savant2_Filter_trimwhitespace::_replace(
  74. "@@@SAVANT:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
  75. return $source;
  76. }
  77. function _replace($search_str, $replace, &$subject)
  78. {
  79. $_len = strlen($search_str);
  80. $_pos = 0;
  81. for ($_i=0, $_count=count($replace); $_i<$_count; $_i++) {
  82. if (($_pos=strpos($subject, $search_str, $_pos))!==false) {
  83. $subject = substr_replace($subject, $replace[$_i], $_pos, $_len);
  84. } else {
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. ?>