import re


def collapse_whitespace(strarg, remove_nl=False):
    """Returns a cleaned-up version of the block text
    It collapses  whitespaces, removes tabs, and, if specified,
    only keeps the tag delimiters(like in cleaneval) as newlines."""
    strarg = re.sub(r'\t+', ' ', strarg)  # replace tabs with spaces
    if remove_nl:
        # remove newlines, used for cleaneval
        # they will be replaced by the remove tags
        strarg = re.sub(r'\n', ' ', strarg)
    strarg = re.sub(r'<[a-zA-Z]+>', '\n', strarg)
    strarg = re.sub(r' +', ' ', strarg)  # collapse whitespace
    return strarg