将字符串打印为 HTML

发布于 2024-09-01 09:00:29 字数 128 浏览 2 评论 0原文

我想知道是否有任何方法可以将纯 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2024-09-08 09:00:29

如果 Genshi 就像 KID 一样工作(它应该如此),那么您所要做的就是

${XML("<p>Hi!</p>")}

我们有一个小函数可以从 wiki 格式转换为 HTML

def wikiFormat(text):
    patternBold = re.compile("(''')(.+?)(''')")
    patternItalic = re.compile("('')(.+?)('')")
    patternBoldItalic = re.compile("(''''')(.+?)(''''')")
    translatedText = (text or "").replace("\n", "<br/>")
    translatedText = patternBoldItalic.sub(r'<b><i>\2</i></b>', textoTraducido or '')
    translatedText = patternBold.sub(r'<b>\2</b>', translatedText or '')
    translatedText = patternItalic.sub(r'<i>\2</i>', translatedText or '')
    return translatedText

您应该根据您的需要调整它。

${XML(wikiFormat(text))}

If Genshi works just as KID (which it should), then all you have to do is

${XML("<p>Hi!</p>")}

We have a small function to transform from a wiki format to HTML

def wikiFormat(text):
    patternBold = re.compile("(''')(.+?)(''')")
    patternItalic = re.compile("('')(.+?)('')")
    patternBoldItalic = re.compile("(''''')(.+?)(''''')")
    translatedText = (text or "").replace("\n", "<br/>")
    translatedText = patternBoldItalic.sub(r'<b><i>\2</i></b>', textoTraducido or '')
    translatedText = patternBold.sub(r'<b>\2</b>', translatedText or '')
    translatedText = patternItalic.sub(r'<i>\2</i>', translatedText or '')
    return translatedText

You should adapt it to your needs.

${XML(wikiFormat(text))}
叹梦 2024-09-08 09:00:29

也许使用

 标签。

Maybe use a <pre> tag.

新雨望断虹 2024-09-08 09:00:29
  1. 通过转义“<”和“&”字符(也许更多,但这两个是绝对最小值)将纯文本转换为 HTML HTML 实体

  2. 用文本“
    ”替换每个换行符,可能仍与换行符组合。

按照这个顺序。

总而言之,这不应该超过几行 Python 代码。 (我不使用Python,但任何Python程序员都应该能够轻松做到这一点。)

编辑我发现网络上的代码作为第一步。对于步骤 2,请参阅此页面<底部的string.replace /a>.

  1. Convert plain text to HTML, by escaping "<" and "&" characters (and maybe some more, but these two are the absolute minimum) as HTML entities

  2. Substitute 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.

浅语花开 2024-09-08 09:00:29

如果有人感兴趣,这就是我解决它的方法。这是数据发送到 genshi 模板之前的 python 代码。

from trac.wiki.formatter import format_to_html
from trac.mimeview.api import Context
...
    context = Context.from_request(req, 'resource')
    data['comment'] = format_to_html(self.env, context, comment, True)
    return template, data, None

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.

from trac.wiki.formatter import format_to_html
from trac.mimeview.api import Context
...
    context = Context.from_request(req, 'resource')
    data['comment'] = format_to_html(self.env, context, comment, True)
    return template, data, None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文