Archive for May, 2008

Make Rails' Auto-linker Accept Parentheses

Wednesday, May 21st, 2008

The Rails helper, auto_link is a handy way to scan a block of text, adding HTML links as they are found. I recognized an issue with its regular expression, however, when dealing with files commonly linked to on VerticalWire.

The links in question look like this:

http://www.website.com/assets/screenshotbundle(5-20-08).zip
Unfortunately, Rails was creating links that looked something like this:
http://www.website.com/assets/screenshotbundle(5-20-08).zip
While parentheses aren't necessarily commonplace in URLs, they're still acceptable; Web browsers won't balk when they're encountered. It's clear that we need to patch our helper to accept parentheses.

Delving into the auto_link method in ActionView::Helpers::TextHelper, it's working off a rather large regular expression defined as the constant, AUTO_LINK_RE. To safely change this constant, you need to do so within the initializer block in environment.rb. If you try to define it by putting a file in the config/initializers directory, you'll get a warning that the constant has already been defined. Here's my updated definition:

Rails::Initializer.run do |config|
  module ActionView
    module Helpers
      module TextHelper
        AUTO_LINK_RE = %r{
          (                          # leading text
            <\w+.*?>|                # leading HTML tag, or
            [^=!:'"/]|               # leading punctuation, or
            |                        # nothing
          )
          (
            (?:https?://)|           # protocol spec, or
            (?:www\.)                # www.*
          )
          (
            [-\w]+                   # subdomain or domain
            (?:\.[-\w]+)*            # remaining subdomains or domain
            (?::\d+)?                # port
            (?:/(?:(?:[~\w\+@%=\(\)-]|(?:[,.;:][^\s$]))+)?)* # path
            (?:\?[\w\+@%&=.;-]+)?    # query string
            (?:\#[\w\-]*)?           # trailing anchor
          )
          ([[:punct:]]|\s|<|$)       # trailing text
        }x
      end
    end
  end
end

Update: I've created a ticket to correct this behavior.

Recruiters are hilarious

Thursday, May 8th, 2008
I just got a call from someone (Josh? Jason?) at a recruiter. It took him a few seconds to tell me he was, in fact, a recruiter, but once he did, I realized I didn't want to talk, told him, "No thanks. Bye." and hung up the phone. I could hear him keep talking on the other end as I pulled away my phone, but hung up none-the-less. Not two seconds after I put down my phone did he call back, and as such, went to my voicemail box. I couldn't wait to hear what the message might entail. It went something like this:
Hey, Adam. It's [what's-his-name] again. I'm going to send you a check in the mail for $1.55 for etiquette lessons, because it's obvious your mother never taught you that it's rude to hang up on someone.
Wah wah. Have fun at you crappy recruiting job, Jessie. Or maybe it was Jeff?