|
| 1 | + |
| 2 | +/* This work is licensed under Creative Commons GNU LGPL License. |
| 3 | +
|
| 4 | + License: http://creativecommons.org/licenses/LGPL/2.1/ |
| 5 | + Version: 0.9 |
| 6 | + Author: Stefan Goessner/2006 |
| 7 | + Web: http://goessner.net/ |
| 8 | +*/ |
| 9 | + |
| 10 | +function jsonT(self, rules) { |
| 11 | + var T = { |
| 12 | + output: false, |
| 13 | + init: function() { |
| 14 | + for (var rule in rules) |
| 15 | + if (rule.substr(0,4) != "self") |
| 16 | + rules["self."+rule] = rules[rule]; |
| 17 | + return this; |
| 18 | + }, |
| 19 | + apply: function(expr) { |
| 20 | + var trf = function(s){ return s.replace(/{([A-Za-z0-9_\$\.\[\]\'@\(\)]+)}/g, |
| 21 | + function($0,$1){return T.processArg($1, expr);})}, |
| 22 | + x = expr.replace(/\[[0-9]+\]/g, "[*]"), res; |
| 23 | + if (x in rules) { |
| 24 | + if (typeof(rules[x]) == "string") |
| 25 | + res = trf(rules[x]); |
| 26 | + else if (typeof(rules[x]) == "function") |
| 27 | + res = trf(rules[x](eval(expr)).toString()); |
| 28 | + } |
| 29 | + else |
| 30 | + res = T.eval(expr); |
| 31 | + return res; |
| 32 | + }, |
| 33 | + processArg: function(arg, parentExpr) { |
| 34 | + var expand = function(a,e){return (e=a.replace(/^\$/,e)).substr(0,4)!="self" ? ("self."+e) : e; }, |
| 35 | + res = ""; |
| 36 | + T.output = true; |
| 37 | + if (arg.charAt(0) == "@") |
| 38 | + res = eval(arg.replace(/@([A-za-z0-9_]+)\(([A-Za-z0-9_\$\.\[\]\']+)\)/, |
| 39 | + function($0,$1,$2){return "rules['self."+$1+"']("+expand($2,parentExpr)+")";})); |
| 40 | + else if (arg != "$") |
| 41 | + res = T.apply(expand(arg, parentExpr)); |
| 42 | + else |
| 43 | + res = T.eval(parentExpr); |
| 44 | + T.output = false; |
| 45 | + return res; |
| 46 | + }, |
| 47 | + eval: function(expr) { |
| 48 | + var v = eval(expr), res = ""; |
| 49 | + if (typeof(v) != "undefined") { |
| 50 | + if (v instanceof Array) { |
| 51 | + for (var i=0; i<v.length; i++) |
| 52 | + if (typeof(v[i]) != "undefined") |
| 53 | + res += T.apply(expr+"["+i+"]"); |
| 54 | + } |
| 55 | + else if (typeof(v) == "object") { |
| 56 | + for (var m in v) |
| 57 | + if (typeof(v[m]) != "undefined") |
| 58 | + res += T.apply(expr+"."+m); |
| 59 | + } |
| 60 | + else if (T.output) |
| 61 | + res += v; |
| 62 | + } |
| 63 | + return res; |
| 64 | + } |
| 65 | + }; |
| 66 | + return T.init().apply("self"); |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +/** |
| 71 | + * Command line command to transform JSON files. |
| 72 | + */ |
| 73 | + |
| 74 | +var path_input = process.argv[2] |
| 75 | +var path_template = process.argv[3] |
| 76 | +var path_output = process.argv[4] |
| 77 | + |
| 78 | +var fs = require('fs') |
| 79 | + |
| 80 | +fs.readFile(path_input, function(err, input_data) { |
| 81 | + input_data = JSON.parse(input_data) |
| 82 | + console.log('Loaded input file ...') |
| 83 | + fs.readFile(path_template, function(err, template) { |
| 84 | + template = eval('template = ') |
| 85 | + console.log('Instantiate template ...') |
| 86 | + out = jsonT(input_data, template) |
| 87 | + fs.writeFile(path_output, out.toString(), function(err) { |
| 88 | + if (err) { |
| 89 | + console.log(err) |
| 90 | + return |
| 91 | + } |
| 92 | + console.log('Success!') |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
0 commit comments