/
block.js
80 lines (64 loc) · 1.73 KB
/
block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
var base = require('./base');
var util = require('../util');
// Do not use base.baseConstructor() since container is optional here
var Block = function (bucket, container) {
this.init();
this.setBucket(bucket);
if (container) {
this.setContainer(container);
}
return this;
};
util.extend(Block.prototype, base.base, {
name: "block",
reparseAs: function (anotherThing) {
this.debug('reparse as ' + anotherThing, this.list);
var tempTokenizer = this.bucket.tokenizer.tokenize('', this.bucket.options);
tempTokenizer.tokens = this.list;
this.list = [
this.bucket[anotherThing].parse(tempTokenizer, this.bucket, this)
];
this.debug('reparse finish', this.list);
},
toString: function (whitespaceType) {
this.debug('toString', this.list);
var out = this.makeString(this.list);
console.log("OUT for", whitespaceType || "block");
console.log(out);
console.log("!!!done");
out = this.reindent(out);
out = this.addWhitespace(whitespaceType || 'block', out);
return out;
},
toStringChangeCase: function () {
// Do not change case if we didn't parse the data
return this.toString();
}
});
exports.canStartWith = function (token, tokens, bucket) {
return token.type == 'BLOCK_OPEN';
};
exports.parse = function (tokens, bucket, container) {
var block = new Block(bucket, container);
var depth = 1;
block.debug('parse', tokens);
// Consume open brace
tokens.next();
while (tokens.anyLeft()) {
var token = tokens.getToken();
if (token.type == 'BLOCK_CLOSE') {
depth --;
if (! depth) {
// Done with this block
tokens.next();
return block;
}
} else if (token.type == 'BLOCK_OPEN') {
depth ++;
}
block.add(token);
tokens.next();
}
return block;
};