rubysl-erb 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc2021bd6015360e2b32d7145cc13e3b85e8f942
4
- data.tar.gz: 3c29e3eb3330205331966c8e1d15f2c531b280c3
3
+ metadata.gz: d2c7f2c992f92371c1ba30649ffc4093d70f7f2a
4
+ data.tar.gz: f4fd159ff6e3a39beb2c084ab2240231ce2611b7
5
5
  SHA512:
6
- metadata.gz: 6060db64dda4dcb7b42709f7d8669c6a6ae75521cf276b1f2d4f449b827713e38663343fe129cf45af0fb0d0011cf7276497950effb13ed0718c7bd3debf0f5b
7
- data.tar.gz: 4f2720329d61c51b93f24b9b160980f913bddedc720b8bc75f0f3bcaa15c6cf17b75dda028747bd294306afd5197825858870d6ae66997ad4e59ece464fe66e3
6
+ metadata.gz: 8b1cd6b883f1df27fc6d651f7b9da5f2c2fff629360dc5d385df0ec626ed009559f31a1ed62b821dc17516aef1310b7fd6a5bf6a73889c201b19ea179ecd591b
7
+ data.tar.gz: 65e5fab7e463c69942265d9dc6ddccb3d4a4e6c63496c8bc7dd6398ec0a33c66252b6191336559d8b267b9d178cd74598108d3fd4c2cea0da9d390dc6a3cdc80
@@ -1,8 +1,7 @@
1
1
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem --version
5
- - gem install rubysl-bundler
6
- script: bundle exec mspec spec
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
7
5
  rvm:
8
- - rbx-nightly-18mode
6
+ - 1.9.3
7
+ - rbx-nightly-19mode
@@ -1,7 +1,8 @@
1
+ # -*- coding: us-ascii -*-
1
2
  # = ERB -- Ruby Templating
2
3
  #
3
4
  # Author:: Masatoshi SEKI
4
- # Documentation:: James Edward Gray II and Gavin Sinclair
5
+ # Documentation:: James Edward Gray II, Gavin Sinclair, and Simon Chiang
5
6
  #
6
7
  # See ERB for primary documentation and ERB::Util for a couple of utility
7
8
  # routines.
@@ -10,6 +11,8 @@
10
11
  #
11
12
  # You can redistribute it and/or modify it under the same terms as Ruby.
12
13
 
14
+ require "cgi/util"
15
+
13
16
  #
14
17
  # = ERB -- Ruby Templating
15
18
  #
@@ -20,7 +23,7 @@
20
23
  # purposes of generating document information details and/or flow control.
21
24
  #
22
25
  # A very simple example is this:
23
- #
26
+ #
24
27
  # require 'erb'
25
28
  #
26
29
  # x = 42
@@ -58,6 +61,24 @@
58
61
  #
59
62
  # See the ERB.new and ERB#result methods for more detail.
60
63
  #
64
+ # == Character encodings
65
+ #
66
+ # ERB (or ruby code generated by ERB) returns a string in the same
67
+ # character encoding as the input string. When the input string has
68
+ # a magic comment, however, it returns a string in the encoding specified
69
+ # by the magic comment.
70
+ #
71
+ # # -*- coding: UTF-8 -*-
72
+ # require 'erb'
73
+ #
74
+ # template = ERB.new <<EOF
75
+ # <%#-*- coding: Big5 -*-%>
76
+ # \_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
77
+ # EOF
78
+ # puts template.result
79
+ #
80
+ # <em>Prints:</em> \_\_ENCODING\_\_ is Big5.
81
+ #
61
82
  #
62
83
  # == Examples
63
84
  #
@@ -68,39 +89,39 @@
68
89
  # <tt>%q{...}</tt> to avoid trouble with the backslash.
69
90
  #
70
91
  # require "erb"
71
- #
92
+ #
72
93
  # # Create template.
73
94
  # template = %q{
74
95
  # From: James Edward Gray II <james@grayproductions.net>
75
96
  # To: <%= to %>
76
97
  # Subject: Addressing Needs
77
- #
98
+ #
78
99
  # <%= to[/\w+/] %>:
79
- #
100
+ #
80
101
  # Just wanted to send a quick note assuring that your needs are being
81
102
  # addressed.
82
- #
103
+ #
83
104
  # I want you to know that my team will keep working on the issues,
84
105
  # especially:
85
- #
106
+ #
86
107
  # <%# ignore numerous minor requests -- focus on priorities %>
87
108
  # % priorities.each do |priority|
88
109
  # * <%= priority %>
89
110
  # % end
90
- #
111
+ #
91
112
  # Thanks for your patience.
92
- #
113
+ #
93
114
  # James Edward Gray II
