将python/genshi换行符转换为html< p>段落 [英] python/genshi newline to html <p> paragraphs

查看:371
本文介绍了将python/genshi换行符转换为html< p>段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用genshi输出注释的内容,但是我不知道如何将换行符转换为HTML段落.

I'm trying to output the content of a comment with gens but I can't figure out how to transform the newlines into HTML paragraphs.

这是一个看起来像这样的测试用例:

Here's a test case of what it should look like:

输入:'foo\n\n\n\n\nbar\nbaz'

输出:<p>foo</p><p>bar</p><p>baz</p>

我到处都在寻找这个功能.我在genshi或python的std lib中找不到它.我正在使用TG 1.0.

I've looked everywhere for this function. I couldn't find it in genshi or in python's std lib. I'm using TG 1.0.

推荐答案

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

例如,

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

发射:

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

根据需要.

这篇关于将python/genshi换行符转换为html&lt; p&gt;段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