Posted on April 1, 2008 at 1:14 A.M.

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!

tshirtman
at 3:25 a.m.
on April 1, 2008

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

Nick
at 5:35 a.m.
on April 1, 2008

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();

Joel
at 10:09 a.m.
on April 1, 2008

Happy Birthday!

at 11:11 a.m.
on April 1, 2008

This kicks hard and rocks harder.

lol

at 11:46 p.m.
on April 2, 2008

That just...doesn't make sense!

at 11:46 a.m.
on April 1, 2008

You dont need to try to be funny only because it is April 1st.

at 11:46 a.m.
on April 1, 2008

BTW, happy birthday :)

at 9:15 a.m.
on April 2, 2008

Thanks now we can read your blog :)

at 10:17 a.m.
on April 2, 2008

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!

tshirtman
at 1:10 p.m.
on April 2, 2008

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 ;)

Search

 

Recent Links

  • git-issues: A distributed issue tracker built-in to Git.
  • I predicted this back in March--can't believe a solution has surfaced so soon. It makes so much sense to build in an issue tracker to a revision control system. Since you're working with the code, might as well track the issues in the same system and take advantage of the extra metadata. This is really cool (and as a bonus, it's written in Python) so I hope to see it really grow and flourish!

  • How I Work Daily
  • Daily blog by Kevin Fricovsky. In addition to having some really great content, he has started to post audio interviews with people from the Django community. This is a site to keep an eye on in the coming days and months.

  • django-arcade
  • Demo site for django-arcade, an open source reusable Django app to add new flash games to any django-powered site. Looks very cool for easily creating game portals. It also comes from my future employer.

  • Facebook Chat and Scalability (with Erlang)
  • Eugene Letuchy talks about how they they took Facebook Chat from no users to 70 million users, with the help of Erlang.

  • Simon Willison: The Implications of OpenID
  • I somehow missed this presentation when it came out, but it's an absolutely fantastic overview and defense of OpenID by Simon Willison. If you are in any way interested in what OpenID is and what it can offer, you owe it to yourself to check out this presentation.

  • StupidXML
  • Probably the simplest XML library that I've seen for Python. Sometimes you just want to generate some stupid XML, and this is the perfect tool for the job.

  • See the rest of my links...

Pownce

Badges

  • django badge
  • apache badge
  • GeoURL
  • XFN Friendly
  • Valid HTML 4.01 Transitional