def build_docs()

in scripts/build_docs.py [0:0]


def build_docs(versions, use_yarn):

    # define the folders that Docusaurus builds into and a temporary one
    build_dir = "build"
    temp_dir = "build__temp"

    for v in versions:
        print(f"Building the docs for version '{v}'...")

        # replace "latest" in redirects to the appropriate version
        if v != "latest":
            for line in fileinput.input("redirects.js", inplace=1):
                print(line.replace("/latest/", f"/{v}/"), end='')

        # set the version in "buildVersion" variable in docusaurus.config.js
        replacement = f'var buildVersion = "{v}";'
        for line in fileinput.input("docusaurus.config.js", inplace=1):
            print(re.sub(r"^var buildVersion.*", replacement, line), end='')

        # remove specific version folder in published_versions/docs if exists
        shutil.rmtree(f"published_versions/docs/{v}", ignore_errors=True)

        # build the docs
        if not use_yarn:
            subprocess.run(["npm", "run", "build"])
        else:
            subprocess.run(["yarn", "build"])

        # move output to temporary directory since docusaurus 2
        # overwrites build directory with each build.
        # the "latest" version is built last to maintain
        # all the non-docs content for latest
        if not os.path.isdir(build_dir):
            sys.exit("ERROR: The docs were not built. Check Docusaurus logs.")
        shutil.copytree(build_dir, temp_dir, dirs_exist_ok=True)

        # restore the redirect file back to URLs with "latest"
        #subprocess.run(["git", "restore", "redirects.js"])
        if v != "latest":
            for line in fileinput.input("redirects.js", inplace=1):
                print(line.replace(f"/{v}/", "/latest/"), end='')

        # save the assets folder to check into GitHub
        # applies to EACH version, since Doc2 attaches an alphanumeric string
        # to each asset; so you can't republish an old version unless you
        # also have the associated assets
        shutil.copytree(f"{build_dir}/assets", 'published_versions/assets', dirs_exist_ok=True)


    # after building ALL versions, rename the temp directory back to "build"
    shutil.rmtree(build_dir)
    shutil.move(temp_dir, build_dir)

    # save the final build output to check into GitHub
    print("Copying build output to ../published_versions. Use that directory to publish the site.")
    shutil.copytree(build_dir, 'published_versions', dirs_exist_ok=True)