Mercurial > eldonilo > lightstring
comparison lib/vkbeautify.0.97.00.beta.js @ 106:c06ec02217ee
many changes
author | Sonny Piers <sonny@fastmail.net> |
---|---|
date | Tue, 26 Jun 2012 12:02:14 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
105:fb50311997b5 | 106:c06ec02217ee |
---|---|
1 /** | |
2 * vkBeautify - javascript plugin to pretty-print or minify text in XML, JSON and CSS formats. | |
3 * | |
4 * Version - 0.97.00.beta | |
5 * Copyright (c) 2012 Vadim Kiryukhin | |
6 * vkiryukhin @ gmail.com | |
7 * http://www.eslinstructor.net/vkbeautify/ | |
8 * | |
9 * Dual licensed under the MIT and GPL licenses: | |
10 * http://www.opensource.org/licenses/mit-license.php | |
11 * http://www.gnu.org/licenses/gpl.html | |
12 * | |
13 * Pretty print | |
14 * | |
15 * vkbeautify.xml(text [,indent_pattern]); | |
16 * vkbeautify.json(text [,indent_pattern]); | |
17 * vkbeautify.css(text [,indent_pattern]); | |
18 * vkbeautify.sql(text [,indent_pattern]); | |
19 * | |
20 * @text - String; text to beatufy; | |
21 * @indent_pattern - Integer | String; | |
22 * Integer: number of white spaces; | |
23 * String: character string to visualize indentation ( can also be a set of white spaces ) | |
24 * Minify | |
25 * | |
26 * vkbeautify.xmlmin(text [,preserve_comments]); | |
27 * vkbeautify.jsonmin(text); | |
28 * vkbeautify.cssmin(text [,preserve_comments]); | |
29 * vkbeautify.sqlmin(text); | |
30 * | |
31 * @text - String; text to minify; | |
32 * @preserve_comments - Bool; [optional]; | |
33 * Set this flag to true to prevent removing comments from @text ( minxml and mincss functions only. ) | |
34 * | |
35 * Examples: | |
36 * vkbeautify.xml(text); // pretty print XML | |
37 * vkbeautify.json(text, 4 ); // pretty print JSON | |
38 * vkbeautify.css(text, '. . . .'); // pretty print CSS | |
39 * vkbeautify.sql(text, '----'); // pretty print SQL | |
40 * | |
41 * vkbeautify.xmlmin(text, true);// minify XML, preserve comments | |
42 * vkbeautify.jsonmin(text);// minify JSON | |
43 * vkbeautify.cssmin(text);// minify CSS, remove comments ( default ) | |
44 * vkbeautify.sqlmin(text);// minify SQL | |
45 * | |
46 */ | |
47 | |
48 (function() { | |
49 | |
50 function createShiftArr(step) { | |
51 | |
52 var space = ' '; | |
53 | |
54 if ( isNaN(parseInt(step)) ) { // argument is string | |
55 space = step; | |
56 } else { // argument is integer | |
57 switch(step) { | |
58 case 1: space = ' '; break; | |
59 case 2: space = ' '; break; | |
60 case 3: space = ' '; break; | |
61 case 4: space = ' '; break; | |
62 case 5: space = ' '; break; | |
63 case 6: space = ' '; break; | |
64 case 7: space = ' '; break; | |
65 case 8: space = ' '; break; | |
66 case 9: space = ' '; break; | |
67 case 10: space = ' '; break; | |
68 case 11: space = ' '; break; | |
69 case 12: space = ' '; break; | |
70 } | |
71 } | |
72 | |
73 var shift = ['\n']; // array of shifts | |
74 for(ix=0;ix<100;ix++){ | |
75 shift.push(shift[ix]+space); | |
76 } | |
77 return shift; | |
78 } | |
79 | |
80 function vkbeautify(){ | |
81 this.step = ' '; // 4 spaces | |
82 this.shift = createShiftArr(this.step); | |
83 }; | |
84 | |
85 vkbeautify.prototype.xml = function(text,step) { | |
86 | |
87 var ar = text.replace(/>\s{0,}</g,"><").replace(/</g,"~::~<").split('~::~'), | |
88 len = ar.length, | |
89 inComment = false, | |
90 deep = 0, | |
91 str = '', | |
92 ix = 0, | |
93 shift = step ? createShiftArr(step) : this.shift; | |
94 | |
95 for(ix=0;ix<len;ix++) { | |
96 // start comment or <![CDATA[...]]> or <!DOCTYPE // | |
97 if(ar[ix].search(/<!/) > -1) { | |
98 str += shift[deep]+ar[ix]; | |
99 inComment = true; | |
100 // end comment or <![CDATA[...]]> // | |
101 if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { | |
102 inComment = false; | |
103 } | |
104 } else | |
105 // end comment or <![CDATA[...]]> // | |
106 if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { | |
107 str += ar[ix]; | |
108 inComment = false; | |
109 } else | |
110 // <elm></elm> // | |
111 if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) && | |
112 /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { | |
113 str += ar[ix]; | |
114 if(!inComment) deep--; | |
115 } else | |
116 // <elm> // | |
117 if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) { | |
118 str = !inComment ? str += shift[deep++]+ar[ix] : str += ar[ix]; | |
119 } else | |
120 // <elm>...</elm> // | |
121 if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { | |
122 str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix]; | |
123 } else | |
124 // </elm> // | |
125 if(ar[ix].search(/<\//) > -1) { | |
126 str = !inComment ? str += shift[--deep]+ar[ix] : str += ar[ix]; | |
127 } else | |
128 // <elm/> // | |
129 if(ar[ix].search(/\/>/) > -1 ) { | |
130 str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix]; | |
131 } else | |
132 // <? xml ... ?> // | |
133 if(ar[ix].search(/<\?/) > -1) { | |
134 str += shift[deep]+ar[ix]; | |
135 } else { | |
136 str += ar[ix]; | |
137 } | |
138 } | |
139 | |
140 return (str[0] == '\n') ? str.slice(1) : str; | |
141 } | |
142 | |
143 vkbeautify.prototype.json = function(text,step) { | |
144 | |
145 var ar = this.jsonmin(text).replace(/\{/g,"~::~{~::~") | |
146 .replace(/\[/g,"[~::~") | |
147 .replace(/\}/g,"~::~}") | |
148 .replace(/\]/g,"~::~]") | |
149 .replace(/\"\,/g,'",~::~') | |
150 .replace(/\,\"/g,',~::~"') | |
151 .replace(/\]\,/g,'],~::~') | |
152 .replace(/~::~\s{0,}~::~/g,"~::~") | |
153 .split('~::~'), | |
154 | |
155 len = ar.length, | |
156 deep = 0, | |
157 str = '', | |
158 ix = 0, | |
159 shift = step ? createShiftArr(step) : this.shift;; | |
160 | |
161 for(ix=0;ix<len;ix++) { | |
162 if( /\{/.exec(ar[ix])) { | |
163 str += shift[deep++]+ar[ix]; | |
164 } else | |
165 if( /\[/.exec(ar[ix])) { | |
166 str += shift[deep++]+ar[ix]; | |
167 } else | |
168 if( /\]/.exec(ar[ix])) { | |
169 str += shift[--deep]+ar[ix]; | |
170 } else | |
171 if( /\}/.exec(ar[ix])) { | |
172 str += shift[--deep]+ar[ix]; | |
173 } else { | |
174 str += shift[deep]+ar[ix]; | |
175 } | |
176 } | |
177 return str.replace(/^\n{1,}/,''); | |
178 } | |
179 | |
180 vkbeautify.prototype.css = function(text, step) { | |
181 | |
182 var ar = text.replace(/\s{1,}/g,' ') | |
183 .replace(/\{/g,"{~::~") | |
184 .replace(/\}/g,"~::~}~::~") | |
185 .replace(/\;/g,";~::~") | |
186 .replace(/\/\*/g,"~::~/*") | |
187 .replace(/\*\//g,"*/~::~") | |
188 .replace(/~::~\s{0,}~::~/g,"~::~") | |
189 .split('~::~'), | |
190 len = ar.length, | |
191 deep = 0, | |
192 str = '', | |
193 ix = 0, | |
194 shift = step ? createShiftArr(step) : this.shift; | |
195 | |
196 for(ix=0;ix<len;ix++) { | |
197 | |
198 if( /\{/.exec(ar[ix])) { | |
199 str += shift[deep++]+ar[ix]; | |
200 } else | |
201 if( /\}/.exec(ar[ix])) { | |
202 str += shift[--deep]+ar[ix]; | |
203 } else | |
204 if( /\*\\/.exec(ar[ix])) { | |
205 str += shift[deep]+ar[ix]; | |
206 } | |
207 else { | |
208 str += shift[deep]+ar[ix]; | |
209 } | |
210 } | |
211 return str.replace(/^\n{1,}/,''); | |
212 } | |
213 | |
214 //---------------------------------------------------------------------------- | |
215 | |
216 function isSubquery(str, parenthesisLevel) { | |
217 return parenthesisLevel - (str.replace(/\(/g,'').length - str.replace(/\)/g,'').length ) | |
218 } | |
219 | |
220 function split_sql(str, tab) { | |
221 | |
222 return str.replace(/\s{1,}/g," ") | |
223 | |
224 .replace(/ AND /ig,"~::~"+tab+tab+"AND ") | |
225 .replace(/ BETWEEN /ig,"~::~"+tab+"BETWEEN ") | |
226 .replace(/ CASE /ig,"~::~"+tab+"CASE ") | |
227 .replace(/ ELSE /ig,"~::~"+tab+"ELSE ") | |
228 .replace(/ END /ig,"~::~"+tab+"END ") | |
229 .replace(/ FROM /ig,"~::~FROM ") | |
230 .replace(/ GROUP\s{1,}BY/ig,"~::~GROUP BY ") | |
231 .replace(/ HAVING /ig,"~::~HAVING ") | |
232 //.replace(/ IN /ig,"~::~"+tab+"IN ") | |
233 .replace(/ IN /ig," IN ") | |
234 | |
235 .replace(/ JOIN /ig,"~::~JOIN ") | |
236 .replace(/ CROSS~::~{1,}JOIN /ig,"~::~CROSS JOIN ") | |
237 .replace(/ INNER~::~{1,}JOIN /ig,"~::~INNER JOIN ") | |
238 .replace(/ LEFT~::~{1,}JOIN /ig,"~::~LEFT JOIN ") | |
239 .replace(/ RIGHT~::~{1,}JOIN /ig,"~::~RIGHT JOIN ") | |
240 | |
241 .replace(/ ON /ig,"~::~"+tab+"ON ") | |
242 .replace(/ OR /ig,"~::~"+tab+tab+"OR ") | |
243 .replace(/ ORDER\s{1,}BY/ig,"~::~ORDER BY ") | |
244 .replace(/ OVER /ig,"~::~"+tab+"OVER ") | |
245 | |
246 .replace(/\(\s{0,}SELECT /ig,"~::~(SELECT ") | |
247 .replace(/\)\s{0,}SELECT /ig,")~::~SELECT ") | |
248 | |
249 .replace(/ THEN /ig," THEN~::~"+tab+"") | |
250 .replace(/ UNION /ig,"~::~UNION~::~") | |
251 .replace(/ USING /ig,"~::~USING ") | |
252 .replace(/ WHEN /ig,"~::~"+tab+"WHEN ") | |
253 .replace(/ WHERE /ig,"~::~WHERE ") | |
254 .replace(/ WITH /ig,"~::~WITH ") | |
255 | |
256 //.replace(/\,\s{0,}\(/ig,",~::~( ") | |
257 //.replace(/\,/ig,",~::~"+tab+tab+"") | |
258 | |
259 .replace(/ ALL /ig," ALL ") | |
260 .replace(/ AS /ig," AS ") | |
261 .replace(/ ASC /ig," ASC ") | |
262 .replace(/ DESC /ig," DESC ") | |
263 .replace(/ DISTINCT /ig," DISTINCT ") | |
264 .replace(/ EXISTS /ig," EXISTS ") | |
265 .replace(/ NOT /ig," NOT ") | |
266 .replace(/ NULL /ig," NULL ") | |
267 .replace(/ LIKE /ig," LIKE ") | |
268 .replace(/\s{0,}SELECT /ig,"SELECT ") | |
269 | |
270 .replace(/~::~{1,}/g,"~::~") | |
271 .split('~::~'); | |
272 } | |
273 | |
274 vkbeautify.prototype.sql = function(text,step) { | |
275 | |
276 var ar_by_quote = text.replace(/\s{1,}/g," ") | |
277 .replace(/\'/ig,"~::~\'") | |
278 .split('~::~'), | |
279 len = ar_by_quote.length, | |
280 ar = [], | |
281 deep = 0, | |
282 tab = this.step,//+this.step, | |
283 inComment = true, | |
284 inQuote = false, | |
285 parenthesisLevel = 0, | |
286 str = '', | |
287 ix = 0, | |
288 shift = step ? createShiftArr(step) : this.shift;; | |
289 | |
290 for(ix=0;ix<len;ix++) { | |
291 if(ix%2) { | |
292 ar = ar.concat(ar_by_quote[ix]); | |
293 } else { | |
294 ar = ar.concat(split_sql(ar_by_quote[ix], tab) ); | |
295 } | |
296 } | |
297 | |
298 len = ar.length; | |
299 for(ix=0;ix<len;ix++) { | |
300 | |
301 parenthesisLevel = isSubquery(ar[ix], parenthesisLevel); | |
302 | |
303 if( /\s{0,}\s{0,}SELECT\s{0,}/.exec(ar[ix])) { | |
304 ar[ix] = ar[ix].replace(/\,/g,",\n"+tab+tab+"") | |
305 } | |
306 | |
307 if( /\s{0,}\(\s{0,}SELECT\s{0,}/.exec(ar[ix])) { | |
308 deep++; | |
309 str += shift[deep]+ar[ix]; | |
310 } else | |
311 if( /\'/.exec(ar[ix]) ) { | |
312 if(parenthesisLevel<1 && deep) { | |
313 deep--; | |
314 } | |
315 str += ar[ix]; | |
316 } | |
317 else { | |
318 str += shift[deep]+ar[ix]; | |
319 if(parenthesisLevel<1 && deep) { | |
320 deep--; | |
321 } | |
322 } | |
323 var junk = 0; | |
324 } | |
325 | |
326 str = str.replace(/^\n{1,}/,'').replace(/\n{1,}/g,"\n"); | |
327 return str; | |
328 } | |
329 | |
330 | |
331 vkbeautify.prototype.xmlmin = function(text, preserveComments) { | |
332 | |
333 var str = preserveComments ? text | |
334 : text.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g,""); | |
335 return str.replace(/>\s{0,}</g,"><"); | |
336 } | |
337 | |
338 vkbeautify.prototype.jsonmin = function(text) { | |
339 | |
340 return text.replace(/\s{0,}\{\s{0,}/g,"{") | |
341 .replace(/\s{0,}\[$/g,"[") | |
342 .replace(/\[\s{0,}/g,"[") | |
343 .replace(/:\s{0,}\[/g,':[') | |
344 .replace(/\s{0,}\}\s{0,}/g,"}") | |
345 .replace(/\s{0,}\]\s{0,}/g,"]") | |
346 .replace(/\"\s{0,}\,/g,'",') | |
347 .replace(/\,\s{0,}\"/g,',"') | |
348 .replace(/\"\s{0,}:/g,'":') | |
349 .replace(/:\s{0,}\"/g,':"') | |
350 .replace(/:\s{0,}\[/g,':[') | |
351 .replace(/\,\s{0,}\[/g,',[') | |
352 .replace(/\,\s{2,}/g,', ') | |
353 .replace(/\]\s{0,},\s{0,}\[/g,'],['); | |
354 } | |
355 | |
356 vkbeautify.prototype.cssmin = function(text, preserveComments) { | |
357 | |
358 var str = preserveComments ? text | |
359 : text.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"") ; | |
360 | |
361 return str.replace(/\s{1,}/g,' ') | |
362 .replace(/\{\s{1,}/g,"{") | |
363 .replace(/\}\s{1,}/g,"}") | |
364 .replace(/\;\s{1,}/g,";") | |
365 .replace(/\/\*\s{1,}/g,"/*") | |
366 .replace(/\*\/\s{1,}/g,"*/"); | |
367 } | |
368 | |
369 vkbeautify.prototype.sqlmin = function(text) { | |
370 return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")"); | |
371 } | |
372 | |
373 window.vkbeautify = new vkbeautify(); | |
374 | |
375 })(); | |
376 |