Module: CsvToMd

Defined in:
lib/csvtomd.rb

Class Method Summary collapse

Class Method Details

.calc_column_countObject



36
37
38
39
40
41
42
43
# File 'lib/csvtomd.rb', line 36

def calc_column_count
  count = 0
  @text.lines.each do |line|
    item_count = line.split(',').length
    count = item_count if item_count > count
  end
  @column_count = count
end

.column_countObject



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

def column_count
  @column_count ||= calc_column_count
end

.convert(text) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/csvtomd.rb', line 5

def convert(text)
  return nil unless text.class == String
  
  @column_count = nil
  @text = text

  if @text.lines.count > 1
    table_lines = @text.lines.map { |l| convert_line(l) }
    table_lines.insert(1, separator)
    table_lines.join("\n")
  else
    @text
  end
end

.convert_line(line) ⇒ Object



20
21
22
23
24
25
# File 'lib/csvtomd.rb', line 20

def convert_line(line)
  tokens = line.chomp.split(",")
  (column_count - tokens.length).times { tokens << "" }
  table_line = tokens.join("|")
  "|#{table_line}|"
end

.separatorObject



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

def separator
  sep = (["---"]*column_count).join("|")
  "|#{sep}|"
end