From 6f5e8ac590e7b6cced795100791dd78ea71a5204 Mon Sep 17 00:00:00 2001 From: Vadim Kiryukhin Date: Sun, 8 Apr 2012 23:57:28 -0700 Subject: [PATCH] indent size is provaded as a param to prototyped functions --- main.js | 10 ++++-- vkbeautify.js | 96 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 66 insertions(+), 40 deletions(-) diff --git a/main.js b/main.js index 5624cb7..32f4e30 100644 --- a/main.js +++ b/main.js @@ -8,15 +8,19 @@ $(document).ready(function() function beatify() { var ta = document.getElementById('ta'); if($('#mode').html() == 'XML') { - ta.value = vkbeautify.xml(ta.value); + //ta.value = vkbeautify.xml(ta.value); + ta.value = vkbeautify.xml(ta.value, 2); } else if($('#mode').html() == 'JSON') { - ta.value = vkbeautify.json(ta.value); + //ta.value = vkbeautify.json(ta.value); + ta.value = vkbeautify.json(ta.value, 4); } else if($('#mode').html() == 'CSS') { - ta.value = vkbeautify.css(ta.value); + ta.value = vkbeautify.css(ta.value, 8); + //ta.value = vkbeautify.css(ta.value); } if($('#mode').html() == 'SQL') { + //ta.value = vkbeautify.sql(ta.value); ta.value = vkbeautify.sql(ta.value); } countChars(); diff --git a/vkbeautify.js b/vkbeautify.js index f859e9b..63c71df 100644 --- a/vkbeautify.js +++ b/vkbeautify.js @@ -1,7 +1,7 @@ /** * vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON and CSS formats. * -* Version - 0.96.02.beta +* Version - 0.97.00.beta * Copyright (c) 2012 Vadim Kiryukhin * vkiryukhin @ gmail.com * http://www.eslinstructor.net/vkbeautify/ @@ -16,6 +16,7 @@ * PARAMETERS: * * @text - String; text to beautify; +* @step - Integer; indent size unit (default is 4 spaces) ; * @preserveComments - Bool (optional, used in minxml and mincss only); * Set this flag to true to prevent removing comments from @text; * @Return - String; @@ -23,9 +24,9 @@ * USAGE: * * var foo = vkbeautify.xml(text); -* var foo = vkbeautify.json(text); -* var foo = vkbeautify.css(text); -* var foo = vkbeautify.sql(text); +* var foo = vkbeautify.json(text [,4]); +* var foo = vkbeautify.css(text [,8]); +* var foo = vkbeautify.sql(text [,2]); * var foo = vkbeautify.xmlmin( text [, true]); * var foo = vkbeautify.jsonmin(text); * var foo = vkbeautify.cssmin( text [, true]); @@ -35,32 +36,50 @@ (function() { -function vkbeautify(){ - this.shift = ['\n']; // array of shifts - this.step = ' '; // 4 spaces - var maxdeep = 100, // nesting level - ix = 0; +function createShiftArr(step) { + + var space = ''; + 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; + default: space = ' '; break; + } - // initialize array with shifts // - for(ix=0;ix\s{0,}<").replace(/ or -1) { - str += this.shift[deep]+ar[ix]; + str += shift[deep]+ar[ix]; inComment = true; // end comment or // if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { @@ -80,23 +99,23 @@ vkbeautify.prototype.xml = function(text) { } 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]; + str = !inComment ? str += 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]; + str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix]; } else // // if(ar[ix].search(/<\//) > -1) { - str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix]; + str = !inComment ? str += shift[--deep]+ar[ix] : str += ar[ix]; } else // // if(ar[ix].search(/\/>/) > -1 ) { - str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; + str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix]; } else // // if(ar[ix].search(/<\?/) > -1) { - str += this.shift[deep]+ar[ix]; + str += shift[deep]+ar[ix]; } else { str += ar[ix]; } @@ -105,7 +124,7 @@ vkbeautify.prototype.xml = function(text) { return (str[0] == '\n') ? str.slice(1) : str; } -vkbeautify.prototype.json = function(text) { +vkbeautify.prototype.json = function(text,step) { var ar = this.jsonmin(text).replace(/\{/g,"~::~{~::~") .replace(/\[/g,"[~::~") @@ -120,28 +139,29 @@ vkbeautify.prototype.json = function(text) { len = ar.length, deep = 0, str = '', - ix = 0; + ix = 0, + shift = step ? createShiftArr(step) : this.shift;; for(ix=0;ix