def webify(v: Any, preserve_newlines: bool = True) -> str:
    """
    Converts a value into an HTML-safe ``str`` (formerly, in Python 2:
    ``unicode``).

    Converts value ``v`` to a string; escapes it to be safe in HTML
    format (escaping ampersands, replacing newlines with ``<br>``, etc.).
    Returns ``""`` for blank input.
    """
    nl = "<br>" if preserve_newlines else " "
    if v is None:
        return ""
    if not isinstance(v, str):
        v = str(v)
    # noinspection PyDeprecation
    return cgi.escape(v).replace("\n", nl).replace("\\n", nl)
Converts a value into an HTML-safe ``str`` (formerly, in Python 2:
    ``unicode``).

    Converts value ``v`` to a string; escapes it to be safe in HTML
    format (escaping ampersands, replacing newlines with ``<br>``, etc.).
    Returns ``""`` for blank input.