Sha256: 091b8b443af019e6047d414ea81b78e57dbb0bf94bbd53f6c643fd496a6187d8

Contents?: true

Size: 807 Bytes

Versions: 6

Compression:

Stored size: 807 Bytes

Contents

module Temple
  module ERB
    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

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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
temple-0.2.0 lib/temple/erb/trimming.rb
temple-0.1.8 lib/temple/erb/trimming.rb
temple-0.1.7 lib/temple/erb/trimming.rb
temple-0.1.6 lib/temple/erb/trimming.rb
temple-0.1.5 lib/temple/erb/trimming.rb
temple-0.1.4 lib/temple/erb/trimming.rb