Module: GrassGis::Support

Defined in:
lib/grassgis/support.rb

Class Method Summary collapse

Class Method Details

.unindent(text, indent = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/grassgis/support.rb', line 5

def unindent(text, indent = nil)
  text = text.gsub(/\t/, ' '*8)
  mx = text.scan(/^ *[^\n\r]/)
           .flatten
           .map{ |s| s[-1,1]==' ' ? nil : (s.size-1) }
           .compact.min
  if mx && mx>0
    text.gsub!(/^ {1,#{mx}}/, "")
  end
  lines = text.split(/\r?\n/)
  if lines.first.strip.empty? || lines.last.strip.empty?
    lines.shift while lines.first.strip.empty?
    lines.pop while lines.last.strip.empty?
  end
  if indent
    indent = ' ' * indent if indent.kind_of?(Numeric)
    lines = lines.map { |line| "#{indent}#{line}" }
  end
  lines.join("\n")
end