Module: Haml::Preserver Private

Defined in:
lib/haml/preserver.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

find_and_preserve, shared by compiled templates and RailsHelpers.

Class Method Summary collapse

Class Method Details

.find_and_preserve(input, tags)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
47
48
49
50
# File 'lib/haml/preserver.rb', line 43

def self.find_and_preserve(input, tags)
  re = regex(tags)
  input.to_s.gsub(re) do |s|
    s =~ re # Can't rely on $1, etc. existing since Rails' SafeBuffer#gsub is incompatible
    name, attributes, content = $1, $2, $3
    "<#{name}#{attributes}>#{yield(content)}</#{name}>"
  end
end

.regex(tags)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The cache is replaced, never mutated, so a racing writer at worst rebuilds a regex.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/haml/preserver.rb', line 29

def self.regex(tags)
  return DEFAULT_REGEX if tags.equal?(DEFAULT_TAGS)

  cache = @regexes
  cache[tags] || begin
    regex = build_regex(tags)
    # Copied so a caller mutating its own list, or a String in it, cannot strand the entry.
    key = tags.map { |tag| tag.dup.freeze }.freeze
    cache = INITIAL_REGEXES if cache.size >= MAX_REGEXES
    @regexes = cache.merge(key => regex).freeze
    regex
  end
end