mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Dedent tabs. (#4347)
This commit is contained in:
parent
9f5e841daf
commit
bda16a518a
|
@ -32,13 +32,22 @@ 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 = [len(line) - len(line.lstrip(' '))
|
whitespace_counts = [
|
||||||
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 whitespace_counts:
|
||||||
whitespace_pattern = '^' + (' ' * min(whitespace_counts))
|
whitespace_pattern = '^' + (' ' * min(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' * min(whitespace_counts))
|
||||||
|
content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
|
||||||
|
|
||||||
return content.strip()
|
return content.strip()
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ from django.test import TestCase
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
|
|
||||||
from rest_framework.compat import apply_markdown
|
from rest_framework.compat import apply_markdown
|
||||||
|
from rest_framework.utils.formatting import dedent
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
|
|
||||||
|
@ -120,3 +121,7 @@ class TestViewNamesAndDescriptions(TestCase):
|
||||||
gte_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_gte_21
|
gte_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_gte_21
|
||||||
lt_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_lt_21
|
lt_21_match = apply_markdown(DESCRIPTION) == MARKED_DOWN_lt_21
|
||||||
self.assertTrue(gte_21_match or lt_21_match)
|
self.assertTrue(gte_21_match or lt_21_match)
|
||||||
|
|
||||||
|
|
||||||
|
def test_dedent_tabs():
|
||||||
|
assert dedent("\tfirst string\n\n\tsecond string") == 'first string\n\n\tsecond string'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user