94
115
  # }.gsub(/^ /, '')
95
- #
116
+ #
96
117
  # message = ERB.new(template, 0, "%<>")
97
- #
118
+ #
98
119
  # # Set up template data.
99
120
  # to = "Community Spokesman <spokesman@ruby_community.org>"
100
121
  # priorities = [ "Run Ruby Quiz",
101
122
  # "Document Modules",
102
123
  # "Answer Questions on Ruby Talk" ]
103
- #
124
+ #
104
125
  # # Produce result.
105
126
  # email = message.result
106
127
  # puts email
@@ -110,19 +131,19 @@
110
131
  # From: James Edward Gray II <james@grayproductions.net>
111
132
  # To: Community Spokesman <spokesman@ruby_community.org>
112
133
  # Subject: Addressing Needs
113
- #
134
+ #
114
135
  # Community:
115
- #
136
+ #
116
137
  # Just wanted to send a quick note assuring that your needs are being addressed.
117
- #
138
+ #
118
139
  # I want you to know that my team will keep working on the issues, especially:
119
- #
140
+ #
120
141
  # * Run Ruby Quiz
121
142
  # * Document Modules
122
143
  # * Answer Questions on Ruby Talk
123
- #
144
+ #
124
145
  # Thanks for your patience.
125
- #
146
+ #
126
147
  # James Edward Gray II
127
148
  #
128
149
  # === Ruby in HTML
@@ -132,7 +153,7 @@
132
153
  # variables in the Product object can be resolved.
133
154
  #
134
155
  # require "erb"
135
- #
156
+ #
136
157
  # # Build template data class.
137
158
  # class Product
138
159
  # def initialize( code, name, desc, cost )
@@ -140,37 +161,37 @@
140
161
  # @name = name
141
162
  # @desc = desc
142
163
  # @cost = cost
143
- #
164
+ #
144
165
  # @features = [ ]
145
166
  # end
146
- #
167
+ #
147
168
  # def add_feature( feature )
148
169
  # @features << feature
149
170
  # end
150
- #
171
+ #
151
172
  # # Support templating of member data.
152
173
  # def get_binding
153
174
  # binding
154
175
  # end
155
- #
176
+ #
156
177
  # # ...
157
178
  # end
158
- #
179
+ #
159
180
  # # Create template.
160
181
  # template = %{
161
182
  # <html>
162
183
  # <head><title>Ruby Toys -- <%= @name %></title></head>
163
184
  # <body>
164
- #
185
+ #
165
186
  # <h1><%= @name %> (<%= @code %>)</h1>
166
187
  # <p><%= @desc %></p>
167
- #
188
+ #
168
189
  # <ul>
169
190
  # <% @features.each do |f| %>
170
191
  # <li><b><%= f %></b></li>
171
192
  # <% end %>
172
193
  # </ul>
173
- #
194
+ #
174
195
  # <p>
175
196
  # <% if @cost < 10 %>
176
197
  # <b>Only <%= @cost %>!!!</b>
@@ -178,13 +199,13 @@
178
199
  # Call for a price, today!
179
200
  # <% end %>
180
201
  # </p>
181
- #
202
+ #
182
203
  # </body>
183
204
  # </html>
184
205
  # }.gsub(/^ /, '')
185
- #
206
+ #
186
207
  # rhtml = ERB.new(template)
187
- #
208
+ #
188
209
  # # Set up template data.
189
210
  # toy = Product.new( "TZ-1002",
190
211
  # "Rubysapien",
@@ -195,7 +216,7 @@
195
216
  # toy.add_feature("Karate-Chop Action!!!")
196
217
  # toy.add_feature("Matz signature on left leg.")
197
218
  # toy.add_feature("Gem studded eyes... Rubies, of course!")
198
- #
219
+ #
199
220
  # # Produce result.
200
221
  # rhtml.run(toy.get_binding)
201
222
  #
@@ -204,10 +225,10 @@
204
225
  # <html>
205
226
  # <head><title>Ruby Toys -- Rubysapien</title></head>
206
227
  # <body>
207
- #
228
+ #
208
229
  # <h1>Rubysapien (TZ-1002)</h1>
209
230
  # <p>Geek's Best Friend! Responds to Ruby commands...</p>
210
- #
231
+ #
211
232
  # <ul>
212
233
  # <li><b>Listens for verbal commands in the Ruby language!</b></li>
213
234
  # <li><b>Ignores Perl, Java, and all C variants.</b></li>
@@ -215,15 +236,15 @@
215
236
  # <li><b>Matz signature on left leg.</b></li>
216
237
  # <li><b>Gem studded eyes... Rubies, of course!</b></li>
217
238
  # </ul>
