-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapplication.coffee
More file actions
184 lines (147 loc) · 4.84 KB
/
application.coffee
File metadata and controls
184 lines (147 loc) · 4.84 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
180
181
182
183
184
$ = jQuery
class Expression extends Spine.Controller
elements:
'input[name=expression]': 'regexp'
'input[name=option]': 'option'
events:
'keyup input': 'onKeyPress'
onKeyPress: (event) ->
try
@value = @buildRegex @regexp.val(), @option.val()
catch error
@.trigger 'update'
buildRegex: (value, option) ->
new RegExp(value, option)
asUrlPart:() ->
encodeURIComponent(@regexp.val() + "||||" + @option.val())
class TestStrings extends Spine.Controller
elements:
'textarea': 'input'
events:
'keyup textarea': 'onKeyPress'
onKeyPress: (event) ->
@getValues(@input.val())
@.trigger 'update'
getValues: (val) ->
@values = val.split('\n')
asUrlPart:() ->
encodeURIComponent(JSON.stringify(@values))
class Results
constructor: (@expression, @test_strings) ->
@expression.bind 'update', @compile
@test_strings.bind 'update', @compile
compile: =>
$('ul#results').empty()
$('ul#groups').empty()
count = 1
if @expression.regexp.val() != ''
try
new RegExp(@expression.regexp.val())
catch
@showRegexError()
return true
if @expression.regexp.val() == '' && @test_strings.input.val() == ''
@showIntro()
return true
else if @expression.regexp.val() == '' || @test_strings.input.val() == ''
@showError()
return true
else unless @test_strings.values
return true
try
for value in @test_strings.values
matches = value.match(@expression.value)
@matchResults(value, matches)
@matchGroups(value, matches, count)
count += 1
@addShareLink(@expression.asUrlPart(), @test_strings.asUrlPart())
@showOutput()
catch error
@showError()
addShareLink: (expression_url, test_strings_url) ->
url = window.location.protocol + "//" + window.location.host
url += "/#" + expression_url + encodeURIComponent("||||") + test_strings_url
$("#share_link").attr("href", url)
###
escape function from Peter Hoffman found at
http://peter-hoffmann.com/2012/coffeescript-string-interpolation-with-html-escaping.html
###
escape: (s) ->
(''+s).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"')
.replace(/'/g, ''').replace(/\//g,'/')
matchResults: (value, matches) ->
return unless matches
string = @generateMatches(value,@expression.value);
@drawResult string
generateMatches: (value, regex) ->
@escape(value.replace(regex, "~~scriptular_begin_match~~$&~~scriptular_end_match~~"))
.replace(/~~scriptular_begin_match~~/g, '<span>')
.replace(/~~scriptular_end_match~~/g, '</span>')
drawResult: (string) ->
$('ul#results').append("<li>#{string}</li>")
matchGroups: (value, matches, count) ->
return unless matches
$('ul#groups').append("<li id='match_#{count}'><h3>Match #{count}</h3><ol></ol></li>")
if @expression.value.global
for match in matches
return if match == ''
@drawGroup(count, match)
else
for match in matches[1..-1]
return if match == ''
@drawGroup(count, match)
drawGroup: (count, match) ->
match = @escape match
$("ul#groups li#match_#{count} ol").append("<li>#{match}</li>")
showIntro: ->
$('#error').hide()
$('#output').hide()
$('#intro').show()
$('#regex-error').hide()
showError: ->
$('#intro').hide()
$('#output').hide()
$('#error').show()
$('#regex-error').hide()
showOutput: ->
$('#intro').hide()
$('#error').hide()
$('#output').show()
$('#regex-error').hide()
showRegexError: ->
$('#intro').hide()
$('#error').hide()
$('#output').hide()
$('#regex-error').show()
class App
constructor: ->
@expression = new Expression(el: '#expression')
@test_strings = new TestStrings(el: '#test_strings')
@results = new Results(@expression, @test_strings)
$('#example').bind 'click', @loadExample
@loadFromHash() if window.location.hash != ''
loadFromHash: () =>
[regex, option, test_strings_from_url] = decodeURIComponent(window.location.hash.substr(1)).split("||||")
test_strings_from_url = JSON.parse(test_strings_from_url)
@load(regex,option,test_strings_from_url)
loadExample: (event) =>
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'
]
@load(regex,option,test_strings)
load: (regex,option,test_strings) =>
$('input[name=expression]').val(regex)
$('input[name=option]').val(option)
$('textarea').val(test_strings.join('\n'))
@expression.onKeyPress()
@test_strings.onKeyPress()
@results.compile
window.App = App
window.$ = $