import re
import textwrap


def dedent_string(s):
    """
    Strip empty lines at the start and end of a string and dedent it.

    Empty and whitespace-only lines at the beginning and end of ``s``
    are removed. The remaining lines are dedented, i.e. common leading
    whitespace is stripped.
    """
    s = re.sub(r'^([^\S\n]*\n)*', '', re.sub(r'(\n[^\S\n]*)*$', '', s))
    return textwrap.dedent(s)