撇開驗證的問題,這是很有用,能夠去除這些字符(不無論如何顯示可靠)沒有必然逃脫其他任何東西。爲此我增加了以下功能'的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}
我希望它那麼簡單,但事實並非如此。我*我*使用UTF-8,它不會驗證。 – Draemon 2010-01-24 03:58:49
驗證XHTML嚴格。雖然HTML 4確實有問題。你需要它是非X的HTML嗎? – 2010-01-24 04:05:46
它是XHTML嚴格的,UTF-8,並且由在線和離線w3c驗證程序正確檢測到。但在線驗證器告訴我:「您在文本中使用了非法字符,HTML使用標準UNICODE聯盟字符庫」 – Draemon 2010-01-24 04:40:16