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

Add github flavored markdown table implementation. #12

Merged
merged 1 commit into from
Oct 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
63 changes: 62 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,64 @@ 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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Robpol86 CI warns for this issue: W:106, 4: __init__ method from base class 'BaseTable' is not called (super-init-not-called), but I think there is no need to call the super here.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should still stick super() it in there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Robpol86 ok, fixed now.

"""Constructor.

:param iter table_data: List (empty or list of lists of strings) representing the table.
"""
# Github flavored markdown table won't support title.
super(GithubFlavoredMarkdownTable, self).__init__(table_data)

@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