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

indent: make dedent selected environment more robust #56

Open
tdegeus opened this issue Mar 14, 2023 · 1 comment
Open

indent: make dedent selected environment more robust #56

tdegeus opened this issue Mar 14, 2023 · 1 comment
Labels

Comments

@tdegeus
Copy link
Owner

tdegeus commented Mar 14, 2023

def _dedent(text: str, partial: list[PlaceholderType]) -> str:
"""
Remove indentation.
:param text: Text.
:param partial:
List of :py:class:`PlaceholderType` to dedent partially.
If the number of lines is less than 3, ``textwrap.dedent`` is applied to all lines.
Otherwise, ``textwrap.dedent`` is applied to ``lines[1:-1]``;
the first and last lines are stripped.
:return: Formatted text.
"""
text, placholders = text_to_placeholders(text, partial, base="TEXDEDENT")
text = _lstrip_lines(text)
# keep common indentation table
# TODO: make more clever
for placeholder in placholders:
tmp = placeholder.content.splitlines()
if len(tmp) <= 2:
placeholder.content = "\n".join(textwrap.dedent(tmp))
else:
placeholder.content = "\n".join(
[tmp[0].lstrip(), textwrap.dedent("\n".join(tmp[1:-1])), tmp[-1].lstrip()]
)
placeholder.space_front = "\n"
text = text_from_placeholders(text, placholders)
return text

@tdegeus tdegeus added the bug label Mar 14, 2023
@tdegeus
Copy link
Owner Author

tdegeus commented Mar 15, 2023

Fixed by #68

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant