如何在 mako 中正确转义输出(对于 XHTML)?

发布于 2024-08-19 07:43:52 字数 616 浏览 6 评论 0原文

尽管提供了一种使用过滤器转义输出的好方法,但它们都没有做正确的事情。 获取字符串:

x=u"&\u0092"

过滤器执行以下操作:

x             Turns the & into an entity but not the \u0092 (valid XML but not XHTML)
h             Exactly the same
u             Escapes both, but obviously uses url escaping
entities      Only converts named entities, so again only the & is escaped
decode.latin1 The same

HTML 使用标准 UNICODE 联盟字符库,并且未定义(除其他外)65 个字符代码(包括 0 到 31 和 127 到 159)

这些似乎是遗漏的字符。有什么想法吗?

编辑

它似乎验证我是否离线使用该文件。这可能是内容类型问题吗?

Despite offering a nice way to escape output using filters, none of them do the right thing.
Taking the string:

x=u"&\u0092"

The filters do the following:

x             Turns the & into an entity but not the \u0092 (valid XML but not XHTML)
h             Exactly the same
u             Escapes both, but obviously uses url escaping
entities      Only converts named entities, so again only the & is escaped
decode.latin1 The same

HTML uses the standard UNICODE Consortium character repertoire, and it leaves undefined (among others) 65 character codes (0 to 31 inclusive and 127 to 159 inclusive)

These seem to be the characters missed. Any ideas?

EDIT

It seems to validate if I use the file offline. Could this be a Content-Type problem?

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

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

发布评论

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

评论(2

·深蓝 2024-08-26 07:43:52

无需将 Unicode 字符转换为 &#xxxx; 形式即可在 HTML 中使用,除非您有意使用 ASCII 字符集。转义命名实体,然后将整个字符串编码为 UTF-8 并像这样写出来,更简单、更高效。您可能应该声明 HTTP 标头或 标记中使用的编码。

编辑:

它似乎验证了我是否离线使用该文件。这可能是内容类型问题吗?

是的。您可以使用 HTTP 标头强制执行 UTF-8 字符集,也可以通过元标记直接在 HTML 中指定它:

<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8" />

It is not necessary to convert Unicode characters to the &#xxxx; form to work in HTML unless you're deliberately using the ASCII charset. It's simpler and more efficient to escape named entities, then encode the whole string to UTF-8 and write it out like that. You should probably declare the encoding being used in the HTTP headers or in a <meta> tag.

EDIT:

It seems to validate if I use the file offline. Could this be a Content-Type problem?

Yes. You can either use HTTP headers to enforce a UTF-8 charset or specify it in the HTML directly via a meta tag:

<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8" />
为你拒绝所有暧昧 2024-08-26 07:43:52

撇开验证问题不谈,能够删除这些字符(无论如何都不能可靠地显示)而不必转义其他任何内容是很有用的。为此,我将以下函数添加到“lib/helpers.py”:

__sgml_invalid = re.compile(r'[\x82-\x8c\x91-\x9c\x9f]', re.UNICODE)

def sgmlsafe(text):
    lookup = {
        130:"‚",    #Single Low-9 Quotation Mark
        131: "ƒ",    #Latin Small Letter F With Hook
        132:"„",    #Double Low-9 Quotation Mark
        133:"…",    #Horizontal Ellipsis
        134:"†",    #Dagger
        135:"‡",    #Double Dagger
        136: "ˆ",    #Modifier Letter Circumflex Accent
        137:"‰",    #Per Mille Sign
        138: "Š",    #Latin Capital Letter S With Caron
        139:"‹",    #Single Left-Pointing Angle Quotation Mark
        140: "Œ",    #Latin Capital Ligature OE
        145:"‘",    #Left Single Quotation Mark
        146:"’",    #Right Single Quotation Mark
        147:"“",    #Left Double Quotation Mark
        148:"”",    #Right Double Quotation Mark
        149:"•",    #Bullet
        150:"–",    #En Dash
        151:"—",    #Em Dash
        152: "˜",    #Small Tilde
        153:"™",    #Trade Mark Sign
        154: "š",    #Latin Small Letter S With Caron
        155:"›",    #Single Right-Pointing Angle Quotation Mark
        156: "œ",    #Latin Small Ligature OE
        159: "Ÿ"     #Latin Capital Letter Y With Diaeresis
        }

    return __sgml_invalid.sub(lambda x: lookup[ord(x.group())], text)

您可以通过编辑environment.py将其用作过滤器:

config['pylons.app_globals'].mako_lookup = TemplateLookup(
    ...
    imports=[....,'from appname.lib.helpers import sgmlsafe',...]

然后它应该可用于您的模板:

${c.content|n,sgmlsafe}

Validation issues aside, it's useful to be able to remove these characters (which don't display reliably anyway) without necessarily escaping anything else. To this end I added the following function to `lib/helpers.py':

__sgml_invalid = re.compile(r'[\x82-\x8c\x91-\x9c\x9f]', re.UNICODE)

def sgmlsafe(text):
    lookup = {
        130:"‚",    #Single Low-9 Quotation Mark
        131: "ƒ",    #Latin Small Letter F With Hook
        132:"„",    #Double Low-9 Quotation Mark
        133:"…",    #Horizontal Ellipsis
        134:"†",    #Dagger
        135:"‡",    #Double Dagger
        136: "ˆ",    #Modifier Letter Circumflex Accent
        137:"‰",    #Per Mille Sign
        138: "Š",    #Latin Capital Letter S With Caron
        139:"‹",    #Single Left-Pointing Angle Quotation Mark
        140: "Œ",    #Latin Capital Ligature OE
        145:"‘",    #Left Single Quotation Mark
        146:"’",    #Right Single Quotation Mark
        147:"“",    #Left Double Quotation Mark
        148:"”",    #Right Double Quotation Mark
        149:"•",    #Bullet
        150:"–",    #En Dash
        151:"—",    #Em Dash
        152: "˜",    #Small Tilde
        153:"™",    #Trade Mark Sign
        154: "š",    #Latin Small Letter S With Caron
        155:"›",    #Single Right-Pointing Angle Quotation Mark
        156: "œ",    #Latin Small Ligature OE
        159: "Ÿ"     #Latin Capital Letter Y With Diaeresis
        }

    return __sgml_invalid.sub(lambda x: lookup[ord(x.group())], text)

And you can make this available as a filter by editing environment.py:

config['pylons.app_globals'].mako_lookup = TemplateLookup(
    ...
    imports=[....,'from appname.lib.helpers import sgmlsafe',...]

It should then be available to your templates:

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