Added all the new documentation files.
[jquery.git] / docs / jsont.js
1 function jsonT(self, rules) {
2    var T = {
3       output: false,
4       init: function() {
5          for (var rule in rules)
6             if (rule.substr(0,4) != "self")
7                rules["self."+rule] = rules[rule];
8          return this;
9       },
10       apply: function(expr) {
11          var trf = function(s){ return s.replace(/{([A-Za-z0-9_\$\.\[\]\'@\(\)]+)}/g, 
12                                   function($0,$1){return T.processArg($1, expr);})},
13              x = expr.replace(/\[[0-9]+\]/g, "[*]"), res;
14          if (x in rules) {
15             if (typeof(rules[x]) == "string")
16                res = trf(rules[x]);
17             else if (typeof(rules[x]) == "function")
18                res = trf(rules[x](eval(expr)).toString());
19          }
20          else 
21             res = T.eval(expr);
22          return res;
23       },
24       processArg: function(arg, parentExpr) {
25          var expand = function(a,e){return (e=a.replace(/^\$/,e)).substr(0,4)!="self" ? ("self."+e) : e; },
26              res = "";
27          T.output = true;
28          if (arg.charAt(0) == "@")
29             res = eval(arg.replace(/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/, 
30                                    function($0,$1,$2){return "rules['self."+$1+"']("+expand($2,parentExpr)+")";}));
31          else if (arg != "$")
32             res = T.apply(expand(arg, parentExpr));
33          else
34             res = T.eval(parentExpr);
35          T.output = false;
36          return res;
37       },
38       eval: function(expr) {
39          var v = eval(expr), res = "";
40          if (typeof(v) != "undefined") {
41             if (v instanceof Array) {
42                for (var i=0; i<v.length; i++)
43                   if (typeof(v[i]) != "undefined")
44                      res += T.apply(expr+"["+i+"]");
45             }
46             else if (typeof(v) == "object") {
47                for (var m in v)
48                   if (typeof(v[m]) != "undefined")
49                      res += T.apply(expr+"."+m);
50             }
51             else if (T.output)
52                res += v;
53          }
54          return res;
55       }
56    };
57    return T.init().apply("self");
58 }