Skip to content

Commit

Permalink
Better command-argument detection (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdegeus authored Feb 2, 2024
1 parent 207626a commit 518ea85
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 3 deletions.
102 changes: 102 additions & 0 deletions tests/test_indent_texindent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import texplain


Expand Down Expand Up @@ -59,3 +61,103 @@ def test_sentence_squash():

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


def test_arguments():
text = r"""
\hypersetup{
pdftitle=\@title,
citecolor=NavyBlue,
filecolor=NavyBlue,
linkcolor=NavyBlue,
urlcolor=NavyBlue,
breaklinks,bookmarksopen=true,
}
"""

formatted = r"""
\hypersetup{
pdftitle=\@title,
citecolor=NavyBlue,
filecolor=NavyBlue,
linkcolor=NavyBlue,
urlcolor=NavyBlue,
breaklinks,
bookmarksopen=true,
}
"""

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


@pytest.mark.skip(reason="TODO: decide how to handle this case.")
def test_arguments_comment():
text = r"""
\hypersetup{%
pdftitle=\@title,
citecolor=NavyBlue,
filecolor=NavyBlue,
linkcolor=NavyBlue,
urlcolor=NavyBlue,
breaklinks,bookmarksopen=true,
}
"""

formatted = r"""
\hypersetup{%
pdftitle=\@title,
citecolor=NavyBlue,
filecolor=NavyBlue,
linkcolor=NavyBlue,
urlcolor=NavyBlue,
breaklinks,
bookmarksopen=true,
}
"""
ret = texplain.indent(text)
assert ret.strip() == formatted.strip()


def test_arguments_nargs_below():
text = r"""
\hypersetup{
pdftitle=\@title,citecolor=NavyBlue}
"""

formatted = r"""
\hypersetup{
pdftitle=\@title,
citecolor=NavyBlue
}
"""
ret = texplain.indent(text)
assert ret.strip() == formatted.strip()


def test_arguments_nargs_above():
text = r"""
\hypersetup{
pdftitle=\@title,citecolor=NavyBlue,bookmarksopen=true}
"""

formatted = r"""
\hypersetup{
pdftitle=\@title,
citecolor=NavyBlue,
bookmarksopen=true
}
"""
ret = texplain.indent(text)
assert ret.strip() == formatted.strip()


def test_arguments_false_detection():
text = r"""
\footnote{
This is a text: A = B, C = D, E = F.
}.
"""
formatted = text
ret = texplain.indent(text)
assert ret.strip() == formatted.strip()
51 changes: 48 additions & 3 deletions texplain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,48 @@ def indent(
return _rstrip_lines(text)


def _argument_block_one_per_line(text: str) -> str:
r"""
Detect is text is a(n) (list of) arguments.
The text should not contain the argument itself:
``\foo{a, b}`` is not allowed, but should be ``a, b``.
For example::
a=b, c=d, e=f
is formatted to::
a=b,
c=d,
e=f
Argument structure:
* ``\w*\s*\=.*``: word followed by ``=`` and anything.
* ``\w*``: word.
:param text: Text.
:return: Formatted text.
"""
arguments = re.split(r"(?<!\\)(\,)", text)
arguments = [i.strip() for i in arguments]
arguments = list(filter(None, arguments))
closing_comma = arguments[-1] == ","
arguments = list(filter(lambda i: i != ",", arguments))

for arg in arguments:
if not (re.match(r"(^\w*\s*\=.*$)+", arg) or re.match(r"(^\w*$)", arg)):
return text

ret = ",\n".join(arguments)

if closing_comma:
ret += ","

return ret


def _detail_one_sentence_per_line(text: str) -> str:
"""
Split text into sentences.
Expand Down Expand Up @@ -1730,6 +1772,11 @@ def _one_sentence_per_line(
:param text: Text.
:param fold: List of placeholder types to fold before formatting (and restore after).
:param base: Base name for placeholders.
:param command:
Check if the text are arguments of a command, and format one argument per line.
TODO: rename argument: ``command`` is not intuitive.
:return: Formatted text.
"""

Expand Down Expand Up @@ -1772,9 +1819,7 @@ def _one_sentence_per_line(
ret += _detail_one_sentence_per_line(text[start:])

if command:
match = [i for i in re.finditer(r"(\w*\=.*\,\ )+", ret)]
if len(match) > 0:
ret = ",\n".join(ret.split(", "))
ret = _argument_block_one_per_line(ret)

return text_from_placeholders(ret, placeholders)

Expand Down

0 comments on commit 518ea85

Please sign in to comment.