From c54b471aa1ad8b6f8c982bbc53c4c43133677159 Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Thu, 8 Feb 2024 10:30:03 +0100 Subject: [PATCH] Minor bugfix table align: empty trailing column --- .pre-commit-config.yaml | 6 +++--- tests/test_align.py | 32 ++++++++++++++++++++++++-------- texplain/__init__.py | 6 +++++- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b6ae599..2db7c79 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: check-toml - id: debug-statements - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks - rev: v2.11.0 + rev: v2.12.0 hooks: - id: pretty-format-yaml args: [--preserve-quotes, --autofix, --indent, '2'] @@ -25,7 +25,7 @@ repos: - id: rst-directive-colons - id: rst-inline-touching-normal - repo: https://github.com/psf/black - rev: 23.12.0 + rev: 24.1.1 hooks: - id: black args: [--safe, --quiet, --line-length=100] @@ -44,7 +44,7 @@ repos: - id: pyupgrade args: [--py36-plus] - repo: https://github.com/PyCQA/flake8 - rev: 6.1.0 + rev: 7.0.0 hooks: - id: flake8 args: ["--max-line-length=100", "--ignore=E203", "--per-file-ignores=tests/*.py:E501"] diff --git a/tests/test_align.py b/tests/test_align.py index 677ea79..3ee63e5 100644 --- a/tests/test_align.py +++ b/tests/test_align.py @@ -17,7 +17,6 @@ def test_tabular_align_no_newline(): 40 & 50 & 60 \end{tabular} """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -34,7 +33,6 @@ def test_tabular_align_empty(): \@author \end{tabular} """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -55,7 +53,6 @@ def test_tabular_align_nested(): \end{tabular} } """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -75,8 +72,29 @@ def test_tabular_align_empty_leading_column(): 1 & 2 & 3 \\ 4 & 5 & 6 \end{tabular} +""" + ret = texplain.indent(text) + assert ret.strip() == formatted.strip() + + +def test_tabular_align_empty_trailing_column(): + text = r""" +\begin{tabular}[t]{ccc} +& foo & foobar \\ +1 & 2 & \\ +4 & 5 & d \\ +& & +\end{tabular} """ + formatted = r""" +\begin{tabular}[t]{ccc} + & foo & foobar \\ + 1 & 2 & \\ + 4 & 5 & d \\ + & & +\end{tabular} +""" ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -97,7 +115,6 @@ def test_tabular_align_empty_column(): & 5 & 6 \end{tabular} """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -109,7 +126,8 @@ def test_tabular_overflow(): Foo & Bar & Baz \\ \midrule $a$ & $b = 1000$ & $e$ \\ - $c'$ & $d = 2$ & $f'$ \\ + $c'$& $d = 2$ & $f'$ \\ + a & & \\ this is a very long column with a lot of text, this is a very long column with a lot of text & this is a very long column with a lot of text, this is a very long column with a lot of text & short \\ \bottomrule \end{tabular} @@ -122,11 +140,11 @@ def test_tabular_overflow(): \midrule $a$ & $b = 1000$ & $e$ \\ $c'$ & $d = 2$ & $f'$ \\ + a & & \\ this is a very long column with a lot of text, this is a very long column with a lot of text & this is a very long column with a lot of text, this is a very long column with a lot of text & short \\ \bottomrule \end{tabular} """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -153,7 +171,6 @@ def test_tabular_math(): \bottomrule \end{tabular} """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() @@ -180,6 +197,5 @@ def test_tabular_math_empty(): \bottomrule \end{tabular} """ - ret = texplain.indent(text) assert ret.strip() == formatted.strip() diff --git a/texplain/__init__.py b/texplain/__init__.py index 1d87d6a..7702425 100644 --- a/texplain/__init__.py +++ b/texplain/__init__.py @@ -1174,6 +1174,10 @@ def _align(text: str, placeholders: dict[list[Placeholder]] = {}, maxwidth: int ret = ["" for _ in range(np.max(icol) + 1)] for j in range(len(line)): ret[icol[j]] = line[j] + # add empty column if the line ends with "& \\" + if len(ret) >= 2: + if ret[-2] == "&" and ret[-1] == r"\\": + ret.insert(-1, "") lines[i] = ret cols = max(cols, len(ret)) @@ -1204,7 +1208,7 @@ def _align(text: str, placeholders: dict[list[Placeholder]] = {}, maxwidth: int # lines too long: no alignment is done if sum(col_width) > maxwidth: for i in range(1, len(lines) - 1): - lines[i] = " ".join(lines[i]) + lines[i] = " ".join(filter(None, lines[i])) return "\n".join(lines) # align columns if needed