(node: Any)
| 291 | |
| 292 | |
| 293 | def _render_table(node: Any) -> Text: |
| 294 | header_row: list[Text] = [] |
| 295 | rows: list[list[Text]] = [] |
| 296 | aligns: list[str | None] = [] |
| 297 | |
| 298 | thead = next((c for c in node.children if c.type == "thead"), None) |
| 299 | tbody = next((c for c in node.children if c.type == "tbody"), None) |
| 300 | |
| 301 | if thead and thead.children: |
| 302 | tr = thead.children[0] |
| 303 | for th in tr.children or []: |
| 304 | header_row.append(_render_inline_container(th)) |
| 305 | aligns.append(th.attrGet("style")) |
| 306 | if tbody: |
| 307 | for tr in tbody.children or []: |
| 308 | row: list[Text] = [] |
| 309 | for td in tr.children or []: |
| 310 | row.append(_render_inline_container(td)) |
| 311 | rows.append(row) |
| 312 | |
| 313 | if not header_row: |
| 314 | return Text("") |
| 315 | |
| 316 | widths = [max(3, cell.cell_len) for cell in header_row] |
| 317 | for row in rows: |
| 318 | for i, cell in enumerate(row): |
| 319 | if i < len(widths): |
| 320 | widths[i] = max(widths[i], cell.cell_len) |
| 321 | |
| 322 | out = Text() |
| 323 | out.append("| ") |
| 324 | for i, cell in enumerate(header_row): |
| 325 | out.append_text(_pad(cell, widths[i], aligns[i] if i < len(aligns) else None)) |
| 326 | out.append(" | ") |
| 327 | out = _rstrip_trailing_space(out) |
| 328 | out.append("\n|") |
| 329 | for w in widths: |
| 330 | out.append("-" * (w + 2)) |
| 331 | out.append("|") |
| 332 | for row in rows: |
| 333 | out.append("\n| ") |
| 334 | for i, cell in enumerate(row): |
| 335 | width = widths[i] if i < len(widths) else cell.cell_len |
| 336 | align = aligns[i] if i < len(aligns) else None |
| 337 | out.append_text(_pad(cell, width, align)) |
| 338 | out.append(" | ") |
| 339 | out = _rstrip_trailing_space(out) |
| 340 | return out |
| 341 | |
| 342 | |
| 343 | def _pad(cell: Text, width: int, align: str | None) -> Text: |
no test coverage detected