-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add markdown_tables.format_table
- Loading branch information
Showing
6 changed files
with
22 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
"""Markdown utilities.""" | ||
"""Markdown table formatting.""" | ||
|
||
from beartype.typing import Any, Dict, List | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
def _format_md_table(headers: List[str], records: List[Dict[str, Any]]) -> List[str]: | ||
"""Format the input as a Github markdown table.""" | ||
|
||
def format_table(headers: list[str], records: list[dict[str, Any]]) -> str: | ||
"""Returns a formatted Github Markdown table.""" | ||
table = [[str(_r[col]) for col in headers] for _r in records] | ||
widths = [max(len(row[col_idx].strip()) for row in [headers, *table]) for col_idx in range(len(headers))] | ||
|
||
def pad(values: List[str]) -> List[str]: | ||
def pad(values: list[str]) -> list[str]: | ||
return [val.strip().ljust(widths[col_idx]) for col_idx, val in enumerate(values)] | ||
|
||
def join(row: List[str], spacer: str = ' ') -> str: | ||
def join(row: list[str], spacer: str = ' ') -> str: | ||
return f'|{spacer}' + f'{spacer}|{spacer}'.join(row) + f'{spacer}|' | ||
|
||
return [ | ||
lines = [ | ||
join(pad(headers)), | ||
join(['-' * widths[col_idx] for col_idx in range(len(headers))], '-'), | ||
*[join(pad(row)) for row in table], | ||
] | ||
return '\n'.join(lines) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters