Sunday, August 21, 2011
Pretty code (2)
So the question is, what are the good ways to format code for blog posts, when you don't control the machine that serves the pages (like on Blogger). The first---and motivating---post in this series is here.
I always prefer simple to complex. I even prefer simple html pages, although I'll ignore the obvious problem that pages are not simple any more when Blogger gets through with them.
So I liked the idea of controlling the color of text in a page with self-defined tags. For example, we can put this in the head of our html document:
It's a seductively simple idea. But.. it requires a way of parsing Python code to generate the appropriate tags. I thought if I enforced some rules that could be done relatively easily.
After a few hours, I have something that sort of works, but it has too many bugs to be useful yet.
The main simplifying rule is that multi-line strings are not allowed. Also, at this point I assume that indentation is a four-space tab.
You can see the results in the graphic (above). I put the script on Dropbox (simple_parser.py). Without a lot more debugging, this seems like it's probably not the best option, but it was an entertaining exercise for the afternoon.