| 78 | |
| 79 | |
| 80 | def parse_table(table_info): |
| 81 | if isinstance(table_info, str): |
| 82 | lines = table_info.strip().split("\n") |
| 83 | elif isinstance(table_info, list): |
| 84 | lines = table_info |
| 85 | |
| 86 | rows = [] |
| 87 | current_rows = [] |
| 88 | divider_count = 0 |
| 89 | header = None |
| 90 | |
| 91 | for i, line in enumerate(lines): |
| 92 | line = line.strip() |
| 93 | # Log output |
| 94 | if line.startswith("["): |
| 95 | continue |
| 96 | if line.startswith("+-"): |
| 97 | if divider_count == 1 and header is None and len(current_rows) == 1: |
| 98 | # The first row was probably a header |
| 99 | header = current_rows.pop() |
| 100 | elif divider_count > 0: |
| 101 | # Commit rows to table |
| 102 | rows.extend(current_rows) |
| 103 | current_rows = [] |
| 104 | divider_count += 1 |
| 105 | # End early |
| 106 | if ((divider_count == 3) or (divider_count == 2 and header is None)) and (i + 1) < len(lines): |
| 107 | return Table(rows, header=header), lines[(i + 1) :] |
| 108 | continue |
| 109 | items = [x.strip() for x in line.split("|")[1:-1]] |
| 110 | |
| 111 | if items: |
| 112 | if not items[0]: |
| 113 | # Empty first column = previous row continues |
| 114 | assert current_rows |
| 115 | for i, item in enumerate(items): |
| 116 | if item: |
| 117 | current_rows[-1][i] += f"\n{item}" |
| 118 | else: |
| 119 | # New row was found |
| 120 | current_rows.append(items) |
| 121 | |
| 122 | # Check empty table |
| 123 | if len(rows) == 0 and header is None: |
| 124 | return None |
| 125 | return Table(rows, header=header) |
| 126 | |
| 127 | |
| 128 | def parse_tables(table_string: str): |