From 518ea8524128db7bebe42ecbe2347a85e2c362aa Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Fri, 2 Feb 2024 11:51:43 +0100 Subject: [PATCH] Better command-argument detection (#113) --- tests/test_indent_texindent.py | 102 +++++++++++++++++++++++++++++++++ texplain/__init__.py | 51 ++++++++++++++++- 2 files changed, 150 insertions(+), 3 deletions(-) diff --git a/tests/test_indent_texindent.py b/tests/test_indent_texindent.py index d399f97..9c02ef3 100644 --- a/tests/test_indent_texindent.py +++ b/tests/test_indent_texindent.py @@ -1,3 +1,5 @@ +import pytest + import texplain @@ -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() diff --git a/texplain/__init__.py b/texplain/__init__.py index 28e9d15..fe68367 100644 --- a/texplain/__init__.py +++ b/texplain/__init__.py @@ -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"(? str: """ Split text into sentences. @@ -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. """ @@ -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)