torchbox / django-pattern-library

UI pattern libraries for Django templates
https://torchbox.github.io/django-pattern-library/
BSD 3-Clause "New" or "Revised" License
360 stars 44 forks source link

Unable to use forward slash in `SECTIONS` paths #246

Open ben-qr opened 3 months ago

ben-qr commented 3 months ago

Issue Summary

Using / in the paths of my PATTERN_LIBRARY.SECTIONS results in a PatternLibraryEmpty exception

Steps to Reproduce

My library settings are like so:

PATTERN_LIBRARY = {
    # Groups of templates for the pattern library navigation. The keys
    # are the group titles and the values are lists of template name prefixes that will
    # be searched to populate the groups.
    "SECTIONS": (
        (
            "Components",
            [
                "patterns/components",
            ],
        ),
        ("Forms", ["patterns/forms"]),
    ),
    # Configure which files to detect as templates.
    "TEMPLATE_SUFFIX": ".html",
    # Set which template components should be rendered inside of,
    # so they may use page-level component dependencies like CSS.
    "PATTERN_BASE_TEMPLATE_NAME": r"patterns\base.html",
}

However, when I try to view a pattern, I get the following error: No templates found matching: '(('Components', ['patterns/components']), ('Forms', ['patterns/forms']))'.

To rectify, I have to change my sections to backslashes:

    "SECTIONS": (
        (
            "Components",
            [
                r"patterns\components",
            ],
        ),
        ("Forms", [r"patterns\forms"]),
    ),

This is on Windows 11, with Django 4.2 & django-pattern-library v1.2.0.

Technical details

Something like this would resolve:

def section_for(template_folder):
    paths = path_to_section()
    for path in paths:
        template_folder = str(template_folder).replace("/", "\\")
        if template_folder.startswith(path.replace("/", "\\")):
            return paths[path], path
    return None, None