Markdown table
Helpers for building GitHub-Flavored Markdown tables.
Provides MarkdownTable, a small dataclass-based builder that handles column alignment, width padding, pipe escaping, configurable newline replacement, and rendering rows supplied either positionally or as mappings keyed by header name.
Align
¶
MarkdownTable
dataclass
¶
A builder for GitHub-Flavored Markdown tables.
Rows can be added positionally via add_row or as dicts keyed by header name via add_row_dict. Cell values are converted with str() and escaped for markdown safety; any formatting beyond that (e.g. numeric precision) is the caller's responsibility.
Attributes:
| Name | Type | Description |
|---|---|---|
headers |
Sequence[str]
|
Column header labels, in display order. |
align |
Sequence[Align]
|
Per-column alignment, in the same order as headers. Defaults to left-alignment for every column if not given. |
rows |
list[Sequence[Any]]
|
The table's row data, appended to via add_row / add_row_dict rather than set directly. |
newline_replacement |
str
|
String substituted for newline characters
found inside cell values, since markdown table cells cannot
contain literal line breaks. Defaults to a single space;
pass " |
Source code in src/pfmsoft/eve_snippets/markdown/markdown_table.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
__post_init__() -> None
¶
Fill in default alignment and validate its length.
Raises:
| Type | Description |
|---|---|
ValueError
|
If align is provided but its length does not match headers. |
Source code in src/pfmsoft/eve_snippets/markdown/markdown_table.py
add_row(row: Sequence[Any]) -> None
¶
Append a row supplied as positional values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Sequence[Any]
|
Cell values in the same order as headers. Values are stringified and escaped at render time, not here. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If len(row) does not match len(headers). |
Source code in src/pfmsoft/eve_snippets/markdown/markdown_table.py
add_row_dict(row: Mapping[str, Any], default: str | None = None) -> None
¶
Append a row supplied as a dict keyed by header name.
Values are pulled out in header order regardless of the dict's iteration order, so key order in row does not matter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
Mapping[str, Any]
|
Mapping from header name to cell value. Keys not present in headers always raise, regardless of default. |
required |
default
|
str | None
|
Fill value used for headers missing from row. If None (the default), missing headers are treated as errors instead of being filled in. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If row contains a key not in headers, or if default is None and row is missing a key that is in headers. |
Source code in src/pfmsoft/eve_snippets/markdown/markdown_table.py
render() -> str
¶
Render the table as a GitHub-Flavored Markdown string.
Column widths are computed from the widest cell (including the header) in each column, then every row is padded to match.
Returns:
| Type | Description |
|---|---|
str
|
The complete markdown table, including header and separator |
str
|
rows, as a single newline-joined string with no trailing |
str
|
newline. If no data rows were added, the output still contains |
str
|
a valid header-only table. |