Module Erubis::HeaderFooterEnhancer
In: erubis/enhancer.rb
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.

Methods

add_text   convert  

Constants

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

Attributes

footer  [RW] 
header  [RW] 

Public Instance methods

[Source]

# File erubis/enhancer.rb, line 541
    def add_text(src, text)
      m = nil
      text.scan(HEADER_FOOTER_PATTERN) do |txt, lspace, word, content, rspace|
        m = Regexp.last_match
        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                    # ruby1.8
      rest = m ? text[m.end(0)..-1] : text  # ruby1.9
      super(src, rest)
    end

[Source]

# File erubis/enhancer.rb, line 559
    def convert(input)
      source = super
      return @src = "#{@header}#{source}#{@footer}"
    end

[Validate]