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 whitespace stripping in the input #598

Closed
wants to merge 2 commits into from
Closed

Add whitespace stripping in the input #598

wants to merge 2 commits into from

Conversation

Kijewski
Copy link
Collaborator

@Kijewski Kijewski commented Jan 6, 2022

In HTML sources indentation can aid readability, but it only bloats the
output. This PR adds the argument "strip" to templates, which lets you
automatically strip the template input:

  • "none": No stripping of the input. The generator still strips spaces
    at the back of the input.
  • "tail": The current default, i.e. pop a single newline character at
    the end.
  • "trim": remove all spaces at the front and back of a line, and remove
    empty lines.
  • "eager": like "trim", but replace multiple whitespaces with a single
    space.

The automatic stripping does not try to understand the input at all. It
does not know what xml:space="preserve", <pre> or <textarea>
means.

@vallentin
Copy link
Collaborator

vallentin commented Jan 6, 2022

I "don't" like the idea of trim. Because whitespace is or can be significant in HTML/CSS, i.e. with inline menus :)

Also, can this be used for #581? Because I looked a bit at that before Christmas, but then I got busy with Christmas and New Year's.

Wait, I misinterpreted it, it's only for whole lines, right? Hmm.

@Kijewski
Copy link
Collaborator Author

Kijewski commented Jan 6, 2022

It's like trim() is called on every line, then empty lines are dropped, and for the eager case s/\s+/ /g applied.

Of course, white spaces are significant in <pre> and <textarea>. But how often do you see <pre> nowadays? When do you use a <textarea> that is populated by static data, not by {{data}}? For these two cases you cannot apply the automatic stripping.

Copy link
Collaborator

@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.

Awesome that you're working on this, did you guys already see rust-lang/rust#92526? It's pretty amazing! This also came up there. I'll link to this PR in from there so that we can feedback from them.

I guess this also needs some good documentation in the book.

My main point of feedback is that I think we'd want a way to change this setting on a crate-wide basis, in the askama.toml configuration. There, we might make it part of an extension/type-based configuration (I think we already have some?) so that people can easily apply this to HTML but not plain text templates. I was going to suggest that we ditch any kind of per-template configuration but it just occurs to me that that might make it harder to test this well (end-to-end) the way you've done here.

askama_shared/src/lib.rs Outdated Show resolved Hide resolved
askama_shared/src/lib.rs Outdated Show resolved Hide resolved
askama_shared/src/lib.rs Outdated Show resolved Hide resolved
@camelid
Copy link

camelid commented Jan 6, 2022

Am I correct in thinking that this is all done at compile-time? If so, that makes this extra-exciting :)

@djc
Copy link
Collaborator

djc commented Jan 6, 2022

Am I correct in thinking that this is all done at compile-time? If so, that makes this extra-exciting :)

Yes, that would all be done at compile time.

@Kijewski
Copy link
Collaborator Author

Kijewski commented Jan 7, 2022

I moved Strip from askama_shared's root into askama_shared::input. That's were the other arguments life.

@djc
Copy link
Collaborator

djc commented Jan 11, 2022

Did you have any thoughts about the configuration approach?

@Kijewski
Copy link
Collaborator Author

Did you have any thoughts about the configuration approach?

Yes, that should be a project-wide configuration. I added the optional strip parameter to the config, too.

But I actually have not idea how to write a test for it. Can you give me some pointers?

@Kijewski
Copy link
Collaborator Author

I'm not convinced anymore that this is the best implementation.

Most likely it would be simpler and better to use Strip::apply() in Generator::write_buf_writable() instead of preformatting the input.

@Kijewski
Copy link
Collaborator Author

Kijewski commented Jan 31, 2022

I simplified the PR. Now only "none" and "eager" are allowed.

In HTML sources indentation can aid readability, but it only bloats the
output. This PR adds the argument "strip" to templates, which lets you
automatically strip the template input:

* "none": Don't strip any spaces in the input
* "tail": Remove a single single newline at the end of the input. This
  is the default
* "trim-lines": Remove all whitespaces at the front and back of all
  lines, and remove empty lines
* "eager": Like "trim", but also replace runs of whitespaces with a
  single space.

The automatic stripping does not try to understand the input at all. It
does not know what `xml:space="preserve"`, `<pre>` or `<textarea>`
means.
@Kijewski Kijewski marked this pull request as draft February 4, 2022 22:04
@Kijewski
Copy link
Collaborator Author

Kijewski commented Feb 4, 2022

My "simplified" version does not work. The "complicated" version https://github.com/djc/askama/commit/766d049536c17a089fd7031e639d9f4999c9e64c did. Reverted and rebased.

@Kijewski
Copy link
Collaborator Author

Superseded by #673.

@Kijewski Kijewski closed this May 21, 2022
@Kijewski Kijewski deleted the pr-strip branch May 21, 2022 13:44
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.

4 participants