visionmedia-lightr 0.0.4 → 0.0.5

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.
@@ -0,0 +1,509 @@
1
+ #--
2
+ # Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ $:.unshift File.dirname(__FILE__)
25
+
26
+ require 'lightr/version'
27
+ require 'lightr/mixins'
28
+ require 'lightr/grammars'
29
+
30
+ module Lightr
31
+ autoload :Common, 'lightr/mixins/common'
32
+ autoload :Grammar, 'lightr/mixins/grammar'
33
+ end
34
+
35
+
36
+ module Lightr
37
+
38
+ ##
39
+ # = Grammar
40
+ #
41
+ # Primary Lightr syntax highlighting mixin, providing
42
+ # the #match method to create your own Lightr DSL.
43
+ #
44
+ # === Examples
45
+ #
46
+ # module Lightr
47
+ # class Ruby
48
+ # include Grammar
49
+ # id = /[\w\d]*/
50
+ # int = /\d[\d_]*/
51
+ # match :comment, /#.*?(\n|\z)/
52
+ # match :constant, /[A-Z]#{id}/
53
+ # match :symbol, /:#{id}/
54
+ # match :number, /-?#{int}(?:\.#{int})?/
55
+ # match nil, /\n|\s|.+?/
56
+ # ...
57
+ #
58
+
59
+ module Grammar
60
+ def self.included base
61
+ base.extend ClassMethods
62
+ base.send :include, InstanceMethods
63
+ end
64
+
65
+ module ClassMethods
66
+ def match name, regexp
67
+ (@matchers ||= []) << [name, regexp]
68
+ end
69
+
70
+ def parse input
71
+ new(input).parse!
72
+ end
73
+ end
74
+
75
+ module InstanceMethods
76
+ attr_reader :input, :output
77
+ alias :to_s :output
78
+
79
+ def initialize input
80
+ @input, @output = input, ''
81
+ @matchers = self.class.instance_variable_get :"@matchers"
82
+ @regexp = Regexp.union *@matchers.transpose[1]
83
+ end
84
+
85
+ def escape string
86
+ string.
87
+ gsub(/&/, '&amp;').
88
+ gsub(/</, '&lt;').
89
+ gsub(/>/, '&gt;').
90
+ gsub(/"/, '&quot;')
91
+ end
92
+
93
+ def wrap string
94
+ @matchers.each do |name, regexp|
95
+ return escape(string) if name.nil? and string =~ regexp
96
+ return %(<span class="#{name}">#{escape(string)}</span>) if string =~ regexp
97
+ end
98
+ end
99
+
100
+ def parse!
101
+ @input.dup.gsub @regexp do |match|
102
+ @output << wrap(match)
103
+ end
104
+ self
105
+ end
106
+ end
107
+
108
+ end
109
+ end
110
+
111
+ module Lightr
112
+ autoload :Ruby, 'lightr/grammars/ruby'
113
+ autoload :JavaScript, 'lightr/grammars/javascript'
114
+ autoload :C, 'lightr/grammars/c'
115
+ end
116
+
117
+
118
+ module Lightr
119
+ class JavaScript
120
+ include Grammar
121
+ id = /\w[\w\d]*/
122
+ int = /\d+/
123
+ match :keyword, /(if|else|throw|break|case|catch|continue|default|delete|do|double|true|false|final|finally|float|for|function|int|instanceof|in|long|new|null|return|short|switch|throw|try|typeof|var|void|while|with)(?:\n|\z| )/
124
+ match :string, /'.*?'|".*?"/
125
+ match :comment, %r{//.*?(\n|\z)}
126
+ match :regexp, %r{/.*?/[\w]*}
127
+ match :self, /this/
128
+ match :capitalized, /[A-Z]#{id}/
129
+ match :number, /-?#{int}(?:\.#{int})?/
130
+ match nil, /\n|\s|.+?/
131
+ end
132
+ end
133
+
134
+ #--
135
+ # Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
136
+ #
137
+ # Permission is hereby granted, free of charge, to any person obtaining
138
+ # a copy of this software and associated documentation files (the
139
+ # "Software"), to deal in the Software without restriction, including
140
+ # without limitation the rights to use, copy, modify, merge, publish,
141
+ # distribute, sublicense, and/or sell copies of the Software, and to
142
+ # permit persons to whom the Software is furnished to do so, subject to
143
+ # the following conditions:
144
+ #
145
+ # The above copyright notice and this permission notice shall be
146
+ # included in all copies or substantial portions of the Software.
147
+ #
148
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
149
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
150
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
151
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
152
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
153
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
154
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
155
+ #++
156
+
157
+ $:.unshift File.dirname(__FILE__)
158
+
159
+ require 'lightr/version'
160
+ require 'lightr/mixins'
161
+ require 'lightr/grammars'
162
+
163
+ module Lightr
164
+ autoload :Common, 'lightr/mixins/common'
165
+ autoload :Grammar, 'lightr/mixins/grammar'
166
+ end
167
+
168
+
169
+ module Lightr
170
+
171
+ ##
172
+ # = Grammar
173
+ #
174
+ # Primary Lightr syntax highlighting mixin, providing
175
+ # the #match method to create your own Lightr DSL.
176
+ #
177
+ # === Examples
178
+ #
179
+ # module Lightr
180
+ # class Ruby
181
+ # include Grammar
182
+ # id = /[\w\d]*/
183
+ # int = /\d[\d_]*/
184
+ # match :comment, /#.*?(\n|\z)/
185
+ # match :constant, /[A-Z]#{id}/
186
+ # match :symbol, /:#{id}/
187
+ # match :number, /-?#{int}(?:\.#{int})?/
188
+ # match nil, /\n|\s|.+?/
189
+ # ...
190
+ #
191
+
192
+ module Grammar
193
+ def self.included base
194
+ base.extend ClassMethods
195
+ base.send :include, InstanceMethods
196
+ end
197
+
198
+ module ClassMethods
199
+ def match name, regexp
200
+ (@matchers ||= []) << [name, regexp]
201
+ end
202
+
203
+ def parse input
204
+ new(input).parse!
205
+ end
206
+ end
207
+
208
+ module InstanceMethods
209
+ attr_reader :input, :output
210
+ alias :to_s :output
211
+
212
+ def initialize input
213
+ @input, @output = input, ''
214
+ @matchers = self.class.instance_variable_get :"@matchers"
215
+ @regexp = Regexp.union *@matchers.transpose[1]
216
+ end
217
+
218
+ def escape string
219
+ string.
220
+ gsub(/&/, '&amp;').
221
+ gsub(/</, '&lt;').
222
+ gsub(/>/, '&gt;').
223
+ gsub(/"/, '&quot;')
224
+ end
225
+
226
+ def wrap string
227
+ @matchers.each do |name, regexp|
228
+ return escape(string) if name.nil? and string =~ regexp
229
+ return %(<span class="#{name}">#{escape(string)}</span>) if string =~ regexp
230
+ end
231
+ end
232
+
233
+ def parse!
234
+ @input.dup.gsub @regexp do |match|
235
+ @output << wrap(match)
236
+ end
237
+ self
238
+ end
239
+ end
240
+
241
+ end
242
+ end
243
+
244
+ module Lightr
245
+ autoload :Ruby, 'lightr/grammars/ruby'
246
+ autoload :JavaScript, 'lightr/grammars/javascript'
247
+ autoload :C, 'lightr/grammars/c'
248
+ end
249
+
250
+
251
+ module Lightr
252
+ class JavaScript
253
+ include Grammar
254
+ id = /\w[\w\d]*/
255
+ int = /\d+/
256
+ match :keyword, /(if|else|throw|break|case|catch|continue|default|delete|do|double|true|false|final|finally|float|for|function|int|instanceof|in|long|new|null|return|short|switch|throw|try|typeof|var|void|while|with)(?:\n|\z| )/
257
+ match :string, /'.*?'|".*?"/
258
+ match :comment, %r{//.*?(\n|\z)}
259
+ match :regexp, %r{/.*?/[\w]*}
260
+ match :self, /this/
261
+ match :capitalized, /[A-Z]#{id}/
262
+ match :number, /-?#{int}(?:\.#{int})?/
263
+ match nil, /\n|\s|.+?/
264
+ end
265
+ end
266
+
267
+ #--
268
+ # Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
269
+ #
270
+ # Permission is hereby granted, free of charge, to any person obtaining
271
+ # a copy of this software and associated documentation files (the
272
+ # "Software"), to deal in the Software without restriction, including
273
+ # without limitation the rights to use, copy, modify, merge, publish,
274
+ # distribute, sublicense, and/or sell copies of the Software, and to
275
+ # permit persons to whom the Software is furnished to do so, subject to
276
+ # the following conditions:
277
+ #
278
+ # The above copyright notice and this permission notice shall be
279
+ # included in all copies or substantial portions of the Software.
280
+ #
281
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
282
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
283
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
284
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
285
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
286
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
287
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288
+ #++
289
+
290
+ $:.unshift File.dirname(__FILE__)
291
+
292
+ require 'lightr/version'
293
+ require 'lightr/mixins'
294
+ require 'lightr/grammars'
295
+
296
+ module Lightr
297
+ autoload :Common, 'lightr/mixins/common'
298
+ autoload :Grammar, 'lightr/mixins/grammar'
299
+ end
300
+
301
+
302
+ module Lightr
303
+
304
+ ##
305
+ # = Grammar
306
+ #
307
+ # Primary Lightr syntax highlighting mixin, providing
308
+ # the #match method to create your own Lightr DSL.
309
+ #
310
+ # === Examples
311
+ #
312
+ # module Lightr
313
+ # class Ruby
314
+ # include Grammar
315
+ # id = /[\w\d]*/
316
+ # int = /\d[\d_]*/
317
+ # match :comment, /#.*?(\n|\z)/
318
+ # match :constant, /[A-Z]#{id}/
319
+ # match :symbol, /:#{id}/
320
+ # match :number, /-?#{int}(?:\.#{int})?/
321
+ # match nil, /\n|\s|.+?/
322
+ # ...
323
+ #
324
+
325
+ module Grammar
326
+ def self.included base
327
+ base.extend ClassMethods
328
+ base.send :include, InstanceMethods
329
+ end
330
+
331
+ module ClassMethods
332
+ def match name, regexp
333
+ (@matchers ||= []) << [name, regexp]
334
+ end
335
+
336
+ def parse input
337
+ new(input).parse!
338
+ end
339
+ end
340
+
341
+ module InstanceMethods
342
+ attr_reader :input, :output
343
+ alias :to_s :output
344
+
345
+ def initialize input
346
+ @input, @output = input, ''
347
+ @matchers = self.class.instance_variable_get :"@matchers"
348
+ @regexp = Regexp.union *@matchers.transpose[1]
349
+ end
350
+
351
+ def escape string
352
+ string.
353
+ gsub(/&/, '&amp;').
354
+ gsub(/</, '&lt;').
355
+ gsub(/>/, '&gt;').
356
+ gsub(/"/, '&quot;')
357
+ end
358
+
359
+ def wrap string
360
+ @matchers.each do |name, regexp|
361
+ return escape(string) if name.nil? and string =~ regexp
362
+ return %(<span class="#{name}">#{escape(string)}</span>) if string =~ regexp
363
+ end
364
+ end
365
+
366
+ def parse!
367
+ @input.dup.gsub @regexp do |match|
368
+ @output << wrap(match)
369
+ end
370
+ self
371
+ end
372
+ end
373
+
374
+ end
375
+ end
376
+
377
+ module Lightr
378
+ autoload :Ruby, 'lightr/grammars/ruby'
379
+ autoload :JavaScript, 'lightr/grammars/javascript'
380
+ autoload :C, 'lightr/grammars/c'
381
+ end
382
+
383
+
384
+ module Lightr
385
+ class JavaScript
386
+ include Grammar
387
+ id = /\w[\w\d]*/
388
+ int = /\d+/
389
+ match :keyword, /(if|else|throw|break|case|catch|continue|default|delete|do|double|true|false|final|finally|float|for|function|int|instanceof|in|long|new|null|return|short|switch|throw|try|typeof|var|void|while|with)(?:\n|\z| )/
390
+ match :string, /'.*?'|".*?"/
391
+ match :comment, %r{//.*?(\n|\z)}
392
+ match :regexp, %r{/.*?/[\w]*}
393
+ match :self, /this/
394
+ match :capitalized, /[A-Z]#{id}/
395
+ match :number, /-?#{int}(?:\.#{int})?/
396
+ match nil, /\n|\s|.+?/
397
+ end
398
+ end
399
+
400
+ #--
401
+ # Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
402
+ #
403
+ # Permission is hereby granted, free of charge, to any person obtaining
404
+ # a copy of this software and associated documentation files (the
405
+ # "Software"), to deal in the Software without restriction, including
406
+ # without limitation the rights to use, copy, modify, merge, publish,
407
+ # distribute, sublicense, and/or sell copies of the Software, and to
408
+ # permit persons to whom the Software is furnished to do so, subject to
409
+ # the following conditions:
410
+ #
411
+ # The above copyright notice and this permission notice shall be
412
+ # included in all copies or substantial portions of the Software.
413
+ #
414
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
415
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
416
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
417
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
418
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
419
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
420
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
421
+ #++
422
+
423
+ $:.unshift File.dirname(__FILE__)
424
+
425
+ require 'lightr/version'
426
+ require 'lightr/mixins'
427
+ require 'lightr/grammars'
428
+
429
+ module Lightr
430
+ autoload :Common, 'lightr/mixins/common'
431
+ autoload :Grammar, 'lightr/mixins/grammar'
432
+ end
433
+
434
+
435
+ module Lightr
436
+
437
+ ##
438
+ # = Grammar
439
+ #
440
+ # Primary Lightr syntax highlighting mixin, providing
441
+ # the #match method to create your own Lightr DSL.
442
+ #
443
+ # === Examples
444
+ #
445
+ # module Lightr
446
+ # class Ruby
447
+ # include Grammar
448
+ # id = /[\w\d]*/
449
+ # int = /\d[\d_]*/
450
+ # match :comment, /#.*?(\n|\z)/
451
+ # match :constant, /[A-Z]#{id}/
452
+ # match :symbol, /:#{id}/
453
+ # match :number, /-?#{int}(?:\.#{int})?/
454
+ # match nil, /\n|\s|.+?/
455
+ # ...
456
+ #
457
+
458
+ module Grammar
459
+ def self.included base
460
+ base.extend ClassMethods
461
+ base.send :include, InstanceMethods
462
+ end
463
+
464
+ module ClassMethods
465
+ def match name, regexp
466
+ (@matchers ||= []) << [name, regexp]
467
+ end
468
+
469
+ def parse input
470
+ new(input).parse!
471
+ end
472
+ end
473
+
474
+ module InstanceMethods
475
+ attr_reader :input, :output
476
+ alias :to_s :output
477
+
478
+ def initialize input
479
+ @input, @output = input, ''
480
+ @matchers = self.class.instance_variable_get :"@matchers"
481
+ @regexp = Regexp.union *@matchers.transpose[1]
482
+ end
483
+
484
+ def escape string
485
+ string.
486
+ gsub(/&/, '&amp;').
487
+ gsub(/</, '&lt;').
488
+ gsub(/>/, '&gt;').
489
+ gsub(/"/, '&quot;')
490
+ end
491
+
492
+ def wrap string
493
+ @matchers.each do |name, regexp|
494
+ return escape(string) if name.nil? and string =~ regexp
495
+ return %(<span class="#{name}">#{escape(string)}</span>) if string =~ regexp
496
+ end
497
+ end
498
+
499
+ def parse!
500
+ @input.dup.gsub @regexp do |match|
501
+ @output << wrap(match)
502
+ end
503
+ self
504
+ end
505
+ end
506
+
507
+ end
508
+ end
509
+