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