Module: ImmosquareCleaner::Markdown

Defined in:
lib/immosquare-cleaner/markdown.rb

Class Method Summary collapse

Class Method Details

.clean(file_path) ⇒ Object



5
6
7
8
9
10
11
12
13
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
# File 'lib/immosquare-cleaner/markdown.rb', line 5

def clean(file_path)
  results        = []
  array_to_parse = []
  lines          = []

  ##============================================================##
  ## We parse each line of the file
  ##============================================================##
  File.foreach(file_path) do |current_line|
    ##============================================================##
    ## We save the last line to know if we need to add a newline
    ##============================================================##
    previous_line = lines.last
    lines << current_line

    ##============================================================##
    ## We add the line to the array if it starts with a pipe
    ##============================================================##
    if current_line.lstrip.start_with?("|")
      array_to_parse << current_line
    else
      if !array_to_parse.empty?
        results << cleaned_array(array_to_parse)
        array_to_parse = []
      end
      new_lines = cleaned_line(previous_line, current_line)
      results += new_lines
    end
  end

  ##============================================================##
  ## Handle the case where the file ends with a table
  ##============================================================##
  results << cleaned_array(array_to_parse) if !array_to_parse.empty?

  results.join
end