On A Roll

So I finally completed a website update that’s been on my list for ages: I’ve added a blogroll (on the right, scroll down past the category tree). I decided a long time ago that I didn’t want to maintain a blogroll by hand. Of the various websites that help you create a blogroll, Bloglines seemed like the best fit- Bloglines is my aggregator, so it already knows what sites I read.

Bloglines offers several ways to share your blogroll. The most basic is your public url, which is a publicly viewable version of your subscriptions within the Bloglines interface. This is a great way to let others see what you read, but not to embed a blogroll within your site.

The next way to create a blogroll via Bloglines, and the option actually designed for the purpose, is the Bloglines Blogroll Wizard (accessible after you’ve logged into Bloglines). It provides an HTML version of your blogroll in two ways- via script or literal HTML. For example, to include my blogroll, I can add the following to my page:

<script language="javascript" type="text/javascript" 
src="http://rpc.bloglines.com/blogroll?id=jclark"></script>

Of course, I really don’t like the idea of using script to build my page. You can get the same HTML that the script would insert via direct URL, but the HTML generated by Bloglines isn’t laid out the way I want. Here’s an excerpt of the HTML generated by Bloglines for my blogroll:

<div class="blogrollmain">
<p /><div class="blogrollfolder">Corporate</div>
<div class="blogrollitem">
    <a href="http://weblogs.oreilly.com/">O'Reilly Weblogs</a></div>
<div class="blogrollitem">
    <a href="http://www.macdevcenter.com/">MacDevCenter.com</a></div>

I don’t mind the <div> wrapped around the entire blogroll, but wrapping every website link in a <div> is a little over the top. And using an empty <p /> tag for page layout? How 1999 is that? The blogroll is just a list of folders, each containing a list of links (assuming you use folders). I wanted to set this up the same way my Categories list is laid out- as a <ul> containing nested <ul> blocks.

Fortunately, there’s another way to get your blogroll data from Bloglines. From the public view, you can choose to “export” your subscriptions. This gives you an OPML file containing all of your blogs and folders. Since OPML is XML (and so is XHTML), this seems like a prime use for XSLT.

I had set out to create a blogroll from Bloglines’ OPML via XSLT months ago, but I got hung up on the mechanics. I had intended to have my webserver retrieve the Bloglines OPML, and to transform it on the webserver. To this end, I asked my hosting company to install an XSLT library for Perl, but I asked for XML::XSLT instead of XML::LibXSLT, and found that XML::XSLT was not complete enough to be effective. I ended up shelving the idea for a while.

When I decided this weekend to finally get my blogroll online, it occurred to me that my blogroll doesn’t change very often. With updates so infrequent, I really don’t need to put the maintence of the blogroll on the server. Instead, I wrote a perl script which retrieves my Bloglines OPML file, converts it to XHTML using an XSLT Stylesheet, and uploads to results to my website, where I use Nelson Minar’s File Plugin for Blosxom to include the file in my page output. I just run this script from my Powerbook whenever I update my Bloglines subscriptions.

A few notes about the script and stylesheet: you might notice that I upload the blogroll into a state/file directory. I’ve modified my copy of the File plugin to look for files in $plugin_dir/state/file, in keeping with the informal standard for Blosxom plugins. Also of interest is the FTP upload code. I didn’t see any need to write my XHTML out to a file just so I could FTP it, but I couldn’t figure out how to upload the contents of a scalar via FTP using the common Perl FTP modules. I ended up using the command line utility curl to perform the FTP upload. Curl will accept a file from stdin, which works perfectly from perl by opening a pipe to the command. The stylesheet uses no id attributes, and only one class attribute for attaching my CSS. This class, set on the outermost <ul>, is ‘categories’, which is the same as my category tree. By sharing rules, I didn’t have to write any new CSS for the blogroll to fit into my layout. Also note that as written, the XSLT doesn’t properly handle top-level subscriptions (those not assigned to a folder in Bloglines). If/when I fix that, I’ll repost the XSLT and make a note here.

You can leave a response, or trackback from your own site.

Leave a Reply