# -*- coding: utf-8 -*-
"""
##########
formatting
##########
A library functions for formatting strings/text.
:Copyright: Copyright (c) 2015 Nick Zarczynski <nick@frez.me>
:License: MIT
:Version: 0.0.1
"""
import re
from docutils.core import publish_parts
camelcase_to_space_separated_regex = r"""
( # start the group
# Case 1
# ======
#
# Match an uppercase character, but only if it is preceded by a lowercase
# character.
#
# Example
# -------
#
# .. correct::
# 'abYZ' -> 'ab YZ'
#
# .. incorrect::
# 'abYZ' -> 'ab Y Z'
(?<=[a-z]) # current position is preceded by a lowercase char
# (positive lookbehind: does not consume any char)
[A-Z] # match an uppercase char
| # or
# Case 2
# ======
#
# Match an uppercase character, but only if it is preceded by a lowercase
# character AND NOT at the start of the string.
#
# Example
# -------
#
# .. correct::
# 'ABCyz' -> 'AB Cyz'
#
# .. incorrect::
# 'ABCyz' -> 'A B Cyz'
(?<!\A) # current position is not at the beginning of the string
# (negative lookbehind: does not consume any char)
[A-Z] # match an uppercase char
(?=[a-z]) # matches if next char is a lowercase char
# lookahead assertion: does not consume any char
) # end the group
"""
"""
Regex to split a camelcased string.
Adapted from: http://stackoverflow.com/a/9283563/645663
"""
def camelcase_to_space_separated(target_string):
"""Convert a camelcase string to a space-separated string.
If a match is found it is replaced with ' \1', which is a string consisting
of a leading blank and the match itself.
"""
return re.sub(camelcase_to_space_separated_regex, r' \1', target_string,
flags=re.VERBOSE)
def dedent_docstring(docstring):
"""Remove leading indent from a docstring.
.. NOTE:: Python's ``textwrap.dedent`` isn't used because it doesn't dedent
multiline docstrings that begin with unindented text (e.g., on
the initial line).
Adapted from: rest_framework.utils.formatting.dedent
"""
whitespace_counts = [len(line) - len(line.lstrip(' '))
for line in docstring.splitlines()[1:]
if line.lstrip()]
# unindent the docstring if needed
if whitespace_counts:
whitespace_pattern = '^' + (' ' * min(whitespace_counts))
docstring = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '',
docstring)
return docstring.strip()
def restructured_text_to_html(restructured_text):
"""Render restructured_text as HTML.
Parameters
----------
restructured_text : str
A string formatted using ReStructured Text.
Returns
-------
str
ReStructured Text converted to HTML.
"""
parts = publish_parts(restructured_text, writer_name='html5')
return parts
# return parts.get('body')