module Haml::Shared

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

Returns:
  • ((String, String)) - The string matched within the balanced pair

Parameters:
  • count (Fixnum) -- The number of opening characters matched
  • finish (Character) -- The character closing the balanced pair.
  • start (Character) -- The character opening the balanced pair.
  • scanner (StringScanner) -- The string scanner to move
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