Module: Haml::Shared

Defined in:
lib/haml/shared.rb

Overview

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

Class Method Summary collapse

Class Method Details

.balance(scanner, start, finish, count = 0) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/haml/shared.rb', line 13

def self.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) ⇒ Object



7
8
9
10
11
# File 'lib/haml/shared.rb', line 7

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

.human_indentation(indentation, was = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/haml/shared.rb', line 25

def self.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