trim spaces and void blanks
authorPierre Habouzit (MadCoder <pierre.habouzit@m4x.org>
Sat, 8 Jan 2005 15:12:16 +0000 (15:12 +0000)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Thu, 26 Jun 2008 21:27:18 +0000 (23:27 +0200)
we now trim redoundant blanks from templates if they come (out of <pre>, <script>, <textarea> tags).

it's not 100% of the unnecessray blanks.  but it still trims a lot.
I assume we gain at least 10% on the size of the file.

moreover, with today's commits, we gain about 0.02s per page creation (according to the times I read on the top),
and a simple "static" page takes ~0.15s to load.  so it's a 5% speedup, with commits that _simplify_ the code...
what do you people want more ?

git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-249

ChangeLog
include/xorg/page.inc.php
include/xorg/smarty.plugins.inc.php

index 0b94c15..32c83b5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -14,6 +14,7 @@ New :
                - Enhancements wrt COOKIE.                                                                                      -MC
                - More homogenous date formating (use %x / %X everywhere).                      -MC
                - No more .head.tpl. (thanks to JM for the tip).                                        -MC
+               - Strip unnecessary spaces from templates at compile time.                      -MC
 
        * Contacts :
                - Brand new PDF of the contact list (using FPDF).                                       -MC
index 38927ff..f8dcd06 100644 (file)
@@ -72,6 +72,7 @@ class XorgPage extends DiogenesCorePage
 
         $this->DiogenesCorePage();
         $this->register_prefilter('at_to_globals');
+        $this->register_prefilter('trimwhitespace');
         $this->addJsLink('javascript/xorg.js');
 
         $this->doAuth();
index 43f7edb..b5b7d10 100644 (file)
@@ -66,5 +66,45 @@ function at_to_globals($tpl_source, &$smarty)
 }
 
 // }}}
+// {{{  function trimwhitespace
+
+function trimwhitespace($source, &$smarty)
+{
+    // Pull out the script blocks
+    preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match);
+    $_script_blocks = $match[0];
+    $source = preg_replace("!<script[^>]+>.*?</script>!is", '@@@SMARTY:TRIM:SCRIPT@@@', $source);
+
+    // Pull out the pre blocks
+    preg_match_all("!<pre>.*?</pre>!is", $source, $match);
+    $_pre_blocks = $match[0];
+    $source = preg_replace("!<pre>.*?</pre>!is", '@@@SMARTY:TRIM:PRE@@@', $source);
+
+    // Pull out the textarea blocks
+    preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match);
+    $_textarea_blocks = $match[0];
+    $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is", '@@@SMARTY:TRIM:TEXTAREA@@@', $source);
+
+    // remove all leading spaces, tabs and carriage returns NOT
+    // preceeded by a php close tag.
+    $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
+
+    // replace script blocks
+    foreach($_script_blocks as $curr_block) {
+        $source = preg_replace("!@@@SMARTY:TRIM:SCRIPT@@@!", $curr_block, $source, 1);
+    }
+    // replace pre blocks
+    foreach($_pre_blocks as $curr_block) {
+        $source = preg_replace("!@@@SMARTY:TRIM:PRE@@@!",$curr_block,$source,1);
+    }
+    // replace textarea blocks
+    foreach($_textarea_blocks as $curr_block) {
+        $source = preg_replace("!@@@SMARTY:TRIM:TEXTAREA@@@!",$curr_block,$source,1);
+    }
+
+    return $source; 
+}
+
+// }}}
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
 ?>