Sha256: 465a706146339d4ced40dacb6aa030b8588a7f1f6d65f5847c9c3a3eef0292b1

Contents?: true

Size: 1.4 KB

Versions: 657

Compression:

Stored size: 1.4 KB

Contents

module Fastlane
  class MarkdownTableFormatter
    # taken from: https://github.com/benbalter/markdown-table-formatter
    def initialize(string, header = true)
      @doc = string
      @header = header
    end

    # converts the markdown string into an array of arrays
    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

    def table
      @table ||= parse
    end

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

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

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

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

    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
  end
end

Version data entries

657 entries across 657 versions & 5 rubygems

Version Path
fastlane-2.132.0.beta.20190920200012 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190919200100 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190918200023 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190917200011 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190916200055 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190915200038 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190914200034 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190913200058 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.132.0.beta.20190912200052 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190911200010 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190910200104 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190909200058 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190908200041 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190907200030 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190906200026 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190905200113 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190904200040 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190903200013 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.131.0.beta.20190902200022 fastlane/lib/fastlane/markdown_table_formatter.rb