218
- #
239
+ #
219
240
  # <p>
220
241
  # Call for a price, today!
221
242
  # </p>
222
- #
243
+ #
223
244
  # </body>
224
245
  # </html>
225
246
  #
226
- #
247
+ #
227
248
  # == Notes
228
249
  #
229
250
  # There are a variety of templating solutions available in various Ruby projects:
@@ -236,7 +257,7 @@
236
257
  # Rails, the web application framework, uses ERB to create views.
237
258
  #
238
259
  class ERB
239
- Revision = '$Date: 2009-02-23 09:44:50 -0800 (Mon, 23 Feb 2009) $' #'
260
+ Revision = '$Date:: $' # :nodoc: #'
240
261
 
241
262
  # Returns revision information for the erb.rb module.
242
263
  def self.version
@@ -247,6 +268,76 @@ end
247
268
  #--
248
269
  # ERB::Compiler
249
270
  class ERB
271
+ # = ERB::Compiler
272
+ #
273
+ # Compiles ERB templates into Ruby code; the compiled code produces the
274
+ # template result when evaluated. ERB::Compiler provides hooks to define how
275
+ # generated output is handled.
276
+ #
277
+ # Internally ERB does something like this to generate the code returned by
278
+ # ERB#src:
279
+ #
280
+ # compiler = ERB::Compiler.new('<>')
281
+ # compiler.pre_cmd = ["_erbout=''"]
282
+ # compiler.put_cmd = "_erbout.concat"
283
+ # compiler.insert_cmd = "_erbout.concat"
284
+ # compiler.post_cmd = ["_erbout"]
285
+ #
286
+ # code, enc = compiler.compile("Got <%= obj %>!\n")
287
+ # puts code
288
+ #
289
+ # <i>Generates</i>:
290
+ #
291
+ # #coding:UTF-8
292
+ # _erbout=''; _erbout.concat "Got "; _erbout.concat(( obj ).to_s); _erbout.concat "!\n"; _erbout
293
+ #
294
+ # By default the output is sent to the print method. For example:
295
+ #
296
+ # compiler = ERB::Compiler.new('<>')
297
+ # code, enc = compiler.compile("Got <%= obj %>!\n")
298
+ # puts code
299
+ #
300
+ # <i>Generates</i>:
301
+ #
302
+ # #coding:UTF-8
303
+ # print "Got "; print(( obj ).to_s); print "!\n"
304
+ #
305
+ # == Evaluation
306
+ #
307
+ # The compiled code can be used in any context where the names in the code
308
+ # correctly resolve. Using the last example, each of these print 'Got It!'
309
+ #
310
+ # Evaluate using a variable:
311
+ #
312
+ # obj = 'It'
313
+ # eval code
314
+ #
315
+ # Evaluate using an input:
316
+ #
317
+ # mod = Module.new
318
+ # mod.module_eval %{
319
+ # def get(obj)
320
+ # #{code}
321
+ # end
322
+ # }
323
+ # extend mod
324
+ # get('It')
325
+ #
326
+ # Evaluate using an accessor:
327
+ #
328
+ # klass = Class.new Object
329
+ # klass.class_eval %{
330
+ # attr_accessor :obj
331
+ # def initialize(obj)
332
+ # @obj = obj
333
+ # end
334
+ # def get_it
335
+ # #{code}
336
+ # end
337
+ # }
338
+ # klass.new('It').get_it
339
+ #
340
+ # Good! See also ERB#def_method, ERB#def_module, and ERB#def_class.
250
341
  class Compiler # :nodoc:
251
342
  class PercentLine # :nodoc:
252
343
  def initialize(str)
@@ -263,21 +354,21 @@ class ERB
263
354
  class Scanner # :nodoc:
264
355
  @scanner_map = {}
265
356
  def self.regist_scanner(klass, trim_mode, percent)
266
- @scanner_map[[trim_mode, percent]] = klass
357
+ @scanner_map[[trim_mode, percent]] = klass
267
358
  end
268
359
 
269
360
  def self.default_scanner=(klass)
270
- @default_scanner = klass
361
+ @default_scanner = klass
271
362
  end
272
363
 
273
364
  def self.make_scanner(src, trim_mode, percent)
274
- klass = @scanner_map.fetch([trim_mode, percent], @default_scanner)
275
- klass.new(src, trim_mode, percent)
365
+ klass = @scanner_map.fetch([trim_mode, percent], @default_scanner)
366
+ klass.new(src, trim_mode, percent)
276
367
  end
277
368
 
278
369
  def initialize(src, trim_mode, percent)
279
- @src = src
280
- @stag = nil
370
+ @src = src
371
+ @stag = nil
281
372
  end
282
373
  attr_accessor :stag
