Parse a markdown table. Returns (rows, columnMismatchCount, endIndex).
(lines, startIdx)
| 338 | # ----------------------------------------------------------------------------- |
| 339 | |
| 340 | def parseMarkdownTable(lines, startIdx): |
| 341 | '''Parse a markdown table. Returns (rows, columnMismatchCount, endIndex).''' |
| 342 | table = [] |
| 343 | headers = [] |
| 344 | columnMismatchCount = 0 |
| 345 | idx = startIdx |
| 346 | |
| 347 | # Parse header row |
| 348 | if idx < len(lines) and '|' in lines[idx]: |
| 349 | headerLine = lines[idx].strip() |
| 350 | headers = [h.strip().strip('`') for h in headerLine.split('|')[1:-1]] |
| 351 | idx += 1 |
| 352 | else: |
| 353 | return [], 0, startIdx |
| 354 | |
| 355 | # Skip separator row |
| 356 | if idx < len(lines) and '|' in lines[idx] and '-' in lines[idx]: |
| 357 | idx += 1 |
| 358 | else: |
| 359 | return [], 0, startIdx |
| 360 | |
| 361 | # Parse data rows |
| 362 | while idx < len(lines): |
| 363 | line = lines[idx].strip() |
| 364 | if not line or not line.startswith('|'): |
| 365 | break |
| 366 | |
| 367 | cells = [c.strip().strip('`') for c in line.split('|')[1:-1]] |
| 368 | if len(cells) == len(headers): |
| 369 | row = {headers[i].lower(): cells[i] for i in range(len(headers))} |
| 370 | table.append(row) |
| 371 | else: |
| 372 | columnMismatchCount += 1 |
| 373 | idx += 1 |
| 374 | |
| 375 | return table, columnMismatchCount, idx |
| 376 | |
| 377 | |
| 378 | # ----------------------------------------------------------------------------- |
no test coverage detected