2013-04-05 00:42:26 +04:00
|
|
|
"""
|
|
|
|
Utility functions to return a formatted name and description for a given view.
|
|
|
|
"""
|
|
|
|
from __future__ import unicode_literals
|
2015-06-18 16:38:29 +03:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
2015-08-07 00:51:35 +03:00
|
|
|
from django.utils.encoding import force_text
|
2013-04-05 00:42:26 +04:00
|
|
|
from django.utils.html import escape
|
|
|
|
from django.utils.safestring import mark_safe
|
2015-06-18 16:38:29 +03:00
|
|
|
|
2015-08-07 00:51:35 +03:00
|
|
|
from rest_framework.compat import apply_markdown
|
2013-04-05 00:42:26 +04:00
|
|
|
|
|
|
|
|
2013-08-19 11:24:27 +04:00
|
|
|
def remove_trailing_string(content, trailing):
|
2013-04-05 00:42:26 +04:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2013-08-19 11:24:27 +04:00
|
|
|
def dedent(content):
|
2013-04-05 00:42:26 +04:00
|
|
|
"""
|
|
|
|
Remove leading indent from a block of text.
|
|
|
|
Used when generating descriptions from docstrings.
|
2013-08-19 11:24:27 +04:00
|
|
|
|
|
|
|
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.
|
2013-04-05 00:42:26 +04:00
|
|
|
"""
|
2014-08-19 05:09:48 +04:00
|
|
|
content = force_text(content)
|
2016-08-02 15:33:14 +03:00
|
|
|
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()
|
|
|
|
]
|
2013-04-05 00:42:26 +04:00
|
|
|
|
|
|
|
# unindent the content if needed
|
|
|
|
if whitespace_counts:
|
|
|
|
whitespace_pattern = '^' + (' ' * min(whitespace_counts))
|
|
|
|
content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
|
2016-08-02 15:33:14 +03:00
|
|
|
elif tab_counts:
|
|
|
|
whitespace_pattern = '^' + ('\t' * min(whitespace_counts))
|
|
|
|
content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
|
2013-04-05 00:42:26 +04:00
|
|
|
|
2013-08-19 11:24:27 +04:00
|
|
|
return content.strip()
|
2013-04-05 00:42:26 +04:00
|
|
|
|
2014-08-19 16:28:07 +04:00
|
|
|
|
2013-08-19 11:24:27 +04:00
|
|
|
def camelcase_to_spaces(content):
|
2013-04-05 00:42:26 +04:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2013-04-05 00:42:26 +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 />')
|
2015-01-14 16:19:56 +03:00
|
|
|
description = '<p>' + description + '</p>'
|
2013-04-05 00:42:26 +04:00
|
|
|
return mark_safe(description)
|