Module: Litbuild::StringIndentation

Included in:
AsciiDocVisitor, BashScriptVisitor, BlueprintParser
Defined in:
lib/litbuild/string_indentation.rb

Instance Method Summary collapse

Instance Method Details

#indent_for(line) ⇒ Object

Utility method to find the amount of blank space at the beginning of a line.



7
8
9
# File 'lib/litbuild/string_indentation.rb', line 7

def indent_for(line)
  /^([[:blank:]]*).*/.match(line)[1].size
end

#strip_indentation_from_array(strings) ⇒ Object

Utility method to strip the common indentation from all strings in an array. Returns both the stripped strings and the amount of indentation removed.



14
15
16
17
# File 'lib/litbuild/string_indentation.rb', line 14

def strip_indentation_from_array(strings)
  common_indent = strings.map { |l| indent_for(l) }.min
  [strings.map { |l| l.slice(common_indent..-1) }, common_indent]
end

#strip_indentation_from_string(string) ⇒ Object

Utility method to strip the common indentation from all lines of a multi-line string. (Does not return the amount of indentation removed.)



22
23
24
# File 'lib/litbuild/string_indentation.rb', line 22

def strip_indentation_from_string(string)
  strip_indentation_from_array(string.lines)[0]
end

#strip_indentation_from_value(value) ⇒ Object

Utility method to strip the common indentation from all lines of a directive value (which is expected to be an array containing a single string element)



29
30
31
# File 'lib/litbuild/string_indentation.rb', line 29

def strip_indentation_from_value(value)
  strip_indentation_from_string(value[0])
end