Sha256: 1dc6ce690efb66a7942362a54e61c4ed098b0d3374a12799de875402256b10dd

Contents?: true

Size: 915 Bytes

Versions: 18

Compression:

Stored size: 915 Bytes

Contents

#
# bitclust/textutils
#
# Copyright (c) 2006 Minero Aoki
#
# This program is free software.
# You can distribute/modify this program under the Ruby License.
#

module BitClust

  # Utility for tweaking text.
  module TextUtils

    module_function

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

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

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

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

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

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

  end

end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
bitclust-core-1.2.6 lib/bitclust/textutils.rb
bitclust-core-1.2.5 lib/bitclust/textutils.rb
bitclust-core-1.2.4 lib/bitclust/textutils.rb
bitclust-core-1.2.3 lib/bitclust/textutils.rb
bitclust-core-1.2.2 lib/bitclust/textutils.rb
bitclust-core-1.2.1 lib/bitclust/textutils.rb
bitclust-core-1.2.0 lib/bitclust/textutils.rb
bitclust-core-1.1.1 lib/bitclust/textutils.rb
bitclust-core-1.1.0 lib/bitclust/textutils.rb
bitclust-core-1.0.0 lib/bitclust/textutils.rb
bitclust-core-0.9.6 lib/bitclust/textutils.rb
bitclust-core-0.9.5 lib/bitclust/textutils.rb
bitclust-core-0.9.4 lib/bitclust/textutils.rb
bitclust-core-0.9.3 lib/bitclust/textutils.rb
bitclust-core-0.9.2 lib/bitclust/textutils.rb
bitclust-core-0.9.1 lib/bitclust/textutils.rb
bitclust-core-0.9.0 lib/bitclust/textutils.rb
bitclust-core-0.8.0 lib/bitclust/textutils.rb