0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Utilities for creating and displaying Markdown tables in Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Markdown Tables

Gem Travis CI Codecov

Utilities for creating and displaying Markdown tables in Ruby.

Installation

  • gem install markdown-tables

Usage

make_table

You can create a Markdown table from an array of column labels and a two-dimensional array of cell data.

data is assumed to be a list of columns. If you'd rather pass data as a list of rows, use the is_rows flag.

To specify the cell alignment, pass align: 'l' for left alignment and 'r' for right alignment. Any other value (or lack thereof) will result in centered cells.

You can also pass an array of alignment values, such as ['l', 'c', 'r'] to set alignment per column.

To leave a cell empty, use nil or an empty string.

require 'markdown-tables'

labels = ['a', 'b', 'c']
data = [[1, 2, 3], [4 ,5, 6], [7, 8, 9]]

puts MarkdownTables.make_table(labels, data)
# |a|b|c|
# |:-:|:-:|:-:|
# |1|4|7|
# |2|5|8|
# |3|6|9|

puts MarkdownTables.make_table(labels, data, is_rows: true)
# |a|b|c|
# |:-:|:-:|:-:|
# |1|2|3|
# |4|5|6|
# |7|8|9|

plain_text

Once you've generated a Markdown table, you can use it to produce a human-readable version of the table.

require 'markdown-tables'

labels = ['unnecessarily', 'lengthy', 'sentence']
data = [['the', 'quick', 'brown'], ['fox', 'jumps', 'over'], ['the', 'lazy', 'dog']]

table = MarkdownTables.make_table(labels, data, is_rows: true, align: ['r', 'c', 'l'])

puts MarkdownTables.plain_text(table)
# |===============|=========|==========|
# | unnecessarily | lengthy | sentence |
# |===============|=========|==========|
# |           the |  quick  | brown    |
# |---------------|---------|----------|
# |           fox |  jumps  | over     |
# |---------------|---------|----------|
# |           the |  lazy   | dog      |
# |===============|=========|==========|