Python / genshi newline in html <p> paragraphs

I am trying to output the contents of a comment using genshi, but I cannot figure out how to convert newlines to HTML paragraphs.

Here is a test example of how it should look:

input: 'foo\n\n\n\n\nbar\nbaz'

output: <p>foo</p><p>bar</p><p>baz</p>

I searched everywhere for this feature. I could not find it in genshi or in python std lib. I am using TG 1.0.

+3
source share
3 answers
def tohtml(manylinesstr):
    return ''.join("<p>%s</p>" % line
          for line in manylinesstr.splitlines()
          if line)

So for example

print repr(tohtml('foo\n\n\n\n\nbar\nbaz'))

emits:

'<p>foo</p><p>bar</p><p>baz</p>'

as needed.

+3
source

Genshi may have a built-in function, but if not, it will do it for you:

output = ''.join([("<p>%s</p>" % l) for l in input.split('\n')])
+2
source

, , TG1 TG2, webhelpers, IMO .

nl2br, format_paragraphs.

+1

Source: https://habr.com/ru/post/1714819/


All Articles