Code Monkey home page Code Monkey logo

Comments (16)

djc avatar djc commented on July 21, 2024

I don't think we offer control over this now, but it might be easy to change the behavior?

On the other hand, not sure if that's always desirable, and if not, how we would spell configuring it.

from askama.

mataha avatar mataha commented on July 21, 2024

On the other hand, not sure if that's always desirable (...)

Definitely not.

(...) and if not, how we would spell configuring it.

My first thought would be {%+ - that is, it should be within whitespace control territory.

EDIT: forgot {%+ implies whitespace preservation - how about {%= as in "equal indent"?

from askama.

djc avatar djc commented on July 21, 2024

My first thought would be {%+ - that is, it should be within whitespace control territory.

EDIT: forgot {%+ implies whitespace preservation - how about {%= as in "equal indent"?

Sounds somewhat reasonable. So we only allow this for macro definitions, right?

@Kijewski @vallentin any thoughts?

from askama.

Kijewski avatar Kijewski commented on July 21, 2024

I can see why one would want this feature, not only in like in mataha's example, but in regular HTML output, too.

I guess this would be reasonably simple to implement, at least if the indentation level was user supplied by some means. I don't think it would be feasible to figure out the indentation level automatically at compile time.

Then Generator could have an indentation_stack: Vec<String> that is pushed to by calls to a macro, and popped from at the end of a macro. In Generator::visit_lit() the topmost indentation_stack gets used to prefix new lines.

from askama.

mataha avatar mataha commented on July 21, 2024

What I've envisioned, though, is figuring out the indentation at compile time. Supplying it would require either some weird syntax ({#{1}+? I don't know), passing indent level through call (how?) or defining it globally in the template attribute (again, how?).

Generator does have access to whitespace in template here and here, right?

from askama.

djc avatar djc commented on July 21, 2024

We could do something like the whitespace after any newlines in the literal preceding the {% call %} node. That seems... a little magic and might not always have a very intuitive result but I suppose it would do the desired thing in a bunch of the common cases.

from askama.

mataha avatar mataha commented on July 21, 2024

That solves my problem in full and - perhaps counterintuitively - is what I would expect.
Besides that, what other cases do you think can occur?

from askama.

djc avatar djc commented on July 21, 2024

Well, all of these could happen:

    {%= call foo(bar) %}
    {% if true %}{%= call foo(bar) %}{% endif %}
    {% if true %}{% else %} bloop {%= call foo(bar) %}{% endif %}

In particular, this last one I think would repeat bloop for every line that the macro produces.

from askama.

mataha avatar mataha commented on July 21, 2024

How about make it either:

  • indent the contents with whitespace if the preceding characters after the last newline are all whitespace (i.e. match ^\s*; in the last case would not indent)
  • indent the contents with whitespace equal to the number of characters preceding a literal after the last newline (in the last case 7, but it would have to account for wide characters and possibly other corner cases...)

from askama.

djc avatar djc commented on July 21, 2024

@Kijewski any thoughts? Inventing a character sequence based on count seems way too magic. But I'm inclined to think the least magic behavior is that = just preserves the contents preceding the block after the last newline.

from askama.

Kijewski avatar Kijewski commented on July 21, 2024

I don't see any advantage in counting white space characters. If I use tabs, spaces, and some odd unicode characters to indent my code, then this exact indentation should be replicated.

In the end, I think everything between \n and {%= should be replicated. Maybe I want to wrap my text in a markdown blockquote >?

The only problem is if there are nodes {{expr}} / {%stmt%} before the {%=. I'd say, just make it a syntax error.

from askama.

djc avatar djc commented on July 21, 2024

I don't think we should make that a syntax error -- it would just only reproduce the whitespace after the {{ expr }} node: I specified the behavior as the "the literal preceding the {% call %}" node. So

 {% if true %}{% else %} bloop {%= call foo(bar) %}{% endif %}

would reproduce bloop on every line before the macro contents, because that's the literal before the {% call %} node.

from askama.

Kijewski avatar Kijewski commented on July 21, 2024

Yeah, makes sense.

  • {% else %} bloop {%= bloop and
  • {% else -%} bloop {%=bloop

?

from askama.

djc avatar djc commented on July 21, 2024

Yup.

from askama.

mataha avatar mataha commented on July 21, 2024

In the end, I think everything between \n and {%= should be replicated. Maybe I want to wrap my text in a markdown blockquote >?

That makes perfect sense. Just saying that at that point it's no longer "whitespace handling", of course.

from askama.

djc avatar djc commented on July 21, 2024

I think that's okay. Just needs good documentation.

from askama.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.