mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-16 19:41:06 +03:00
Fixed dedent
for tab indent.
This commit is contained in:
parent
f9a0db3ee0
commit
b99272c425
|
@ -32,23 +32,18 @@ def dedent(content):
|
||||||
unindented text on the initial line.
|
unindented text on the initial line.
|
||||||
"""
|
"""
|
||||||
content = force_text(content)
|
content = force_text(content)
|
||||||
whitespace_counts = [
|
lines = [line for line in content.splitlines()[1:] if line.lstrip()]
|
||||||
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()
|
|
||||||
]
|
|
||||||
|
|
||||||
# unindent the content if needed
|
# unindent the content if needed
|
||||||
if whitespace_counts:
|
if lines:
|
||||||
whitespace_pattern = '^' + (' ' * min(whitespace_counts))
|
whitespace_counts = min([len(line) - len(line.lstrip(' ')) for line in lines])
|
||||||
content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
|
tab_counts = min([len(line) - len(line.lstrip('\t')) for line in lines])
|
||||||
elif tab_counts:
|
if whitespace_counts:
|
||||||
whitespace_pattern = '^' + ('\t' * min(whitespace_counts))
|
whitespace_pattern = '^' + (' ' * whitespace_counts)
|
||||||
content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
|
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()
|
return content.strip()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -124,4 +124,8 @@ class TestViewNamesAndDescriptions(TestCase):
|
||||||
|
|
||||||
|
|
||||||
def test_dedent_tabs():
|
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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user