django-rest-framework/rest_framework/utils/formatting.py

70 lines
2.3 KiB
Python
Raw Normal View History

"""
Utility functions to return a formatted name and description for a given view.
"""
from __future__ import unicode_literals
import re
2015-08-07 00:51:35 +03:00
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.safestring import mark_safe
2015-08-07 00:51:35 +03:00
from rest_framework.compat import apply_markdown
def remove_trailing_string(content, trailing):
"""
Strip trailing component `trailing` from `content` if it exists.
Used when generating names from view classes.
"""
if content.endswith(trailing) and content != trailing:
return content[:-len(trailing)]
return content
def dedent(content):
"""
Remove leading indent from a block of text.
Used when generating descriptions from docstrings.
Note that python's `textwrap.dedent` doesn't quite cut it,
as it fails to dedent multiline docstrings that include
unindented text on the initial line.
"""
2014-08-19 05:09:48 +04:00
content = force_text(content)
2017-01-31 22:57:52 +03:00
lines = [line for line in content.splitlines()[1:] if line.lstrip()]
# unindent the content if needed
2017-01-31 22:57:52 +03:00
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()
2014-08-19 16:28:07 +04:00
def camelcase_to_spaces(content):
"""
Translate 'CamelCaseNames' to 'Camel Case Names'.
Used when generating names from view classes.
"""
camelcase_boundry = '(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))'
content = re.sub(camelcase_boundry, ' \\1', content).strip()
return ' '.join(content.split('_')).title()
2014-08-19 16:28:07 +04:00
def markup_description(description):
"""
Apply HTML markup to the given description.
"""
if apply_markdown:
description = apply_markdown(description)
else:
description = escape(description).replace('\n', '<br />')
description = '<p>' + description + '</p>'
return mark_safe(description)