init
[garradin.git] / include / libs / template_lite / plugins / prefilter.jstrip.php
1 <?php
2 /*
3 * Template Lite plugin converted from Smarty
4 * -------------------------------------------------------------
5 * File: prefilter.jstrip.php
6 * Type: prefilter
7 * Name: jstrip
8 * Version: 1.0
9 * Date: 01 Nov 2004
10 * Purpose: dummy compiler to compress javascript
11 * Install: Drop into the plugin directory,
12 * call load_filter('pre','jstrip');
13 * from your application.
14 * -------------------------------------------------------------
15 */
16
17 function template_prefilter_jstrip($tpl_source, &$template_object)
18 {
19 return preg_replace_callback("/\{jstrip\}(.*?)\{\/jstrip\}/s","template_prefilter_jstrip_cb", $tpl_source);
20 }
21
22 function template_prefilter_jstrip_one($code)
23 {
24 return template_prefilter_jstrip_cb(array("", $code), false);
25 }
26
27 function template_prefilter_jstrip_cb($m, $literal=true)
28 {
29 $c=$m[1];
30 $o=""; //stripped output
31 $comment=0; //comments
32 $string=""; //current string delimiter
33 $last=""; //last char in the output
34 for ($i=0;$i<strlen($c);$i++)
35 {
36 //if ($i%100==0) {
37 //print_v(array($i,$string,$comment));
38 //}
39 $s=true; //save the character ?
40 //if we're in a string or phpcode
41 if (!empty($string))
42 {
43 //end of the string
44 if ($c[$i]==$string OR substr($c,$i,2)==$string)
45 {
46 $string="";
47 }
48 //not in a string
49 }
50 else
51 {
52 //strip comments
53 if (substr($c,$i,2)=="//")
54 {
55 $comment=1;
56 }
57
58 if (substr($c,$i,2)=="/*")
59 {
60 $comment=2;
61 }
62
63 if ($comment==1 AND $c[$i]=="\n")
64 {
65 $comment=0;
66 }
67
68 if ($comment==2 AND substr($c,$i-1,2)=="*/")
69 {
70 $comment=0;
71 $s=false;
72 }
73
74 if ($comment==0)
75 {
76 //start a string
77 if ($c[$i]=="'" OR $c[$i]=='"')
78 {
79 $string=$c[$i];
80 }
81
82 //start phpcode
83 if (substr($c,$i,2)=="<"."?")
84 {
85 $string="?".">";
86 }
87
88 //line break
89 if ($c[$i]=="\n" OR $c[$i]=="\r")
90 {
91 //is the current line finished ?
92 // ")" and "}" is not OK ! (var x=function a() {}.......var )
93 $finishers=array(";","{","(",",","\n",":");
94 if (in_array($last,$finishers))
95 {
96 $s=false;
97 }
98 }
99
100 //a space ! can we cut it ?
101 if ($c[$i]==" " OR $c[$i]=="\t")
102 {
103 $cutme=array(" ","\t","}","{",")","(","[","]","<",">","=",";","+","-","/","*","\n",":","&");
104 if (in_array($c[$i-1],$cutme) OR in_array($c[$i+1],$cutme))
105 {
106 $s=false;
107 }
108 }
109 //todo : rename vars/functions !!
110 }
111 }
112 //save the character
113 if ($s AND $comment==0)
114 {
115 $o.=$c[$i];
116 $last=$c[$i];
117 }
118 }
119
120 if ($literal)
121 {
122 return "{literal}".$o."{/literal}";
123 }
124 else
125 {
126 return $o;
127 }
128 }
129
130 ?>?>