From a1315ecb376862d213aad9f8b2e8c9f0b74d0a6c Mon Sep 17 00:00:00 2001 From: Kyle King Date: Fri, 23 Aug 2024 19:36:20 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Pad=20with=20Unicode=20Di?= =?UTF-8?q?splay=20Width=20(#21)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mdformat_tables/plugin.py | 29 +++++++++++++++++++++++------ pyproject.toml | 2 +- tests/fixtures-compact.md | 11 +++++++++++ tests/fixtures.md | 11 +++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/mdformat_tables/plugin.py b/mdformat_tables/plugin.py index 4cc7c1a..8c4a91a 100644 --- a/mdformat_tables/plugin.py +++ b/mdformat_tables/plugin.py @@ -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.""" @@ -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]: @@ -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] @@ -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]))] diff --git a/pyproject.toml b/pyproject.toml index 2e84c78..8d3d7fe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 = [ diff --git a/tests/fixtures-compact.md b/tests/fixtures-compact.md index 1099ed7..81bde7a 100644 --- a/tests/fixtures-compact.md +++ b/tests/fixtures-compact.md @@ -135,3 +135,14 @@ a a :\- . + +Expanded Unicode (https://github.com/executablebooks/mdformat-tables/issues/16) +. +| 模型 | 时间 | +|-------|------| +| BBFN | 2021-07 | +. +| 模型 | 时间 | +| -- | -- | +| BBFN | 2021-07 | +. diff --git a/tests/fixtures.md b/tests/fixtures.md index c80e990..d514c9f 100644 --- a/tests/fixtures.md +++ b/tests/fixtures.md @@ -135,3 +135,14 @@ a a :\- . + +Expanded Unicode (https://github.com/executablebooks/mdformat-tables/issues/16) +. +| 模型 | 时间 | +|-------|------| +| BBFN | 2021-07 | +. +| 模型 | 时间 | +| ---- | ------- | +| BBFN | 2021-07 | +.