How to avoid output (for XHTML) in mako?

Although it offers a great way to avoid output using filters, none of them do the right thing. Take line:

x=u"&\u0092"

Filters perform the following actions:

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 leaves undefined (among others) 65 character codes (from 0 to 31 inclusive and from 127 to 159 inclusive)

The characters seem to be missing. Any ideas?

EDIT

It seems that I am checking the use of the file offline. Could this be a Content-Type issue?

+3
source share
2 answers

Unicode &#xxxx; HTML, ASCII. , UTF-8 . , , HTTP <meta>.

EDIT:

, . Content-Type?

. HTTP- UTF-8 HTML :

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

, ( ), - . `lib/helpers.py ':

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

def sgmlsafe(text):
    lookup = {
        130:"&#8218;",    #Single Low-9 Quotation Mark
        131: "&#402;",    #Latin Small Letter F With Hook
        132:"&#8222;",    #Double Low-9 Quotation Mark
        133:"&#8230;",    #Horizontal Ellipsis
        134:"&#8224;",    #Dagger
        135:"&#8225;",    #Double Dagger
        136: "&#710;",    #Modifier Letter Circumflex Accent
        137:"&#8240;",    #Per Mille Sign
        138: "&#352;",    #Latin Capital Letter S With Caron
        139:"&#8249;",    #Single Left-Pointing Angle Quotation Mark
        140: "&#338;",    #Latin Capital Ligature OE
        145:"&#8216;",    #Left Single Quotation Mark
        146:"&#8217;",    #Right Single Quotation Mark
        147:"&#8220;",    #Left Double Quotation Mark
        148:"&#8221;",    #Right Double Quotation Mark
        149:"&#8226;",    #Bullet
        150:"&#8211;",    #En Dash
        151:"&#8212;",    #Em Dash
        152: "&#732;",    #Small Tilde
        153:"&#8482;",    #Trade Mark Sign
        154: "&#353;",    #Latin Small Letter S With Caron
        155:"&#8250;",    #Single Right-Pointing Angle Quotation Mark
        156: "&#339;",    #Latin Small Ligature OE
        159: "&#376;"     #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}
+1

Source: https://habr.com/ru/post/1729745/


All Articles