Beispiel #1
0
def do_lineprefix(s, prefix):
    """Return a copy of the string with each line prepended by the given string.
     :param prefix: String to prepend each line with
     .. versionadded:: 2.11
        Add block auto-indent feature
    """
    newline = u'\n'

    if isinstance(s, Markup):
        prefix = Markup(prefix)
        newline = Markup(newline)

    lines = s.splitlines()
    rv = newline.join(prefix + line if line else line for line in lines)

    return rv
Beispiel #2
0
def do_indent(
    s, width=4, first=False, blank=False, indentfirst=None
):
    """Return a copy of the string with each line indented by 4 spaces. The
    first line and blank lines are not indented by default.

    :param width: Number of spaces to indent by.
    :param first: Don't skip indenting the first line.
    :param blank: Don't skip indenting empty lines.

    .. versionchanged:: 2.10
        Blank lines are not indented by default.

        Rename the ``indentfirst`` argument to ``first``.
    """
    if indentfirst is not None:
        warnings.warning(DeprecationWarning(
            'The "indentfirst" argument is renamed to "first".'
        ), stacklevel=2)
        first = indentfirst

    indention = u' ' * width
    newline = u'\n'

    if isinstance(s, Markup):
        indention = Markup(indention)
        newline = Markup(newline)

    s += newline  # this quirk is necessary for splitlines method

    if blank:
        rv = (newline + indention).join(s.splitlines())
    else:
        lines = s.splitlines()
        rv = lines.pop(0)

        if lines:
            rv += newline + newline.join(
                indention + line if line else line for line in lines
            )

    if first:
        rv = indention + rv

    return rv