from bs4 import BeautifulSoup

def process_paragraph(paragraph):
    user_texts = paragraph.text.split(') ')
    active_users = [text for text in user_texts if not text.endswith('(0 edits)')]
    return len(active_users) > 1

def highlight_active_users(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')
    paragraphs = soup.find_all('p')

    # Sort paragraphs alphabetically
    sorted_paragraphs = sorted(paragraphs, key=lambda p: p.text.lower())

    # Clear the existing body content
    soup.body.clear()

    # Add sorted paragraphs back into the body
    for paragraph in sorted_paragraphs:
        if process_paragraph(paragraph):
            paragraph['style'] = 'background-color: #ffff99;'
        soup.body.append(paragraph)

    return str(soup)

def process_html(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as file:
        html_content = file.read()

    highlighted_html = highlight_active_users(html_content)

    with open(output_file, 'w', encoding='utf-8') as file:
        file.write(highlighted_html)

if __name__ == "__main__":
    input_file = 'collisions.html'
    output_file = 'highlighted_collisions.html'
    process_html(input_file, output_file)

