2008/10/24

Let Google do the syntax highlighting

Guido recently posted some sample code to his new blog. The first comment complained that the code was "really crappy", improperly indented and hard to read.

Surprisingly, something good came of the rude complaint. Subsequent commenters described how to make simple changes to the blog template which would delegate code syntax highlighting to a Javascript library, google-code-prettify, which is hosted on Google Code.

This looks a lot simpler than the procedure I've been using, which involves embedding a lengthy blog template stylesheet and, whenever I want to post highlighted code, a 2-3 step incantation from within TextMate.

Anyhoo, this post is a test to see if I can incorporate google-code-prettify into this blog.

def genURLs(station):
tmpl = ("ftp://ftp.ncdc.noaa.gov/pub/data/asos-fivemin/6401-"
"{year}/64010{station}{year}{month:02d}.dat")
today = datetime.date.today()
year = 2004
month = 6
while True:
d = datetime.date(year, month, 1)
if d > today:
return
yield tmpl.format(year=year, month=month, station=station)

month += 1
if month >= 13:
month = 1
year += 1

And another -- this time in C++
#include "test_qt_sample.h"

void TestQString::toUpper() {
QString str = "Hello";
QVERIFY(str.toUpper() == "HELLO");
}

QTEST_MAIN(TestQString)

One more, with long source lines.
#!/usr/bin/env python
# encoding: utf-8

msg = "Surprisingly, something good came of the rude complaint. Subsequent commenters described how to make simple changes to the blog template which would delegate code syntax highlighting to a Javascript library, google-code-prettify, which is hosted on Google Code."

print len(msg.split())

I love the Python community. It is filled with people who respond to every complaint and criticism with courteous, useful solutions. (Google Code ain't bad, either :)

No comments: