Module: Erubis::HeaderFooterEnhancer

Included in:
HeaderFooterEruby
Defined in:
lib/erubis/enhancer.rb

Overview

experimental

allow header and footer in eRuby script

ex.

====================
## without header and footer
$ cat ex1.eruby
<% def list_items(list) %>
<%   for item in list %>
<li><%= item %></li>
<%   end %>
<% end %>

$ erubis -s ex1.eruby
_buf = []; def list_items(list)
;   for item in list
; _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
';   end
; end
;
_buf.join

## with header and footer
$ cat ex2.eruby
<!--#header:
def list_items(list)
 #-->
<%  for item in list %>
<li><%= item %></li>
<%  end %>
<!--#footer:
end
 #-->

$ erubis -s -c HeaderFooterEruby ex4.eruby

def list_items(list)
 _buf = []; _buf << '
';  for item in list
; _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
';  end
; _buf << '
';
_buf.join
end

====================

this is language-independent.

Constant Summary collapse

/(.*?)(^[ \t]*)?<!--\#(\w+):(.*?)\#-->([ \t]*\r?\n)?/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

Returns the value of attribute footer.



537
538
539
# File 'lib/erubis/enhancer.rb', line 537

def footer
  @footer
end

#headerObject

Returns the value of attribute header.



537
538
539
# File 'lib/erubis/enhancer.rb', line 537

def header
  @header
end

Class Method Details

.descObject

:nodoc:



518
519
520
# File 'lib/erubis/enhancer.rb', line 518

def self.desc   # :nodoc:
  "allow header/footer in document (ex. '<!--#header: #-->')"
end

Instance Method Details

#add_text(src, text) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/erubis/enhancer.rb', line 524

def add_text(src, text)
  text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace|
    flag_trim = @trim && lspace && rspace
    super(src, txt)
    content = "#{lspace}#{content}#{rspace}" if flag_trim
    super(src, lspace) if !flag_trim && lspace
    instance_variable_set("@#{word}", content)
    super(src, rspace) if !flag_trim && rspace
  end
  rest = $' || text
  super(src, rest)
end

#convert(input) ⇒ Object



539
540
541
542
# File 'lib/erubis/enhancer.rb', line 539

def convert(input)
  source = super
  return @src = "#{@header}#{source}#{@footer}"
end