JsRender/NodeToJs.vala
[app.Builder.js] / JsRender / NodeToJs.vala
1 /**
2  * 
3  * Code to convert node tree to Javascript...
4  * 
5  * usage : x = (new JsRender.NodeToJs(node)).munge();
6  * 
7 */
8
9
10
11
12 public class JsRender.NodeToJs : Object {
13
14          Node node;
15         Gee.ArrayList<string>  doubleStringProps;
16         string pad;
17         Gee.ArrayList<string> els;
18         //Gee.ArrayList<string> skip;
19         Gee.HashMap<string,string> ar_props;
20
21
22         
23         public NodeToJs( Node node, Gee.ArrayList<string> doubleStringProps, string pad) 
24         {
25                 this.node = node;
26                 this.doubleStringProps = doubleStringProps;
27                 this.pad = pad;
28                 this.els = new Gee.ArrayList<string>(); 
29                 this.skip = new Gee.ArrayList<string>();
30                 this.ar_props = new Gee.HashMap<string,string>();
31
32         }
33         
34         public string munge ( )
35         {
36                 //return this.mungeToString(this.node);
37
38                 this.checkChildren();
39                 this.readProps();
40                 this.readArrayProps();
41                 this.readListeners();
42                 this.iterChildren();
43                 
44                 if (this.els.size < 1) {
45                         return "";
46                 }
47                 // oprops...    
48                         
49                 var spad = pad.substring(0, this.pad.length-4);
50                 var str_props = gLibStringListJoin(",\n" + this.pad , this.els) ;
51                 //print ("STR PROPS: " + str_props);
52                 
53                 return   "{\n" +
54                         pad  + str_props + 
55                         "\n" + spad +  "}";
56                      
57         } 
58
59         string gLibStringListJoin( string sep, Gee.ArrayList<string> ar) 
60         {
61                 var ret = "";
62                 for (var i = 0; i < ar.size; i++) {
63                         ret += i>0 ? sep : "";
64                         ret += ar.get(i);
65                 }
66                 return ret;
67
68         }
69         public string mungeChild(string pad ,  Node cnode)
70         {
71                 var x = new  NodeToJs(cnode, this.doubleStringProps, pad);
72                 return x.munge();
73         }
74         
75         
76
77         
78         public void checkChildren () 
79         {
80                 
81                  
82                 // look throught he chilren == looking for * prop.. -- fixme might not work..
83                 
84                 
85                 if (!this.node.hasChildren()) {
86                         return;
87                 }
88                 // look for '*props'
89            
90                 for (var ii =0; ii< this.node.items.size; ii++) {
91                         var pl = this.node.items.get(ii);
92                         if (!pl.props.has_key("* prop")) {
93                                 //newitems.add(pl);
94                                 continue;
95                         }
96                         
97                         //print(JSON.stringify(pl,null,4));
98                         // we have a prop...
99                         //var prop = pl['*prop'] + '';
100                         //delete pl['*prop'];
101                         var prop = pl.get("* prop");
102                         print("got prop "+ prop + "\n");
103                         
104                         // name ends in [];
105                         if (! Regex.match_simple("\\[\\]$", prop)) {
106                                 // it's a standard prop..
107                                 
108                                 // munge property..??
109                                 this.els.add( prop  + " : " + this.mungeChild (  this.pad + "   ",  pl));
110                                 
111                                 
112                                 //keys.push(prop);
113                                 continue;
114                         }
115
116
117
118                         
119                         var sprop  = prop.replace("[]", "");
120                         print("sprop is : " + sprop + "\n");
121                         
122                         // it's an array type..
123                         var old = "";
124                         if (!this.ar_props.has_key(sprop)) {
125                                 
126                                 this.ar_props.set(sprop, "");
127                                 
128                         } else {
129                                 old = this.ar_props.get(sprop);
130                         }
131                         var nstr  = old += old.length > 0 ? ",\n" : "";
132                         nstr += this.mungeChild( this.pad + "           ",   pl);
133                         
134                         this.ar_props.set(sprop, nstr);
135                          
136                         
137                 }
138                  
139         }
140         /*
141  * Standardize this crap...
142  * 
143  * standard properties (use to set)
144  *          If they are long values show the dialog..
145  * 
146  * bool is_xxx  :: can show a pulldown.. (true/false)
147  * string html  
148  * $ string html  = string with value interpolated eg. baseURL + ".." 
149  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
150  * $ untypedvalue = javascript untyped value...  
151  * _ string html ... = translatable..
152  * 
153  * object properties (not part of the GOjbect being wrapped?
154  * # Gee.ArrayList<Xcls_fileitem> fileitems
155  * 
156  * signals
157  * @ void open 
158  * 
159  * methods -- always text editor..
160  * | void clearFiles
161  * | someJSmethod
162  * 
163  * specials
164  * * prop -- string
165  * * args  -- string
166  * * ctor -- string
167  * * init -- big string?
168  * 
169  * event handlers (listeners)
170  *   just shown 
171  * 
172  * -----------------
173  * special ID values
174  *  +XXXX -- indicates it's a instance property / not glob...
175  *  *XXXX -- skip writing glob property (used as classes that can be created...)
176  * 
177  * 
178  */
179         public void readProps()
180         {
181                 string left;
182                 Regex func_regex ;
183                 try {
184                         func_regex = new Regex("^\\s+|\\s+$");
185                 } catch (Error e) {
186                         print("failed to build regex");
187                         return;
188                 }
189                 var piter = this.node.props.map_iterator();
190                 while (piter.next() ) {
191                         var kk = piter.get_key().split(" ");
192                         var v = piter.get_value();
193                         var k = kk[kk.length-1];
194
195                         
196                         //if (this.skip.contains(k) ) {
197                         //      continue;
198                         //}
199                         if (  Regex.match_simple("\\[\\]$", k)) {
200                                 
201                                 
202
203                         }
204                         
205                         string leftv = k;
206                         // skip builder stuff. prefixed with  '.' .. just like unix fs..
207                         if (kk[0][0] == '.') { // |. or . -- do not output..
208                                 continue;
209                         }
210                          if (kk[0][0] == '*') {
211                                 // ignore '*prop';
212                                 continue;
213                          }
214                                 
215                         
216                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
217                                 left = "'" + leftv + "'";
218                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
219                                 var val = this.node.quoteString(leftv);
220                                 
221                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
222                         } else {
223                                 left = leftv;
224                         }
225                         left += " : ";
226                         
227                         
228                          
229                         // next.. is it a function.. or a raw string..
230                         if (
231                                 kk[0][0] == '|' 
232                                 || 
233                                 kk[0][0] == '$' 
234                                 || 
235                                 kk[0] == "function"
236                                
237                                 // ??? any others that are raw output..
238                                 ) {
239                                 // does not hapepnd with arrays.. 
240                                 if (v.length < 1) {  //if (typeof(el) == 'string' && !obj[i].length) { //skip empty.
241                                         continue;
242                                 }
243                                 /*
244                                 print(v);
245                                 string str = "";
246                                 try {
247                                         str = func_regex.replace(v,v.length, 0, "");
248                                 } catch(Error e) {
249                                         print("regex failed");
250                                         return "";
251                                 }
252                                 */
253                                 var str = v.strip();
254                                   
255                                 var lines = str.split("\n");
256                                 var nstr = "" + str;
257                                 if (lines.length > 0) {
258                                         nstr =  string.joinv("\n" + this.pad, lines);
259                                 }
260                                 //print("==> " +  str + "\n");
261                                 this.els.add(left + nstr);
262                                 continue;
263                         }
264                         // standard..
265                         
266                         
267                         if (
268                                 Lang.isNumber(v) 
269                                 || 
270                                 Lang.isBoolean(v)
271                                 ||
272                                 kk[0].down() == "boolean"
273                                 || 
274                                 kk[0].down() == "bool"
275                                 || 
276                                 kk[0].down() == "number"
277                                 || 
278                                 kk[0].down() == "int"
279                             ) { // boolean or number...?
280                                 this.els.add(left + v.down() );
281                                 continue;
282                         }
283                         
284                         // strings..
285                         if (this.doubleStringProps.size < 1) {
286                                 this.els.add(left + this.node.quoteString(v));
287                                 continue;
288                         }
289                    
290                         if (this.doubleStringProps.index_of(k) > -1) {
291                                 els.add(left + this.node.quoteString(v));
292                                 continue;
293                         }
294                         var vv = this.node.quoteString(v);
295                         // single quote.. v.substring(1, v.length-1).replace("'", "\\'") + "'";
296                         this.els.add(left + "'" + vv.substring(1, vv.length-2).replace("'", "\\'") + "'");
297                         
298
299                    
300                    
301                    
302                 }
303         }
304         public void readArrayProps()
305         {
306         
307                 // handle the childitems  that are arrays.. eg. button[] = {  }...
308                 
309                 string left;
310                 
311                 var iter = this.ar_props.map_iterator();
312                 while (iter.next()) {
313                         var k = iter.get_key();
314                         var right = iter.get_value();
315                         
316                         string leftv = k[0] == '|' ? k.substring(1) : k;
317                         if (Lang.isKeyword(leftv) || Lang.isBuiltin(leftv)) {
318                                 left = "'" + leftv + "'";
319                         } else if (Regex.match_simple("[^A-Za-z_]+",leftv)) { // not plain a-z... - quoted.
320                                 var val = this.node.quoteString(leftv);
321                                 
322                                 left = "'" + val.substring(1, val.length-2).replace("'", "\\'") + "'";
323                         } else {
324                                 left = leftv;
325                         }
326                         left += " : ";
327                         
328                          
329                         
330                         
331                         if (right.length > 0){
332                                 this.els.add(left + "[\n" +  this.pad + "        " +  right + "\n" + this.pad + "]");
333                         }
334                 
335                         
336                 }
337
338         }
339         public void readListeners()
340         {
341                 
342                 if (this.node.listeners.size < 1) {
343                         return;
344                 }
345                         // munge the listeners.
346                         //print("ADDING listeners?");
347                 
348                 var liter = this.node.listeners.map_iterator();
349         
350                 var itms = "listeners : {\n";
351                 var i =0;
352                 while (liter.next()) {
353                         
354                         itms += i >0 ? ",\n" : "";      
355                         // 
356                         var str = liter.get_value().strip();
357                         var lines = str.split("\n");
358                         if (lines.length > 0) {
359                                 str = string.joinv("\n" + this.pad + "     ", lines);
360                         }
361                         
362
363                         
364                         itms +=  this.pad + "   "  + liter.get_key().replace("|", "")  + " : " + str;
365
366                         i++;
367                 
368                         
369                 }
370                 itms += "\n" + this.pad + "}";
371                 //print ( "ADD " + itms); 
372                 this.els.add(itms);
373
374         }
375
376         public void iterChildren()
377         {
378                 
379                 
380                 // finally munge the children...
381                 if (this.node.items.size < 0) {
382                         return;
383                 }
384                 var itms = "items : [\n";
385                 var n = 0;
386                 for(var i = 0; i < this.node.items.size;i++) {
387                         var ele = this.node.items.get(i);
388                         if (ele.props.has_key("* prop")) {
389                                 continue;
390                         }
391                         if (n > 0) {
392                                  itms += ",\n";
393                         }
394                         n++;
395                         itms += this.pad + "    "  +
396                                 this.mungeChild( this.pad + "           ",  ele);
397                         
398                         
399                 }
400                 itms +=  "\n"+  this.pad + "]"  + "\n";
401                 this.els.add(itms);
402         }
403
404                 // finally output listeners...
405                 
406         
407
408 }
409         
410          
411         
412