import re


def collapse_spaces(value):
    """
    First replace multiple newlines with a single newline,
    then collapse multiple non-newline whitespace characters
    """
    return re.sub(r'(?![\r\n])\s+', ' ', re.sub(r'[\r\n]+', '\n', value))