Greasemonkey and microformats (4)

Once more unto the breach, dear friends! In this third fourth (already?) installment of Microformats and Greasemonkey, we’ll wonder in sheer amazement at the unifying power of our two leading subjects when carelessly wielded against mounds of unstructured ignorance. How’s that for a beginning?

The previous post ended abruptly in a cliffhanger-wannabe fashion, suggesting great things to come from the UnitFormat Greasemonkey script. While it won’t regrow your lost hair or help you make fame and fortune, there are pesky measurement unit conversion tasks it can handle for you. Let’s throw an example.

Suppose you are publishing information about your wonderful hybrid car fuel consumption habits. You should know for a fact that the usual way to do that differs fundamentally between the UK (and the USA, for that matter) and the rest of the world. I may be thinking litres per 100 kilometres, while you are used to miles per gallon. Being a semantically conscious blogger, you may annotate your data this way:

My car does usually 
<abbr title="48" class="unit mi/gal l/100km">48 miles per gallon</abbr>

Live, it would look this way: 48 miles per gallon. With UnitFormat installed, you’d see this:

48 miles per gallon*

Note: if you have already installed UnitFormat, you ought to see two asterisks. That’s all right.

This is the generated HTML code. Note that it’s in the form of another measurement unit microformat, so screen readers and other tools may have a share of the benefits: it lacks a target unit, nevertheless.

<abbr title="48" class="unit mi/gal l/100km">48 miles per gallon</abbr><sup><abbr title="4.90030384" class="unit l/100km"><a title="4.90030384 l/100km">*</a></abbr></sup>

This is a tad more difficult than just converting feet to centimetres, but by now I’d bet you’ve already realised something far more interesting. Second example:

I paid <abbr title="23220" class="unit USD EUR">$23,200</abbr> for it.

Guess what? Google Calc handles the conversion all right:

I paid $23,220* for it.

Online currency conversions at your fingertips! But wait, there’s more. Third example:

I'm expecting a lifetime average cost of 
<abbr title="0.45" class="unit USD/mi EUR/km">45 cents per mile</abbr>.

This winds up as:

I’m expecting a lifetime average cost of 45 cents per mile*.

Complex units with variable conversion factors! Google Calc let’s you specify nearly anything, but you should stay with the simplest unit names (following standards and common sense whenever possible). Composite units should not have spaces in their names (but /, * and ^ are explicitly allowed, so units like m/s^2 are possible). Aren’t microformats powerful, and isn’t life great?