Class: TextHandler

Inherits:
Object show all
Defined in:
lib/text_handler.rb

Class Method Summary collapse

Class Method Details

.normalize_line(string) ⇒ Object



9
10
11
12
# File 'lib/text_handler.rb', line 9

def self.normalize_line(string)
    string.strip!
    string.squeeze(' ')
end

.normalize_text(text) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/text_handler.rb', line 14

def self.normalize_text(text)
    if !text.is_a?(String) && !text.is_a?(Array)
        raise "Argument must be a String or a Array"
        return ''
    end

    if text.is_a? String
        # Trying to normalize for different OSes
        text = text.gsub(/\015\012?/, "\n")
        text = text.split(/\n/) # Now it's an Array
    end
    
    text = TextHandler.remove_padding_lines(text)
    new_text = Array.new
    found_double = false
    text.each_with_index do |line, i|
        next_line = text[i+1]
        if line.blank?
            if next_line.blank?
                found_double = true
                new_text << "$doubled$"
            else
                new_text << "$single$"
            end
        else
            new_text << TextHandler.normalize_line(line)
        end
    end
    new_text.select! do |line|
        found_double ? (line != "$single$") : true
    end

    new_text = new_text.chunk{|n| n.gsub("$doubled$", "")}.map(&:first)
    new_text = new_text.map{|n| n.gsub("$single$", "") }
    new_text
end