pythongenshi

Genshi: for loop inserts line breaks


Source code: I have the following program.

import genshi
from genshi.template import MarkupTemplate

html = '''
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
        <head>
        </head>
        <body>
            <py:for each="i in range(3)">
                <py:choose>
                    <del py:when="i == 1">
                        ${i}
                    </del>
                    <py:otherwise>
                        ${i}
                    </py:otherwise>
                </py:choose>
            </py:for>
        </body>
    </html>
'''

template = MarkupTemplate(html)
stream = template.generate()
html = stream.render('html')

print(html)

Expected output: the numbers are printed consecutively with no whitespace (and most critically no line-break) between them.

<html>
    <head>
    </head>
    <body>
            0<del>1</del>2
    </body>
</html>

Actual output: It outputs the following:

<html>
    <head>
    </head>
    <body>
            0
            <del>1</del>
            2
    </body>
</html>

Question: How do I eliminate the line-breaks? I can deal with the leading whitespace by stripping it from the final HTML, but I don't know how to get rid of the line-breaks. I need the contents of the for loop to be displayed as a single continuous "word" (e.g. 012 instead of 0 \n 1 \n 2).

What I've tried:


Solution

  • Not ideal, but I did finally find an acceptable solution. The trick is to put the closing caret for a given tag on the next line right before the next tag's opening caret.

    <body>
        <py:for each="i in range(3)"
            ><py:choose
                ><del py:when="i == 1">${i}</del
                ><py:otherwise>${i}</py:otherwise
            ></py:choose
        </py:for>
    </body>
    

    Source: https://css-tricks.com/fighting-the-space-between-inline-block-elements/

    If anyone has a better approach I'd love to hear it.