rubysl-erb 1.0.0 → 1.0.1

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: a0a309abd0a5fc4a4e438ea153e9afee2d4e185b
4
- data.tar.gz: a303752e0095e1d345be42cc050ab9b303446bf2
3
+ metadata.gz: bc2021bd6015360e2b32d7145cc13e3b85e8f942
4
+ data.tar.gz: 3c29e3eb3330205331966c8e1d15f2c531b280c3
5
5
  SHA512:
6
- metadata.gz: 97eadb1a9718c954ce185f2c1acb9a727b46cc606affbc4d427697fa871bc78eab65a5ed5bde2f90616102abb98e4be946d71f9da9353111ea55a537bf663ad1
7
- data.tar.gz: 6cbb814906db25026a9ed99ad8b185d54e8de868c996ebe7e233f8a4e2c0594fc93150de2929ff1c05a2564bc765d247519f9be779bb38b0d7bc42ab9b8ef316
6
+ metadata.gz: 6060db64dda4dcb7b42709f7d8669c6a6ae75521cf276b1f2d4f449b827713e38663343fe129cf45af0fb0d0011cf7276497950effb13ed0718c7bd3debf0f5b
7
+ data.tar.gz: 4f2720329d61c51b93f24b9b160980f913bddedc720b8bc75f0f3bcaa15c6cf17b75dda028747bd294306afd5197825858870d6ae66997ad4e59ece464fe66e3
@@ -0,0 +1 @@
1
+ require "rubysl/erb"
@@ -1,7 +1,2 @@
1
- require "rubysl-erb/version"
2
-
3
- module RubySL
4
- module Erb
5
- # Your code goes here...
6
- end
7
- end
1
+ require "rubysl/erb/erb"
2
+ require "rubysl/erb/version"
@@ -0,0 +1,868 @@
1
+ # = ERB -- Ruby Templating
2
+ #
3
+ # Author:: Masatoshi SEKI
4
+ # Documentation:: James Edward Gray II and Gavin Sinclair
5
+ #
6
+ # See ERB for primary documentation and ERB::Util for a couple of utility
7
+ # routines.
8
+ #
9
+ # Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
10
+ #
11
+ # You can redistribute it and/or modify it under the same terms as Ruby.
12
+
13
+ #
14
+ # = ERB -- Ruby Templating
15
+ #
16
+ # == Introduction
17
+ #
18
+ # ERB provides an easy to use but powerful templating system for Ruby. Using
19
+ # ERB, actual Ruby code can be added to any plain text document for the
20
+ # purposes of generating document information details and/or flow control.
21
+ #
22
+ # A very simple example is this:
23
+ #
24
+ # require 'erb'
25
+ #
26
+ # x = 42
27
+ # template = ERB.new <<-EOF
28
+ # The value of x is: <%= x %>
29
+ # EOF
30
+ # puts template.result(binding)
31
+ #
32
+ # <em>Prints:</em> The value of x is: 42
33
+ #
34
+ # More complex examples are given below.
35
+ #
36
+ #
37
+ # == Recognized Tags
38
+ #
39
+ # ERB recognizes certain tags in the provided template and converts them based
40
+ # on the rules below:
41
+ #
42
+ # <% Ruby code -- inline with output %>
43
+ # <%= Ruby expression -- replace with result %>
44
+ # <%# comment -- ignored -- useful in testing %>
45
+ # % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
46
+ # %% replaced with % if first thing on a line and % processing is used
47
+ # <%% or %%> -- replace with <% or %> respectively
48
+ #
49
+ # All other text is passed through ERB filtering unchanged.
50
+ #
51
+ #
52
+ # == Options
53
+ #
54
+ # There are several settings you can change when you use ERB:
55
+ # * the nature of the tags that are recognized;
56
+ # * the value of <tt>$SAFE</tt> under which the template is run;
57
+ # * the binding used to resolve local variables in the template.
58
+ #
59
+ # See the ERB.new and ERB#result methods for more detail.
60
+ #
61
+ #
62
+ # == Examples
63
+ #
64
+ # === Plain Text
65
+ #
66
+ # ERB is useful for any generic templating situation. Note that in this example, we use the
67
+ # convenient "% at start of line" tag, and we quote the template literally with
68
+ # <tt>%q{...}</tt> to avoid trouble with the backslash.
69
+ #
70
+ # require "erb"
71
+ #
72
+ # # Create template.
73
+ # template = %q{
74
+ # From: James Edward Gray II <james@grayproductions.net>
75
+ # To: <%= to %>
76
+ # Subject: Addressing Needs
77
+ #
78
+ # <%= to[/\w+/] %>:
79
+ #
80
+ # Just wanted to send a quick note assuring that your needs are being
81
+ # addressed.
82
+ #
83
+ # I want you to know that my team will keep working on the issues,
84
+ # especially:
85
+ #
86
+ # <%# ignore numerous minor requests -- focus on priorities %>
87
+ # % priorities.each do |priority|
88
+ # * <%= priority %>
89
+ # % end
90
+ #
91
+ # Thanks for your patience.
92
+ #
93
+ # James Edward Gray II
94
+ # }.gsub(/^ /, '')
95
+ #
96
+ # message = ERB.new(template, 0, "%<>")
97
+ #
98
+ # # Set up template data.
99
+ # to = "Community Spokesman <spokesman@ruby_community.org>"
100
+ # priorities = [ "Run Ruby Quiz",
101
+ # "Document Modules",
102
+ # "Answer Questions on Ruby Talk" ]
103
+ #
104
+ # # Produce result.
105
+ # email = message.result
106
+ # puts email
107
+ #
108
+ # <i>Generates:</i>
109
+ #
110
+ # From: James Edward Gray II <james@grayproductions.net>
111
+ # To: Community Spokesman <spokesman@ruby_community.org>
112
+ # Subject: Addressing Needs
113
+ #
114
+ # Community:
115
+ #
116
+ # Just wanted to send a quick note assuring that your needs are being addressed.
117
+ #
118
+ # I want you to know that my team will keep working on the issues, especially:
119
+ #
120
+ # * Run Ruby Quiz
121
+ # * Document Modules
122
+ # * Answer Questions on Ruby Talk
123
+ #
124
+ # Thanks for your patience.
125
+ #
126
+ # James Edward Gray II
127
+ #
128
+ # === Ruby in HTML
129
+ #
130
+ # ERB is often used in <tt>.rhtml</tt> files (HTML with embedded Ruby). Notice the need in
131
+ # this example to provide a special binding when the template is run, so that the instance
132
+ # variables in the Product object can be resolved.
133
+ #
134
+ # require "erb"
135
+ #
136
+ # # Build template data class.
137
+ # class Product
138
+ # def initialize( code, name, desc, cost )
139
+ # @code = code
140
+ # @name = name
141
+ # @desc = desc
142
+ # @cost = cost
143
+ #
144
+ # @features = [ ]
145
+ # end
146
+ #
147
+ # def add_feature( feature )
148
+ # @features << feature
149
+ # end
150
+ #
151
+ # # Support templating of member data.
152
+ # def get_binding
153
+ # binding
154
+ # end
155
+ #
156
+ # # ...
157
+ # end
158
+ #
159
+ # # Create template.
160
+ # template = %{
161
+ # <html>
162
+ # <head><title>Ruby Toys -- <%= @name %></title></head>
163
+ # <body>
164
+ #
165
+ # <h1><%= @name %> (<%= @code %>)</h1>
166
+ # <p><%= @desc %></p>
167
+ #
168
+ # <ul>
169
+ # <% @features.each do |f| %>
170
+ # <li><b><%= f %></b></li>
171
+ # <% end %>
172
+ # </ul>
173
+ #
174
+ # <p>
175
+ # <% if @cost < 10 %>
176
+ # <b>Only <%= @cost %>!!!</b>
177
+ # <% else %>
178
+ # Call for a price, today!
179
+ # <% end %>
180
+ # </p>
181
+ #
182
+ # </body>
183
+ # </html>
184
+ # }.gsub(/^ /, '')
185
+ #
186
+ # rhtml = ERB.new(template)
187
+ #
188
+ # # Set up template data.
189
+ # toy = Product.new( "TZ-1002",
190
+ # "Rubysapien",
191
+ # "Geek's Best Friend! Responds to Ruby commands...",
192
+ # 999.95 )
193
+ # toy.add_feature("Listens for verbal commands in the Ruby language!")
194
+ # toy.add_feature("Ignores Perl, Java, and all C variants.")
195
+ # toy.add_feature("Karate-Chop Action!!!")
196
+ # toy.add_feature("Matz signature on left leg.")
197
+ # toy.add_feature("Gem studded eyes... Rubies, of course!")
198
+ #
199
+ # # Produce result.
200
+ # rhtml.run(toy.get_binding)
201
+ #
202
+ # <i>Generates (some blank lines removed):</i>
203
+ #
204
+ # <html>
205
+ # <head><title>Ruby Toys -- Rubysapien</title></head>
206
+ # <body>
207
+ #
208
+ # <h1>Rubysapien (TZ-1002)</h1>
209
+ # <p>Geek's Best Friend! Responds to Ruby commands...</p>
210
+ #
211
+ # <ul>
212
+ # <li><b>Listens for verbal commands in the Ruby language!</b></li>
213
+ # <li><b>Ignores Perl, Java, and all C variants.</b></li>
214
+ # <li><b>Karate-Chop Action!!!</b></li>
215
+ # <li><b>Matz signature on left leg.</b></li>
216
+ # <li><b>Gem studded eyes... Rubies, of course!</b></li>
217
+ # </ul>
218
+ #
219
+ # <p>
220
+ # Call for a price, today!
221
+ # </p>
222
+ #
223
+ # </body>
224
+ # </html>
225
+ #
226
+ #
227
+ # == Notes
228
+ #
229
+ # There are a variety of templating solutions available in various Ruby projects:
230
+ # * ERB's big brother, eRuby, works the same but is written in C for speed;
231
+ # * Amrita (smart at producing HTML/XML);
232
+ # * cs/Template (written in C for speed);
233
+ # * RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere;
234
+ # * and others; search the RAA.
235
+ #
236
+ # Rails, the web application framework, uses ERB to create views.
237
+ #
238
+ class ERB
239
+ Revision = '$Date: 2009-02-23 09:44:50 -0800 (Mon, 23 Feb 2009) $' #'
240
+
241
+ # Returns revision information for the erb.rb module.
242
+ def self.version
243
+ "erb.rb [2.1.0 #{ERB::Revision.split[1]}]"
244
+ end
245
+ end
246
+
247
+ #--
248
+ # ERB::Compiler
249
+ class ERB
250
+ class Compiler # :nodoc:
251
+ class PercentLine # :nodoc:
252
+ def initialize(str)
253
+ @value = str
254
+ end
255
+ attr_reader :value
256
+ alias :to_s :value
257
+
258
+ def empty?
259
+ @value.empty?
260
+ end
261
+ end
262
+
263
+ class Scanner # :nodoc:
264
+ @scanner_map = {}
265
+ def self.regist_scanner(klass, trim_mode, percent)
266
+ @scanner_map[[trim_mode, percent]] = klass
267
+ end
268
+
269
+ def self.default_scanner=(klass)
270
+ @default_scanner = klass
271
+ end
272
+
273
+ 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)
276
+ end
277
+
278
+ def initialize(src, trim_mode, percent)
279
+ @src = src
280
+ @stag = nil
281
+ end
282
+ attr_accessor :stag
283
+
284
+ def scan; end
285
+ end
286
+
287
+ class TrimScanner < Scanner # :nodoc:
288
+ 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
301
+ end
302
+ attr_accessor :stag
303
+
304
+ def scan(&block)
305
+ @stag = nil
306
+ if @percent
307
+ @src.each do |line|
308
+ percent_line(line, &block)
309
+ end
310
+ else
311
+ @scan_line.call(@src, &block)
312
+ end
313
+ nil
314
+ end
315
+
316
+ 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
325
+ yield(PercentLine.new(line.chomp))
326
+ end
327
+ end
328
+
329
+ def scan_line(line)
330
+ line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
331
+ tokens.each do |token|
332
+ next if token.empty?
333
+ yield(token)
334
+ end
335
+ end
336
+ end
337
+
338
+ def trim_line1(line)
339
+ line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
340
+ tokens.each do |token|
341
+ next if token.empty?
342
+ if token == "%>\n"
343
+ yield('%>')
344
+ yield(:cr)
345
+ else
346
+ yield(token)
347
+ end
348
+ end
349
+ end
350
+ end
351
+
352
+ def trim_line2(line)
353
+ head = nil
354
+ line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
355
+ tokens.each do |token|
356
+ next if token.empty?
357
+ head = token unless head
358
+ if token == "%>\n"
359
+ yield('%>')
360
+ if is_erb_stag?(head)
361
+ yield(:cr)
362
+ else
363
+ yield("\n")
364
+ end
365
+ head = nil
366
+ else
367
+ yield(token)
368
+ head = nil if token == "\n"
369
+ end
370
+ end
371
+ end
372
+ end
373
+
374
+ def explicit_trim_line(line)
375
+ line.scan(/(.*?)(^[ \t]*<%\-|<%\-|<%%|%%>|<%=|<%#|<%|-%>\n|-%>|%>|\z)/m) do |tokens|
376
+ tokens.each do |token|
377
+ next if token.empty?
378
+ if @stag.nil? && /[ \t]*<%-/ =~ token
379
+ yield('<%')
380
+ elsif @stag && token == "-%>\n"
381
+ yield('%>')
382
+ yield(:cr)
383
+ elsif @stag && token == '-%>'
384
+ yield('%>')
385
+ else
386
+ yield(token)
387
+ end
388
+ end
389
+ end
390
+ end
391
+
392
+ ERB_STAG = %w(<%= <%# <%)
393
+ def is_erb_stag?(s)
394
+ ERB_STAG.member?(s)
395
+ end
396
+ end
397
+
398
+ Scanner.default_scanner = TrimScanner
399
+
400
+ class SimpleScanner < Scanner # :nodoc:
401
+ def scan
402
+ @src.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
403
+ tokens.each do |token|
404
+ next if token.empty?
405
+ yield(token)
406
+ end
407
+ end
408
+ end
409
+ end
410
+
411
+ Scanner.regist_scanner(SimpleScanner, nil, false)
412
+
413
+ begin
414
+ require 'strscan'
415
+ class SimpleScanner2 < Scanner # :nodoc:
416
+ def scan
417
+ stag_reg = /(.*?)(<%%|<%=|<%#|<%|\z)/m
418
+ etag_reg = /(.*?)(%%>|%>|\z)/m
419
+ scanner = StringScanner.new(@src)
420
+ while ! scanner.eos?
421
+ scanner.scan(@stag ? etag_reg : stag_reg)
422
+ yield(scanner[1])
423
+ yield(scanner[2])
424
+ end
425
+ end
426
+ end
427
+ Scanner.regist_scanner(SimpleScanner2, nil, false)
428
+
429
+ class ExplicitScanner < Scanner # :nodoc:
430
+ def scan
431
+ stag_reg = /(.*?)(^[ \t]*<%-|<%%|<%=|<%#|<%-|<%|\z)/m
432
+ etag_reg = /(.*?)(%%>|-%>|%>|\z)/m
433
+ scanner = StringScanner.new(@src)
434
+ while ! scanner.eos?
435
+ scanner.scan(@stag ? etag_reg : stag_reg)
436
+ yield(scanner[1])
437
+
438
+ elem = scanner[2]
439
+ if /[ \t]*<%-/ =~ elem
440
+ yield('<%')
441
+ elsif elem == '-%>'
442
+ yield('%>')
443
+ yield(:cr) if scanner.scan(/(\n|\z)/)
444
+ else
445
+ yield(elem)
446
+ end
447
+ end
448
+ end
449
+ end
450
+ Scanner.regist_scanner(ExplicitScanner, '-', false)
451
+
452
+ rescue LoadError
453
+ end
454
+
455
+ 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
463
+ end
464
+ attr_reader :script
465
+
466
+ def push(cmd)
467
+ @line << cmd
468
+ end
469
+
470
+ def cr
471
+ @script << (@line.join('; '))
472
+ @line = []
473
+ @script << "\n"
474
+ end
475
+
476
+ 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
483
+ end
484
+ end
485
+
486
+ def content_dump(s)
487
+ n = s.count("\n")
488
+ if n > 0
489
+ s.dump + "\n" * n
490
+ else
491
+ s.dump
492
+ end
493
+ end
494
+
495
+ def compile(s)
496
+ out = Buffer.new(self)
497
+
498
+ content = ''
499
+ scanner = make_scanner(s)
500
+ scanner.scan do |token|
501
+ next if token.nil?
502
+ next if token == ''
503
+ if scanner.stag.nil?
504
+ case token
505
+ when PercentLine
506
+ out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
507
+ content = ''
508
+ out.push(token.to_s)
509
+ 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
550
+ end
551
+ out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
552
+ out.close
553
+ out.script
554
+ end
555
+
556
+ def prepare_trim_mode(mode)
557
+ case mode
558
+ when 1
559
+ return [false, '>']
560
+ when 2
561
+ return [false, '<>']
562
+ when 0
563
+ return [false, nil]
564
+ 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
575
+ else
576
+ return [false, nil]
577
+ end
578
+ end
579
+
580
+ def make_scanner(src)
581
+ Scanner.make_scanner(src, @trim_mode, @percent)
582
+ end
583
+
584
+ def initialize(trim_mode)
585
+ @percent, @trim_mode = prepare_trim_mode(trim_mode)
586
+ @put_cmd = 'print'
587
+ @insert_cmd = @put_cmd
588
+ @pre_cmd = []
589
+ @post_cmd = []
590
+ end
591
+ attr_reader :percent, :trim_mode
592
+ attr_accessor :put_cmd, :insert_cmd, :pre_cmd, :post_cmd
593
+ end
594
+ end
595
+
596
+ #--
597
+ # ERB
598
+ class ERB
599
+ #
600
+ # Constructs a new ERB object with the template specified in _str_.
601
+ #
602
+ # An ERB object works by building a chunk of Ruby code that will output
603
+ # the completed template when run. If _safe_level_ is set to a non-nil value,
604
+ # ERB code will be run in a separate thread with <b>$SAFE</b> set to the
605
+ # provided level.
606
+ #
607
+ # If _trim_mode_ is passed a String containing one or more of the following
608
+ # 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
+ #
614
+ # _eoutvar_ can be used to set the name of the variable ERB will build up
615
+ # its output in. This is useful when you need to run multiple ERB
616
+ # templates through the same binding and/or when you want to control where
617
+ # output ends up. Pass the name of the variable to be used inside a String.
618
+ #
619
+ # === Example
620
+ #
621
+ # require "erb"
622
+ #
623
+ # # build data class
624
+ # class Listings
625
+ # PRODUCT = { :name => "Chicken Fried Steak",
626
+ # :desc => "A well messages pattie, breaded and fried.",
627
+ # :cost => 9.95 }
628
+ #
629
+ # attr_reader :product, :price
630
+ #
631
+ # def initialize( product = "", price = "" )
632
+ # @product = product
633
+ # @price = price
634
+ # end
635
+ #
636
+ # def build
637
+ # b = binding
638
+ # # create and run templates, filling member data variables
639
+ # ERB.new(<<-'END_PRODUCT'.gsub(/^\s+/, ""), 0, "", "@product").result b
640
+ # <%= PRODUCT[:name] %>
641
+ # <%= PRODUCT[:desc] %>
642
+ # END_PRODUCT
643
+ # ERB.new(<<-'END_PRICE'.gsub(/^\s+/, ""), 0, "", "@price").result b
644
+ # <%= PRODUCT[:name] %> -- <%= PRODUCT[:cost] %>
645
+ # <%= PRODUCT[:desc] %>
646
+ # END_PRICE
647
+ # end
648
+ # end
649
+ #
650
+ # # setup template data
651
+ # listings = Listings.new
652
+ # listings.build
653
+ #
654
+ # puts listings.product + "\n" + listings.price
655
+ #
656
+ # _Generates_
657
+ #
658
+ # Chicken Fried Steak
659
+ # A well messages pattie, breaded and fried.
660
+ #
661
+ # Chicken Fried Steak -- 9.95
662
+ # A well messages pattie, breaded and fried.
663
+ #
664
+ def initialize(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
665
+ @safe_level = safe_level
666
+ compiler = ERB::Compiler.new(trim_mode)
667
+ set_eoutvar(compiler, eoutvar)
668
+ @src = compiler.compile(str)
669
+ @filename = nil
670
+ end
671
+
672
+ # The Ruby code generated by ERB
673
+ attr_reader :src
674
+
675
+ # The optional _filename_ argument passed to Kernel#eval when the ERB code
676
+ # is run
677
+ attr_accessor :filename
678
+
679
+ #
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.
683
+ #
684
+ def set_eoutvar(compiler, eoutvar = '_erbout')
685
+ compiler.put_cmd = "#{eoutvar}.concat"
686
+ 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
697
+ end
698
+
699
+ # Generate results and print them. (see ERB#result)
700
+ def run(b=new_toplevel)
701
+ print self.result(b)
702
+ end
703
+
704
+ #
705
+ # 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
+ #
709
+ # _b_ accepts a Binding or Proc object which is used to set the context of
710
+ # code evaluation.
711
+ #
712
+ def result(b=new_toplevel)
713
+ if @safe_level
714
+ proc {
715
+ $SAFE = @safe_level
716
+ eval(@src, b, (@filename || '(erb)'), 1)
717
+ }.call
718
+ else
719
+ eval(@src, b, (@filename || '(erb)'), 1)
720
+ end
721
+ end
722
+
723
+ def new_toplevel
724
+ # New binding each time *near* toplevel for unspecified runs
725
+ TOPLEVEL_BINDING.dup
726
+ end
727
+ private :new_toplevel
728
+
729
+ # Define _methodname_ as instance method of _mod_ from compiled ruby source.
730
+ #
731
+ # example:
732
+ # filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml
733
+ # erb = ERB.new(File.read(filename))
734
+ # erb.def_method(MyClass, 'render(arg1, arg2)', filename)
735
+ # print MyClass.new.render('foo', 123)
736
+ def def_method(mod, methodname, fname='(ERB)')
737
+ mod.module_eval("def #{methodname}\n" + self.src + "\nend\n", fname, 0)
738
+ end
739
+
740
+ # Create unnamed module, define _methodname_ as instance method of it, and return it.
741
+ #
742
+ # example:
743
+ # filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml
744
+ # erb = ERB.new(File.read(filename))
745
+ # erb.filename = filename
746
+ # MyModule = erb.def_module('render(arg1, arg2)')
747
+ # class MyClass
748
+ # include MyModule
749
+ # end
750
+ def def_module(methodname='erb')
751
+ mod = Module.new
752
+ def_method(mod, methodname, @filename || '(ERB)')
753
+ mod
754
+ end
755
+
756
+ # Define unnamed class which has _methodname_ as instance method, and return it.
757
+ #
758
+ # example:
759
+ # class MyClass_
760
+ # def initialize(arg1, arg2)
761
+ # @arg1 = arg1; @arg2 = arg2
762
+ # end
763
+ # end
764
+ # filename = 'example.rhtml' # @arg1 and @arg2 are used in example.rhtml
765
+ # erb = ERB.new(File.read(filename))
766
+ # erb.filename = filename
767
+ # MyClass = erb.def_class(MyClass_, 'render()')
768
+ # print MyClass.new('foo', 123).render()
769
+ def def_class(superklass=Object, methodname='result')
770
+ cls = Class.new(superklass)
771
+ def_method(cls, methodname, @filename || '(ERB)')
772
+ cls
773
+ end
774
+ end
775
+
776
+ #--
777
+ # ERB::Util
778
+ class ERB
779
+ # A utility module for conversion routines, often handy in HTML generation.
780
+ module Util
781
+ public
782
+ #
783
+ # 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
+ #
790
+ # _Generates_
791
+ #
792
+ # is a &gt; 0 &amp; a &lt; 10?
793
+ #
794
+ def html_escape(s)
795
+ s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
796
+ end
797
+ alias h html_escape
798
+ module_function :h
799
+ module_function :html_escape
800
+
801
+ #
802
+ # 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
+ #
809
+ # _Generates_
810
+ #
811
+ # Programming%20Ruby%3A%20%20The%20Pragmatic%20Programmer%27s%20Guide
812
+ #
813
+ def url_encode(s)
814
+ s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
815
+ end
816
+ alias u url_encode
817
+ module_function :u
818
+ module_function :url_encode
819
+ end
820
+ end
821
+
822
+ #--
823
+ # ERB::DefMethod
824
+ class ERB
825
+ # Utility module to define eRuby script as instance method.
826
+ #
827
+ # === Example
828
+ #
829
+ # example.rhtml:
830
+ # <% for item in @items %>
831
+ # <b><%= item %></b>
832
+ # <% end %>
833
+ #
834
+ # example.rb:
835
+ # require 'erb'
836
+ # class MyClass
837
+ # extend ERB::DefMethod
838
+ # def_erb_method('render()', 'example.rhtml')
839
+ # def initialize(items)
840
+ # @items = items
841
+ # end
842
+ # end
843
+ # print MyClass.new([10,20,30]).render()
844
+ #
845
+ # result:
846
+ #
847
+ # <b>10</b>
848
+ #
849
+ # <b>20</b>
850
+ #
851
+ # <b>30</b>
852
+ #
853
+ module DefMethod
854
+ public
855
+ # define _methodname_ as instance method of current module, using ERB object or eRuby file
856
+ def def_erb_method(methodname, erb_or_fname)
857
+ if erb_or_fname.kind_of? String
858
+ fname = erb_or_fname
859
+ erb = ERB.new(File.read(fname))
860
+ erb.def_method(self, methodname, fname)
861
+ else
862
+ erb = erb_or_fname
863
+ erb.def_method(self, methodname, erb.filename || '(ERB)')
864
+ end
865
+ end
866
+ module_function :def_erb_method
867
+ end
868
+ end
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
- module Erb
3
- VERSION = "1.0.0"
2
+ module ERB
3
+ VERSION = "1.0.1"
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.}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-erb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
@@ -79,7 +79,9 @@ files:
79
79
  - LICENSE
80
80
  - README.md
81
81
  - Rakefile
82
+ - lib/erb.rb
82
83
  - lib/rubysl/erb.rb
84
+ - lib/rubysl/erb/erb.rb
83
85
  - lib/rubysl/erb/version.rb
84
86
  - rubysl-erb.gemspec
85
87
  - spec/def_class_spec.rb