Initial repo created
[timetracker.git] / WEB-INF / lib / smarty / plugins / outputfilter.trimwhitespace.php
1 <?php
2 /**
3  * Smarty plugin
4  *
5  * @package Smarty
6  * @subpackage PluginsFilter
7  */
8
9 /**
10  * Smarty trimwhitespace outputfilter plugin
11  *
12  * File:     outputfilter.trimwhitespace.php<br>
13  * Type:     outputfilter<br>
14  * Name:     trimwhitespace<br>
15  * Date:     Jan 25, 2003<br>
16  * Purpose:  trim leading white space and blank lines from
17  *           template source after it gets interpreted, cleaning
18  *           up code and saving bandwidth. Does not affect
19  *           <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br>
20  * Install:  Drop into the plugin directory, call
21  *           <code>$smarty->load_filter('output','trimwhitespace');</code>
22  *           from application.
23  * @author   Monte Ohrt <monte at ohrt dot com>
24  * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
25  * @version  1.3
26  * @param string $source input string
27  * @param object &$smarty Smarty object
28  * @return string filtered output
29  */
30 function smarty_outputfilter_trimwhitespace($source, $smarty)
31 {
32     // Pull out the script blocks
33     preg_match_all("!<script[^>]*?>.*?</script>!is", $source, $match);
34     $_script_blocks = $match[0];
35     $source = preg_replace("!<script[^>]*?>.*?</script>!is",
36                            '@@@SMARTY:TRIM:SCRIPT@@@', $source);
37
38     // Pull out the pre blocks
39     preg_match_all("!<pre[^>]*?>.*?</pre>!is", $source, $match);
40     $_pre_blocks = $match[0];
41     $source = preg_replace("!<pre[^>]*?>.*?</pre>!is",
42                            '@@@SMARTY:TRIM:PRE@@@', $source);
43     
44     // Pull out the textarea blocks
45     preg_match_all("!<textarea[^>]*?>.*?</textarea>!is", $source, $match);
46     $_textarea_blocks = $match[0];
47     $source = preg_replace("!<textarea[^>]*?>.*?</textarea>!is",
48                            '@@@SMARTY:TRIM:TEXTAREA@@@', $source);
49
50     // remove all leading spaces, tabs and carriage returns NOT
51     // preceeded by a php close tag.
52     $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source));
53
54     // replace textarea blocks
55     smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:TEXTAREA@@@",$_textarea_blocks, $source);
56
57     // replace pre blocks
58     smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:PRE@@@",$_pre_blocks, $source);
59
60     // replace script blocks
61     smarty_outputfilter_trimwhitespace_replace("@@@SMARTY:TRIM:SCRIPT@@@",$_script_blocks, $source);
62
63     return $source;
64 }
65
66 function smarty_outputfilter_trimwhitespace_replace($search_str, $replace, &$subject) {
67     $_len = strlen($search_str);
68     $_pos = 0;
69     for ($_i=0, $_count=count($replace); $_i<$_count; $_i++)
70         if (($_pos=strpos($subject, $search_str, $_pos))!==false)
71             $subject = substr_replace($subject, $replace[$_i], $_pos, $_len);
72         else
73             break;
74
75 }
76
77 ?>