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

Minor bugfix table align: empty trailing column #116

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading