Skip to content

Removing additional newlines left by block-like structures (solved) #375

Answered by shakfu
shakfu asked this question in Usage Questions
Discussion options

You must be logged in to vote

Ok, another few iterations helped.

I finally figured out a preprocessor function which solves this issue more generally:

def escape_extra_newlines(text):
    patterns = ['<%block', '<%include'] # extend patterns as required
    results = []
    for line in text.splitlines():
        if any(line.startswith(p) for p in patterns):
            line += '\\'
        results.append(line)
    return '\n'.join(results)

Which can be used as follows:

lookup = TemplateLookup(
    directories=['templates'],
    preprocessor= escape_extra_newlines,
    default_filters=['str', 'trim'], # in case you also want to trim whitespace via .strip() 
)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zzzeek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #371 on December 07, 2022 14:09.