def convert_table(self, block):
        """"Converts a table to grid table format"""
        lines_orig = block.split('\n')
        lines_orig.pop() # Remove extra newline at end of block
        widest_cell = [] # Will hold the width of the widest cell for each column
        widest_word = [] # Will hold the width of the widest word for each column
        widths = []      # Will hold the computed widths of grid table columns

        rows = []   # Will hold table cells during processing
        lines = []  # Will hold the finished table

        has_border = False  # Will be set to True if this is a bordered table

        width_unit = 0.0 # This number is used to divide up self.width according
                         # to the following formula:
                         #
                         # self.width = width_unit * maxwidth
                         #
                         # Where maxwidth is the sum over all elements of
                         # widest_cell.

        # Only process tables, leave everything else untouched

        if not self.test(None, block):
            return lines_orig

        if lines_orig[0].startswith('|'):
            has_border = True

        # Initialize width arrays

        for i in range(0, len(self._split_row(lines_orig[0], has_border))):
            widest_cell.append(0)
            widest_word.append(0)
            widths.append(0)

        # Parse lines into array of cells and record width of widest cell/word

        for line in lines_orig:
            row = self._split_row(line, has_border)
            # pad widest_cell to account for under length first row
            for i in range(0, len(row) - len(widest_cell)):
                widest_cell.append(0)
                widest_word.append(0)
                widths.append(0)
            for i in range(0, len(row)):
                # Record cell width
                if len(row[i]) > widest_cell[i]:
                    widest_cell[i] = len(row[i])
                # Record longest word
                words = row[i].split()
                for word in words:
                    # Keep URLs from throwing the word length count off too badly.
                    match = re.match(r'\[(.*?)\]\(.*?\)', word)
                    if match:
                       word = match.group(1)

                    if len(word) > widest_word[i]:
                        widest_word[i] = len(word)
            rows.append(row)

        # Remove table header divider line from rows
        rows.pop(1)

        # Compute first approximation of column widths based on maximum cell width

        for width in widest_cell:
            width_unit += float(width)

        width_unit = self.width / width_unit

        for i in range(0, len(widest_cell)):
            widths[i] = int(widest_cell[i] * width_unit)

        # Add rounding errors to narrowest column
        if sum(widths) < self.width:
            widths[widths.index(min(widths))] += self.width - sum(widths)

        # Attempt to correct first approximation of column widths based on
        # words that fail to fit their cell's width (if this fails textwrap
        # will break up long words but since it does not add hyphens this
        # should be avoided)

        for i in range(0, len(widths)):
            if widths[i] < widest_word[i]:
                offset = widest_word[i] - widths[i]
                for j in range(0, len(widths)):
                    if widths[j] - widest_word[j] >= offset:
                        widths[j] -= offset
                        widths[i] += offset
                        offset = 0

        lines.append(self.ruler_line(widths, linetype='-'))

        # Only add header row if it contains more than just whitespace
        if ''.join(rows[0]).strip() != '':
                lines.extend(self.wrap_row(widths, rows[0]))
                lines.append(self.ruler_line(widths, linetype='='))

        for row in rows[1:]:
            # Skip empty rows
            if ''.join(row).strip() == '':
                continue
            lines.extend(self.wrap_row(widths, row))
            lines.append(self.ruler_line(widths, linetype='-'))

        # Append empty line after table
        lines.append('')

        return lines
Converts a table to grid table format