import re


def fix_whitespace(code: str) -> str:
    """Perform basic whitespace post-processing.

    This corrects a couple of formatting issues that Jinja templates
    may struggle with (particularly blank line count, which is tough to
    get consistently right when ``if`` or ``for`` are involved).

    Args:
        code (str): A string of code to be formatted.

    Returns
        str: Formatted code.
    """
    # Remove trailing whitespace from any line.
    code = re.sub(r'[ ]+\n', '\n', code)

    # Ensure at most two blank lines before top level definitions.
    code = re.sub(r'\s+\n\s*\n\s*\n(class|def|@|#|_)', r'\n\n\n\1', code)

    # Ensure at most one line before nested definitions.
    code = re.sub(r'\s+\n\s*\n((    )+)(\w|_|@|#)', r'\n\n\1\3', code)

    # All files shall end in one and exactly one line break.
    return f'{code.rstrip()}\n'