283
374
 
@@ -286,44 +377,44 @@ class ERB
286
377
 
287
378
  class TrimScanner < Scanner # :nodoc:
288
379
  def initialize(src, trim_mode, percent)
289
- super
290
- @trim_mode = trim_mode
291
- @percent = percent
292
- if @trim_mode == '>'
293
- @scan_line = self.method(:trim_line1)
294
- elsif @trim_mode == '<>'
295
- @scan_line = self.method(:trim_line2)
296
- elsif @trim_mode == '-'
297
- @scan_line = self.method(:explicit_trim_line)
298
- else
299
- @scan_line = self.method(:scan_line)
300
- end
380
+ super
381
+ @trim_mode = trim_mode
382
+ @percent = percent
383
+ if @trim_mode == '>'
384
+ @scan_line = self.method(:trim_line1)
385
+ elsif @trim_mode == '<>'
386
+ @scan_line = self.method(:trim_line2)
387
+ elsif @trim_mode == '-'
388
+ @scan_line = self.method(:explicit_trim_line)
389
+ else
390
+ @scan_line = self.method(:scan_line)
391
+ end
301
392
  end
302
393
  attr_accessor :stag
303
-
394
+
304
395
  def scan(&block)
305
- @stag = nil
306
- if @percent
307
- @src.each do |line|
308
- percent_line(line, &block)
309
- end
310
- else
396
+ @stag = nil
397
+ if @percent
398
+ @src.each_line do |line|
399
+ percent_line(line, &block)
400
+ end
401
+ else
311
402
  @scan_line.call(@src, &block)
312
- end
313
- nil
403
+ end
404
+ nil
314
405
  end
315
406
 
316
407
  def percent_line(line, &block)
317
- if @stag || line[0] != ?%
318
- return @scan_line.call(line, &block)
319
- end
320
-
321
- line[0] = ''
322
- if line[0] == ?%
323
- @scan_line.call(line, &block)
324
- else
408
+ if @stag || line[0] != ?%
409
+ return @scan_line.call(line, &block)
410
+ end
411
+
412
+ line[0] = ''
413
+ if line[0] == ?%
414
+ @scan_line.call(line, &block)
415
+ else
325
416
  yield(PercentLine.new(line.chomp))
326
- end
417
+ end
327
418
  end
328
419
 
329
420
  def scan_line(line)
@@ -332,7 +423,7 @@ class ERB
332
423
  next if token.empty?
333
424
  yield(token)
334
425
  end
335
- end
426
+ end
336
427
  end
337
428
 
338
429
  def trim_line1(line)
@@ -346,11 +437,11 @@ class ERB
346
437
  yield(token)
347
438
  end
348
439
  end
349
- end
440
+ end
350
441
  end
351
442
 
352
443
  def trim_line2(line)
353
- head = nil
444
+ head = nil
354
445
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
355
446
  tokens.each do |token|
356
447
  next if token.empty?
@@ -368,7 +459,7 @@ class ERB
368
459
  head = nil if token == "\n"
369
460
  end
370
461
  end
371
- end
462
+ end
372
463
  end
373
464
 
374
465
  def explicit_trim_line(line)
@@ -391,7 +482,7 @@ class ERB
391
482
 
