Module: Haml::Shared

Extended by:
Shared
Included in:
Shared
Defined in:
lib/haml/shared.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#balance(scanner, start, finish, count = 0) ⇒ (String, String)

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

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

Parameters:

  • scanner (StringScanner)

    The string scanner to move

  • start (Character)

    The character opening the balanced pair. A ‘Fixnum` in 1.8, a `String` in 1.9

  • finish (Character)

    The character closing the balanced pair. A ‘Fixnum` in 1.8, a `String` in 1.9

  • count (Fixnum) (defaults to: 0)

    The number of opening characters matched before calling this method

Returns:

  • ((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.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/haml/shared.rb', line 41

def balance(scanner, start, finish, count = 0)
  str = ''
  scanner = StringScanner.new(scanner) unless scanner.is_a? StringScanner
  regexp = Regexp.new("(.*?)[\\#{start.chr}\\#{finish.chr}]", Regexp::MULTILINE)
  while scanner.scan(regexp)
    str << scanner.matched
    count += 1 if scanner.matched[-1] == start
    count -= 1 if scanner.matched[-1] == finish
    return [str.strip, scanner.rest] if count == 0
  end
end

#handle_interpolation(str) {|scan| ... } ⇒ String

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`), and the text prior to that as the first (`scan`).

Yield Parameters:

  • scan (StringScanner)

    The scanner scanning through the string

Returns:

  • (String)

    The text remaining in the scanner after all ‘#{`s have been processed



18
19
20
21
22
# File 'lib/haml/shared.rb', line 18

def handle_interpolation(str)
  scan = StringScanner.new(str)
  yield scan while scan.scan(/(.*?)(\\*)\#\{/)
  scan.rest
end

#human_indentation(indentation, was = false) ⇒ String

Formats a string for use in error messages about indentation.

Parameters:

  • indentation (String)

    The string used for indentation

  • was (Boolean) (defaults to: false)

    Whether or not to add ‘“was”` or `“were”` (depending on how many characters were in `indentation`)

Returns:

  • (String)

    The name of the indentation (e.g. ‘“12 spaces”`, `“1 tab”`)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/haml/shared.rb', line 59

def human_indentation(indentation, was = false)
  if !indentation.include?(?\t)
    noun = 'space'
  elsif !indentation.include?(?\s)
    noun = 'tab'
  else
    return indentation.inspect + (was ? ' was' : '')
  end

  singular = indentation.length == 1
  if was
    was = singular ? ' was' : ' were'
  else
    was = ''
  end

  "#{indentation.length} #{noun}#{'s' unless singular}#{was}"
end