File = { open: function(path){ var fso = new ActiveXObject("Scripting.FileSystemObject"); var ts = fso.OpenTextFile(path,1); var str = ts.ReadAll(); ts.Close(); return str; }, save: function(path,str,appendFlag){ var fso = new ActiveXObject("Scripting.FileSystemObject"); var ts = fso.OpenTextFile(path,(appendFlag? 8:2),1); ts.Write(str); ts.Close(); } }; main = function(stream){ // bottom to top (return order) stream = stream.replace(/[\t ]+/gm,' ' ).replace(/\{/g,"\n{"+"\n" ).replace(/\}/g,"\n}"+"\n" ).replace(/;/g,";"+"\n" ).replace(/\/\*/g,"\n/*"+"\n" ).replace(/\*\//g,"\n*/"+"\n" ).replace(/\s*[\n\r\f]+\s*/gm,"\n"); /* All of our indentation is now removed. {}'s all start at the first character on the line. Every line is <= 1 statement; they can still be multi-lined. We also removed extra newline characters. The next step is to perform indentation based on the {} depth. */ var tabs = ""; for(i = 0; i < stream.length-1; i++){ // the -1 means we don't have to check twice, and is an impossible indentation condition. if(stream.charAt(i) == '{'){ tabs += "\t"; continue; } if(stream.charAt(i) == '}'){ if(tabs.length == 0){ WScript.Echo('You have more } characters than { characters at some point,'+ 'so I couldn\'t indent properly. The program probably won\'t compile.'); return stream; // stop indenting, there's a syntax error. } tabs = tabs.substr(1); stream = stream.substr(0,i-1)+"}"+"\n"+stream.substring(i+1); // how the heck do I get rid of the newline after the endbracket reliably? continue; } if(stream.charAt(i) == "\n"){ stream = stream.substr(0,i+1)+tabs+stream.substring(i+1); } } stream = stream.replace(/\s*[\n\r\f]+/gm,"\n" ).replace(/\}[\s\n]*,/gm,'},'); return stream; } File.save(WScript.Arguments(0),main(File.open(WScript.Arguments(0))));