diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..b5bd367 --- /dev/null +++ b/.npmignore @@ -0,0 +1,10 @@ +html +php +test +node_modules +change.log +jquery*.js +main.js +*.html +*.css +*.iml \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8111245 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: node_js +node_js: + - 0.6 + - 0.8 \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..9a101eb --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require("./vkbeautify.js"); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3173917 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "vkbeautify", + "version": "0.99.0", + "author": "Vadim Kiryukhin ", + "description": "javascript plugin to pretty-print or minify text in XML, JSON, CSS and SQL formats", + "repository": { + "type": "git", + "url": "git://github.com/rappid/rAppid.js.git" + }, + "keywords": [ + "vkbeautify", "pretty", "print" + ], + "devDependencies": { + "mocha": "latest", + "chai": "latest" + }, + "license": "MIT", + "homepage": "http://www.eslinstructor.net/vkbeautify/" +} diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..41ed028 --- /dev/null +++ b/test/test.js @@ -0,0 +1,35 @@ +var vkbeautify = require(".."), + expect = require('chai').expect; + +describe('vkbeautify', function () { + + it("should export the library", function () { + expect(vkbeautify).to.exist; + expect(vkbeautify.VkBeautify).to.be.an.instanceof(Function); + expect(vkbeautify.vkbeautify).to.be.an.instanceof(vkbeautify.VkBeautify); + }); + + + describe('xml', function () { + + it("root tag", function () { + + var input = "", + output = vkbeautify.vkbeautify.xml(input); + + expect(output).to.eql(""); + + }); + + it("should indent xml", function () { + + var input = "", + output = vkbeautify.vkbeautify.xml(input); + + expect(output).to.eql("\n \n"); + + }); + + }); + +}); \ No newline at end of file diff --git a/vkbeautify.js b/vkbeautify.js index 13cee3a..4e9dd40 100644 --- a/vkbeautify.js +++ b/vkbeautify.js @@ -1,358 +1,379 @@ /** -* vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON, CSS and SQL formats. -* -* Version - 0.99.00.beta -* Copyright (c) 2012 Vadim Kiryukhin -* vkiryukhin @ gmail.com -* http://www.eslinstructor.net/vkbeautify/ -* -* Dual licensed under the MIT and GPL licenses: -* http://www.opensource.org/licenses/mit-license.php -* http://www.gnu.org/licenses/gpl.html -* -* Pretty print -* -* vkbeautify.xml(text [,indent_pattern]); -* vkbeautify.json(text [,indent_pattern]); -* vkbeautify.css(text [,indent_pattern]); -* vkbeautify.sql(text [,indent_pattern]); -* -* @text - String; text to beatufy; -* @indent_pattern - Integer | String; -* Integer: number of white spaces; -* String: character string to visualize indentation ( can also be a set of white spaces ) -* Minify -* -* vkbeautify.xmlmin(text [,preserve_comments]); -* vkbeautify.jsonmin(text); -* vkbeautify.cssmin(text [,preserve_comments]); -* vkbeautify.sqlmin(text); -* -* @text - String; text to minify; -* @preserve_comments - Bool; [optional]; -* Set this flag to true to prevent removing comments from @text ( minxml and mincss functions only. ) -* -* Examples: -* vkbeautify.xml(text); // pretty print XML -* vkbeautify.json(text, 4 ); // pretty print JSON -* vkbeautify.css(text, '. . . .'); // pretty print CSS -* vkbeautify.sql(text, '----'); // pretty print SQL -* -* vkbeautify.xmlmin(text, true);// minify XML, preserve comments -* vkbeautify.jsonmin(text);// minify JSON -* vkbeautify.cssmin(text);// minify CSS, remove comments ( default ) -* vkbeautify.sqlmin(text);// minify SQL -* -*/ - -(function() { - -function createShiftArr(step) { - - var space = ' '; - - if ( isNaN(parseInt(step)) ) { // argument is string - space = step; - } else { // argument is integer - switch(step) { - case 1: space = ' '; break; - case 2: space = ' '; break; - case 3: space = ' '; break; - case 4: space = ' '; break; - case 5: space = ' '; break; - case 6: space = ' '; break; - case 7: space = ' '; break; - case 8: space = ' '; break; - case 9: space = ' '; break; - case 10: space = ' '; break; - case 11: space = ' '; break; - case 12: space = ' '; break; - } - } - - var shift = ['\n']; // array of shifts - for(ix=0;ix<100;ix++){ - shift.push(shift[ix]+space); - } - return shift; -} - -function vkbeautify(){ - this.step = ' '; // 4 spaces - this.shift = createShiftArr(this.step); -}; - -vkbeautify.prototype.xml = function(text,step) { - - var ar = text.replace(/>\s{0,}<") - .replace(/ or -1) { - str += 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 += shift[deep++]+ar[ix] : str += ar[ix]; - } else - // ... // - if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { - str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix]; - } else - // // - if(ar[ix].search(/<\//) > -1) { - str = !inComment ? str += shift[--deep]+ar[ix] : str += ar[ix]; - } else - // // - if(ar[ix].search(/\/>/) > -1 ) { - str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix]; - } else - // // - if(ar[ix].search(/<\?/) > -1) { - str += shift[deep]+ar[ix]; - } else - // xmlns // - if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { - str += shift[deep]+ar[ix]; - } - - else { - str += ar[ix]; - } - } - - return (str[0] == '\n') ? str.slice(1) : str; -} - -vkbeautify.prototype.json = function(text,step) { - - var step = step ? step : this.step; - - if (typeof JSON === 'undefined' ) return text; - - if ( typeof text === "string" ) return JSON.stringify(JSON.parse(text), null, step); - if ( typeof text === "object" ) return JSON.stringify(text, null, step); - - return text; // text is not string nor object -} - -vkbeautify.prototype.css = function(text, step) { - - 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, - shift = step ? createShiftArr(step) : this.shift; - - for(ix=0;ix/g,"") - .replace(/[ \r\n\t]{1,}xmlns/g, ' xmlns'); - return str.replace(/>\s{0,}<"); -} - -vkbeautify.prototype.jsonmin = function(text) { - - if (typeof JSON === 'undefined' ) return text; - - return JSON.stringify(JSON.parse(text), null, 0); - -} - -vkbeautify.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,"*/"); -} - -vkbeautify.prototype.sqlmin = function(text) { - return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")"); -} - -window.vkbeautify = new vkbeautify(); - -})(); + * vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON, CSS and SQL formats. + * + * Version - 0.99.00.beta + * Copyright (c) 2012 Vadim Kiryukhin + * vkiryukhin @ gmail.com + * http://www.eslinstructor.net/vkbeautify/ + * + * Dual licensed under the MIT and GPL licenses: + * http://www.opensource.org/licenses/mit-license.php + * http://www.gnu.org/licenses/gpl.html + * + * Pretty print + * + * vkbeautify.xml(text [,indent_pattern]); + * vkbeautify.json(text [,indent_pattern]); + * vkbeautify.css(text [,indent_pattern]); + * vkbeautify.sql(text [,indent_pattern]); + * + * @text - String; text to beatufy; + * @indent_pattern - Integer | String; + * Integer: number of white spaces; + * String: character string to visualize indentation ( can also be a set of white spaces ) + * Minify + * + * vkbeautify.xmlmin(text [,preserve_comments]); + * vkbeautify.jsonmin(text); + * vkbeautify.cssmin(text [,preserve_comments]); + * vkbeautify.sqlmin(text); + * + * @text - String; text to minify; + * @preserve_comments - Bool; [optional]; + * Set this flag to true to prevent removing comments from @text ( minxml and mincss functions only. ) + * + * Examples: + * vkbeautify.xml(text); // pretty print XML + * vkbeautify.json(text, 4 ); // pretty print JSON + * vkbeautify.css(text, '. . . .'); // pretty print CSS + * vkbeautify.sql(text, '----'); // pretty print SQL + * + * vkbeautify.xmlmin(text, true);// minify XML, preserve comments + * vkbeautify.jsonmin(text);// minify JSON + * vkbeautify.cssmin(text);// minify CSS, remove comments ( default ) + * vkbeautify.sqlmin(text);// minify SQL + * + */ + +(function (exports) { + "use strict"; + + function createShiftArr(step) { + + var space = ' '; + + if (isNaN(parseInt(step))) { // argument is string + space = step; + } else { // argument is integer + switch (step) { + case 1: + space = ' '; + break; + case 2: + space = ' '; + break; + case 3: + space = ' '; + break; + case 4: + space = ' '; + break; + case 5: + space = ' '; + break; + case 6: + space = ' '; + break; + case 7: + space = ' '; + break; + case 8: + space = ' '; + break; + case 9: + space = ' '; + break; + case 10: + space = ' '; + break; + case 11: + space = ' '; + break; + case 12: + space = ' '; + break; + } + } + + var shift = ['\n']; // array of shifts + for (var ix = 0; ix < 100; ix++) { + shift.push(shift[ix] + space); + } + return shift; + } + + var VkBeautify = function() { + this.step = ' '; // 4 spaces + this.shift = createShiftArr(this.step); + }; + + VkBeautify.prototype.xml = function (text, step) { + + var ar = text.replace(/>\s{0,}<") + .replace(//g, "") + .replace(/[ \r\n\t]{1,}xmlns/g, ' xmlns'); + return str.replace(/>\s{0,}<"); + }; + + VkBeautify.prototype.jsonmin = function (text) { + + if (typeof JSON === 'undefined') { + return text; + } + + return JSON.stringify(JSON.parse(text), null, 0); + + }; + + VkBeautify.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, "*/"); + }; + + VkBeautify.prototype.sqlmin = function (text) { + return text.replace(/\s{1,}/g, " ").replace(/\s{1,}\(/, "(").replace(/\s{1,}\)/, ")"); + }; + + exports.VkBeautify = VkBeautify; + exports.vkbeautify = new VkBeautify(); + +})(typeof(exports) === "undefined" ? this : exports);