Sha256: 5b2ea7e58f0564fdc6dbdb7080d2f48cdcd28e4b6ea338f9d6cd21b01ebc2788

Contents?: true

Size: 1.09 KB

Versions: 23

Compression:

Stored size: 1.09 KB

Contents

# This is a module compatible with Haml::Helpers.
# It is included by ActionView in initializer.
module Hamlit
  module Helpers
    extend self

    DEFAULT_PRESERVE_TAGS = %w[textarea pre code].freeze

    def find_and_preserve(input = nil, tags = DEFAULT_PRESERVE_TAGS, &block)
      return find_and_preserve(capture_haml(&block), input || tags) if block

      tags = tags.each_with_object('') do |t, s|
        s << '|' unless s.empty?
        s << Regexp.escape(t)
      end

      re = /<(#{tags})([^>]*)>(.*?)(<\/\1>)/im
      input.to_s.gsub(re) do |s|
        s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible
        "<#{$1}#{$2}>#{preserve($3)}</#{$1}>"
      end
    end

    def preserve(input = nil, &block)
      return preserve(capture_haml(&block)) if block
      s = input.to_s.chomp("\n")
      s.gsub!(/\n/, '&#x000A;')
      s.delete!("\r")
      s
    end

    # NOTE: currently Hamlit::Helpers is enabled by default on only
    # Rails environment. Thus you can use capture.
    def capture_haml(*args, &block)
      capture(*args, &block)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
hamlit-1.1.1 lib/hamlit/helpers.rb
hamlit-1.1.0 lib/hamlit/helpers.rb
hamlit-1.0.0 lib/hamlit/helpers.rb
hamlit-0.6.2 lib/hamlit/helpers.rb
hamlit-0.6.1 lib/hamlit/helpers.rb
hamlit-0.6.0 lib/hamlit/helpers.rb
hamlit-0.5.3 lib/hamlit/helpers.rb
hamlit-0.5.2 lib/hamlit/helpers.rb
hamlit-0.5.1 lib/hamlit/helpers.rb
hamlit-0.5.0 lib/hamlit/helpers.rb
hamlit-0.4.3 lib/hamlit/helpers.rb
hamlit-0.4.2 lib/hamlit/helpers.rb
hamlit-0.4.1 lib/hamlit/helpers.rb
hamlit-0.3.4 lib/hamlit/helpers.rb
hamlit-0.3.3 lib/hamlit/helpers.rb
hamlit-0.3.2 lib/hamlit/helpers.rb
hamlit-0.3.1 lib/hamlit/helpers.rb
hamlit-0.3.0 lib/hamlit/helpers.rb
hamlit-0.2.0 lib/hamlit/helpers.rb
hamlit-0.1.3 lib/hamlit/helpers.rb