Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Add github flavored markdown table implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Oct 16, 2015
1 parent c4f19d2 commit 31c6038
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion terminaltables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
https://pypi.python.org/pypi/terminaltables
"""

from terminaltables.tables import AsciiTable, DoubleTable, SingleTable
from terminaltables.tables import AsciiTable, DoubleTable, GithubFlavoredMarkdownTable, SingleTable


assert AsciiTable
assert DoubleTable
assert SingleTable
assert GithubFlavoredMarkdownTable
65 changes: 64 additions & 1 deletion terminaltables/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os

from terminaltables.base_table import BaseTable
from terminaltables.base_table import BaseTable, join_row


class AsciiTable(BaseTable):
Expand Down Expand Up @@ -92,3 +92,66 @@ class DoubleTable(WindowsTableDouble):
"""Cross-platform table with box-drawing characters. On Windows it's double borders, on Linux/OSX it's unicode."""

pass


class GithubFlavoredMarkdownTable(BaseTable):
"""Github flavored markdown table.
https://help.github.com/articles/github-flavored-markdown/#tables
"""

CHAR_VERTICAL = '|'
CHAR_HORIZONTAL = '-'

def __init__(self, table_data):
"""Constructor.
:param iter table_data: List (empty or list of lists of strings) representing the table.
"""
self.table_data = table_data
self.justify_columns = dict() # {0: 'right', 1: 'left', 2: 'center'}
self.padding_left = 1
self.padding_right = 1

@property
def table(self):
"""Return a large string of the entire table ready to be printed to the terminal."""
padded_table_data = self.padded_table_data
column_widths = [c + self.padding_left + self.padding_right for c in self.column_widths]
final_table_data = list()

for row_index, row_data in enumerate(padded_table_data):
row = join_row(
row_data,
self.CHAR_VERTICAL,
self.CHAR_VERTICAL,
self.CHAR_VERTICAL
)
final_table_data.append(row)

if row_index != 0:
continue

# Header row separator.
column_separators = []
for column_index, column_width in enumerate(column_widths):
column_justify = self.justify_columns.get(column_index)
if column_justify == 'left':
separator = ':' + self.CHAR_HORIZONTAL * (column_width - 1)
elif column_justify == 'right':
separator = self.CHAR_HORIZONTAL * (column_width - 1) + ':'
elif column_justify == 'center':
separator = self.CHAR_HORIZONTAL * (column_width - 2)
separator = ':' + separator + ':'
else:
separator = self.CHAR_HORIZONTAL * column_width
column_separators.append(separator)
row = join_row(
column_separators,
self.CHAR_VERTICAL,
self.CHAR_VERTICAL,
self.CHAR_VERTICAL
)
final_table_data.append(row)

return '\n'.join(final_table_data)
53 changes: 53 additions & 0 deletions tests/test_tables_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Test end to end showing Github flavored markdown table."""

from terminaltables import GithubFlavoredMarkdownTable


def test_simple():
"""Simple GithubFlavoredMarkdownTable test."""
table_data = [
['Name', 'Color', 'Type'],
['Avocado', 'green', 'nut'],
['Tomato', 'red', 'fruit'],
['Lettuce', 'green', 'vegetable'],
]
table = GithubFlavoredMarkdownTable(table_data)

expected = (
'| Name | Color | Type |\n'
'|---------|-------|-----------|\n'
'| Avocado | green | nut |\n'
'| Tomato | red | fruit |\n'
'| Lettuce | green | vegetable |'
)
assert expected == table.table

table.justify_columns[0] = 'center'
expected = (
'| Name | Color | Type |\n'
'|:-------:|-------|-----------|\n'
'| Avocado | green | nut |\n'
'| Tomato | red | fruit |\n'
'| Lettuce | green | vegetable |'
)
assert expected == table.table

table.justify_columns[1] = 'left'
expected = (
'| Name | Color | Type |\n'
'|:-------:|:------|-----------|\n'
'| Avocado | green | nut |\n'
'| Tomato | red | fruit |\n'
'| Lettuce | green | vegetable |'
)
assert expected == table.table

table.justify_columns[2] = 'right'
expected = (
'| Name | Color | Type |\n'
'|:-------:|:------|----------:|\n'
'| Avocado | green | nut |\n'
'| Tomato | red | fruit |\n'
'| Lettuce | green | vegetable |'
)
assert expected == table.table

0 comments on commit 31c6038

Please sign in to comment.