[Python-ideas] Add "htmlcharrefreplace" error handler
M.-A. Lemburg
mal at egenix.com
Tue Jun 11 17:04:03 CEST 2013
On 11.06.2013 16:49, Serhiy Storchaka wrote:
> I propose to add "htmlcharrefreplace" error handler which is similar to "xmlcharrefreplace" error
> handler but use html entity names if possible.
>
>>>> '∀ x∈ℜ'.encode('ascii', 'xmlcharrefreplace')
> b'∀ x∈ℜ'
>>>> '∀ x∈ℜ'.encode('ascii', 'htmlcharrefreplace')
> b'∀ x∈ℜ'
>
> Possible implementation:
>
> import codecs
> from html.entities import codepoint2name
>
> def htmlcharrefreplace_errors(exc):
> if not isinstance(exc, UnicodeEncodeError):
> raise exc
> try:
> replace = r'&%s;' % codepoint2name[ord(exc.object[exc.start])]
> except KeyError:
> return codecs.xmlcharrefreplace_errors(exc)
> return replace, exc.start + 1
>
> codecs.register_error('htmlcharrefreplace', htmlcharrefreplace_errors)
>
> Even if do not register this handler from the start, it may be worth to provide
> htmlcharrefreplace_errors() in the html or html.entities module.
+1 on that one as well :-)
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Jun 11 2013)
>>> Python Projects, Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2013-07-01: EuroPython 2013, Florence, Italy ... 20 days to go
2013-07-16: Python Meeting Duesseldorf ... 35 days to go
::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list