Sha256: b1b765231d71e548132f128e850f9b77b30d695446897c796281544df391b40a

Contents?: true

Size: 1.4 KB

Versions: 468

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

468 entries across 468 versions & 1 rubygems

Version Path
fastlane-2.74.1 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.74.0 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.74.0.beta.20180108010004 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.74.0.beta.20180107010004 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.74.0.beta.20180106010004 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.73.0 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.73.0.beta.20180105010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.73.0.beta.20180104010004 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.73.0.beta.20180103010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0.beta.20180102010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0.beta.20180101010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0.beta.20171231010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.71.1 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0.beta.20171230010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0.beta.20171229010003 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.72.0.beta.20171228010004 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.71.0 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.71.0.beta.20171227010004 fastlane/lib/fastlane/markdown_table_formatter.rb
fastlane-2.71.0.beta.20171226010004 fastlane/lib/fastlane/markdown_table_formatter.rb