js-beautify 1.9.0-beta2 → 1.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +162 -48
- package/README.md +10 -10
- package/js/lib/beautifier.js +685 -382
- package/js/lib/beautifier.min.js +1 -1
- package/js/lib/beautify-css.js +75 -35
- package/js/lib/beautify-html.js +565 -285
- package/js/lib/beautify.js +584 -140
- package/js/src/core/directives.js +2 -2
- package/js/src/core/inputscanner.js +58 -18
- package/js/src/core/output.js +3 -6
- package/js/src/core/pattern.js +94 -0
- package/js/src/core/templatablepattern.js +179 -0
- package/js/src/core/tokenizer.js +7 -22
- package/js/src/core/whitespacepattern.js +105 -0
- package/js/src/html/beautifier.js +34 -24
- package/js/src/html/tokenizer.js +48 -32
- package/js/src/javascript/acorn.js +4 -3
- package/js/src/javascript/beautifier.js +5 -6
- package/js/src/javascript/tokenizer.js +105 -82
- package/package.json +3 -3
- package/js/src/html/templatablereader.js +0 -160
|
@@ -34,7 +34,7 @@ function Directives(start_block_pattern, end_block_pattern) {
|
|
|
34
34
|
this.__directives_block_pattern = new RegExp(start_block_pattern + / beautify( \w+[:]\w+)+ /.source + end_block_pattern, 'g');
|
|
35
35
|
this.__directive_pattern = / (\w+)[:](\w+)/g;
|
|
36
36
|
|
|
37
|
-
this.__directives_end_ignore_pattern = new RegExp(
|
|
37
|
+
this.__directives_end_ignore_pattern = new RegExp(start_block_pattern + /\sbeautify\signore:end\s/.source + end_block_pattern, 'g');
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
Directives.prototype.get_directives = function(text) {
|
|
@@ -55,7 +55,7 @@ Directives.prototype.get_directives = function(text) {
|
|
|
55
55
|
};
|
|
56
56
|
|
|
57
57
|
Directives.prototype.readIgnored = function(input) {
|
|
58
|
-
return input.
|
|
58
|
+
return input.readUntilAfter(this.__directives_end_ignore_pattern);
|
|
59
59
|
};
|
|
60
60
|
|
|
61
61
|
|
|
@@ -28,6 +28,8 @@
|
|
|
28
28
|
|
|
29
29
|
'use strict';
|
|
30
30
|
|
|
31
|
+
var regexp_has_sticky = RegExp.prototype.hasOwnProperty('sticky');
|
|
32
|
+
|
|
31
33
|
function InputScanner(input_string) {
|
|
32
34
|
this.__input = input_string || '';
|
|
33
35
|
this.__input_length = this.__input.length;
|
|
@@ -67,14 +69,32 @@ InputScanner.prototype.peek = function(index) {
|
|
|
67
69
|
return val;
|
|
68
70
|
};
|
|
69
71
|
|
|
72
|
+
// This is a JavaScript only helper function (not in python)
|
|
73
|
+
// Javascript doesn't have a match method
|
|
74
|
+
// and not all implementation support "sticky" flag.
|
|
75
|
+
// If they do not support sticky then both this.match() and this.test() method
|
|
76
|
+
// must get the match and check the index of the match.
|
|
77
|
+
// If sticky is supported and set, this method will use it.
|
|
78
|
+
// Otherwise it will check that global is set, and fall back to the slower method.
|
|
79
|
+
InputScanner.prototype.__match = function(pattern, index) {
|
|
80
|
+
pattern.lastIndex = index;
|
|
81
|
+
var pattern_match = pattern.exec(this.__input);
|
|
82
|
+
|
|
83
|
+
if (pattern_match && !(regexp_has_sticky && pattern.sticky)) {
|
|
84
|
+
if (pattern_match.index !== index) {
|
|
85
|
+
pattern_match = null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return pattern_match;
|
|
90
|
+
};
|
|
91
|
+
|
|
70
92
|
InputScanner.prototype.test = function(pattern, index) {
|
|
71
93
|
index = index || 0;
|
|
72
94
|
index += this.__position;
|
|
73
|
-
pattern.lastIndex = index;
|
|
74
95
|
|
|
75
96
|
if (index >= 0 && index < this.__input_length) {
|
|
76
|
-
|
|
77
|
-
return pattern_match && pattern_match.index === index;
|
|
97
|
+
return !!this.__match(pattern, index);
|
|
78
98
|
} else {
|
|
79
99
|
return false;
|
|
80
100
|
}
|
|
@@ -88,9 +108,8 @@ InputScanner.prototype.testChar = function(pattern, index) {
|
|
|
88
108
|
};
|
|
89
109
|
|
|
90
110
|
InputScanner.prototype.match = function(pattern) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (pattern_match && pattern_match.index === this.__position) {
|
|
111
|
+
var pattern_match = this.__match(pattern, this.__position);
|
|
112
|
+
if (pattern_match) {
|
|
94
113
|
this.__position += pattern_match[0].length;
|
|
95
114
|
} else {
|
|
96
115
|
pattern_match = null;
|
|
@@ -98,28 +117,30 @@ InputScanner.prototype.match = function(pattern) {
|
|
|
98
117
|
return pattern_match;
|
|
99
118
|
};
|
|
100
119
|
|
|
101
|
-
InputScanner.prototype.read = function(
|
|
120
|
+
InputScanner.prototype.read = function(starting_pattern, until_pattern, until_after) {
|
|
102
121
|
var val = '';
|
|
103
|
-
var match
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
val +=
|
|
122
|
+
var match;
|
|
123
|
+
if (starting_pattern) {
|
|
124
|
+
match = this.match(starting_pattern);
|
|
125
|
+
if (match) {
|
|
126
|
+
val += match[0];
|
|
108
127
|
}
|
|
109
128
|
}
|
|
129
|
+
if (until_pattern && (match || !starting_pattern)) {
|
|
130
|
+
val += this.readUntil(until_pattern, until_after);
|
|
131
|
+
}
|
|
110
132
|
return val;
|
|
111
133
|
};
|
|
112
134
|
|
|
113
|
-
InputScanner.prototype.readUntil = function(pattern,
|
|
135
|
+
InputScanner.prototype.readUntil = function(pattern, until_after) {
|
|
114
136
|
var val = '';
|
|
115
137
|
var match_index = this.__position;
|
|
116
138
|
pattern.lastIndex = this.__position;
|
|
117
139
|
var pattern_match = pattern.exec(this.__input);
|
|
118
140
|
if (pattern_match) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
match_index = pattern_match.index;
|
|
141
|
+
match_index = pattern_match.index;
|
|
142
|
+
if (until_after) {
|
|
143
|
+
match_index += pattern_match[0].length;
|
|
123
144
|
}
|
|
124
145
|
} else {
|
|
125
146
|
match_index = this.__input_length;
|
|
@@ -134,6 +155,26 @@ InputScanner.prototype.readUntilAfter = function(pattern) {
|
|
|
134
155
|
return this.readUntil(pattern, true);
|
|
135
156
|
};
|
|
136
157
|
|
|
158
|
+
InputScanner.prototype.get_regexp = function(pattern, match_from) {
|
|
159
|
+
var result = null;
|
|
160
|
+
var flags = 'g';
|
|
161
|
+
if (match_from && regexp_has_sticky) {
|
|
162
|
+
flags = 'y';
|
|
163
|
+
}
|
|
164
|
+
// strings are converted to regexp
|
|
165
|
+
if (typeof pattern === "string" && pattern !== '') {
|
|
166
|
+
// result = new RegExp(pattern.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), flags);
|
|
167
|
+
result = new RegExp(pattern, flags);
|
|
168
|
+
} else if (pattern) {
|
|
169
|
+
result = new RegExp(pattern.source, flags);
|
|
170
|
+
}
|
|
171
|
+
return result;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
InputScanner.prototype.get_literal_regexp = function(literal_string) {
|
|
175
|
+
return RegExp(literal_string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'));
|
|
176
|
+
};
|
|
177
|
+
|
|
137
178
|
/* css beautifier legacy helpers */
|
|
138
179
|
InputScanner.prototype.peekUntilAfter = function(pattern) {
|
|
139
180
|
var start = this.__position;
|
|
@@ -148,5 +189,4 @@ InputScanner.prototype.lookBack = function(testVal) {
|
|
|
148
189
|
.toLowerCase() === testVal;
|
|
149
190
|
};
|
|
150
191
|
|
|
151
|
-
|
|
152
192
|
module.exports.InputScanner = InputScanner;
|
package/js/src/core/output.js
CHANGED
|
@@ -121,14 +121,11 @@ OutputLine.prototype.last = function() {
|
|
|
121
121
|
|
|
122
122
|
OutputLine.prototype.push = function(item) {
|
|
123
123
|
this.__items.push(item);
|
|
124
|
-
this.__character_count += item.length;
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
OutputLine.prototype.push_raw = function(item) {
|
|
128
|
-
this.push(item);
|
|
129
124
|
var last_newline_index = item.lastIndexOf('\n');
|
|
130
125
|
if (last_newline_index !== -1) {
|
|
131
126
|
this.__character_count = item.length - last_newline_index;
|
|
127
|
+
} else {
|
|
128
|
+
this.__character_count += item.length;
|
|
132
129
|
}
|
|
133
130
|
};
|
|
134
131
|
|
|
@@ -341,7 +338,7 @@ Output.prototype.add_raw_token = function(token) {
|
|
|
341
338
|
}
|
|
342
339
|
this.current_line.set_indent(-1);
|
|
343
340
|
this.current_line.push(token.whitespace_before);
|
|
344
|
-
this.current_line.
|
|
341
|
+
this.current_line.push(token.text);
|
|
345
342
|
this.space_before_token = false;
|
|
346
343
|
this.non_breaking_space = false;
|
|
347
344
|
this.previous_token_wrapped = false;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/*jshint node:true */
|
|
2
|
+
/*
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person
|
|
9
|
+
obtaining a copy of this software and associated documentation files
|
|
10
|
+
(the "Software"), to deal in the Software without restriction,
|
|
11
|
+
including without limitation the rights to use, copy, modify, merge,
|
|
12
|
+
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
13
|
+
and to permit persons to whom the Software is furnished to do so,
|
|
14
|
+
subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be
|
|
17
|
+
included in all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
23
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
24
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
25
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
'use strict';
|
|
30
|
+
|
|
31
|
+
function Pattern(input_scanner, parent) {
|
|
32
|
+
this._input = input_scanner;
|
|
33
|
+
this._starting_pattern = null;
|
|
34
|
+
this._match_pattern = null;
|
|
35
|
+
this._until_pattern = null;
|
|
36
|
+
this._until_after = false;
|
|
37
|
+
|
|
38
|
+
if (parent) {
|
|
39
|
+
this._starting_pattern = this._input.get_regexp(parent._starting_pattern, true);
|
|
40
|
+
this._match_pattern = this._input.get_regexp(parent._match_pattern, true);
|
|
41
|
+
this._until_pattern = this._input.get_regexp(parent._until_pattern);
|
|
42
|
+
this._until_after = parent._until_after;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
Pattern.prototype.read = function() {
|
|
47
|
+
var result = this._input.read(this._starting_pattern);
|
|
48
|
+
if (!this._starting_pattern || result) {
|
|
49
|
+
result += this._input.read(this._match_pattern, this._until_pattern, this._until_after);
|
|
50
|
+
}
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
Pattern.prototype.read_match = function() {
|
|
55
|
+
return this._input.match(this._match_pattern);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
Pattern.prototype.until_after = function(pattern) {
|
|
59
|
+
var result = this._create();
|
|
60
|
+
result._until_after = true;
|
|
61
|
+
result._until_pattern = this._input.get_regexp(pattern);
|
|
62
|
+
result._update();
|
|
63
|
+
return result;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
Pattern.prototype.until = function(pattern) {
|
|
67
|
+
var result = this._create();
|
|
68
|
+
result._until_after = false;
|
|
69
|
+
result._until_pattern = this._input.get_regexp(pattern);
|
|
70
|
+
result._update();
|
|
71
|
+
return result;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
Pattern.prototype.starting_with = function(pattern) {
|
|
75
|
+
var result = this._create();
|
|
76
|
+
result._starting_pattern = this._input.get_regexp(pattern, true);
|
|
77
|
+
result._update();
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
Pattern.prototype.matching = function(pattern) {
|
|
82
|
+
var result = this._create();
|
|
83
|
+
result._match_pattern = this._input.get_regexp(pattern, true);
|
|
84
|
+
result._update();
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
Pattern.prototype._create = function() {
|
|
89
|
+
return new Pattern(this._input, this);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
Pattern.prototype._update = function() {};
|
|
93
|
+
|
|
94
|
+
module.exports.Pattern = Pattern;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/*jshint node:true */
|
|
2
|
+
/*
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person
|
|
9
|
+
obtaining a copy of this software and associated documentation files
|
|
10
|
+
(the "Software"), to deal in the Software without restriction,
|
|
11
|
+
including without limitation the rights to use, copy, modify, merge,
|
|
12
|
+
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
13
|
+
and to permit persons to whom the Software is furnished to do so,
|
|
14
|
+
subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be
|
|
17
|
+
included in all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
23
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
24
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
25
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
'use strict';
|
|
30
|
+
|
|
31
|
+
var Pattern = require('./pattern').Pattern;
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
var template_names = {
|
|
35
|
+
django: false,
|
|
36
|
+
erb: false,
|
|
37
|
+
handlebars: false,
|
|
38
|
+
php: false
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// This lets templates appear anywhere we would do a readUntil
|
|
42
|
+
// The cost is higher but it is pay to play.
|
|
43
|
+
function TemplatablePattern(input_scanner, parent) {
|
|
44
|
+
Pattern.call(this, input_scanner, parent);
|
|
45
|
+
this.__template_pattern = null;
|
|
46
|
+
this._disabled = Object.assign({}, template_names);
|
|
47
|
+
this._excluded = Object.assign({}, template_names);
|
|
48
|
+
|
|
49
|
+
if (parent) {
|
|
50
|
+
this.__template_pattern = this._input.get_regexp(parent.__template_pattern);
|
|
51
|
+
this._excluded = Object.assign(this._excluded, parent._excluded);
|
|
52
|
+
this._disabled = Object.assign(this._disabled, parent._disabled);
|
|
53
|
+
}
|
|
54
|
+
var pattern = new Pattern(input_scanner);
|
|
55
|
+
this.__patterns = {
|
|
56
|
+
handlebars_comment: pattern.starting_with(/{{!--/).until_after(/--}}/),
|
|
57
|
+
handlebars: pattern.starting_with(/{{/).until_after(/}}/),
|
|
58
|
+
php: pattern.starting_with(/<\?(?:[=]|php)/).until_after(/\?>/),
|
|
59
|
+
erb: pattern.starting_with(/<%[^%]/).until_after(/[^%]%>/),
|
|
60
|
+
// django coflicts with handlebars a bit.
|
|
61
|
+
django: pattern.starting_with(/{%/).until_after(/%}/),
|
|
62
|
+
django_value: pattern.starting_with(/{{/).until_after(/}}/),
|
|
63
|
+
django_comment: pattern.starting_with(/{#/).until_after(/#}/)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
TemplatablePattern.prototype = new Pattern();
|
|
67
|
+
|
|
68
|
+
TemplatablePattern.prototype._create = function() {
|
|
69
|
+
return new TemplatablePattern(this._input, this);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
TemplatablePattern.prototype._update = function() {
|
|
73
|
+
this.__set_templated_pattern();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
TemplatablePattern.prototype.disable = function(language) {
|
|
77
|
+
var result = this._create();
|
|
78
|
+
result._disabled[language] = true;
|
|
79
|
+
result._update();
|
|
80
|
+
return result;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
TemplatablePattern.prototype.exclude = function(language) {
|
|
84
|
+
var result = this._create();
|
|
85
|
+
result._excluded[language] = true;
|
|
86
|
+
result._update();
|
|
87
|
+
return result;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
TemplatablePattern.prototype.read = function() {
|
|
91
|
+
var result = '';
|
|
92
|
+
if (this._match_pattern) {
|
|
93
|
+
result = this._input.read(this._starting_pattern);
|
|
94
|
+
} else {
|
|
95
|
+
result = this._input.read(this._starting_pattern, this.__template_pattern);
|
|
96
|
+
}
|
|
97
|
+
var next = this._read_template();
|
|
98
|
+
while (next) {
|
|
99
|
+
if (this._match_pattern) {
|
|
100
|
+
next += this._input.read(this._match_pattern);
|
|
101
|
+
} else {
|
|
102
|
+
next += this._input.readUntil(this.__template_pattern);
|
|
103
|
+
}
|
|
104
|
+
result += next;
|
|
105
|
+
next = this._read_template();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (this._until_after) {
|
|
109
|
+
result += this._input.readUntilAfter(this._until_pattern);
|
|
110
|
+
}
|
|
111
|
+
return result;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
TemplatablePattern.prototype.__set_templated_pattern = function() {
|
|
115
|
+
var items = [];
|
|
116
|
+
|
|
117
|
+
if (!this._disabled.php) {
|
|
118
|
+
items.push(this.__patterns.php._starting_pattern.source);
|
|
119
|
+
}
|
|
120
|
+
if (!this._disabled.handlebars) {
|
|
121
|
+
items.push(this.__patterns.handlebars._starting_pattern.source);
|
|
122
|
+
}
|
|
123
|
+
if (!this._disabled.erb) {
|
|
124
|
+
items.push(this.__patterns.erb._starting_pattern.source);
|
|
125
|
+
}
|
|
126
|
+
if (!this._disabled.django) {
|
|
127
|
+
items.push(this.__patterns.django._starting_pattern.source);
|
|
128
|
+
items.push(this.__patterns.django_value._starting_pattern.source);
|
|
129
|
+
items.push(this.__patterns.django_comment._starting_pattern.source);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (this._until_pattern) {
|
|
133
|
+
items.push(this._until_pattern.source);
|
|
134
|
+
}
|
|
135
|
+
this.__template_pattern = this._input.get_regexp('(?:' + items.join('|') + ')');
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
TemplatablePattern.prototype._read_template = function() {
|
|
139
|
+
var resulting_string = '';
|
|
140
|
+
var c = this._input.peek();
|
|
141
|
+
if (c === '<') {
|
|
142
|
+
var peek1 = this._input.peek(1);
|
|
143
|
+
//if we're in a comment, do something special
|
|
144
|
+
// We treat all comments as literals, even more than preformatted tags
|
|
145
|
+
// we just look for the appropriate close tag
|
|
146
|
+
if (!this._disabled.php && !this._excluded.php && peek1 === '?') {
|
|
147
|
+
resulting_string = resulting_string ||
|
|
148
|
+
this.__patterns.php.read();
|
|
149
|
+
}
|
|
150
|
+
if (!this._disabled.erb && !this._excluded.erb && peek1 === '%') {
|
|
151
|
+
resulting_string = resulting_string ||
|
|
152
|
+
this.__patterns.erb.read();
|
|
153
|
+
}
|
|
154
|
+
} else if (c === '{') {
|
|
155
|
+
if (!this._disabled.handlebars && !this._excluded.handlebars) {
|
|
156
|
+
resulting_string = resulting_string ||
|
|
157
|
+
this.__patterns.handlebars_comment.read();
|
|
158
|
+
resulting_string = resulting_string ||
|
|
159
|
+
this.__patterns.handlebars.read();
|
|
160
|
+
}
|
|
161
|
+
if (!this._disabled.django) {
|
|
162
|
+
// django coflicts with handlebars a bit.
|
|
163
|
+
if (!this._excluded.django && !this._excluded.handlebars) {
|
|
164
|
+
resulting_string = resulting_string ||
|
|
165
|
+
this.__patterns.django_value.read();
|
|
166
|
+
}
|
|
167
|
+
if (!this._excluded.django) {
|
|
168
|
+
resulting_string = resulting_string ||
|
|
169
|
+
this.__patterns.django_comment.read();
|
|
170
|
+
resulting_string = resulting_string ||
|
|
171
|
+
this.__patterns.django.read();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return resulting_string;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
module.exports.TemplatablePattern = TemplatablePattern;
|
package/js/src/core/tokenizer.js
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
var InputScanner = require('../core/inputscanner').InputScanner;
|
|
32
32
|
var Token = require('../core/token').Token;
|
|
33
33
|
var TokenStream = require('../core/tokenstream').TokenStream;
|
|
34
|
+
var WhitespacePattern = require('./whitespacepattern').WhitespacePattern;
|
|
34
35
|
|
|
35
36
|
var TOKEN = {
|
|
36
37
|
START: 'TK_START',
|
|
@@ -42,11 +43,9 @@ var Tokenizer = function(input_string, options) {
|
|
|
42
43
|
this._input = new InputScanner(input_string);
|
|
43
44
|
this._options = options || {};
|
|
44
45
|
this.__tokens = null;
|
|
45
|
-
this.__newline_count = 0;
|
|
46
|
-
this.__whitespace_before_token = '';
|
|
47
46
|
|
|
48
|
-
this.
|
|
49
|
-
this.
|
|
47
|
+
this._patterns = {};
|
|
48
|
+
this._patterns.whitespace = new WhitespacePattern(this._input);
|
|
50
49
|
};
|
|
51
50
|
|
|
52
51
|
Tokenizer.prototype.tokenize = function() {
|
|
@@ -125,28 +124,14 @@ Tokenizer.prototype._is_closing = function(current_token, open_token) { // jshin
|
|
|
125
124
|
};
|
|
126
125
|
|
|
127
126
|
Tokenizer.prototype._create_token = function(type, text) {
|
|
128
|
-
var token = new Token(type, text,
|
|
129
|
-
|
|
130
|
-
|
|
127
|
+
var token = new Token(type, text,
|
|
128
|
+
this._patterns.whitespace.newline_count,
|
|
129
|
+
this._patterns.whitespace.whitespace_before_token);
|
|
131
130
|
return token;
|
|
132
131
|
};
|
|
133
132
|
|
|
134
133
|
Tokenizer.prototype._readWhitespace = function() {
|
|
135
|
-
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
var resulting_string = this._input.read(this._whitespace_pattern);
|
|
139
|
-
if (resulting_string === ' ') {
|
|
140
|
-
this.__whitespace_before_token = resulting_string;
|
|
141
|
-
} else if (resulting_string !== '') {
|
|
142
|
-
this._newline_pattern.lastIndex = 0;
|
|
143
|
-
var nextMatch = this._newline_pattern.exec(resulting_string);
|
|
144
|
-
while (nextMatch[2]) {
|
|
145
|
-
this.__newline_count += 1;
|
|
146
|
-
nextMatch = this._newline_pattern.exec(resulting_string);
|
|
147
|
-
}
|
|
148
|
-
this.__whitespace_before_token = nextMatch[1];
|
|
149
|
-
}
|
|
134
|
+
return this._patterns.whitespace.read();
|
|
150
135
|
};
|
|
151
136
|
|
|
152
137
|
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/*jshint node:true */
|
|
2
|
+
/*
|
|
3
|
+
|
|
4
|
+
The MIT License (MIT)
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
|
|
7
|
+
|
|
8
|
+
Permission is hereby granted, free of charge, to any person
|
|
9
|
+
obtaining a copy of this software and associated documentation files
|
|
10
|
+
(the "Software"), to deal in the Software without restriction,
|
|
11
|
+
including without limitation the rights to use, copy, modify, merge,
|
|
12
|
+
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
13
|
+
and to permit persons to whom the Software is furnished to do so,
|
|
14
|
+
subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be
|
|
17
|
+
included in all copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
20
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
21
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
22
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
23
|
+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
24
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
25
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
'use strict';
|
|
30
|
+
|
|
31
|
+
var Pattern = require('../core/pattern').Pattern;
|
|
32
|
+
|
|
33
|
+
function WhitespacePattern(input_scanner, parent) {
|
|
34
|
+
Pattern.call(this, input_scanner, parent);
|
|
35
|
+
if (parent) {
|
|
36
|
+
this._line_regexp = this._input.get_regexp(parent._line_regexp);
|
|
37
|
+
} else {
|
|
38
|
+
this.__set_whitespace_patterns('', '');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.newline_count = 0;
|
|
42
|
+
this.whitespace_before_token = '';
|
|
43
|
+
}
|
|
44
|
+
WhitespacePattern.prototype = new Pattern();
|
|
45
|
+
|
|
46
|
+
WhitespacePattern.prototype.__set_whitespace_patterns = function(whitespace_chars, newline_chars) {
|
|
47
|
+
whitespace_chars += '\\t ';
|
|
48
|
+
newline_chars += '\\n\\r';
|
|
49
|
+
|
|
50
|
+
this._match_pattern = this._input.get_regexp(
|
|
51
|
+
'[' + whitespace_chars + newline_chars + ']+', true);
|
|
52
|
+
this._newline_regexp = this._input.get_regexp(
|
|
53
|
+
'\\r\\n|[' + newline_chars + ']');
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
WhitespacePattern.prototype.read = function() {
|
|
57
|
+
this.newline_count = 0;
|
|
58
|
+
this.whitespace_before_token = '';
|
|
59
|
+
|
|
60
|
+
var resulting_string = this._input.read(this._match_pattern);
|
|
61
|
+
if (resulting_string === ' ') {
|
|
62
|
+
this.whitespace_before_token = ' ';
|
|
63
|
+
} else if (resulting_string) {
|
|
64
|
+
var matches = this.__split(this._newline_regexp, resulting_string);
|
|
65
|
+
this.newline_count = matches.length - 1;
|
|
66
|
+
this.whitespace_before_token = matches[this.newline_count];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return resulting_string;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
WhitespacePattern.prototype.matching = function(whitespace_chars, newline_chars) {
|
|
73
|
+
var result = this._create();
|
|
74
|
+
result.__set_whitespace_patterns(whitespace_chars, newline_chars);
|
|
75
|
+
result._update();
|
|
76
|
+
return result;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
WhitespacePattern.prototype._create = function() {
|
|
80
|
+
return new WhitespacePattern(this._input, this);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
WhitespacePattern.prototype.__split = function(regexp, input_string) {
|
|
84
|
+
regexp.lastIndex = 0;
|
|
85
|
+
var start_index = 0;
|
|
86
|
+
var result = [];
|
|
87
|
+
var next_match = regexp.exec(input_string);
|
|
88
|
+
while (next_match) {
|
|
89
|
+
result.push(input_string.substring(start_index, next_match.index));
|
|
90
|
+
start_index = next_match.index + next_match[0].length;
|
|
91
|
+
next_match = regexp.exec(input_string);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (start_index < input_string.length) {
|
|
95
|
+
result.push(input_string.substring(start_index, input_string.length));
|
|
96
|
+
} else {
|
|
97
|
+
result.push('');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return result;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
module.exports.WhitespacePattern = WhitespacePattern;
|