Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve_handlebar_syntax regex should be improved #271

Open
IwanBurg opened this issue Apr 8, 2022 · 0 comments
Open

preserve_handlebar_syntax regex should be improved #271

IwanBurg opened this issue Apr 8, 2022 · 0 comments

Comments

@IwanBurg
Copy link

IwanBurg commented Apr 8, 2022

Problem:
preserve_handlebar_syntax=True doesn't preserve handlebars with characters before or after the handlebars.

Examples of html that don't preserve handlebars:
a href="mailto:{{ Test }}"
a href="{{ Test }}?subject=x"

Current regex code:

stripped = re.sub(
                    r'="{{(.*?)}}"',
                    lambda match: '="{{' + escape(match.groups()[0]) + '}}"',
                    stripped,
                )
out = re.sub(
                    r'="%7B%7B(.+?)%7D%7D"',
                    lambda match: '="{{' + unescape(unquote(match.groups()[0])) + '}}"',
                    out,
                )

Proposed regex code:

stripped = re.sub(
                    r'="([^"]*){{(.*?)}}([^"]*?)"',
                    lambda match: '="' +
                                  match.groups()[0] +
                                  '{{' + escape(match.groups()[1]) + '}}' +
                                  match.groups()[2] + '"',
                    stripped,
                )

https://regex101.com/r/tLC41B/2

out = re.sub(
                    r'="([^"]*)%7B%7B(.+?)%7D%7D([^"]*?)"',
                    lambda match: '="' +
                                  match.groups()[0] +
                                  '{{' + unescape(unquote(match.groups()[1])) + '}}' +
                                  match.groups()[2] + '"',
                    out,
                )

https://regex101.com/r/ADvQjO/1

I tried making a pull request:
#270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant