diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index ca5b33c5e8..78cb37e56f 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -32,23 +32,18 @@ def dedent(content): unindented text on the initial line. """ content = force_text(content) - whitespace_counts = [ - len(line) - len(line.lstrip(' ')) - for line in content.splitlines()[1:] if line.lstrip() - ] - tab_counts = [ - len(line) - len(line.lstrip('\t')) - for line in content.splitlines()[1:] if line.lstrip() - ] + lines = [line for line in content.splitlines()[1:] if line.lstrip()] # unindent the content if needed - if whitespace_counts: - whitespace_pattern = '^' + (' ' * min(whitespace_counts)) - content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) - elif tab_counts: - whitespace_pattern = '^' + ('\t' * min(whitespace_counts)) - content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) - + if lines: + whitespace_counts = min([len(line) - len(line.lstrip(' ')) for line in lines]) + tab_counts = min([len(line) - len(line.lstrip('\t')) for line in lines]) + if whitespace_counts: + whitespace_pattern = '^' + (' ' * whitespace_counts) + content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) + elif tab_counts: + whitespace_pattern = '^' + ('\t' * tab_counts) + content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) return content.strip() diff --git a/tests/test_description.py b/tests/test_description.py index 08d8bddece..001a3ea217 100644 --- a/tests/test_description.py +++ b/tests/test_description.py @@ -124,4 +124,8 @@ def test_markdown(self): def test_dedent_tabs(): - assert dedent("\tfirst string\n\n\tsecond string") == 'first string\n\n\tsecond string' + result = 'first string\n\nsecond string' + assert dedent(" first string\n\n second string") == result + assert dedent("first string\n\n second string") == result + assert dedent("\tfirst string\n\n\tsecond string") == result + assert dedent("first string\n\n\tsecond string") == result pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy