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



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

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



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

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

.n_minimum_indent(lines) ⇒ Object



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

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

.unindent(line, n) ⇒ Object



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

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

.unindent_block(lines) ⇒ Object



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

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