def encode_whitespace(text):
    """
    Encode whitespace in text as HTML so that all whitespace (specifically
    indentation and line breaks) is preserved when the text is rendered in a
    web browser.

    :param text: The plain text (a string).
    :returns: The text converted to HTML (a string).
    """
    text = text.replace('\r\n', '\n')
    text = text.replace('\n', '<br>\n')
    text = text.replace(' ', '&nbsp;')
    return text
