Class: CLIMarkdown::MDTableCleanup

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

Constant Summary collapse

PAD_CHAR =
"\u00A0"

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ MDTableCleanup

Returns a new instance of MDTableCleanup.



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

def initialize(input)
  @string = input
end

Instance Method Details

#column_width(idx) ⇒ Object



62
63
64
65
# File 'lib/mdless/tables.rb', line 62

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

#column_widthsObject



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

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

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

  @widths
end

#header_separator_rowObject



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

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, alignment, length) ⇒ Object



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

def pad(string, alignment, length)
  naked = string.uncolor.remove_pre_post.strip
  case alignment
  when :center
    naked.strip.center(length, PAD_CHAR).sub(/#{Regexp.escape(naked)}/, string)
  when :right
    naked.strip.rjust(length, PAD_CHAR).sub(/#{Regexp.escape(naked)}/, string)
  when :left
    naked.strip.ljust(length, PAD_CHAR).sub(/#{Regexp.escape(naked)}/, string)
  else
    naked.strip.ljust(length, PAD_CHAR).sub(/#{Regexp.escape(naked)}/, string)
  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
55
56
# File 'lib/mdless/tables.rb', line 9

def parse
  @format_row = []
  @table = []
  fmt = []
  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 =~ /^[\|:\- ]+$/
      fmt = row_array
    else
      @table.push row_array
    end
    cols = row_array.length if row_array.length > cols
  end

  fmt.each_with_index do |cell, i|
    cell.strip!
    f = case cell
      when /^:.*?:$/
        :center
      when /[^:]+:$/
        :right
      else
        :just
      end
    @format_row.push(f)
  end

  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



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

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

#tableObject



58
59
60
# File 'lib/mdless/tables.rb', line 58

def table
  @table ||= parse
end

#table_borderObject



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

def table_border
  output = []
  @format_row.each_with_index do |column, i|
    output.push separator(column_width(i), column)
  end
  "+#{output.join("+").gsub(/:/, "-")}+"
end

#to_mdObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mdless/tables.rb', line 127

def to_md
  output = []
  t = table.clone
  t.each do |row|
    new_row = row.map.with_index { |cell, i| pad(cell, @format_row[i], column_width(i)) }.join(" | ")
    output.push("| #{new_row} |")
  end
  output.insert(1, header_separator_row)
  output.insert(0, table_border)
  output.push(table_border)
  output.join("\n")
end