将字符串打印为 HTML
我想知道是否有任何方法可以将纯 unicode 字符串转换为 Genshi 中的 HTML,例如,它将换行符呈现为
。
我希望它能够呈现在文本区域中输入的一些文本。
提前致谢!
I would like to know if is there any way to convert a plain unicode string to HTML in Genshi, so, for example, it renders newlines as <br/>
.
I want this to render some text entered in a textarea.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果 Genshi 就像 KID 一样工作(它应该如此),那么您所要做的就是
我们有一个小函数可以从 wiki 格式转换为 HTML
您应该根据您的需要调整它。
If Genshi works just as KID (which it should), then all you have to do is
We have a small function to transform from a wiki format to HTML
You should adapt it to your needs.
也许使用
Maybe use a
<pre>
tag.通过转义“
<
”和“&
”字符(也许更多,但这两个是绝对最小值)将纯文本转换为 HTML HTML 实体用文本“
”替换每个换行符,可能仍与换行符组合。按照这个顺序。
总而言之,这不应该超过几行 Python 代码。 (我不使用Python,但任何Python程序员都应该能够轻松做到这一点。)
编辑我发现网络上的代码作为第一步。对于步骤 2,请参阅此页面<底部的
string.replace
/a>.Convert plain text to HTML, by escaping "
<
" and "&
" characters (and maybe some more, but these two are the absolute minimum) as HTML entitiesSubstitute every newline with the text "
<br />
", possibly still combined with a newline.In that order.
All in all that shouldn't be more than a few lines of Python code. (I don't do Python but any Python programmer should be able to do that, easily.)
edit I found code on the web for the first step. For step 2, see
string.replace
at the bottom of this page.如果有人感兴趣,这就是我解决它的方法。这是数据发送到 genshi 模板之前的 python 代码。
In case anyone is interested, this is how I solved it. This is the python code before the data is sent to the genshi template.