Module: BitClust::TextUtils

Included in:
RDCompiler
Defined in:
lib/bitclust/textutils.rb

Overview

Utility for tweaking text.

Constant Summary collapse

INDENT_RE =
{
  2 => /\A {2}/,
  4 => /\A {4}/,
  8 => /\A {8}/
}

Class Method Summary collapse

Class Method Details

.detab(str, ts = 8) ⇒ Object


17
18
19
20
21
22
23
24
# File 'lib/bitclust/textutils.rb', line 17

def detab(str, ts = 8)
  add = 0
  str.gsub(/\t/) {
    len = ts - ($~.begin(0) + add) % ts
    add += len - 1
    ' ' * len
  }
end

.n_indent(line) ⇒ Object


35
36
37
# File 'lib/bitclust/textutils.rb', line 35

def n_indent(line)
  line.slice(/\A\s*/).size
end

.n_minimum_indent(lines) ⇒ Object


31
32
33
# File 'lib/bitclust/textutils.rb', line 31

def n_minimum_indent(lines)
  lines.reject {|line| line.strip.empty? }.map {|line| n_indent(line) }.min
end

.unindent(line, n) ⇒ Object


45
46
47
48
# File 'lib/bitclust/textutils.rb', line 45

def unindent(line, n)
  re = (INDENT_RE[n] ||= /\A {#{n}}/)
  line.sub(re, '')
end

.unindent_block(lines) ⇒ Object


26
27
28
29
# File 'lib/bitclust/textutils.rb', line 26

def unindent_block(lines)
  n = n_minimum_indent(lines)
  lines.map {|line| unindent(line, n) }
end