Dr Nic

Sentences with links

I have suggested I’d release 0.9 many times on Google Groups.

You see sentences like this in many many blogs. I have empathy for the authors who diligently markup each word with a different URL, but it is a wonderful way to describe and hyperlink in a meaningful way.

In the above fabricated, never-before-published quote, all the links are from a Google Groups search. So I knew I’d want to auto-generate it, because the mere thought of manual labour drives me to automation.

This resulted in the new sentence_with_links gem (I like bundling small chunks of code in new gems instead of stand alone library files so that the README file and test cases are encapsulated with the library code).


> gem install sentence_with_links


That is, String now has a with_links method, that generates the HTML as above. What if there are more links than words in the sentence? See the “Jessica Simpson” image search example below.

Mashup with Google Groups

For the above demonstration, let’s use our handy Hpricot parser to scrape the GoogleGroups search results, and .

Mashup with Google Images using JSON API

Demo: There are many Jessica Simpson images on Google
(also here, here, here, here, here, here, here, here, here, here, here, and here).

Note how the with_links function handles more links than sentence words? It appends a multitude of ‘here’ links. If you can think of more pleasurable alternatives, let me know. Either way, we don’t want to miss out on any important Jessica Simpson images. They are all there.

Instead of HTML, let’s parse some JSON returned by Google’s alter-ego search interface SearchMash, which provides a JSON API.

First, get the JSON Ruby parser:

gem install json

Then slap it all together…

And there you have it, sentences with links, the lazy Ruby way.

Related posts:

  1. Meta-Magic in Ruby: Dr Nic Unplugged in Stockholm Last nights’ Ruby meeting in Stockholm had a great turn...
  2. 53 cheat sheets and growing I said previously that errtheblog’s cheat app would have 100s+...
  3. [ANN] Dr Nic’s Magic Models 0.8 – Validate Anything, Anytime, Anywhere Ladies and Gentlemen, welcome one and all to the greatest...
  4. Exporting Netvibes bookmarks to Del.icio.us For 12 months I’ve been using Netvibes to store bookmarks....

6 Responses to “Sentences with links”

  1. Tim Lucas says:

    Turns out the comment spam bot doesn’t like posting the results of such frivolous fun. Bummer.

  2. Dr Nic says:

    @Tim – I found it amongst 600 spam :)

  3. While it’s amazing that Rails makes coding like this easy, I’d certainly follow Tim Lucas and never use it.

    Note there are so many usability problems with this approach when you don’t tell people where the links point.

    More info at Justaddwater.dk: Blog Usability: Linking.

    Rails is about teaching developers good habits, and with all respect for the ease and elegancy of your code, you are actually making it easy for Rails developers to jump through hoops and break usability rules.

    As this is available as a gem makes it even worse — it’s even easier to break basic usability rules.

    Just my comment for this, as I think that Rails will be better if we focus on making it easy to develop web apps that are developed fast, and follow certain conventions: Both the traditional rails conventions, but also the basic usability conventions :)

  4. Dr Nic says:

    @jesper – I don’t think the style of integrating multiple common links into a shared sentence is unique to Rails bloggers. I’m sure I’ve seen it all over the place. Perhaps adding “title” attributes to each link would allow the reader some idea where each link goes.

    I agree with Tim too, I might never use the code again. This blog is in WordPress and it was a bit of a pain copy+pasting the resulting string into the blog. Also, it was a relatively unique source – Google Groups search. Another example of a useful source to scrap from might be del.icio.us.

    I developed it as a gem because that to me is good Ruby lib development – the tests are paired with the libraries.

    I’ll certainly be amused by the first other person to actually use the gem :)

  5. Sergey says:

    This wonderful site, is worth dropping a line in your guestbook to say thanks!
    bad credit loan mortgage.