It’s hard for me to believe that in the not so distant past I worked as a web producer for O’Reilly’s now defunct Online Publishing Group. Basically I was a sort of HTML jockey, shuttling articles through their content management system while ensuring that they all had consistent markup.
Since the articles O’Reilly published online dealt predominantly with programming, the vast majority contained code snippets and samples. Thus one of my primary roles was to ensure that the embedded code displayed properly on the web. Our HTML “style guide” dictated that all code be wrapped in <pre><code></code></pre> tags. I remember at the time thinking it a bit redundant—I occasionally embed code on my blog, but for simplicity’s sake, I use <pre> alone.
I discovered today another value to the semantic use of HTML: machine translation. I was looking through my referrer logs, and noticed someone had translated one of my posts with code samples. And sure enough, Google translated everything within the <pre> tags, mangling the code. Gah!
This made me think all the way back to my O’Reilly days, and I wondered what Google would do if I also wrapped the code block in <code> tags. Sure enough, they respected the semantic “intent” of the <code> tag and left it untranslated—BUT they also stripped out the newlines, collapsing multiple lines of code to a single line. So if anyone from the Google Translate team is reading, consider this a bug report.
Note: Apparently Google will also avoid translating tags with class="notranslate".
It’s funny to think that in a small way I’ve helped countless users transition their Haloscan comments (usually embedded in Blogger posts) to WordPress over the past four and a half years, judging by the 203 comments that the two posts above have accumulated. Though it will be a bummer for the few users who are not able to make the transition, and thus lose their comments, I have to say it will be nice to have one less smidgen of code to support.
As an aside, I always get a chuckle out of the following image from Sam Ruby’s Rights vs Responsibilities post:
Since code costs essentially zero to distribute, my first thoughts are not on what rights I want to assert, but what obligations I wish to assume. If you look at software licenses, both commercial and open, this is something that they are careful to enumerate. In most cases, it essentially comes down to: “if it breaks, you get to keep both halves”.
Have you ever wanted to write a plugin that sits between a request for a WordPress page (or post) and the fulfillment of that request? In order to do so, use the pre_get_postsaction hook which passes a WP Query object as a parameter to your plugin function. This action is called early in the request flow, before any headers or content has been sent to the browser. So you can probe or modify the query object in order to create your own custom behavior.
As an example, I wanted to redirect all requests that come in for http://justinsomnia.org/random/ to a random post from my blog. Note the random link in my nav bar above. Here’s how you might do that:
<?php
/*
Plugin Name: Random Link
*/
add_action('pre_get_posts', 'random_link');
function random_link($query)
{
if ($query->get('pagename') == 'random') {
global $wpdb;
$sql = "select $wpdb->posts.ID
from $wpdb->posts
where post_password = ''
and post_type = 'post'
and post_status = 'publish'
order by rand()
limit 1";
$post_id = $wpdb->get_var($sql);
$permalink = get_permalink($post_id);
header("Location: " . $permalink, true, 302);
exit;
}
}
The result is a pretty serendipitous (and addictive) way to surf my archives.