Sha256: c56077505618c650d4c8a34224d3870a8c9bdf796f476b3f3d2b7d13b1138fdb

Contents?: true

Size: 1017 Bytes

Versions: 4

Compression:

Stored size: 1017 Bytes

Contents

module Temple
  module ERB
    # ERB trimming
    # Set option :trim_mode to
    #    <> - omit newline for lines starting with <% and ending in %>
    #    >  - omit newline for lines ending in %>
    #
    # @api public
    class Trimming < Filter
      def on_multi(*exps)
        case options[:trim_mode]
        when '>'
          exps.each_cons(2) do |a, b|
            if code?(a) && static?(b)
              b[1].gsub!(/^\n/, '')
            end
          end
        when '<>'
          exps.each_with_index do |exp, i|
            if code?(exp) &&
                (!exps[i-1] || static?(exps[i-1]) && exps[i-1][1] =~ /\n$/) &&
                (exps[i+1] && static?(exps[i+1]) && exps[i+1][1] =~ /^\n/)
              exps[i+1][1].gsub!(/^\n/, '') if exps[i+1]
            end
          end
        end
        [:multi, *exps]
      end

      protected

      def code?(exp)
        exp[0] == :escape || exp[0] == :code
      end

      def static?(exp)
        exp[0] == :static
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
temple-0.3.4 lib/temple/erb/trimming.rb
temple-0.3.2 lib/temple/erb/trimming.rb
temple-0.3.1 lib/temple/erb/trimming.rb
temple-0.3.0 lib/temple/erb/trimming.rb