Tuesday, March 31, 2009

Syntax Highlighting on Blogger with Pygments

It seems that Blogger is not a "by coders for coders" type of service. Not at all, actually. Currently, there's no easy way to post syntax highlighted code on your blog. Although, this blog is pretty new, I have a hunch I'll be posting bits of code quite often, so I thought I should find a way to do it, and do it right. Most of the solutions on the web today imply the use of complicated javascript libraries, overloading your blog and your readers' browsers and bandwidths. The solution I have come with is simple and CSS-only, so even those that browse with Javascript deactivated will see highlighted code. I will be using Pygments, a great tool able to highlight lots of programming and markup languages, made by the guys at Pocoo. Installing Pygments on Ubuntu was a breeze: $ sudo apt-get install python-pygments The first thing we need is a stylesheet. Pygments highlights the code by creating lots of <span>s with different CSS classes. Pygments supports multiple styles, I'll be using the default one. In Blogger, we will put our code between <code class="syntax"> ... </code> tags (it's just an example, you can use what you want). Here's how to get a stylesheet: $ pygmentize -S default -f html -a "code.syntax" > syntax.css Now, by default, the <code> element is inline. We need to make it a block element and add a few styles for it. So, edit the newly created syntax.css using your favorite text editor and put this in code.syntax: code.syntax { background: #f8f8f8; /* this was here by default */ display: block; overflow: auto; padding: 3px 5px; margin: 5px 3px; border: solid 1px #afafaf; color: #000; white-space: pre; } Now, upload this file to your webhost of choice (I use Google Pages) and add this code to your Blogger template, right before the </head> tag: <link rel="stylesheet" type="text/css" href="http://example.com/syntax.css"> Time to post some highlighted code. Finally! To post syntax highlighted code, run this command: $ pygmentize -f html -O nowrap /path/to/file Copy all that is outputted and paste it in your post inside <code class="syntax"> ... </code> tags. Done! Here's an example of pygmented code: #!/usr/bin/env python import sys import traceback import utils from bin.server import LearndServer if __name__ == '__main__': learnd = LearndServer( ) try: learnd.run() except KeyboardInterrupt: utils.log("KeyboardInterrupt - Exiting.") learnd.close() except: traceback.print_exc() utils.log("Exiting.") learnd.close() A note for people using tabs (like me). You will find that Blogger auto(not-so)magically converts tabs to single spaces. That can suck, so here's a workaround: $ pygmentize -f html -O nowrap /path/to/file | expand That will expand your tabs to 8 spaces by default. To change the number of spaces, pass the -t X parameter to the expand command. For example: $ pygmentize -f html -O nowrap /path/to/file | expand -t 4 And that about wraps it up, I guess. If I get the time, maybe I'll hack together a nice PyGTK application to ease this a little bit. Play around with different pygmentize parameters and configurations, let me know if you find out some really cool trick :).

The "Now Playing" Feature

Notice the "Now Playing" widget in the right sidebar? Just goes to show the awesome things you can do in Python in one evening. It's basically a Rhythmbox plugin that sends information about the currently playing song over HTTP. A small PHP script takes that and writes a simple Javascript file that just does a document.write() which is then included in the sidebar. Awesome! :-) I still have a few improvements to make (like add a configuration window, write my own asynchronous URL loader so I can POST the information etc.) and I will put it on the Third Party Rhythmbox plugins page. Here's how it looks like, in case you pass by at a time when I'm not listening to anything (probably sleeping or at school... or both):

Sunday, March 29, 2009

Why Oracle is stupid

We're learning Oracle SQL in school. Now, this may seem a good thing, but it is not, unfortuantely. For one, the Oracle "enhanced" SQL blows. It is completely un-standard and to quote an experienced programmer: "If you want to learn assbackward SQL, learn Oracle". To quote another experienced programmer, "It's a great hole in the ground into which money can be poured". The only good thing about Oracle is that big companies use it for the support they provide (if that can be called good). What bugs me the most, though, is the way they are force feeding it into our brains. They are teaching us each and every command we will never use in our entire lifetime. Why? I and all the programmers I know always code with the documentation opened. So when I need to know about a function, or anything else, I just search the documentation and find what I need to know. Naturally, the things I use most will remain in my memory after a (short) while and I won't have to search the documentation for it again. But there's absolutely no reason why I should remember everything. It's just stupid. And even if I learn everything now and pass my exams, I'll forget it by the end of the month. So what's the point? I get a certificate (for which I pay €100) that proves... What? I don't know, but I hope it proves something.