templatablepattern.js
5.91 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*jshint node:true */
/*
The MIT License (MIT)
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use strict';
var Pattern = require('./pattern').Pattern;
var template_names = {
django: false,
erb: false,
handlebars: false,
php: false
};
// This lets templates appear anywhere we would do a readUntil
// The cost is higher but it is pay to play.
function TemplatablePattern(input_scanner, parent) {
Pattern.call(this, input_scanner, parent);
this.__template_pattern = null;
this._disabled = Object.assign({}, template_names);
this._excluded = Object.assign({}, template_names);
if (parent) {
this.__template_pattern = this._input.get_regexp(parent.__template_pattern);
this._excluded = Object.assign(this._excluded, parent._excluded);
this._disabled = Object.assign(this._disabled, parent._disabled);
}
var pattern = new Pattern(input_scanner);
this.__patterns = {
handlebars_comment: pattern.starting_with(/{{!--/).until_after(/--}}/),
handlebars: pattern.starting_with(/{{/).until_after(/}}/),
php: pattern.starting_with(/<\?(?:[=]|php)/).until_after(/\?>/),
erb: pattern.starting_with(/<%[^%]/).until_after(/[^%]%>/),
// django coflicts with handlebars a bit.
django: pattern.starting_with(/{%/).until_after(/%}/),
django_value: pattern.starting_with(/{{/).until_after(/}}/),
django_comment: pattern.starting_with(/{#/).until_after(/#}/)
};
}
TemplatablePattern.prototype = new Pattern();
TemplatablePattern.prototype._create = function() {
return new TemplatablePattern(this._input, this);
};
TemplatablePattern.prototype._update = function() {
this.__set_templated_pattern();
};
TemplatablePattern.prototype.disable = function(language) {
var result = this._create();
result._disabled[language] = true;
result._update();
return result;
};
TemplatablePattern.prototype.exclude = function(language) {
var result = this._create();
result._excluded[language] = true;
result._update();
return result;
};
TemplatablePattern.prototype.read = function() {
var result = '';
if (this._match_pattern) {
result = this._input.read(this._starting_pattern);
} else {
result = this._input.read(this._starting_pattern, this.__template_pattern);
}
var next = this._read_template();
while (next) {
if (this._match_pattern) {
next += this._input.read(this._match_pattern);
} else {
next += this._input.readUntil(this.__template_pattern);
}
result += next;
next = this._read_template();
}
if (this._until_after) {
result += this._input.readUntilAfter(this._until_pattern);
}
return result;
};
TemplatablePattern.prototype.__set_templated_pattern = function() {
var items = [];
if (!this._disabled.php) {
items.push(this.__patterns.php._starting_pattern.source);
}
if (!this._disabled.handlebars) {
items.push(this.__patterns.handlebars._starting_pattern.source);
}
if (!this._disabled.erb) {
items.push(this.__patterns.erb._starting_pattern.source);
}
if (!this._disabled.django) {
items.push(this.__patterns.django._starting_pattern.source);
items.push(this.__patterns.django_value._starting_pattern.source);
items.push(this.__patterns.django_comment._starting_pattern.source);
}
if (this._until_pattern) {
items.push(this._until_pattern.source);
}
this.__template_pattern = this._input.get_regexp('(?:' + items.join('|') + ')');
};
TemplatablePattern.prototype._read_template = function() {
var resulting_string = '';
var c = this._input.peek();
if (c === '<') {
var peek1 = this._input.peek(1);
//if we're in a comment, do something special
// We treat all comments as literals, even more than preformatted tags
// we just look for the appropriate close tag
if (!this._disabled.php && !this._excluded.php && peek1 === '?') {
resulting_string = resulting_string ||
this.__patterns.php.read();
}
if (!this._disabled.erb && !this._excluded.erb && peek1 === '%') {
resulting_string = resulting_string ||
this.__patterns.erb.read();
}
} else if (c === '{') {
if (!this._disabled.handlebars && !this._excluded.handlebars) {
resulting_string = resulting_string ||
this.__patterns.handlebars_comment.read();
resulting_string = resulting_string ||
this.__patterns.handlebars.read();
}
if (!this._disabled.django) {
// django coflicts with handlebars a bit.
if (!this._excluded.django && !this._excluded.handlebars) {
resulting_string = resulting_string ||
this.__patterns.django_value.read();
}
if (!this._excluded.django) {
resulting_string = resulting_string ||
this.__patterns.django_comment.read();
resulting_string = resulting_string ||
this.__patterns.django.read();
}
}
}
return resulting_string;
};
module.exports.TemplatablePattern = TemplatablePattern;