Module: Haml::Util

Defined in:
lib/haml_lint/extensions/haml_util_unescape_interpolation_tracking.rb

Overview

Haml does heavy transformations to strings that contain interpolation without a way of perfectly inverting that transformation.

We need this monkey patch to have a way of recovering the original strings as they are in the haml files, so that we can use them and then autocorrect them.

The HamlLint::Document carries over a hash of interpolation to original string. The below patches are there to extract said information from Haml’s parsing.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unescape_interpolation_to_original_cacheObject

The cache for the current Thread (technically Fiber)



13
14
15
# File 'lib/haml_lint/extensions/haml_util_unescape_interpolation_tracking.rb', line 13

def self.unescape_interpolation_to_original_cache
  Thread.current[:haml_lint_unescape_interpolation_to_original_cache] ||= {}
end

.unescape_interpolation_to_original_cache_take_and_wipeObject

As soon as a HamlLint::Document has finished processing a HAML souce, this gets called to get a copy of this cache and clear up for the next HAML processing



19
20
21
22
23
# File 'lib/haml_lint/extensions/haml_util_unescape_interpolation_tracking.rb', line 19

def self.unescape_interpolation_to_original_cache_take_and_wipe
  value = unescape_interpolation_to_original_cache.dup
  unescape_interpolation_to_original_cache.clear
  value
end

Instance Method Details

#unescape_interpolation_with_original_tracking(str, escape_html = nil) ⇒ Object Also known as: unescape_interpolation

Overriding the unescape_interpolation method to store the return and original string in the cache.



27
28
29
30
31
# File 'lib/haml_lint/extensions/haml_util_unescape_interpolation_tracking.rb', line 27

def unescape_interpolation_with_original_tracking(str, escape_html = nil)
  value = unescape_interpolation_without_original_tracking(str, escape_html)
  Haml::Util.unescape_interpolation_to_original_cache[value] = str
  value
end