HTML_Compressor - Version 0.1.0

Version Notes

Initial Relase

Download this release

Release Info

Developer Razorphyn
Extension HTML_Compressor
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/local/Razorphyn/HTMLCompressor/Model/Observer.php ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Razorphyn_HTMLCompressor_Model_Observer
3
+ {
4
+ public function alterOutput($observer)
5
+ {
6
+ $lib_path = Mage::getBaseDir('lib').'/Razorphyn/html_compressor.php';
7
+
8
+ include_once($lib_path);
9
+
10
+ //Retrieve html body
11
+ $response = $observer->getResponse();
12
+ $html = $response->getBody();
13
+
14
+ //Compress HTML
15
+ $html=html_compress($html);
16
+
17
+ //Send Response
18
+ $response->setBody($html);
19
+ }
20
+ }
21
+ ?>
app/code/local/Razorphyn/HTMLCompressor/Model/extime.log ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ 2014-12-14T16:23:50+00:00 DEBUG (7): Execution time: 0.0063199996948242
2
+ 2014-12-14T16:25:32+00:00 DEBUG (7): Execution time: 0.010541915893555
3
+ 2014-12-14T16:25:49+00:00 DEBUG (7): Execution time: 0.0093181133270264
app/code/local/Razorphyn/HTMLCompressor/etc/config.xml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <global>
4
+ <models>
5
+ <HTMLCompressor>
6
+ <class>Razorphyn_HTMLCompressor_Model</class>
7
+ </HTMLCompressor>
8
+ </models>
9
+ <events>
10
+ <http_response_send_before>
11
+ <observers>
12
+ <Razorphyn_HTMLCompressor_Observer_Unique>
13
+ <type>singleton</type>
14
+ <class>HTMLCompressor/observer</class>
15
+ <method>alterOutput</method>
16
+ </Razorphyn_HTMLCompressor_Observer_Unique>
17
+ </observers>
18
+ </http_response_send_before>
19
+ </events>
20
+ </global>
21
+ </config>
app/etc/modules/Razorphyn_HTMLCompressor.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Razorphyn_HTMLCompressor>
5
+ <active>true</active>
6
+ <codePool>local</codePool>
7
+ </Razorphyn_HTMLCompressor>
8
+ </modules>
9
+ </config>
lib/Razorphyn/html_compressor.php ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * HTML Compressor
4
+ * Author: Razorphyn
5
+ * Suport Email: est.garndi@gmail.com
6
+ * Copyright: WTFPL(http://www.wtfpl.net/)
7
+ **/
8
+ ?>
9
+ <?php
10
+
11
+ function html_compress($string){
12
+
13
+ //Substitute style, script, pre and cdata tag with unique id
14
+ global $idarray;
15
+ $idarray=array();
16
+
17
+ $search=array(
18
+ '@<(\s)*pre[^>]*>(?:[^<]+|)</pre>@', //Find PRE Tag
19
+ '@<(\s)*textarea(\b[^>]*?>[\\s\\S]*?</textarea>)\s*@' //Find TEXTAREA
20
+ );
21
+ $string=preg_replace_callback($search,
22
+ function($m){
23
+ $id='<!['.uniqid().']!>';
24
+ global $idarray;
25
+ $idarray[]=array($id,$m[0]);
26
+ return $id;
27
+ },
28
+ $string
29
+ );
30
+
31
+
32
+ $search = array(
33
+ '@( |\t|\f)+@', // Shorten multiple whitespace sequences
34
+ '@^\s+|\s+$@', // Trim lines
35
+ "/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/" //Remove empty lines
36
+ );
37
+ $replace = array(' ','',"\n");
38
+
39
+ $string = preg_replace($search, $replace, $string);
40
+
41
+ $search=array(
42
+ '@<!--\[if\s(?:[^<]+|<(?!!\[endif\]-->))*<!\[endif\]-->@', //Find IE Comments
43
+ '@<(\s*?)script(\b[^>]*?)>([\s\S]*?)</script>(\s*)@', //Find SCRIPT Tag
44
+ '@//<!\[CDATA\[(?:[^<]+|)//]]>@', //Find CDATA
45
+ '@<(\s)*style(\b[^>]*>)([\s\S]*?)</style>\s*@' //Find STYLE Tag
46
+ );
47
+ $string=preg_replace_callback($search,
48
+ function($m){
49
+ $id='<!['.uniqid().']!>';
50
+ global $idarray;
51
+ $idarray[]=array($id,$m[0]);
52
+ return $id;
53
+ },
54
+ $string
55
+ );
56
+
57
+ $search = array(
58
+ '@(class|id|value|alt|href|src|style|title)=(\'\s*?\'|"\s*?")@', //Remove empty attribute
59
+ '@<!--([\s\S]*?)-->@', // Strip comments excluded IE
60
+ '@(\r\n|\n|\r)@', // Strip break line
61
+ '@( |\t|\f)+@', // Shorten multiple whitespace sequences
62
+ '@^\s+|\s+$@' // Trim lines
63
+ );
64
+ $replace = array(' ','',' ',' ','');
65
+ $string = preg_replace($search, $replace, $string);
66
+
67
+ //Remove blank lines
68
+ $string=preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/"," ", $string);
69
+
70
+
71
+ //Replace unique id with script, style, pre original tag
72
+ $c=count($idarray);
73
+ for($i=0;$i<$c;$i++){
74
+ $string = str_replace($idarray[$i][0], "\n".$idarray[$i][1]."\n", $string);
75
+ }
76
+
77
+ return $string;
78
+ }
79
+ ?>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>HTML_Compressor</name>
4
+ <version>0.1.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.wtfpl.net/">WTFPL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Simple html compressor based on regex</summary>
10
+ <description>Remove unnecessary space usage(white space, empty lines, etc) and leaves pre, textarea, script, style, cdata and IE comments untouched</description>
11
+ <notes>Initial Relase</notes>
12
+ <authors><author><name>Razorphyn</name><user>Razorphyn</user><email>est.grandi@gmail.com</email></author></authors>
13
+ <date>2014-12-17</date>
14
+ <time>16:32:15</time>
15
+ <contents><target name="magelib"><dir><dir name="Razorphyn"><file name="html_compressor.php" hash="d7e29983ce7520bc82c221f1848824d1"/></dir></dir></target><target name="magelocal"><dir><dir name="Razorphyn"><dir name="HTMLCompressor"><dir name="Model"><file name="Observer.php" hash="591d43cc8e4a344391cb1349828c6784"/><file name="extime.log" hash="c4fc0905d6fd6884da9f4dd6135adce5"/></dir><dir name="etc"><file name="config.xml" hash="aa5611a7c00cd19f3434c1fefcb63c47"/></dir></dir></dir></dir></target><target name="mageetc"><dir><dir name="modules"><file name="Razorphyn_HTMLCompressor.xml" hash="372046018f3123d810bfb2d4590388b0"/></dir></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.3.0</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>