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