392
483
  ERB_STAG = %w(<%= <%# <%)
393
484
  def is_erb_stag?(s)
394
- ERB_STAG.member?(s)
485
+ ERB_STAG.member?(s)
395
486
  end
396
487
  end
397
488
 
@@ -404,10 +495,10 @@ class ERB
404
495
  next if token.empty?
405
496
  yield(token)
406
497
  end
407
- end
498
+ end
408
499
  end
409
500
  end
410
-
501
+
411
502
  Scanner.regist_scanner(SimpleScanner, nil, false)
412
503
 
413
504
  begin
@@ -427,23 +518,23 @@ class ERB
427
518
  Scanner.regist_scanner(SimpleScanner2, nil, false)
428
519
 
429
520
  class ExplicitScanner < Scanner # :nodoc:
430
- def scan
521
+ def scan
431
522
  stag_reg = /(.*?)(^[ \t]*<%-|<%%|<%=|<%#|<%-|<%|\z)/m
432
523
  etag_reg = /(.*?)(%%>|-%>|%>|\z)/m
433
524
  scanner = StringScanner.new(@src)
434
525
  while ! scanner.eos?
435
- scanner.scan(@stag ? etag_reg : stag_reg)
526
+ scanner.scan(@stag ? etag_reg : stag_reg)
436
527
  yield(scanner[1])
437
528
 
438
529
  elem = scanner[2]
439
530
  if /[ \t]*<%-/ =~ elem
440
531
  yield('<%')
441
532
  elsif elem == '-%>'
442
- yield('%>')
443
- yield(:cr) if scanner.scan(/(\n|\z)/)
444
- else
445
- yield(elem)
446
- end
533
+ yield('%>')
534
+ yield(:cr) if scanner.scan(/(\n|\z)/)
535
+ else
536
+ yield(elem)
537
+ end
447
538
  end
448
539
  end
449
540
  end
@@ -453,37 +544,37 @@ class ERB
453
544
  end
454
545
 
455
546
  class Buffer # :nodoc:
456
- def initialize(compiler)
457
- @compiler = compiler
458
- @line = []
459
- @script = ""
460
- @compiler.pre_cmd.each do |x|
461
- push(x)
462
- end
547
+ def initialize(compiler, enc=nil)
548
+ @compiler = compiler
549
+ @line = []
550
+ @script = enc ? "#coding:#{enc.to_s}\n" : ""
551
+ @compiler.pre_cmd.each do |x|
552
+ push(x)
553
+ end
463
554
  end
464
555
  attr_reader :script
465
556
 
466
557
  def push(cmd)
467
- @line << cmd
558
+ @line << cmd
468
559
  end
469
-
560
+
470
561
  def cr
471
- @script << (@line.join('; '))
472
- @line = []
473
- @script << "\n"
562
+ @script << (@line.join('; '))
563
+ @line = []
564
+ @script << "\n"
474
565
  end
475
-
566
+
476
567
  def close
477
- return unless @line
478
- @compiler.post_cmd.each do |x|
479
- push(x)
480
- end
481
- @script << (@line.join('; '))
482
- @line = nil
568
+ return unless @line
569
+ @compiler.post_cmd.each do |x|
570
+ push(x)
571
+ end
572
+ @script << (@line.join('; '))
573
+ @line = nil
483
574
  end
484
575
  end
485
576
 
486
- def content_dump(s)
577
+ def content_dump(s) # :nodoc:
487
578
  n = s.count("\n")
488
579
  if n > 0
489
580
  s.dump + "\n" * n
@@ -492,95 +583,111 @@ class ERB
492
583
  end
493
584
  end
494
585
 
586
+ def add_put_cmd(out, content)
587
+ out.push("#{@put_cmd} #{content_dump(content)}")
588
+ end
589
+
590
+ def add_insert_cmd(out, content)
591
+ out.push("#{@insert_cmd}((#{content}).to_s)")
592
+ end
593
+
594
+ # Compiles an ERB template into Ruby code. Returns an array of the code
595
+ # and encoding like ["code", Encoding].
495
596
  def compile(s)
496
- out = Buffer.new(self)
597
+ enc = s.encoding
598
+ raise ArgumentError, "#{enc} is not ASCII compatible" if enc.dummy?
599
+ s = s.dup.force_encoding("ASCII-8BIT") # don't use constant Enoding::ASCII_8BIT for miniruby
600
+ enc = detect_magic_comment(s) || enc
601
+ out = Buffer.new(self, enc)
497
602
 
498
603
  content = ''
499
604
  scanner = make_scanner(s)
500
605
  scanner.scan do |token|
501
- next if token.nil?
606
+ next if token.nil?
502
607
  next if token == ''
503
- if scanner.stag.nil?
504
- case token
608
+ if scanner.stag.nil?
609
+ case token
505
610
  when PercentLine
506
- out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
507
- content = ''
611
+ add_put_cmd(out, content) if content.size > 0
612
+ content = ''
508
613
  out.push(token.to_s)
509
614
  out.cr
510
- when :cr
511
- out.cr
512
- when '<%', '<%=', '<%#'
513
- scanner.stag = token
514
- out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
515
- content = ''
516
- when "\n"
517
- content << "\n"
518
- out.push("#{@put_cmd} #{content_dump(content)}")
519
- content = ''
520
- when '<%%'
521
- content << '<%'
522
- else
523
- content << token
524
- end
525
- else
526
- case token
527
- when '%>'
528
- case scanner.stag
529
- when '<%'
530
- if content[-1] == ?\n
531
- content.chop!
532
- out.push(content)
533
- out.cr
534
- else
535
- out.push(content)
536
- end
537
- when '<%='
538
- out.push("#{@insert_cmd}((#{content}).to_s)")
539
- when '<%#'
540
- # out.push("# #{content_dump(content)}")
541
- end
542
- scanner.stag = nil
543
- content = ''
544
- when '%%>'
545
- content << '%>'
546
- else
547
- content << token
548
- end
549
- end
615
+ when :cr
616
+ out.cr
617
+ when '<%', '<%=', '<%#'
618
+ scanner.stag = token
619
+ add_put_cmd(out, content) if content.size > 0
620
+ content = ''
621
+ when "\n"
622
+ content << "\n"
623
+ add_put_cmd(out, content)
624
+ content = ''
625
+ when '<%%'
626
+ content << '<%'
627
+ else
628
+ content << token
629
+ end
630
+ else
631
+ case token
632
+ when '%>'
633
+ case scanner.stag
634
+ when '<%'
635
+ if content[-1] == ?\n
636
+ content.chop!
637
+ out.push(content)
638
+ out.cr
639
+ else
640
+ out.push(content)
641
+ end
642
+ when '<%='
643
+ add_insert_cmd(out, content)
644
+ when '<%#'
645
+ # out.push("# #{content_dump(content)}")
646
+ end
647
+ scanner.stag = nil
648
+ content = ''
649
+ when '%%>'
650
+ content << '%>'
651
+ else
652
+ content << token
653
+ end
654
+ end
550
655
  end
551
- out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
656
+ add_put_cmd(out, content) if content.size > 0
552
657
  out.close
553
- out.script
658
+ return out.script, enc
554
659
  end
555
660
 
556
- def prepare_trim_mode(mode)
661
+ def prepare_trim_mode(mode) # :nodoc:
557
662
  case mode
558
663
  when 1
559
- return [false, '>']
664
+ return [false, '>']
560
665
  when 2
561
- return [false, '<>']
666
+ return [false, '<>']
562
667
  when 0
563
- return [false, nil]
668
+ return [false, nil]
564
669
  when String
565
- perc = mode.include?('%')
566
- if mode.include?('-')
567
- return [perc, '-']
568
- elsif mode.include?('<>')
569
- return [perc, '<>']
570
- elsif mode.include?('>')
571
- return [perc, '>']
572
- else
573
- [perc, nil]
574
- end
670
+ perc = mode.include?('%')
671
+ if mode.include?('-')
672
+ return [perc, '-']
673
+ elsif mode.include?('<>')
674
+ return [perc, '<>']
675
+ elsif mode.include?('>')
676
+ return [perc, '>']
677
+ else
678
+ [perc, nil]
679
+ end
575
680
  else
576
- return [false, nil]
681
+ return [false, nil]
577
682
  end
578
683
  end
579
684
 
580
- def make_scanner(src)
685
+ def make_scanner(src) # :nodoc:
581
686
  Scanner.make_scanner(src, @trim_mode, @percent)
582
687
  end
583
688
 
689
+ # Construct a new compiler using the trim_mode. See ERB::new for available
690
+ # trim modes.
584
691
  def initialize(trim_mode)
585
692
  @percent, @trim_mode = prepare_trim_mode(trim_mode)
586
693
  @put_cmd = 'print'
@@ -589,7 +696,30 @@ class ERB
589
696
  @post_cmd = []
590
697
  end
591
698
  attr_reader :percent, :trim_mode
592
- attr_accessor :put_cmd, :insert_cmd, :pre_cmd, :post_cmd
699
+
700
+ # The command to handle text that ends with a newline
701
+ attr_accessor :put_cmd
702
+
703
+ # The command to handle text that is inserted prior to a newline
704
+ attr_accessor :insert_cmd
705
+
706
+ # An array of commands prepended to compiled code
707
+ attr_accessor :pre_cmd
708
+
709
+ # An array of commands appended to compiled code
710
+ attr_accessor :post_cmd
711
+
712
+ private
713
+ def detect_magic_comment(s)
714
+ if /\A<%#(.*)%>/ =~ s or (@percent and /\A%#(.*)/ =~ s)
715
+ comment = $1
716
+ comment = $1 if comment[/-\*-\s*(.*?)\s*-*-$/]
717
+ if %r"coding\s*[=:]\s*([[:alnum:]\-_]+)" =~ comment
718
+ enc = $1.sub(/-(?:mac|dos|unix)/i, '')
719
+ enc = Encoding.find(enc)
720
+ end
721
+ end
722
+ end
593
723
  end
594
724
  end
595
725
 
@@ -598,19 +728,20 @@ end
598
728
  class ERB
599
729
  #
600
730
  # Constructs a new ERB object with the template specified in _str_.
601
- #
731
+ #
602
732
  # An ERB object works by building a chunk of Ruby code that will output
603
733
  # the completed template when run. If _safe_level_ is set to a non-nil value,
604
734
  # ERB code will be run in a separate thread with <b>$SAFE</b> set to the
605
735
  # provided level.
606
- #
736
+ #
607
737
  # If _trim_mode_ is passed a String containing one or more of the following
608
738
  # modifiers, ERB will adjust its code generation as listed:
609
- #
610
- # % enables Ruby code processing for lines beginning with %
611
- # <> omit newline for lines starting with <% and ending in %>
612
- # > omit newline for lines ending in %>
613
- #
739
+ #
740
+ # % enables Ruby code processing for lines beginning with %
741
+ # <> omit newline for lines starting with <% and ending in %>
742
+ # > omit newline for lines ending in %>
743
+ # - omit blank lines ending in -%>
744
+ #
614
745
  # _eoutvar_ can be used to set the name of the variable ERB will build up
615
746
  # its output in. This is useful when you need to run multiple ERB
616
747
  # templates through the same binding and/or when you want to control where
@@ -619,20 +750,20 @@ class ERB
619
750
  # === Example
620
751
  #
621
752
  # require "erb"
622
- #
753
+ #
623
754
  # # build data class
624
755
  # class Listings
625
756
  # PRODUCT = { :name => "Chicken Fried Steak",
626
757
  # :desc => "A well messages pattie, breaded and fried.",
627
758
  # :cost => 9.95 }
628
- #
759
+ #
629
760
  # attr_reader :product, :price
630
- #
761
+ #
631
762
  # def initialize( product = "", price = "" )
632
763
  # @product = product
633
764
  # @price = price
634
765
  # end
635
- #
766
+ #
636
767
  # def build
637
768
  # b = binding
638
769
  # # create and run templates, filling member data variables
@@ -646,29 +777,36 @@ class ERB
646
777
  # END_PRICE
647
778
  # end
648
779
  # end
649
- #
780
+ #
650
781
  # # setup template data
651
782
  # listings = Listings.new
652
783
  # listings.build
653
- #
784
+ #
654
785
  # puts listings.product + "\n" + listings.price
655
786
  #
656
787
  # _Generates_
657
788
  #
658
789
  # Chicken Fried Steak
659
790
  # A well messages pattie, breaded and fried.
660
- #
791
+ #
661
792
  # Chicken Fried Steak -- 9.95
662
793
  # A well messages pattie, breaded and fried.
663
- #
794
+ #
664
795
  def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
665
796
  @safe_level = safe_level
666
- compiler = ERB::Compiler.new(trim_mode)
797
+ compiler = make_compiler(trim_mode)
667
798
  set_eoutvar(compiler, eoutvar)
668
- @src = compiler.compile(str)
799
+ @src, @enc = *compiler.compile(str)
669
800
  @filename = nil
670
801
  end
671
802
 
803
+ ##
804
+ # Creates a new compiler for ERB. See ERB::Compiler.new for details
805
+
806
+ def make_compiler(trim_mode)
807
+ ERB::Compiler.new(trim_mode)
808
+ end
809
+
672
810
  # The Ruby code generated by ERB
673
811
  attr_reader :src
674
812
 
@@ -677,23 +815,15 @@ class ERB
677
815
  attr_accessor :filename
678
816
 
679
817
  #
680
- # Can be used to set _eoutvar_ as described in ERB#new. It's probably easier
681
- # to just use the constructor though, since calling this method requires the
682
- # setup of an ERB _compiler_ object.
818
+ # Can be used to set _eoutvar_ as described in ERB::new. It's probably
819
+ # easier to just use the constructor though, since calling this method
820
+ # requires the setup of an ERB _compiler_ object.
683
821
  #
684
822
  def set_eoutvar(compiler, eoutvar = '_erbout')
685
823
  compiler.put_cmd = "#{eoutvar}.concat"
686
824
  compiler.insert_cmd = "#{eoutvar}.concat"
687
-
688
- cmd = []
689
- cmd.push "#{eoutvar} = ''"
690
-
691
- compiler.pre_cmd = cmd
692
-
693
- cmd = []
694
- cmd.push(eoutvar)
695
-
696
- compiler.post_cmd = cmd
825
+ compiler.pre_cmd = ["#{eoutvar} = ''"]
826
+ compiler.post_cmd = ["#{eoutvar}.force_encoding(__ENCODING__)"]
697
827
  end
698
828
 
699
829
  # Generate results and print them. (see ERB#result)
@@ -703,25 +833,28 @@ class ERB
703
833
 
704
834
  #
705
835
  # Executes the generated ERB code to produce a completed template, returning
706
- # the results of that code. (See ERB#new for details on how this process can
707
- # be affected by _safe_level_.)
708
- #
836
+ # the results of that code. (See ERB::new for details on how this process
837
+ # can be affected by _safe_level_.)
838
+ #
709
839
  # _b_ accepts a Binding or Proc object which is used to set the context of
710
840
  # code evaluation.
711
841
  #
712
842
  def result(b=new_toplevel)
713
843
  if @safe_level
714
- proc {
715
- $SAFE = @safe_level
716
- eval(@src, b, (@filename || '(erb)'), 1)
844
+ proc {
845
+ $SAFE = @safe_level
846
+ eval(@src, b, (@filename || '(erb)'), 0)
717
847
  }.call
718
848
  else
719
- eval(@src, b, (@filename || '(erb)'), 1)
849
+ eval(@src, b, (@filename || '(erb)'), 0)
720
850
  end
721
851
  end
722
852
 
853
+ ##
854
+ # Returns a new binding each time *near* TOPLEVEL_BINDING for runs that do
855
+ # not specify a binding.
856
+
723
857
  def new_toplevel
724
- # New binding each time *near* toplevel for unspecified runs
725
858
  TOPLEVEL_BINDING.dup
726
859
  end
727
860
  private :new_toplevel
@@ -734,7 +867,11 @@ class ERB
734
867
  # erb.def_method(MyClass, 'render(arg1, arg2)', filename)
735
868
  # print MyClass.new.render('foo', 123)
736
869
  def def_method(mod, methodname, fname='(ERB)')
737
- mod.module_eval("def #{methodname}\n" + self.src + "\nend\n", fname, 0)
870
+ src = self.src
871
+ magic_comment = "#coding:#{@enc}\n"
872
+ mod.module_eval do
873
+ eval(magic_comment + "def #{methodname}\n" + src + "\nend\n", binding, fname, -2)
874
+ end
738
875
  end
739
876
 
740
877
  # Create unnamed module, define _methodname_ as instance method of it, and return it.
@@ -781,37 +918,39 @@ class ERB
781
918
  public
782
919
  #
783
920
  # A utility method for escaping HTML tag characters in _s_.
784
- #
785
- # require "erb"
786
- # include ERB::Util
787
- #
788
- # puts html_escape("is a > 0 & a < 10?")
789
- #
921
+ #
922
+ # require "erb"
923
+ # include ERB::Util
924
+ #
925
+ # puts html_escape("is a > 0 & a < 10?")
926
+ #
790
927
  # _Generates_
791
- #
792
- # is a &gt; 0 &amp; a &lt; 10?
928
+ #
929
+ # is a &gt; 0 &amp; a &lt; 10?
793
930
  #
794
931
  def html_escape(s)
795
- s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
932
+ CGI.escapeHTML(s.to_s)
796
933
  end
797
934
  alias h html_escape
798
935
  module_function :h
799
936
  module_function :html_escape
800
-
937
+
801
938
  #
802
939
  # A utility method for encoding the String _s_ as a URL.
803
- #
804
- # require "erb"
805
- # include ERB::Util
806
- #
807
- # puts url_encode("Programming Ruby: The Pragmatic Programmer's Guide")
808
- #
940
+ #
941
+ # require "erb"
942
+ # include ERB::Util
943
+ #
944
+ # puts url_encode("Programming Ruby: The Pragmatic Programmer's Guide")
945
+ #
809
946
  # _Generates_
810
- #
811
- # Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
947
+ #
948
+ # Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
812
949
  #
813
950
  def url_encode(s)
814
- s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
951
+ s.to_s.dup.force_encoding("ASCII-8BIT").gsub(/[^a-zA-Z0-9_\-.]/n) {
952
+ sprintf("%%%02X", $&.unpack("C")[0])
953
+ }
815
954
  end
816
955
  alias u url_encode
817
956
  module_function :u
@@ -852,7 +991,8 @@ class ERB
852
991
  #
853
992
  module DefMethod
854
993
  public
855
- # define _methodname_ as instance method of current module, using ERB object or eRuby file
994
+ # define _methodname_ as instance method of current module, using ERB
995
+ # object or eRuby file
856
996
  def def_erb_method(methodname, erb_or_fname)
857
997
  if erb_or_fname.kind_of? String
858
998
  fname = erb_or_fname
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
- module ERB
3
- VERSION = "1.0.1"
2
+ module Erb
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -3,7 +3,7 @@ require './lib/rubysl/erb/version'
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "rubysl-erb"
6
- spec.version = RubySL::ERB::VERSION
6
+ spec.version = RubySL::Erb::VERSION
7
7
  spec.authors = ["Brian Shirai"]
8
8
  spec.email = ["brixen@gmail.com"]
9
9
  spec.description = %q{Ruby standard library erb.}
@@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
21
  spec.add_development_dependency "mspec", "~> 1.5"
22
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-erb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-25 00:00:00.000000000 Z
11
+ date: 2013-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
- - !ruby/object:Gem::Dependency
56
- name: rubysl-prettyprint
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '1.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '1.0'
69
55
  description: Ruby standard library erb.
70
56
  email:
71
57
  - brixen@gmail.com