My girlfriend started a cooking blog recently, if you want to keep track of what I eat, I suggest you subscribe to her feed. ;) To help her American followers (and others suffering under the burden of a unit system from the middle ages), I kludged together a little gadget to add to her Blogger pages. The gadget tries to convert a few metric units to something equivalent in stones, feet and that Fahrenheit thing.
replace method in
can match value, a possible prefix and unit, make backreferences and pass those to a conversion
function and replace the matched text in the blog posts with converted values in one go, like so:
1 2 3 4 5 6 7 8 9
But, what is that unicode
\u00bd, you say?
Oh, well as it happens some keyboard layouts have a 1/2 character and some people like to use it too….
Das_Konvertor() then does its magic using a case construct to multiply values based on the prefix of a unit, and then uses a second case construct to decide to convert in what way based on the type of unit encountered.
The code for das_Konvertor() is a bit long, but it looks somewhat like this:
1 2 3 4
It would be fun, and very web2.0ish to, instead of doing my own ugly conversions, pass the calculation to be done to the almighty Google calculator in an XMLHttpRequest and display the result. If it keeps raining in the weekends, I might do so. ;)