# SPDX-License-Identifier: GPL-3.0-or-later

# Copyright (C) jasperredis 2026

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# There should a copy of the GNU General Public License  along with this
# script in the LICENSE-GPL file at the repository root. If not, see:
# <https://www.gnu.org/licenses/>

# This script is to create a HTML tree of this website's pages for the sitemap
# page's "File Tree" header.
# The reason that there is no intendation is that Emacs can do that
# automagically.

import os
from glob import glob

PATH = "../"
tree = {}


def do_walk(extension):
    for walk in os.walk(PATH):
        for file in glob(os.path.join(walk[0], f'*.{extension}')):
            here = tree
            items = file.removeprefix("../").strip("/").split("/")
            for i in items:
                here = here.setdefault(i, {})


ADDITIONS = {"LICENSE-CC-BY-SA": {}, "LICENSE-GPL": {}, "_redirects": {}}
EXTENSIONS = ["html", "png", "jpg", "jpeg", "css", "js", "md", "xml", "txt",
              "woff2", "ttf", "svg", "gif"]

for i in EXTENSIONS:
    do_walk(i)
tree.update(ADDITIONS)


def make_html_tree(tree, ref=""):
    html = ["<ul>"]
    for name, subdirs in sorted(tree.items()):
        here_ref = f"{ref}/{name}"
        if subdirs == {}:
            html.append(f"<li><a href=\"{here_ref}\">{name}</a></li>")
        else:
            html.append(
                f"<li>{name}\n{make_html_tree(subdirs, ref=here_ref)}\n</li>")
    html.append("</ul>")
    return "\n".join(html)


print(make_html_tree(tree))
