Created
November 18, 2009 00:43
-
-
Save dryan/237435 to your computer and use it in GitHub Desktop.
Django middleware for wrapping ampersands in a span
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Requires BeautifulSoup | |
| class Ampersands(object): | |
| from BeautifulSoup import BeautifulSoup | |
| import re | |
| def process_response( self, request, response ): | |
| if response.has_header('Content-Type') and response['Content-Type'].startswith('text/html') and request.META['PATH_INFO'][0:7] != '/admin/': | |
| content = BeautifulSoup(response.content) | |
| try: | |
| for t in content.body.findAll(text = re.compile( r'.*&.*' )): | |
| text = unicode(t) | |
| if not str(t.parent) == '<span class="amp">&</span>': | |
| text = text.replace('&', '<span class="amp">&</span>') | |
| t.replaceWith(text) | |
| response.content = content | |
| except: | |
| pass | |
| return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment