django-pattern-library icon indicating copy to clipboard operation
django-pattern-library copied to clipboard

Unable to use forward slash in `SECTIONS` paths

Open ben-qr opened this issue 1 year ago • 0 comments

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
  • Python version: Run python --version.
  • Django version: Look in your requirements.txt, or run pip show django | grep Version.
  • Browser version: You can use https://www.whatsmybrowser.org/ to find this out.

ben-qr avatar Jun 10 '24 07:06 ben-qr