1"""GithubFlavoredMarkdownTable class."""
2
3from terminaltables.ascii_table import AsciiTable
4from terminaltables.build import combine
5
6
7class GithubFlavoredMarkdownTable(AsciiTable):
8    """Github flavored markdown table.
9
10    https://help.github.com/articles/github-flavored-markdown/#tables
11
12    :ivar iter table_data: List (empty or list of lists of strings) representing the table.
13    :ivar dict justify_columns: Horizontal justification. Keys are column indexes (int). Values are right/left/center.
14    """
15
16    def __init__(self, table_data):
17        """Constructor.
18
19        :param iter table_data: List (empty or list of lists of strings) representing the table.
20        """
21        # Github flavored markdown table won't support title.
22        super(GithubFlavoredMarkdownTable, self).__init__(table_data)
23
24    def horizontal_border(self, _, outer_widths):
25        """Handle the GitHub heading border.
26
27        E.g.:
28        |:---|:---:|---:|----|
29
30        :param _: Unused.
31        :param iter outer_widths: List of widths (with padding) for each column.
32
33        :return: Prepared border strings in a generator.
34        :rtype: iter
35        """
36        horizontal = str(self.CHAR_INNER_HORIZONTAL)
37        left = self.CHAR_OUTER_LEFT_VERTICAL
38        intersect = self.CHAR_INNER_VERTICAL
39        right = self.CHAR_OUTER_RIGHT_VERTICAL
40
41        columns = list()
42        for i, width in enumerate(outer_widths):
43            justify = self.justify_columns.get(i)
44            width = max(3, width)  # Width should be at least 3 so justification can be applied.
45            if justify == 'left':
46                columns.append(':' + horizontal * (width - 1))
47            elif justify == 'right':
48                columns.append(horizontal * (width - 1) + ':')
49            elif justify == 'center':
50                columns.append(':' + horizontal * (width - 2) + ':')
51            else:
52                columns.append(horizontal * width)
53
54        return combine(columns, left, intersect, right)
55
56    def gen_table(self, inner_widths, inner_heights, outer_widths):
57        """Combine everything and yield every line of the entire table with borders.
58
59        :param iter inner_widths: List of widths (no padding) for each column.
60        :param iter inner_heights: List of heights (no padding) for each row.
61        :param iter outer_widths: List of widths (with padding) for each column.
62        :return:
63        """
64        for i, row in enumerate(self.table_data):
65            # Yield the row line by line (e.g. multi-line rows).
66            for line in self.gen_row_lines(row, 'row', inner_widths, inner_heights[i]):
67                yield line
68            # Yield heading separator.
69            if i == 0:
70                yield self.horizontal_border(None, outer_widths)
71