Todd Fast's Blog

Tuesday, Jan 25, 2005

Chris Webster's blog

He's kept quiet about it, but my colleague Chris Webster has a blog. Chris is an Open API black belt and was one of the lead engineers on the NetBeans 4.1 release. He worked hard to bring all sorts of cool J2EE development features to the NetBeans IDE. After a tour of duty in the NetBeans group, Chris is now back working with me on building Service-Oriented Architecture tools in the Java Studio Enterprise group. He's sure to have plenty of useful and interesting things to say in the coming year, so please pay him a visit.

HOWTO: Adding Technorati Cosmos Links to a Roller Blog

As I mention in the article below, my technique was primarily a hack to get around what I assumed was either a missing macro or missing documentation. Patrick Chanezon is more familiar with Velocity and has explained the proper way to escape links in Roller. Cheers, P@.

After seeing the convenient Technorati Cosmos links on Tim Bray's blog, I wanted to add them to my Roller-based blog. Unfortunately, it was a bit of trouble, so I'm blogging my findings here for my fellow Roller bloggers.

The Technorati Cosmos URL takes the following form:

http://www.technorati.com/cosmos/search.html?rank=&url=[blog permalink]

To use this URL, we merely need to supply the [blog permalink] in the template above and put the result in an anchor tag. In Roller, you can get the relative permalink (relative to the base domain and roller context path) with the following variable:

$entry.permaLink

However, if we want to avoid hard-coding the domain and Roller context path, you can prepend the following variable:

$absBaseURL$entry.permaLink

Now, putting this together in an anchor tag and remembering to escape the embedded URL, we have the following (note, this should all be one line when used in HTML):

<a href='http://www.technorati.com/cosmos/search.html?rank=&amp;
url=$absBaseURL$entry.permaLink'>...</a>

This almost works, but there is one subtlety with Roller's permalink syntax that caused a bit of trouble for me.

Specifically, Roller includes a document fragment in its permalink URL, and document fragments must be preceded by a hash mark (#). When tacked onto the end of the search URL above, the browser interprets the hash mark and fragment as belonging to the actual link instead of being part of the value of the url query parameter. Thus, when sending the search request to Technorati, the browser omits the fragment identifier and the search ends up returning the wrong result set. In order for the full permalink including the fragment identifier to be sent as part of the request, the hash mark preceeding the fragment identifier must be escaped so that it will be interpreted as part of the value of the url query parameter. Unfortunately, Roller doesn't include any macros (that I could find) to escape the hash mark.

My admittedly hack-esque solution was to use a little bit of JavaScript to escape the fragment identifier when the link is clicked. While this technique won't work if the user has turned off JavaScript, that condition is fairly uncommon these days; until someone addresses the missing macro in Roller, this technique should suffice for the majority of readers.

First, add the following JavaScript to your master Weblog page template's <head> tag:

<script language="JavaScript" type="text/JavaScript">
function replaceCharacters(conversionString, inChar, outChar)
{
    var convertedString = conversionString.split(inChar);
    convertedString = convertedString.join(outChar);
    return convertedString;
}

function escapeHref(object, inChar, outChar)
{
    object.href=replaceCharacters(object.href,inChar,outChar);
}
</script>

Now, modify your _day template and add the following anchor so that it appears once per entry:

<a href='http://www.technorati.com/cosmos/search.html?rank=&amp;
url=$absBaseURL$entry.permaLink' onclick='escapeHref(this,"#","%23")'>
...
</a>

Remember that although I've split lines above, you should put all of this on a single line in your template.

That's all there is to it. When the link is clicked, all hash mark occurrences in the href attribute will be replaced by the escaped form, "%23", before the URL is sent to Technorati.

[Find more about at Technorati]
[Find more about at Technorati]
[Find more about at Technorati]

Archives

Tags

Feeds

Search

Links

Navigation

Referers