File: trimming.rb

package info (click to toggle)
ruby-temple 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 476 kB
  • sloc: ruby: 3,347; makefile: 6
file content (24 lines) | stat: -rw-r--r-- 665 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# frozen_string_literal: true
module Temple
  module ERB
    # ERB trimming like in erubis
    # Deletes spaces around '<% %>' and leave spaces around '<%= %>'.
    # @api public
    class Trimming < Filter
      define_options trim: true

      def on_multi(*exps)
        exps = exps.each_with_index.map do |e,i|
          if e.first == :static && i > 0 && exps[i-1].first == :code
            [:static, e.last.lstrip]
          elsif e.first == :static && i < exps.size-1 && exps[i+1].first == :code
            [:static, e.last.rstrip]
          else
            e
          end
        end if options[:trim]
        [:multi, *exps]
      end
    end
  end
end