Module | Haml::Shared
In: | lib/haml/shared.rb

This module contains functionality that‘s shared between Haml and Sass.

### Methods

balance handle_interpolation human_indentation

### Public Instance methods

balance(scanner, start, finish, count = 0)

Moves a scanner through a balanced pair of characters. For example:

        Foo (Bar (Baz bang) bop) (Bang (bop bip))
        ^                       ^
        from                    to

@param scanner [StringScanner] The string scanner to move @param start [Character] The character opening the balanced pair.

      A `Fixnum` in 1.8, a `String` in 1.9

@param finish [Character] The character closing the balanced pair.

      A `Fixnum` in 1.8, a `String` in 1.9

@param count [Fixnum] The number of opening characters matched

      before calling this method

@return [(String, String)] The string matched within the balanced pair

      and the rest of the string.
      `["Foo (Bar (Baz bang) bop)", " (Bang (bop bip))"]` in the example above.

[Source]

        # File lib/haml/shared.rb, line 41
    41:     def balance(scanner, start, finish, count = 0)
    42:       str = ''
    43:       scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner
    44:       regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
    45:       while scanner.scan(regexp)
    46:         str << scanner.matched
    47:         count += 1 if scanner.matched[-1] == start
    48:         count -= 1 if scanner.matched[-1] == finish
    49:         return [str.strip, scanner.rest] if count == 0
    50:       end
    51:     end

handle_interpolation(str) {|scan while scan.scan(/(.*?)(\\*)\| ...}

Scans through a string looking for the interoplation-opening `#{` and, when it‘s found, yields the scanner to the calling code so it can handle it properly.

The scanner will have any backslashes immediately in front of the `#{` as the second capture group (`scan[2]`), and the text prior to that as the first (`scan[1]`).

@yieldparam scan [StringScanner] The scanner scanning through the string @return [String] The text remaining in the scanner after all `#{`s have been processed

[Source]

        # File lib/haml/shared.rb, line 18
    18:     def handle_interpolation(str)
    19:       scan = StringScanner.new(str)
    20:       yield scan while scan.scan(/(.*?)(\\*)\#\{/)
    21:       scan.rest
    22:     end

human_indentation(indentation, was = false)

Formats a string for use in error messages about indentation.

@param indentation [String] The string used for indentation @param was [Boolean] Whether or not to add `"was"` or `"were"`

      (depending on how many characters were in `indentation`)

@return [String] The name of the indentation (e.g. `"12 spaces"`, `"1 tab"`)

[Source]

        # File lib/haml/shared.rb, line 59
    59:     def human_indentation(indentation, was = false)
    60:       if !indentation.include?(?\t)
    61:         noun = 'space'
    62:       elsif !indentation.include?(?\s)
    63:         noun = 'tab'
    64:       else
    65:         return indentation.inspect + (was ? ' was' : '')
    66:       end
    67:
    68:       singular = indentation.length == 1
    69:       if was
    70:         was = singular ? ' was' : ' were'
    71:       else
    72:         was = ''
    73:       end
    74:
    75:       "#{indentation.length} #{noun}#{'s' unless singular}#{was}"
    76:     end

[Validate]