phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
63 stars 3 forks source link

Help understanding behaviour of buffered comments followed by newlines #85

Closed alessandro-fazzi closed 3 years ago

alessandro-fazzi commented 3 years ago

Hello,

I encountered an issue with the following code:

// Buffered comment
| Foo
// Buffered comment
<= here's a newline

Please note the newline at the end of the snippet

I expected to get:

<!--  Buffered comment -->
Foo<!--  Buffered comment -->

But I actually get:

<!--  Buffered comment -->
Foo<!--  Buffered comment
 -->

Removing the newline will fix it, but having a comment at the end of the file will require a newline (at least to not be blamed by GIT). Another way to fix it is to add an empty character as last line of my file

// Buffered comment
| Foo
// Buffered comment
| &nbsp;

which code will bring to

<!--  Buffered comment -->
Foo<!--  Buffered comment -->
&nbsp;

and it's ok as a workaround, but I'd like to get the behaviour.

Keep in mind this is not merely for science: I'm dealing with "magic" comments in HTML which will be parsed and interpreted, thus I need to have a sturdy, specific formatting of the final HTML, not just valid.

The fact happens bot with and without pretty print.

As a final note about this behaviour:

// Buffered comment
| Foo
// Buffered comment

| &nbsp;

outputs

<!--  Buffered comment -->
Foo<!--  Buffered comment

 -->
&nbsp;

Thanks in advance for you help and your time.

kylekatarnls commented 3 years ago

Hello,

You can also just add a . and so nothing is printed in the output:

// Buffered comment
| Foo
// Buffered comment
.

To get the exact indent of the source code in your comment you also can use:

<!-- Buffered comment -->
| Foo
<!-- Buffered comment -->

I don't think I will fix this. But if someone want to propose a pull-request for this I would be glad to review and merge it if it does not break any other syntax.

Thanks.

alessandro-fazzi commented 3 years ago

Thanks for your alternative suggestions; the . is a nicer workaround and I'd be ok to use plain HTML comments in my scenario.

I'll also live in peace with this "thing" not fixed ;) and I thank you for your kind and quick reply.

Best