aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/urls.py
blob: baae154d728973a823fe6f59e47b99e307e78d1e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import typing
from pathlib import Path

from django_distill import distill_path

from . import utils, views

app_name = "content"


def __get_all_files(root: Path, folder: Path | None = None) -> list[str]:
    """Find all folders and markdown files recursively starting from `root`."""
    if not folder:
        folder = root

    results = []

    for item in folder.iterdir():
        name = item.relative_to(root).__str__().replace("\\", "/")

        if item.is_dir():
            results.append(name)
            results.extend(__get_all_files(root, item))
        else:
            path, extension = name.rsplit(".", maxsplit=1)
            if extension == "md":
                results.append(path)

    return results


DISTILL_RETURN = typing.Iterator[dict[str, str]]


def get_all_pages() -> DISTILL_RETURN:
    """Yield a dict of all page categories."""
    for location in __get_all_files(Path("pydis_site", "apps", "content", "resources")):
        yield {"location": location}


def get_all_tags() -> DISTILL_RETURN:
    """Return all tag names and groups in static builds."""
    # We instantiate the set with None here to make filtering it out later easier
    # whether it was added in the loop or not
    groups = {None}
    for tag in utils.get_tags_static():
        groups.add(tag.group)
        yield {"location": (f"{tag.group}/" if tag.group else "") + tag.name}

    groups.remove(None)
    for group in groups:
        yield {"location": group}


urlpatterns = [
    distill_path("", views.PageOrCategoryView.as_view(), name='pages'),
    distill_path(
        "tags/<path:location>/",
        views.TagView.as_view(),
        name="tag",
        distill_func=get_all_tags
    ),
    distill_path(
        "<path:location>/",
        views.PageOrCategoryView.as_view(),
        name='page_category',
        distill_func=get_all_pages
    ),
]