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