Class: Fastlane::MarkdownTableFormatter

Inherits:
Object
  • Object
show all
Defined in:
fastlane/lib/fastlane/markdown_table_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(string, header = true) ⇒ MarkdownTableFormatter



4
5
6
7
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 4

def initialize(string, header = true)
  @doc = string
  @header = header
end

Instance Method Details

#column_width(column) ⇒ Object



26
27
28
29
30
31
32
33
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 26

def column_width(column)
  width = 0
  table.each do |row|
    length = row[column].strip.length
    width = length if length > width
  end
  width
end

#header_separator_rowObject



43
44
45
46
47
48
49
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 43

def header_separator_row
  output = []
  [*0...table.first.length].each do |column|
    output.push(separator(column_width(column)))
  end
  output
end

#pad(string, length) ⇒ Object



35
36
37
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 35

def pad(string, length)
  string.strip.ljust(length, ' ')
end

#parseObject

converts the markdown string into an array of arrays



10
11
12
13
14
15
16
17
18
19
20
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 10

def parse
  @table = []
  rows = @doc.split(/\r?\n/)
  rows.each do |row|
    row_array = row.split("|")
    row_array.each(&:strip!)
    @table.push(row_array)
  end
  @table.delete_at(1) if @header # strip header separator
  @table
end

#separator(length) ⇒ Object



39
40
41
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 39

def separator(length)
  "".ljust(length, '-')
end

#tableObject



22
23
24
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 22

def table
  @table ||= parse
end

#to_mdObject



51
52
53
54
55
56
57
58
59
60
# File 'fastlane/lib/fastlane/markdown_table_formatter.rb', line 51

def to_md
  output = ""
  t = table.clone
  t.insert(1, header_separator_row) if @header
  t.each_with_index do |row, index|
    row.map!.with_index { |cell_row, index_row| pad(cell_row, column_width(index_row)) }
    output += "#{row.join(' | ').lstrip} |\n"
  end
  output
end