Skip to content
This repository has been archived by the owner on Jan 14, 2022. It is now read-only.

Utilities for creating and displaying Markdown tables in Ruby

License

Notifications You must be signed in to change notification settings

christopher-dG/markdown-tables

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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      |
# |===============|=========|==========|