# This script is used to remove the <pre> tags from the HTML files in the build directory that are generated by Manim.
# For more information, please refer to the following link: https://github.com/ManimCommunity/manim/issues/3994

import argparse
import os

from bs4 import BeautifulSoup

parser = argparse.ArgumentParser(description="Process a file path.")
parser.add_argument("path", type=str, help="The path of html build.")
args = parser.parse_args()


def clean_build():
    def remove_pre_tags(html_content):
        soup = BeautifulSoup(html_content, "html.parser")
        for tag in soup.find_all("pre", {"data-manim-binder": True}):
            tag.decompose()
        return str(soup)

    for root, _, files in os.walk(args.path):
        for filename in files:
            if filename.endswith(".html"):
                file_path = os.path.join(root, filename)
                with open(file_path, encoding="utf-8") as file:
                    html_content = file.read()
                cleaned_html = remove_pre_tags(html_content)
                with open(file_path, "w", encoding="utf-8") as file:
                    file.write(cleaned_html)


clean_build()
