Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 FIX: Pad with Unicode Display Width #21

Merged
merged 12 commits into from
Aug 23, 2024
29 changes: 23 additions & 6 deletions mdformat_tables/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from markdown_it import MarkdownIt
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Postprocess, Render
from wcwidth import wcswidth

_COMPACT_TABLES = False
"""user-specified flag for toggling compact tables."""
Expand All @@ -26,6 +27,22 @@ def update_mdit(mdit: MarkdownIt) -> None:
_COMPACT_TABLES = mdit.options["mdformat"].get("compact_tables", False)


def _lpad(text: str, width: int) -> str:
indent = width - wcswidth(text)
return " " * max(0, indent) + text


def _rpad(text: str, width: int) -> str:
outdent = width - wcswidth(text)
return text + " " * max(0, outdent)


def _center(text: str, width: int) -> str:
text_len = wcswidth(text)
indent = (width - text_len) // 2 + text_len
return _rpad(_lpad(text, indent), width)


def _to_string(
rows: Sequence[Sequence[str]], align: Sequence[Sequence[str]], widths: Sequence[int]
) -> List[str]:
Expand All @@ -40,19 +57,19 @@ def format_delimiter_cell(index: int, align: str) -> str:
)
return ":-:" if delim == "::" else delim

pad = {"": _rpad, "<": _rpad, ">": _lpad, "^": _center}

header = join_row(
f"{{:{al or '<'}{widths[i]}}}".format(text)
for i, (text, al) in enumerate(zip(rows[0], align[0]))
pad[al](text, widths[i]) for i, (text, al) in enumerate(zip(rows[0], align[0]))
)
delimiter = join_row(
(format_delimiter_cell(i, al) for i, al in enumerate(align[0]))
)
rows = [
join_row(
f"{{:{al or '<'}{widths[i]}}}".format(text)
for i, (text, al) in enumerate(zip(row, als))
pad[_al](text, widths[i]) for i, (text, _al) in enumerate(zip(row, aligns))
)
for row, als in zip(rows[1:], align[1:])
for row, aligns in zip(rows[1:], align[1:])
]
return [header, delimiter, *rows]

Expand Down Expand Up @@ -83,7 +100,7 @@ def _calculate_width(col_idx: int) -> int:
"""Work out the widths for each column."""
if _COMPACT_TABLES:
return 0
return max(3, *(len(row[col_idx]) for row in rows))
return max(3, *(wcswidth(row[col_idx]) for row in rows))

widths = [_calculate_width(col_idx) for col_idx in range(len(rows[0]))]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [
keywords = "mdformat,markdown,markdown-it"

requires-python=">=3.7.0"
requires=["mdformat>=0.7.5,<0.8.0"]
requires=["mdformat>=0.7.5,<0.8.0", "wcwidth>=0.2.13"]

[tool.flit.metadata.requires-extra]
test = [
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures-compact.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,14 @@ a
a
:\-
.

Expanded Unicode (https://github.com/executablebooks/mdformat-tables/issues/16)
.
| 模型 | 时间 |
|-------|------|
| BBFN | 2021-07 |
.
| 模型 | 时间 |
| -- | -- |
| BBFN | 2021-07 |
.
11 changes: 11 additions & 0 deletions tests/fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,14 @@ a
a
:\-
.

Expanded Unicode (https://github.com/executablebooks/mdformat-tables/issues/16)
.
| 模型 | 时间 |
|-------|------|
| BBFN | 2021-07 |
.
| 模型 | 时间 |
| ---- | ------- |
| BBFN | 2021-07 |
.
Loading