“安全的” python HTML 文本格式化(ala Textile)

发布于 2024-08-10 09:16:01 字数 220 浏览 4 评论 0原文

我一直在寻找纺织风格的现有 python 库来格式化文本以供用户输入。

如果只是我输入它,只输入 Textile 就可以了,但由于输入是针对 django 应用程序的,它将接受用户输入并显示它,同时仍然保持一些格式。

我设法在我看到的现有库中找到了一些小漏洞。他们有时不会以应有的方式逃避事情,会让我直接输入 HTML,这样的例子不胜枚举。

那么我可以使用哪些推荐的转换引擎呢?

I've been looking around for an existing python library in the style of textile to format text for users to enter.

If it was just me entering it, just textile would have been fine, but since the input is meant for a django app that will take user input and display it, while still maintaining some formatting.

I managed to find little loopholes here in there in the existing libraries I saw. They sometimes wouldn't escape things the way they should have, would let me input straight HTML and the list goes on.

So what are some recommendations of conversion engines I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

情话墙 2024-08-17 09:16:01

如果您使用 Django,则可以尝试安全 Markdown:

{% load markup %}

{{ foo|markdown:"safe" }}

您需要安装 Markdown,并在 settings.py 应用中安装 django.contrib.markup

如果你想在保存时清理 HTML,我很幸运地使用 feedparser 的清理 (http://www.feedparser. org/)。

import feedparser

body = feedparser._sanitizeHTML(body, 'utf8')

If you're using Django, you could try safe markdown:

{% load markup %}

{{ foo|markdown:"safe" }}

You'll need to have markdown installed, and django.contrib.markup in your settings.py apps.

If you want to sanitize HTML on save, I've had good luck using feedparser's sanitize (http://www.feedparser.org/).

import feedparser

body = feedparser._sanitizeHTML(body, 'utf8')
找回味觉 2024-08-17 09:16:01

如果您正在寻找纺织解决方案:django 标记使用的 PyTextile 实际上有一个 Textile_restricted() 函数,由于某种原因,该函数从未进入 django.contrib.markup。您可以使用此功能来提供受限纺织品。通过向模型添加一个调用textile_restricted的方法,或者使用自定义模板标签textile_restricted(由以下代码定义):

from django import template
from django.conf import settings
from django.utils.encoding import smart_str, force_unicode
from django.utils.safestring import mark_safe

register = template.Library()

def textile_restricted(value):
    try:
        import textile
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in {% textile %} filter: The Python textile library isn't installed.")
        return force_unicode(value)
    else:
        return mark_safe(force_unicode(textile.textile_restricted(smart_str(value))))
textile_restricted.is_safe = True

register.filter(textile_restricted)

If you are looking for a textile solution: the PyTextile that django markup uses actually has a textile_restricted() function, which for some reason, never made it into django.contrib.markup. You can use this function to provide restricted textile. Either by adding a method to your model that calls textile_restricted, or use a custom template tag textile_restricted, defined by the following code:

from django import template
from django.conf import settings
from django.utils.encoding import smart_str, force_unicode
from django.utils.safestring import mark_safe

register = template.Library()

def textile_restricted(value):
    try:
        import textile
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in {% textile %} filter: The Python textile library isn't installed.")
        return force_unicode(value)
    else:
        return mark_safe(force_unicode(textile.textile_restricted(smart_str(value))))
textile_restricted.is_safe = True

register.filter(textile_restricted)
浮光之海 2024-08-17 09:16:01

您是否尝试过包含的 django.contrib.markup 库?

Did you try the included django.contrib.markup libraries?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文