|
1 | 1 | (function(exports) {
|
2 |
| - exports.indenter = { |
| 2 | + exports.indent = { |
3 | 3 | indentCSS: indentJS,
|
4 | 4 | indentJS: indentJS,
|
5 | 5 | indentHTML: indentHTML
|
|
24 | 24 | {
|
25 | 25 | name: "regex",
|
26 | 26 | startToken: [function(string, rule) {
|
27 |
| - var re = /[,=:!&|?};][\s]*\/[^/]|^\/[^/]/; |
| 27 | + var re = /[(,=:[!&|?{};][\s]*\/[^/]|^[\s]*\/[^/]/; |
28 | 28 | var startIndex = string.search(re);
|
29 | 29 | if (startIndex != -1) {
|
30 | 30 | startIndex = string.indexOf('/', startIndex);
|
|
112 | 112 | },
|
113 | 113 | {
|
114 | 114 | name: "bracket",
|
115 |
| - startToken: [/[^\(]\(/], |
| 115 | + startToken: [/\(/], |
116 | 116 | endToken: [/\)/],
|
117 | 117 | indent: true,
|
118 | 118 | advance: true
|
119 | 119 | },
|
120 |
| - { |
121 |
| - name: "bracket", |
122 |
| - startToken: [/^\(/], |
123 |
| - endToken: [/\)/], |
124 |
| - indent: false, |
125 |
| - advance: true |
126 |
| - }, |
127 | 120 | {
|
128 | 121 | name: "array",
|
129 | 122 | startToken: [/\[/],
|
|
141 | 134 | {
|
142 | 135 | name: "var",
|
143 | 136 | startToken: [/var[\s]+/],
|
144 |
| - endToken: [/\;/, /\=/], |
| 137 | + endToken: [/\;/], |
145 | 138 | indent: true
|
146 | 139 | },
|
147 | 140 | {
|
|
197 | 190 | var lines = code.split(/[\r]?\n/gi);
|
198 | 191 | var lineCount = lines.length;
|
199 | 192 | var newLines = [];
|
200 |
| - var indents = 0; |
201 |
| - var indentAfter = 0; |
| 193 | + var indentBuffer = []; |
202 | 194 | var activeRules = [];
|
203 | 195 | var lastRule;
|
| 196 | + var indents = 0; |
204 | 197 | var l = 0;
|
205 | 198 | var pos = 0;
|
206 | 199 | var matchEnd, matchStart;
|
|
216 | 209 | if (matchEnd.matchIndex == -1) {
|
217 | 210 | if (lastRule.ignore) {
|
218 | 211 | // last rule is still active, and it's telling us to ignore.
|
219 |
| - newLines[l] = getIndentation() + line; |
220 | 212 | incrementLine();
|
221 | 213 | continue;
|
222 | 214 | }
|
223 | 215 | }
|
224 | 216 | else if (lastRule.ignore || matchStart.matchIndex == -1 || matchEnd.matchIndex <= matchStart.matchIndex) {
|
225 |
| - if (matchEnd.matchIndex == 0 && lastRule.indent) { |
226 |
| - indents--; |
227 |
| - } |
228 |
| - else if (lastRule.indent) { |
229 |
| - indentAfter--; |
| 217 | + if (lastRule.indent) { |
| 218 | + consumeIndentation(); |
| 219 | + if (matchEnd.matchIndex == 0) { |
| 220 | + calculateIndents(); |
| 221 | + } |
230 | 222 | }
|
231 | 223 | pos = matchEnd.cursor;
|
232 | 224 | removeLastRule();
|
|
239 | 231 | }
|
240 | 232 | else {
|
241 | 233 | // No new token match end, no new match start
|
242 |
| - newLines[l] = getIndentation() + line; |
243 | 234 | incrementLine();
|
244 | 235 | }
|
245 | 236 | }
|
|
250 | 241 | pos = match.cursor;
|
251 | 242 | lastRule = match.rule;
|
252 | 243 | activeRules.push(lastRule);
|
253 |
| - if (lastRule.indent) indentAfter++; |
| 244 | + if (lastRule.indent) |
| 245 | + incrementIndentation(); |
254 | 246 | }
|
255 | 247 |
|
256 | 248 | function removeLastRule() {
|
257 | 249 | activeRules.pop();
|
258 | 250 | lastRule = activeRules[activeRules.length-1];
|
259 | 251 | }
|
260 | 252 |
|
261 |
| - function getIndentation() { |
262 |
| - return (new Array(indents+1)).join(indentation); |
| 253 | + function calculateIndents() { |
| 254 | + indents = 0; |
| 255 | + for (var b,i=0; i<indentBuffer.length; i++) { |
| 256 | + b = indentBuffer[i]; |
| 257 | + if (b.open && b.line != l) |
| 258 | + indents++; |
| 259 | + } |
263 | 260 | }
|
264 | 261 |
|
265 | 262 | function incrementLine() {
|
| 263 | + newLines[l] = repeatString(indentation, indents) + line; |
266 | 264 | l++;
|
267 | 265 | pos = 0;
|
268 |
| - if (indentAfter != 0) { |
269 |
| - indents += indentAfter; |
270 |
| - indentAfter = 0; |
| 266 | + calculateIndents(); |
| 267 | + } |
| 268 | + |
| 269 | + function incrementIndentation() { |
| 270 | + var matched = indentBuffer[indentBuffer.length-1]; |
| 271 | + if (matched && matched.line == l) { |
| 272 | + matched.indent++; |
| 273 | + } |
| 274 | + else { |
| 275 | + indentBuffer.push({ |
| 276 | + indent: 1, |
| 277 | + open: true, |
| 278 | + line: l |
| 279 | + }); |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + function consumeIndentation() { |
| 284 | + var lastElem = indentBuffer[indentBuffer.length-1]; |
| 285 | + if (lastElem) { |
| 286 | + lastElem.open = l == lastElem.line; |
| 287 | + if (--lastElem.indent <= 0) { |
| 288 | + indentBuffer.pop(); |
| 289 | + } |
271 | 290 | }
|
272 | 291 | }
|
273 | 292 | }
|
| 293 | + |
| 294 | + function repeatString(baseString, repeat) { |
| 295 | + return (new Array(repeat+1)).join(baseString); |
| 296 | + } |
274 | 297 |
|
275 | 298 | function cleanEscapedChars(string) {
|
276 | 299 | return string.replace(/\\(u[0-9A-Za-z]{4}|u\{[0-9A-Za-z]{1,6}]\}|x[0-9A-Za-z]{2}|.)/g, '0');
|
|
334 | 357 | };
|
335 | 358 | }
|
336 | 359 | })(this);
|
337 |
| - |
338 |
| - |
339 |
| -function hereDoc(f) { |
340 |
| - return f.toString(). |
341 |
| - replace(/^[^\/]+\/\*!?/, ''). |
342 |
| - replace(/\*\/[^\/]+$/, ''); |
343 |
| -} |
344 |
| - |
345 |
| -var fs = require('fs'); |
346 |
| -var self = this; |
347 |
| -var code = ` |
348 |
| -` |
349 |
| -var doc = hereDoc(function() {/*! |
350 |
| - if ((insideRule || enteringConditionalGroup) && |
351 |
| - !(lookBack("&") || foundNestedPseudoClass()) && |
352 |
| - !lookBack("(")) { |
353 |
| - // 'property: value' delimiter |
354 |
| - // which could be in a conditional group query |
355 |
| - insidePropertyValue = true; |
356 |
| - output.push(':'); |
357 |
| - print.singleSpace(); |
358 |
| - } |
359 |
| - */}) |
360 |
| - |
361 |
| - |
362 |
| -console.log( |
363 |
| - self.indenter.indentJS(doc, ' ') |
364 |
| -); |
365 |
| -// fs.readFile( __dirname + '/file.js', function (err, data) { |
366 |
| -// if (err) { |
367 |
| -// throw err; |
368 |
| -// } |
369 |
| -// console.log( |
370 |
| -// self.indenter.indentJS(data.toString(), ' ') |
371 |
| -// ); |
372 |
| -// }); |
0 commit comments