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

Templates with a dash `"-"` in the path are no longer resolved #229

Closed migonzalvar closed 10 months ago

migonzalvar commented 1 year ago

Issue Summary

I think I found a regression. After #223, templates with a dash "-" in the path are no longer resolved.

Steps to Reproduce

  1. Add a pattern with a dash, for example, patterns/components/quote-block/quote-block.html
  2. Navigate to http://localhost:8000/pattern-library/
  3. An error is shown: Reverse for 'display_pattern' with arguments '('patterns/components/quote-block/quote-block.html',)' not found. 1 pattern(s) tried: ['\\-/pattern\\-library/pattern/(?P<pattern_template_name>[\\w./-\\\\]+.html)$']

In version 1.0.0 it works just fine.

Technical details

In this snippet, I prove that the previous and current regexp does not behave the same with respect to dashes.

import re
template = "pattern/my-component.html"
previous = r"^pattern/(?P<pattern_template_name>[\w./-]+.html)$"
current = r"^pattern/(?P<pattern_template_name>[\w./-\\]+.html)$"
print("1.0.0", re.match(previous, template))
print("1.0.1", re.match(current, template))
$ python poc.py 
1.0.0 <re.Match object; span=(0, 25), match='pattern/my-component.html'>
1.0.1 None

Versions:

PS: Thank you for this awesome library.

zerolab commented 1 year ago

We need to use r"^pattern/(?P<pattern_template_name>[\w./\-\\]+.html)$"

since \w./-\\ is interpreted as word, or . or range from / to \

thibaudcolas commented 11 months ago

Ah, good catch @migonzalvar! While reviewing this I missed the significance of having the hyphen as the last item in the pattern.

zerolab commented 10 months ago

Fixed in #230 and out in version 1.1