faqts : Computers : Programming : Languages : Python : Snippets : Web Programming / Manipulating HTML files

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 4 people (75%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

HTMLizing text

Aug 13th, 2002 20:58
Ellen Spertus, Nathan Wallace, unknown unknown, Hans Nowak, Snippet 335, Gerrit Holl


"""
Packages: text.html
"""
"""
> > Is there a function in the standard Python library to HTML-ize text,
> > i.e. to replace 'a > b & c < d' with 'a > b & c < d'?
> > 
> > (I realize this can be done with regular expressions, but is there
> > an easy way?)
> 
> The following function replaces [<&>] unconditionally.
>     def escapeall(s):
> 	s = string.replace(s, '&', '&')		# do this first
> 	s = string.replace(s, '<', '<')
> 	s = string.replace(s, '>', '>')
> 	return s
> I used this in my 'HTMLtag' module which generates HTML tags.
This is better:
"""
def escapeall(s):
        import htmlentitydefs
        for k, v in htmlentitydefs.items():
                s = string.replace(s, v, '&' + k)
	return s
------
The above doesn't work under Python 2.2.  Use this instead:
def escapeall(s):
        import htmlentitydefs
        s = s.replace('&', '&')
        for k, v in htmlentitydefs.entitydefs.items():
                if v != '&':
                        s = s.replace(v, '&' + k)
        return s