pallets / jinja

A very fast and expressive template engine.

Home Page:https://jinja.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Filter regex_replace: |regex_replace("/index.html$","/")

kno10 opened this issue · comments

For generating, e.g., canonical URLs it is nice to have a function to strip, e.g., index.html from the end of a string.
Regular replace would replace anywhere in a string.

This appears to be popular:

salt:
https://github.com/saltstack/salt/blob/6acef263b176faad42d05719686b9c592a7ca475/salt/utils/jinja.py#L507-L529

Ansible:
https://github.com/ansible/ansible/blob/716631189cb5a3f66b3add98f39e64e98bc17bf7/lib/ansible/plugins/filter/core.py#L124-L135

commented

It makes sense for these tools, even though the built-in removesuffix method strings have in recent Python 3 versions seems to be almost enough for that.

I don't think it belongs into core jinja though - in most contexts this type of logic is better suited elsewhere (for example in case of Flask or even Django, you typically want to let your web framework build URLs and never modify string URLs).

The use of regex is not limited to URLs.

IMHO it is better to have one copy of this functionality in main jekyll, instead of multiple variants of it in salt, ansible, etc.