import re


def clean_whitespace(str_):
    """Normalizes whitespace.

    Replaces consecutive newlines with single newline and all 
      other whitespace with a single space.

    Args:
      str_: (str) Input document.

    Returns:
      str_: (str) Output document with normalized whitespace.  
    """
    def sub_func(match):
        if match.group(0) == '\n':
            return '\n'
        return ' '

    str_ = str_.strip().lower() if str_.isupper() else str_.strip()
    str_ = '\n'.join(filter(None, map(str.strip, str_.split('\n'))))
    str_ = re.sub(r'\s+', sub_func, str_)

    return str_