-
Notifications
You must be signed in to change notification settings - Fork 26
/
application.js
276 lines (231 loc) · 8.38 KB
/
application.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Generated by CoffeeScript 1.12.7
(function() {
var $, App, Expression, Results, TestStrings,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$ = jQuery;
Expression = (function(superClass) {
extend(Expression, superClass);
function Expression() {
return Expression.__super__.constructor.apply(this, arguments);
}
Expression.prototype.elements = {
'input[name=expression]': 'regexp',
'input[name=option]': 'option'
};
Expression.prototype.events = {
'keyup input': 'onKeyPress'
};
Expression.prototype.onKeyPress = function(event) {
var error;
try {
this.value = this.buildRegex(this.regexp.val(), this.option.val());
} catch (error1) {
error = error1;
}
return this.trigger('update');
};
Expression.prototype.buildRegex = function(value, option) {
return new RegExp(value, option);
};
Expression.prototype.asUrlPart = function() {
return encodeURIComponent(this.regexp.val() + "||||" + this.option.val());
};
return Expression;
})(Spine.Controller);
TestStrings = (function(superClass) {
extend(TestStrings, superClass);
function TestStrings() {
return TestStrings.__super__.constructor.apply(this, arguments);
}
TestStrings.prototype.elements = {
'textarea': 'input'
};
TestStrings.prototype.events = {
'keyup textarea': 'onKeyPress'
};
TestStrings.prototype.onKeyPress = function(event) {
this.getValues(this.input.val());
return this.trigger('update');
};
TestStrings.prototype.getValues = function(val) {
return this.values = val.split('\n');
};
TestStrings.prototype.asUrlPart = function() {
return encodeURIComponent(JSON.stringify(this.values));
};
return TestStrings;
})(Spine.Controller);
Results = (function() {
function Results(expression, test_strings1) {
this.expression = expression;
this.test_strings = test_strings1;
this.compile = bind(this.compile, this);
this.expression.bind('update', this.compile);
this.test_strings.bind('update', this.compile);
}
Results.prototype.compile = function() {
var count, error, i, len, matches, ref, value;
$('ul#results').empty();
$('ul#groups').empty();
count = 1;
if (this.expression.regexp.val() !== '') {
try {
new RegExp(this.expression.regexp.val());
} catch (error1) {
this.showRegexError();
return true;
}
}
if (this.expression.regexp.val() === '' && this.test_strings.input.val() === '') {
this.showIntro();
return true;
} else if (this.expression.regexp.val() === '' || this.test_strings.input.val() === '') {
this.showError();
return true;
} else if (!this.test_strings.values) {
return true;
}
try {
ref = this.test_strings.values;
for (i = 0, len = ref.length; i < len; i++) {
value = ref[i];
matches = value.match(this.expression.value);
this.matchResults(value, matches);
this.matchGroups(value, matches, count);
count += 1;
}
this.addShareLink(this.expression.asUrlPart(), this.test_strings.asUrlPart());
return this.showOutput();
} catch (error1) {
error = error1;
return this.showError();
}
};
Results.prototype.addShareLink = function(expression_url, test_strings_url) {
var url;
url = window.location.protocol + "//" + window.location.host;
url += "/#" + expression_url + encodeURIComponent("||||") + test_strings_url;
return $("#share_link").attr("href", url);
};
/*
escape function from Peter Hoffman found at
http://peter-hoffmann.com/2012/coffeescript-string-interpolation-with-html-escaping.html
*/
Results.prototype.escape = function(s) {
return ('' + s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\//g, '/');
};
Results.prototype.matchResults = function(value, matches) {
var string;
if (!matches) {
return;
}
string = this.generateMatches(value, this.expression.value);
return this.drawResult(string);
};
Results.prototype.generateMatches = function(value, regex) {
return this.escape(value.replace(regex, "~~scriptular_begin_match~~$&~~scriptular_end_match~~")).replace(/~~scriptular_begin_match~~/g, '<span>').replace(/~~scriptular_end_match~~/g, '</span>');
};
Results.prototype.drawResult = function(string) {
return $('ul#results').append("<li>" + string + "</li>");
};
Results.prototype.matchGroups = function(value, matches, count) {
var i, j, len, len1, match, ref;
if (!matches) {
return;
}
$('ul#groups').append("<li id='match_" + count + "'><h3>Match " + count + "</h3><ol></ol></li>");
if (this.expression.value.global) {
for (i = 0, len = matches.length; i < len; i++) {
match = matches[i];
if (match === '') {
return;
}
this.drawGroup(count, match);
}
} else {
ref = matches.slice(1);
for (j = 0, len1 = ref.length; j < len1; j++) {
match = ref[j];
if (match === '') {
return;
}
this.drawGroup(count, match);
}
}
};
Results.prototype.drawGroup = function(count, match) {
match = this.escape(match);
return $("ul#groups li#match_" + count + " ol").append("<li>" + match + "</li>");
};
Results.prototype.showIntro = function() {
$('#error').hide();
$('#output').hide();
$('#intro').show();
return $('#regex-error').hide();
};
Results.prototype.showError = function() {
$('#intro').hide();
$('#output').hide();
$('#error').show();
return $('#regex-error').hide();
};
Results.prototype.showOutput = function() {
$('#intro').hide();
$('#error').hide();
$('#output').show();
return $('#regex-error').hide();
};
Results.prototype.showRegexError = function() {
$('#intro').hide();
$('#error').hide();
$('#output').hide();
return $('#regex-error').show();
};
return Results;
})();
App = (function() {
function App() {
this.load = bind(this.load, this);
this.loadExample = bind(this.loadExample, this);
this.loadFromHash = bind(this.loadFromHash, this);
this.expression = new Expression({
el: '#expression'
});
this.test_strings = new TestStrings({
el: '#test_strings'
});
this.results = new Results(this.expression, this.test_strings);
$('#example').bind('click', this.loadExample);
if (window.location.hash !== '') {
this.loadFromHash();
}
}
App.prototype.loadFromHash = function() {
var option, ref, regex, test_strings_from_url;
ref = decodeURIComponent(window.location.hash.substr(1)).split("||||"), regex = ref[0], option = ref[1], test_strings_from_url = ref[2];
test_strings_from_url = JSON.parse(test_strings_from_url);
return this.load(regex, option, test_strings_from_url);
};
App.prototype.loadExample = function(event) {
var option, regex, test_strings;
event.preventDefault();
regex = "^(https?)://((?:[A-Z0-9]*\\\.?)*)((?:\\\/?[A-Z0-9])*)";
option = 'i';
test_strings = ['https://github.com/jonmagic/scriptular', 'http://scriptular.com', 'http://www.google.com', 'http://www.guardian.co.uk'];
return this.load(regex, option, test_strings);
};
App.prototype.load = function(regex, option, test_strings) {
$('input[name=expression]').val(regex);
$('input[name=option]').val(option);
$('textarea').val(test_strings.join('\n'));
this.expression.onKeyPress();
this.test_strings.onKeyPress();
return this.results.compile;
};
return App;
})();
window.App = App;
window.$ = $;
}).call(this);