April Fools

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!

17 Comments So Far...

By 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

 

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

 

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

Happy Birthday!

 

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

This kicks hard and rocks harder.

lol

 

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

That just...doesn't make sense!

 

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

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

 

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

BTW, happy birthday :)

 

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

Thanks now we can read your blog :)

 

By Luke Hoersten 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!

 

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

 

By plastic injection moulding at 9:20 p.m. on June 13, 2009

that's a great post, thank you!

 

By jordan shoes at 3:10 a.m. on June 25, 2009

that's a great post, thank you!

 

By jordan shoes at 3:11 a.m. on June 25, 2009

Thanks now we can read your blog :)

 

By ugg boots at 3:12 a.m. on June 25, 2009

This, on the other hand, is awesome! Creative, fun, and it's a simple way to learn how to create middleware. Well done!

 

By nike shoes at 3:13 a.m. on June 25, 2009

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

 

By tiffany jewellery at 3:14 a.m. on June 25, 2009

Ok, as I wrote in leetspeak on the first comment I will say it clear:

 

By Forbes at 4:55 p.m. on July 2, 2009

I have been here many times and have always found this to be.
I am from Estonia and learning to read in English, give true I wrote the following sentence: "We are proud to say, we offer a higher most of the flea market is under shade trees or."

Waiting for a reply :-), Forbes.

 

Voice your opinion...