# -*- coding: us-ascii -*-
# = ERB -- Ruby Templating
# Author:: Masatoshi SEKI
# Documentation:: James Edward Gray II, Gavin Sinclair, and Simon Chiang
# See ERB for primary documentation and ERB::Util for a couple of utility
# Copyright (c) 1999-2000,2002,2003 Masatoshi SEKI
# You can redistribute it and/or modify it under the same terms as Ruby.
# = ERB -- Ruby Templating
# ERB provides an easy to use but powerful templating system for Ruby. Using
# ERB, actual Ruby code can be added to any plain text document for the
# purposes of generating document information details and/or flow control.
# A very simple example is this:
# template = ERB.new <<-EOF
# The value of x is: <%= x %>
# puts template.result(binding)
# <em>Prints:</em> The value of x is: 42
# More complex examples are given below.
# ERB recognizes certain tags in the provided template and converts them based
# <% Ruby code -- inline with output %>
# <%= Ruby expression -- replace with result %>
# <%# comment -- ignored -- useful in testing %>
# % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
# %% replaced with % if first thing on a line and % processing is used
# <%% or %%> -- replace with <% or %> respectively
# All other text is passed through ERB filtering unchanged.
# There are several settings you can change when you use ERB:
# * the nature of the tags that are recognized;
# * the value of <tt>$SAFE</tt> under which the template is run;
# * the binding used to resolve local variables in the template.
# See the ERB.new and ERB#result methods for more detail.
# ERB (or Ruby code generated by ERB) returns a string in the same
# character encoding as the input string. When the input string has
# a magic comment, however, it returns a string in the encoding specified
# # -*- coding: utf-8 -*-
# template = ERB.new <<EOF
# <%#-*- coding: Big5 -*-%>
# \_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
# <em>Prints:</em> \_\_ENCODING\_\_ is Big5.
# ERB is useful for any generic templating situation. Note that in this example, we use the
# convenient "% at start of line" tag, and we quote the template literally with
# <tt>%q{...}</tt> to avoid trouble with the backslash.
# From: James Edward Gray II <james@grayproductions.net>
# Subject: Addressing Needs
# Just wanted to send a quick note assuring that your needs are being
# I want you to know that my team will keep working on the issues,
# <%# ignore numerous minor requests -- focus on priorities %>
# % priorities.each do |priority|
# Thanks for your patience.
# message = ERB.new(template, 0, "%<>")
# # Set up template data.
# to = "Community Spokesman <spokesman@ruby_community.org>"
# priorities = [ "Run Ruby Quiz",
# "Answer Questions on Ruby Talk" ]
# From: James Edward Gray II <james@grayproductions.net>
# To: Community Spokesman <spokesman@ruby_community.org>
# Subject: Addressing Needs
# Just wanted to send a quick note assuring that your needs are being addressed.
# I want you to know that my team will keep working on the issues, especially:
# * Answer Questions on Ruby Talk
# Thanks for your patience.
# ERB is often used in <tt>.rhtml</tt> files (HTML with embedded Ruby). Notice the need in
# this example to provide a special binding when the template is run, so that the instance
# variables in the Product object can be resolved.
# # Build template data class.
# def initialize( code, name, desc, cost )
# def add_feature( feature )
# # Support templating of member data.
# <head><title>Ruby Toys -- <%= @name %></title></head>
# <h1><%= @name %> (<%= @code %>)</h1>
# <% @features.each do |f| %>
# <li><b><%= f %></b></li>
# <b>Only <%= @cost %>!!!</b>
# Call for a price, today!
# rhtml = ERB.new(template)
# # Set up template data.
# toy = Product.new( "TZ-1002",
# "Geek's Best Friend! Responds to Ruby commands...",
# toy.add_feature("Listens for verbal commands in the Ruby language!")
# toy.add_feature("Ignores Perl, Java, and all C variants.")
# toy.add_feature("Karate-Chop Action!!!")
# toy.add_feature("Matz signature on left leg.")
# toy.add_feature("Gem studded eyes... Rubies, of course!")
# rhtml.run(toy.get_binding)
# <i>Generates (some blank lines removed):</i>
# <head><title>Ruby Toys -- Rubysapien</title></head>
# <h1>Rubysapien (TZ-1002)</h1>
# <p>Geek's Best Friend! Responds to Ruby commands...</p>
# <li><b>Listens for verbal commands in the Ruby language!</b></li>
# <li><b>Ignores Perl, Java, and all C variants.</b></li>
# <li><b>Karate-Chop Action!!!</b></li>
# <li><b>Matz signature on left leg.</b></li>
# <li><b>Gem studded eyes... Rubies, of course!</b></li>
# Call for a price, today!
# There are a variety of templating solutions available in various Ruby projects:
# * ERB's big brother, eRuby, works the same but is written in C for speed;
# * Amrita (smart at producing HTML/XML);
# * cs/Template (written in C for speed);
# * RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere;
# * and others; search {RubyGems.org}[https://rubygems.org/] or
# {The Ruby Toolbox}[https://www.ruby-toolbox.com/].
# Rails, the web application framework, uses ERB to create views.
Revision = '$Date:: 2014-12-12 19:48:57 +0900#$' # :nodoc: #'
# Returns revision information for the erb.rb module.
"erb.rb [2.1.0 #{ERB::Revision.split[1]}]"
# Compiles ERB templates into Ruby code; the compiled code produces the
# template result when evaluated. ERB::Compiler provides hooks to define how
# generated output is handled.
# Internally ERB does something like this to generate the code returned by
# compiler = ERB::Compiler.new('<>')
# compiler.pre_cmd = ["_erbout=''"]
# compiler.put_cmd = "_erbout.concat"
# compiler.insert_cmd = "_erbout.concat"
# compiler.post_cmd = ["_erbout"]
# code, enc = compiler.compile("Got <%= obj %>!\n")
# _erbout=''; _erbout.concat "Got "; _erbout.concat(( obj ).to_s); _erbout.concat "!\n"; _erbout
# By default the output is sent to the print method. For example:
# compiler = ERB::Compiler.new('<>')
# code, enc = compiler.compile("Got <%= obj %>!\n")
# print "Got "; print(( obj ).to_s); print "!\n"
# The compiled code can be used in any context where the names in the code
# correctly resolve. Using the last example, each of these print 'Got It!'
# Evaluate using a variable:
# Evaluate using an input:
# Evaluate using an accessor:
# klass = Class.new Object
# Good! See also ERB#def_method, ERB#def_module, and ERB#def_class.
class PercentLine # :nodoc:
def self.regist_scanner(klass, trim_mode, percent)
@scanner_map[[trim_mode, percent]] = klass
def self.default_scanner=(klass)
def self.make_scanner(src, trim_mode, percent)
klass = @scanner_map.fetch([trim_mode, percent], @default_scanner)
klass.new(src, trim_mode, percent)
def initialize(src, trim_mode, percent)
class TrimScanner < Scanner # :nodoc:
def initialize(src, trim_mode, percent)
@scan_line = self.method(:trim_line1)
@scan_line = self.method(:trim_line2)
@scan_line = self.method(:explicit_trim_line)
@scan_line = self.method(:scan_line)
percent_line(line, &block)
@scan_line.call(@src, &block)
def percent_line(line, &block)
if @stag || line[0] != ?%
return @scan_line.call(line, &block)
@scan_line.call(line, &block)
yield(PercentLine.new(line.chomp))
line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
head = nil if token == "\n"
def explicit_trim_line(line)
line.scan(/(.*?)(^[ \t]*<%\-|<%\-|<%%|%%>|<%=|<%#|<%|-%>\n|-%>|%>|\z)/m) do |tokens|
if @stag.nil? && /[ \t]*<%-/ =~ token
elsif @stag && token == "-%>\n"
elsif @stag && token == '-%>'
ERB_STAG = %w(<%= <%# <%)
Scanner.default_scanner = TrimScanner
class SimpleScanner < Scanner # :nodoc:
@src.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|