You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Removing additional newlines left by block-like structures (solved) #371
I'm using mako to generate cpp code. I found that if I use blocks, additional newlines are added as a result of the block tags. While this is not a huge deal, it wasn't a problem with the jinja2 output that mako is supposed to replace.
After doing some research on this, it seems there is no built-in switch where I could switch off these extra new lines and I would have to add a backslash to the end of the block tag. This works but it is manual and ugly.
There was a post on the mako google groups where it was suggested by the author of mako to use a preprocessor to solve this automatically.
I've tried to do this, but the problem is that a backslash is an escape character in regex and an escape character python but itself needs to be escaped so you have to do one of:
So basically, there doesn't seem to be a way (at least to me) via regex where one can insert this backslash escape without inserting extraneous characters.
lookup=TemplateLookup(
directories=['templates'],
preprocessor=escape_extra_newlines,
default_filters=['str', 'trim'], # in case you also want to trim whitespace via .strip()
)
changed the title [-]Removing additional newlines from blocks (solved)[/-][+]Removing additional newlines left by block-like structures (solved)[/+]on Dec 3, 2022
Activity
shakfu commentedon Dec 2, 2022
Ok, another few iterations helped.
I finally figured out a preprocessor function which solves this issue more generally:
Which can be used as follows:
[-]struggling with whitespace[/-][+]Removing additional newlines from blocks (solved)[/+][-]Removing additional newlines from blocks (solved)[/-][+]Removing additional newlines left by block-like structures (solved)[/+]