Class: CLIMarkdown::MDTableCleanup

Inherits:
Object
  • Object
show all
Defined in:
lib/mdless/tables.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ MDTableCleanup

Returns a new instance of MDTableCleanup.



4
5
6
7
# File 'lib/mdless/tables.rb', line 4

def initialize(input)
  @string = input
  @format_row = []
end

Instance Method Details

#column_width(i) ⇒ Object



60
61
62
63
# File 'lib/mdless/tables.rb', line 60

def column_width(i)
  @widths ||= column_widths
  @widths[i]
end

#column_widthsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mdless/tables.rb', line 65

def column_widths
  @widths = []
  @format_row.length.times do
    @widths.push(0)
  end

  table.each do |row|
    @format_row.each_with_index do |cell, i|
      length = row[i].strip.length
      @widths[i] = length if length > @widths[i]
    end
  end

  @widths
end

#header_separator_rowObject



109
110
111
112
113
114
115
# File 'lib/mdless/tables.rb', line 109

def header_separator_row
  output = []
  @format_row.each_with_index do |column, i|
    output.push separator(column_width(i), column)
  end
  "|#{output.join('|')}|"
end

#pad(string, type, length) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mdless/tables.rb', line 81

def pad(string,type,length)
  string.strip!
  if type == 'center'
    string.center(length, ' ')
  elsif type == 'right'
    string.rjust(length, ' ')
  elsif type == 'left'
    string.ljust(length, ' ')
  else
    string.ljust(length, ' ')
  end
end

#parseObject



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
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/mdless/tables.rb', line 9

def parse
  @table = []
  format = []
  cols = 0
  rows = @string.split(/\r?\n/)
  rows.each do |row|
    row.strip!
    row.sub!(/^\s*\|?/,'').sub!(/\|?\s*$/,'')
    row_array = row.split(/\|/)
    row_array.map! { |cell| cell.strip }
    if row =~ /^[\|:\- ]+$/
      format = row_array
    else
      @table.push row_array
    end
    cols = row_array.length if row_array.length > cols
  end

  format.each_with_index {|cell, i|
    f = 'left'
    if cell =~ /^:.*?:$/
      f = 'center'
    elsif cell =~ /:$/
      f = 'right'
    else
      f = 'just'
    end
    @format_row.push(f)
  }

  if @format_row.length < cols
    (cols - @format_row.length).times do
      @format_row.push('left')
    end
  end

  @table.map! do |row|
    if row.length < cols
      (cols - row.length).times do
        row.push("")
      end
    end
    row
  end
  @table
end

#separator(length, alignment) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mdless/tables.rb', line 94

def separator(length, alignment)
  out = "".ljust(length,'-')
  case alignment
  when 'left'
    out = ':' + out + '-'
  when 'right'
    out = '-' + out + ':'
  when 'center'
    out = ":#{out}:"
  else
    out = "-#{out}-"
  end
  out
end

#tableObject



56
57
58
# File 'lib/mdless/tables.rb', line 56

def table
  @table ||= parse
end

#to_mdObject



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mdless/tables.rb', line 117

def to_md
  output = []
  t = table.clone

  t.each_with_index do |row, index|
    row.map!.with_index { |cell, i| cell = pad(cell, @format_row[i], column_width(i)) }
    output.push("| #{row.join(' | ').lstrip} |")
  end
  output.insert(1, header_separator_row)
  output.join("\n")
end