Class: CLIMarkdown::MDTableCleanup
- Inherits:
-
Object
- Object
- CLIMarkdown::MDTableCleanup
- Defined in:
- lib/mdless/tables.rb
Constant Summary collapse
- PAD_CHAR =
'⎕'
Instance Method Summary collapse
- #column_width(i) ⇒ Object
- #column_widths ⇒ Object
- #header_separator_row ⇒ Object
-
#initialize(input) ⇒ MDTableCleanup
constructor
A new instance of MDTableCleanup.
- #pad(string, type, length) ⇒ Object
- #parse ⇒ Object
- #separator(length, alignment) ⇒ Object
- #table ⇒ Object
- #table_border ⇒ Object
- #to_md ⇒ Object
Constructor Details
#initialize(input) ⇒ MDTableCleanup
Returns a new instance of MDTableCleanup.
6 7 8 9 |
# File 'lib/mdless/tables.rb', line 6 def initialize(input) @string = input @format_row = [] end |
Instance Method Details
#column_width(i) ⇒ Object
63 64 65 66 |
# File 'lib/mdless/tables.rb', line 63 def column_width(i) @widths ||= column_widths @widths[i] end |
#column_widths ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mdless/tables.rb', line 68 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_row ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/mdless/tables.rb', line 112 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
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mdless/tables.rb', line 84 def pad(string,type,length) string.strip! if type == 'center' string.center(length, PAD_CHAR) elsif type == 'right' string.rjust(length, PAD_CHAR) elsif type == 'left' string.ljust(length, PAD_CHAR) else string.ljust(length, PAD_CHAR) end end |
#parse ⇒ Object
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 57 |
# File 'lib/mdless/tables.rb', line 11 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| cell.strip! 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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mdless/tables.rb', line 97 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 |
#table ⇒ Object
59 60 61 |
# File 'lib/mdless/tables.rb', line 59 def table @table ||= parse end |
#table_border ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/mdless/tables.rb', line 120 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_md ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/mdless/tables.rb', line 128 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.insert(0, table_border) output.push(table_border) output.join("\n").gsub(/((?<=\| )#{PAD_CHAR}+|#{PAD_CHAR}+(?= \|))/) {|m| " "*m.length } end |