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