import html

def clean_text(text):
    text = html.unescape(text)
    text = text.replace('\r', '')
    text = text.strip()

    # Split text into paragraphs and headings
    paragraphs = []
    lines = text.split('\n')
    paragraph = ''
    for line in lines:
        if line.strip() == '':
            if paragraph != '':
                paragraphs.append(paragraph)
                paragraph = ''
        elif line.isupper():
            if paragraph != '':
                paragraphs.append(paragraph)
                paragraph = ''
            paragraph += line + '\n'
        else:
            if paragraph != '' and not paragraph.endswith('\n'):
                paragraph += ' '
            paragraph += line

    # Add the last paragraph
    if paragraph != '':
        paragraphs.append(paragraph.strip())

    # Join the paragraphs with two line breaks and headings with three line breaks
    text = '\n'.join(paragraphs)
    # text = re.sub(r'\s', ' ', text).strip()

    text = text.encode('ascii', 'ignore').decode('utf-8')

    return text