Skip to content

Commit

Permalink
Minor bugfix table align: empty trailing column (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus authored Feb 8, 2024
1 parent ed708e9 commit 3aff92d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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]
Expand All @@ -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"]
Expand Down
32 changes: 24 additions & 8 deletions tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def test_tabular_align_no_newline():
40 & 50 & 60
\end{tabular}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()

Expand All @@ -34,7 +33,6 @@ def test_tabular_align_empty():
\@author
\end{tabular}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()

Expand All @@ -55,7 +53,6 @@ def test_tabular_align_nested():
\end{tabular}
}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()

Expand All @@ -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()

Expand All @@ -97,7 +115,6 @@ def test_tabular_align_empty_column():
& 5 & 6
\end{tabular}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()

Expand All @@ -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}
Expand All @@ -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()

Expand All @@ -153,7 +171,6 @@ def test_tabular_math():
\bottomrule
\end{tabular}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()

Expand All @@ -180,6 +197,5 @@ def test_tabular_math_empty():
\bottomrule
\end{tabular}
"""

ret = texplain.indent(text)
assert ret.strip() == formatted.strip()
6 changes: 5 additions & 1 deletion texplain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3aff92d

Please sign in to comment.