April Fool's Day rocks! Maybe I enjoy it because it happens to share the same day as my birthday, but I think it's more to do with the fact that everyone's having fun, being lighthearted, and simply not taking things too seriously. Last year the blog wasn't in any shape to do anything fun for the occasion, but this year it took me about 10 minutes to whip up some middleware fun. That's right, if you can read and understand this right now, the secret is out: a bit of Django middleware is all that's needed to turn your blog into l33t-sp34k central.
I'll even go one step further than telling you how I did it, I'll give you the code:
import re
import lxml.html
trans = {
'cks': r'xxors',
'lol': r'r0flc0pt3r',
'the': r'teh',
'a': r'4',
'e': r'3',
'f': r'ph',
'g': r'6',
'h': r'|-|',
'i': r'1',
'o': r'0',
's': r'5',
}
def is_code_block(node):
return node.attrib.get('class', None) == 'highlight'
def recursive_leetifier(node):
for child in node.iterchildren():
if child.text and not is_code_block(child):
for pattern in trans.iterkeys():
child.text = re.compile(pattern, re.I).sub(trans[pattern], child.text)
if not is_code_block(child):
recursive_leetifier(child)
class LeetSpeakMiddleware(object):
def process_response(self, request, response):
try:
html = lxml.html.fromstring(response.content)
recursive_leetifier(html)
response.content = lxml.html.tostring(html)
except:
pass
return response
The idea behind it is simple: Let the request go completely through Django's request/response cycle, and just before returning the correct response, parse the HTML and convert all of the actual content to l33t by doing some simple regular expression substitution. I'm using lxml.html simply because I attended Ian Bicking's talk at Pycon 2008 and was intrigued. I must say that the familiar ElementTree interface helped a lot in getting this code up and running in a short amount of time.
Hopefully you all find this holiday to be as fun as I do, and maybe I'll see some more l33t next year!
All Content


1N |255 TEH (0|)3 15 7I24N5F0|2M3[) 2!
17'5 4 |317 71|2|21NG 70 |234[)
XKTHXBY |-|4PPV 4pr1l p|-|00l'5 D4y
Pretty cool, but there is no reason to translate google anaylitics javascript and any javascript at all :)
v4r p463Tr4ck3r = _64t._63tTr4ck3r("U4-1959803-2");p463Tr4ck3r._1n1tD4t4();
Happy Birthday!
This kicks hard and rocks harder.
lol
That just...doesn't make sense!
You dont need to try to be funny only because it is April 1st.
BTW, happy birthday :)
Thanks now we can read your blog :)
Normally I hate April fools because it means all my feeds are trash. All of the exciting news could be completely fake. Many people take April fools day to be "lie your ass off day".
This, on the other hand, is awesome! Creative, fun, and it's a simple way to learn how to create middleware. Well done!
Ok, as I wrote in leetspeak on the first comment I will say it clear:
In rss (at least viewed with google reader) the code was leetspeak'ed too, so it was a bit of a pain to read, without comming to the site.
and happy birthday, late ;)