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

Add suppress_whitespace config option and add config parameter to the derive macro #664

Merged
merged 7 commits into from
Apr 21, 2022

Conversation

GuillaumeGomez
Copy link
Collaborator

I recently disovered in jinja (with Flask in python) that we could enable automatic_block_trim, allowing us to remove all the - and also preventing us to forget them. I saw it was missing in this crate so I thought it'd be a good addition.

So now, about the newly added config parameter in the derive macro: I realized when writing the tests that I couldn't have a specific configuration for a given test, making it quite complex. So the easiest solution was to create this new parameter. If you prefer, I can open a separate PR for it (its implementation is in the second commit). Of course, it's only if both features make sense to you.

Finally: I didn't find out where to put the documentation for config. Do you have a suggestion?

In any case: thanks a lot for writing this crate! It's really nice to use.

askama_shared/src/generator.rs Outdated Show resolved Hide resolved
testing/tests/gen_ws_tests.py Outdated Show resolved Hide resolved
testing/tests/gen_ws_tests.py Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Collaborator Author

Fixed the typos. :)

@vallentin vallentin requested a review from djc April 8, 2022 13:53
Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you see the earlier discussion in #598? In addition to the smaller questions below, I'm not sure I like the specified behavior much where the only effect of a - when automatic_block_trim is enabled is to disable the trim on the other side of the block. IMO the design I think we discussed earlier where a + could be added to locally revert the default seems much clearer, since it is explicit and does not cause sort spooky action at a distance (even if the distance is limited).

What do you think? I forget what Jinja and Tera do exactly...

askama_shared/src/generator.rs Show resolved Hide resolved
book/src/configuration.md Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Collaborator Author

GuillaumeGomez commented Apr 11, 2022

Did you see the earlier discussion in #598? In addition to the smaller questions below, I'm not sure I like the specified behavior much where the only effect of a - when automatic_block_trim is enabled is to disable the trim on the other side of the block. IMO the design I think we discussed earlier where a + could be added to locally revert the default seems much clearer, since it is explicit and does not cause sort spooky action at a distance (even if the distance is limited).

What do you think? I forget what Jinja and Tera do exactly...

I just checked and they indeed use + to revert the automatic trimming (my bad, I thought the - was doing it). So if you agree, I can make the change.

Btw, what do you think of the new derive template argument?

@djc
Copy link
Owner

djc commented Apr 11, 2022

I think adding a config argument to the template attribute makes sense in principle, will review the changes in more detail once you've split the commit.

@GuillaumeGomez
Copy link
Collaborator Author

Thanks for confirming. Then I'll make the changes as soon as possible.

@GuillaumeGomez
Copy link
Collaborator Author

Updated! Little sum up of what I did:

  • I split the refactoring into its own commit.
  • I then made a new commit where I add the support for the new config parameter.
  • I adding the parsing for the + into its own commit as well.
  • I extended the tests to handle both + and - (in case the automatic_block_trim option is enabled).
  • I extended the documentation for automatic_block_trim.
  • I added documentation for config (finally found where the template parameters documentation was).

@djc
Copy link
Owner

djc commented Apr 11, 2022

Sounds great! I'll review this tomorrow.

askama_shared/src/generator.rs Outdated Show resolved Hide resolved
askama_shared/src/generator.rs Outdated Show resolved Hide resolved
askama_shared/src/parser.rs Outdated Show resolved Hide resolved
askama_shared/src/parser.rs Outdated Show resolved Hide resolved
askama_shared/src/parser.rs Outdated Show resolved Hide resolved
askama_shared/src/lib.rs Outdated Show resolved Hide resolved
testing/tests/gen_ws_tests.py Outdated Show resolved Hide resolved
book/src/configuration.md Outdated Show resolved Hide resolved
book/src/configuration.md Outdated Show resolved Hide resolved
book/src/configuration.md Outdated Show resolved Hide resolved
@GuillaumeGomez GuillaumeGomez changed the title Add automatic_block_trim config option and add config parameter to the derive macro Add suppress_whitespace config option and add config parameter to the derive macro Apr 13, 2022
@GuillaumeGomez
Copy link
Collaborator Author

Applied suggestions!

askama_shared/src/generator.rs Outdated Show resolved Hide resolved
@GuillaumeGomez
Copy link
Collaborator Author

Fixed the invalid rebase. Sorry for the noise.

Copy link
Owner

@djc djc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

@djc
Copy link
Owner

djc commented Apr 13, 2022

@Kijewski @vallentin any opinions?

@GuillaumeGomez
Copy link
Collaborator Author

Any news here? :)

@djc
Copy link
Owner

djc commented Apr 18, 2022

Given that it's been the Easter weekend, I'll give them like 24 more hours to respond. :)

@GuillaumeGomez
Copy link
Collaborator Author

That is a very good point. We'll see if they survived the chocolate day. 😆

Copy link
Collaborator

@vallentin vallentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the direction this is heading. Unless I'm misinterpreting something, and please correct me if I am. For HTML I'd love if it was possible to say "Trim all but 1 space". Given that in HTML <span>Foo</span><span>Bar</span> vs <span>Foo</span> <span>Bar</span> are rendered differently.

While <span>Foo</span> <span>Bar</span>, <span>Foo</span> <span>Bar</span>, <span>Foo</span>\n\n<<span>Bar</span>, <span>Foo</span>\n\n \n \n\n\n<span>Bar</span> are all rendered the same.

Anyways, this is more of a side-comment, but a feature I'd love to see extended later :)

askama_shared/src/generator.rs Show resolved Hide resolved
@GuillaumeGomez
Copy link
Collaborator Author

I really like the direction this is heading. Unless I'm misinterpreting something, and please correct me if I am. For HTML I'd love if it was possible to say "Trim all but 1 space". Given that in HTML <span>Foo</span><span>Bar</span> vs <span>Foo</span> <span>Bar</span> are rendered differently.

While <span>Foo</span> <span>Bar</span>, <span>Foo</span> <span>Bar</span>, <span>Foo</span>\n\n<<span>Bar</span>, <span>Foo</span>\n\n \n \n\n\n<span>Bar</span> are all rendered the same.

Anyways, this is more of a side-comment, but a feature I'd love to see extended later :)

It only impacts jinja block, not pure HTML. So unless you have a jinja block between your two <span>, it won't change the whitespace there (I hope you understood correctly ^^').

@vallentin
Copy link
Collaborator

vallentin commented Apr 19, 2022

It only impacts jinja block, not pure HTML. So unless you have a jinja block between your two <span>, it won't change the whitespace there (I hope you understood correctly ^^').

Yeah, I interpreted that much. But as in example, I have some loops like <ul> {% for %} <li>...</li> {% endfor %} </ul>, then the space between the <li> elements would be removed, right? :)

@GuillaumeGomez
Copy link
Collaborator Author

Absolutely! But if you want to keep them, you can use + like this: <ul> {% for %+} <li>...</li> {+% endfor %} </ul>.

@vallentin
Copy link
Collaborator

vallentin commented Apr 19, 2022

Absolutely! But if you want to keep them, you can use + like this: <ul> {% for %+} <li>...</li> {+% endfor %} </ul>.

Yeah, of course, and it's really nice, don't get me wrong :)

My point was "a feature I'd love to see extended later". I'd love to see an extension of this feature in the future, where it would be possible to configure, so it trims all but 1 space between all "items" :)

@GuillaumeGomez
Copy link
Collaborator Author

Oh I see. Sorry I misunderstood your point. Yes, it would be a nice extension! Don't forget to open an issue so it's not forgotten. 😉

Copy link
Collaborator

@Kijewski Kijewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, and the implementation looks good to me. Nice!

@djc djc merged commit b16085a into djc:main Apr 21, 2022
@GuillaumeGomez GuillaumeGomez deleted the automatic-trim branch April 21, 2022 08:36
@GuillaumeGomez
Copy link
Collaborator Author

Thanks a lot everyone for the careful reviews and suggestions, it was very nice contributing here! I'll try to take a look at @vallentin's suggestion too.

@djc
Copy link
Owner

djc commented Apr 21, 2022

@GuillaumeGomez thanks to you for doing the work on this change and dealing with my feedback!

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

Successfully merging this pull request may close these issues.

None yet

4 participants