[pygtk] quoting text for markup
Tony Nelson
tonynelson at georgeanelson.com
Fri Mar 20 02:40:43 WST 2009
At 08:28 -0700 2009/03/19, Darren Hart wrote:
>On Thu, Mar 19, 2009 at 12:59 AM, Walter Leibbrandt
><walter at translate.org.za> wrote:
>> Hi
>>
>> Darren Hart wrote:
>>>
>>> When adding text with an & character to a cellrenderertext I get some
>>> rendering glitches (and the actual text doesn't get rendered at all).
>>> Is there a standard mechanism for quoting text destined for the markup
>>> property?
>>
>> Since Pango markup strings are XML strings, you should also quote your
>> markup strings as such. That means that an & should become &.
>
>RIght & < and > - but my question was if there is some python call to
>do this or if I just have to roll my own. I recently added this to my
>code base:
>
>def quote_markup(str):
> return str.replace("&", "&").replace(">", ">").replace("<", "<")
>
>It just would be nice if gtk offered something like that - or at least
>didn't break if you put an & in the text. My version is pretty
>braindead, but if I use it carefully in my app, it works fine.
See xml.sax.saxutils for a core Python version.
I also have one, derived from BeautifulSoup.py, that should not damage
existing character entity references:
base_entities = { '&':'amp', '<':'lt', '>':'gt' }
base_entities_re = re.compile("([<>]|&(?!#\d+;|#x[0-9a-fA-F]+;|\w+;))")
def safe_escape(s):
'''Escape bare <>&, but don't damage properly formed character
entity references. Note that although is is legal to leave
off the ";" if the result is not ambiguous, browsers differ
in how they handle such, and we will escape them.
'''
return base_entities_re.sub(
lambda m: '&%s;' % base_entities[m.group(0)[0]], s )
It's fairly new, but I think it works.
--
____________________________________________________________________
TonyN.:' <mailto:tonynelson at georgeanelson.com>
' <http://www.georgeanelson.com/>
More information about the pygtk
mailing list