import textwrap


def dedent(text, baseline_index=None):
    """
    Safely clean all whitespace at the left of a paragraph.

    Args:
        text (str): The text to dedent.
        baseline_index (int or None, optional): Which row to use as a 'base'
            for the indentation. Lines will be dedented to this level but
            no further. If None, indent so as to completely deindent the
            least indented text.

    Returns:
        text (str): Dedented string.

    Notes:
        This is useful for preserving triple-quoted string indentation
        while still shifting it all to be next to the left edge of the
        display.

    """
    if not text:
        return ""
    if baseline_index is None:
        return textwrap.dedent(text)
    else:
        lines = text.split('\n')
        baseline = lines[baseline_index]
        spaceremove = len(baseline) - len(baseline.lstrip(' '))
        return "\n".join(line[min(spaceremove, len(line) - len(line.lstrip(' '))):]
                         for line in lines)