looking for wrong seperator
[Pman.Core] / JsTemplate.php
1 <?php
2 /***
3  *
4  * usage:
5  *
6  * just need to add this to HTML
7  *
8  * <script src="{baseURL}/Core/JsTemplate.js">
9  *
10  * 
11  *
12  * 
13  *
14  * first part should return a list of files to include.
15  * $x = new Pman_Core_JsTemplate($cfg)
16  *
17  * $x->to
18  *
19  * second part should compile and deliver.
20  *
21  * 
22  *
23  * // should return {baseurl}/Pman/JsTemplate/mod/file
24  *
25  * 
26  *
27  *
28  */
29 require_once 'Pman.php';
30
31 class Pman_Core_JsTemplate extends Pman {
32     
33     
34     var $modDir = false;
35     
36     function getAuth()
37     {
38         parent::getAuth();
39         return true;
40          
41     }
42      
43     function get($v, $opts=array())
44     {
45         
46         $this->sessionState(0);
47         // get the modules.
48         header('Content-type: text/javascript');
49         
50         $ff = HTML_FlexyFramework::get();
51         
52         $pr = $ff->project;
53         
54         $mods = $this->modulesList();
55         //print_r($mods);
56         
57         //$ar = explode(PATH_SEPARATOR, $ff->HTML_Template_Flexy['templateDir']);
58         array_push($mods, $pr);
59         
60         foreach($mods as $mod )
61         {
62             $prefix = $mod == $pr  ?  "" : "{$pr}.";
63             //var_dump($prefix);
64             $pdir = $mod == $pr  ? '' : ($pr .'/') ;
65          
66             $dir =  $this->rootDir .'/'.$pdir .  $mod . '/jtemplates';
67             if (!file_exists($dir)) {
68                 echo '// missing directory '. htmlspecialchars($dir) ."\n";
69                 continue;
70             }
71             // got a directory..
72              
73             $ar = glob("$dir/*.html") ;
74             if (empty($ar)) {
75                 echo '// no template is directory '. htmlspecialchars($dir) ."\n";
76                 continue;
77             }
78             
79             echo "{$prefix}{$mod} = {$prefix}{$mod} || {};\n";
80             echo "{$prefix}{$mod}.template = {$prefix}{$mod}.template   || {};\n\n";
81             
82             foreach(glob("$dir/*.html") as $fn) {
83                 $name = "{$prefix}{$mod}.template." . preg_replace('/\.html$/i', '', basename($fn));
84                 echo $this->compile($fn, $name) . "\n";
85                 
86
87             }
88   //              testing..
89 //new HTML_FlexyFramework_JsTemplate('/home/alan/gitlive/web.mtrack/MTrackWeb/jtemplates/TimelineTicket.html', 'Pman.template.TimelineTicket');
90             
91             
92             
93         }
94         exit;
95         
96         
97         
98     }
99     
100     
101     function compile($fn, $name)
102     {
103         // cached? - check file see if we have cached contents.
104         
105         
106         $contents = file_get_contents($fn);
107         $ar = preg_split('/(\{[^\\n}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
108         
109         
110         
111         
112         //echo '<PRE>' . htmlspecialchars(print_r($ar,true));
113         
114         $out= array();
115         
116         $head = "$name = function(t)\n{\n    var ret = '';\n\n";
117         
118         $funcs = array();
119         // do not allow nested functions..?
120         $fstart = -1;
121         $indent = 2;
122         $inscript = false;
123         $ret = &$out;
124         foreach($ar as $item) {
125             $in = str_repeat("    ", $indent);
126             $indent  = max($indent , 1);
127             //var_Dump(substr($item,-3,2));
128             switch(true) {
129                 case (!strlen($item)):
130                     continue 2;
131                 
132                 case ($inscript && ($item != '{end:}')):
133                     $ret[count($ret)-1] .= $item;
134                     continue 2;
135                 
136                 case ($inscript && ($item == '{end:}')):
137                     $inscript = false;
138                     continue 2;
139                  
140              
141                 case ($item[0] != '{'):
142                     if (!strlen(trim($item))) {
143                         continue 2;
144                     }
145                     $ret[] = $in . "ret += ". json_encode($item) . ";";
146                     continue 2;
147                 
148                 
149                 case ($item == '{script:}'): 
150                     $inscript = true;
151                      $ret[] = '';
152                     continue 2;
153                 
154                 case ($item[1] == '!'):
155                     $ret[] = $in . substr($item,2,-1) .';';
156                     continue 2;
157                 
158                 
159                 case (substr($item,1,3) == 'if('):
160                     $ret[] = $in . substr($item,1,-1) . ' {';
161                     $indent++;
162                     continue 2;
163                 
164                 case (substr($item,1,5) == 'else:'):
165                     $indent--;
166                     $in = str_repeat("    ", $indent);
167                     $ret[] = $in . "} else { ";
168                     $indent++;
169                     continue 2;
170                  
171                 case (substr($item,1,4) == 'end:'):
172                     $indent--;
173                     $in = str_repeat("    ", $indent);
174                     $ret[] = $in . "}";
175                     if ($fstart == $indent) {
176                         $fstart = -1;
177                         $ret = &$out;
178                     }
179                     continue 2;
180                 
181                 case (substr($item,1,7) == 'return:'):
182                     $ret[] = $in . "return;";
183                     continue 2;
184                 
185                 case (substr($item,1,9) == 'function:'):
186                     $fstart = $indent;
187                     $indent++;
188                     $ret = &$funcs;
189                     $def  = substr($item,10,-1) ;
190                     list($name,$body) = explode('(', $def, 2);
191                     
192                     
193                     $ret[] = $in . "var $name = function (" .  $body  . '{';
194                     continue 2;
195                 
196                 default:
197                     if (substr($item,-3,2) == ':h') {
198                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
199                         continue 2;
200                     }
201                     if (substr($item,-3,2) == ':b') {
202                         $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-3).').split("\n").join("<br/>\n");';
203                         continue 2;
204                     }
205                     $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-1).');';
206                     continue 2;
207                 
208             }
209             
210             
211         }
212         $in = str_repeat("    ", $indent);
213         $ret[] = $in .  "return ret;\n}\n";
214         return $head . implode("\n",$funcs) . "\n\n" .implode("\n",$out) ;
215         //echo '<PRE>' . htmlspecialchars(implode("\n",$ret));
216         
217         
218         
219     }
220     
221     
222     
223 }
224
225 // 
226