/** * pretty-data - nodejs plugin to pretty-print or minify data in XML, JSON and CSS formats. * * Version - 0.40.0 * Copyright (c) 2012 Vadim Kiryukhin * vkiryukhin @ gmail.com * http://www.eslinstructor.net/pretty-data/ * * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * pd.xml(data ) - pretty print XML; * pd.json(data) - pretty print JSON; * pd.css(data ) - pretty print CSS; * pd.sql(data) - pretty print SQL; * * pd.xmlmin(data [, preserveComments] ) - minify XML; * pd.jsonmin(data) - minify JSON; * pd.cssmin(data [, preserveComments] ) - minify CSS; * pd.sqlmin(data) - minify SQL; * * PARAMETERS: * * @data - String; XML, JSON, CSS or SQL text to beautify; * @preserveComments - Bool (optional, used in minxml and mincss only); * Set this flag to true to prevent removing comments from @text; * @Return - String; * * USAGE: * * var pd = require('pretty-data').pd; * * var xml_pp = pd.xml(xml_text); * var xml_min = pd.xmlmin(xml_text [,true]); * var json_pp = pd.json(json_text); * var json_min = pd.jsonmin(json_text); * var css_pp = pd.css(css_text); * var css_min = pd.cssmin(css_text [, true]); * var sql_pp = pd.sql(sql_text); * var sql_min = pd.sqlmin(sql_text); * * TEST: * comp-name:pretty-data$ node ./test/test_xml * comp-name:pretty-data$ node ./test/test_json * comp-name:pretty-data$ node ./test/test_css * comp-name:pretty-data$ node ./test/test_sql */ function pp() { this.shift = ['\n']; // array of shifts this.step = ' ', // 2 spaces maxdeep = 100, // nesting level ix = 0; // initialize array with shifts // for(ix=0;ix\s{0,}<") .replace(/ or -1) { str += this.shift[deep]+ar[ix]; inComment = true; // end comment or // if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { inComment = false; } } else // end comment or // if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { str += ar[ix]; inComment = false; } else // // if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) && /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { str += ar[ix]; if(!inComment) deep--; } else // // if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) { str = !inComment ? str += this.shift[deep++]+ar[ix] : str += ar[ix]; } else // ... // if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; } else // // if(ar[ix].search(/<\//) > -1) { str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix]; } else // // if(ar[ix].search(/\/>/) > -1 ) { str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; } else // // if(ar[ix].search(/<\?/) > -1) { str += this.shift[deep]+ar[ix]; } else // xmlns // if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { str += this.shift[deep]+ar[ix]; } else { str += ar[ix]; } } return (str[0] == '\n') ? str.slice(1) : str; } // ----------------------- JSON section ---------------------------------------------------- pp.prototype.json = function(text) { if ( typeof text === "string" ) { return JSON.stringify(JSON.parse(text), null, this.step); } if ( typeof text === "object" ) { return JSON.stringify(text, null, this.step); } return null; } // ----------------------- CSS section ---------------------------------------------------- pp.prototype.css = function(text) { var ar = text.replace(/\s{1,}/g,' ') .replace(/\{/g,"{~::~") .replace(/\}/g,"~::~}~::~") .replace(/\;/g,";~::~") .replace(/\/\*/g,"~::~/*") .replace(/\*\//g,"*/~::~") .replace(/~::~\s{0,}~::~/g,"~::~") .split('~::~'), len = ar.length, deep = 0, str = '', ix = 0; for(ix=0;ix/g,""); return str.replace(/>\s{0,}<"); } pp.prototype.jsonmin = function(text) { return text.replace(/\s{0,}\{\s{0,}/g,"{") .replace(/\s{0,}\[$/g,"[") .replace(/\[\s{0,}/g,"[") .replace(/:\s{0,}\[/g,':[') .replace(/\s{0,}\}\s{0,}/g,"}") .replace(/\s{0,}\]\s{0,}/g,"]") .replace(/\"\s{0,}\,/g,'",') .replace(/\,\s{0,}\"/g,',"') .replace(/\"\s{0,}:/g,'":') .replace(/:\s{0,}\"/g,':"') .replace(/:\s{0,}\[/g,':[') .replace(/\,\s{0,}\[/g,',[') .replace(/\,\s{2,}/g,', ') .replace(/\]\s{0,},\s{0,}\[/g,'],['); } pp.prototype.cssmin = function(text, preserveComments) { var str = preserveComments ? text : text.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"") ; return str.replace(/\s{1,}/g,' ') .replace(/\{\s{1,}/g,"{") .replace(/\}\s{1,}/g,"}") .replace(/\;\s{1,}/g,";") .replace(/\/\*\s{1,}/g,"/*") .replace(/\*\/\s{1,}/g,"*/"); } pp.prototype.sqlmin = function(text) { return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")"); } // -------------------------------------------------------------------------------------------- exports.pd= new pp;