SmugMug

Posted by Jason Terk on Friday, December 25

Updated 2010-04-04 - The regex in the to_html methos was updated again to do an even better job of preserving whitespace.

Updated 2010-03-21 - The regex in the to_html method on the SmugMug class was updated to preserve whitespace.

Up until now to post photos from SmugMug I've crafted the link and image markup by hand, which is pretty tedious and prone to error. Today I ginned up an extension to Markdown (RDiscount, really) that allows automatic generation of the markup from the image title and URL. Here's how it works:

The first step is to create a new Hobix entry class (located in BLOG_ROOT/lib/local.rb):

module Hobix
  # Entries that use Markdown for formatting and automatically handle SmugMug
  # photos.
  class SmugMugEntry < Entry
    # Use SmugMug
    def self.text_processor
      SmugMug
    end

    # The YAML type for this entry class.
    def to_yaml_type
      "!goterkyourself.com,2009/SmugMugEntry"
    end

    # Register with the YAML engine.
    YAML::add_domain_type('goterkyourself.com,2009', 'SmugMugEntry') do |type, val|
      self.maker(val)
    end
  end
end

Now, any Hobix entry created with the type goterkyourself.com,2009/SmugMugEntry will use the SmugMug photo processor. All that remains now is to define the SmugMug class, which does the heavy lifting. This class is nested inside Hobix::SmugMugEntry:

# A SmugMug text processor that wraps Markdown with SmugMug specific
# capabilities.
#
# The syntax is:
#
#   $[Image Caption](Image URL)
#
#   i.e.: $[Bob Swimming](http://account.smugmug.com/Some/Category/1234567_abc12#123456789_AaBbC)
#
# Note that, for simplicity's sake, a SmugMug image blob must be on its
# own line
#
class SmugMug < RDiscount
  def to_html
    # Create a temporary string for the replacement so @text doesn't get
    # mangled.
    text = @text

    # Replace
    if text and not text.empty?
      text.gsub!(/^([ \t]*)\$\[(.*)\]\((.*)\)([ \t]*)$/) do |match|
        begin
          caption = $2
          image_url = $3
          image_id = image_url.split("#")[1]
          image_uri = URI.parse(image_url)

          raise "Missing image ID" if image_id.nil? or image_id.empty?

          "#{$1}" +
          "<a href=\"#{image_url}\">" +
            "<img src=\"http://#{image_uri.host}/photos/#{image_id}-640x640.jpg\" "+
                 "alt=\"#{caption}\" " +
                 "title=\"#{caption}\" " +
                 "class=\"photo\"/>" +
          "</a>" +
          "#{$4}"
        rescue
          # Just return the match on poorly formatted image blobs.
          $stderr.puts "Bad SmugMug blob: #{match}"
          match
        end
      end
    end

    m = RDiscount.new(text)
    m.to_html
  end
end

Once this is in place $[Amelie in her sheep hat!](http://gty.smugmug.com/Children/Amelie/7135981_oYtj9#705169136_SeMGC) becomes:

Amelie in her sheep hat!

0 comments

Droning

Posted by Jason Terk on Thursday, December 24

Bruce Schneier on the Predator drone unencrypted video snafu:

Sometimes mediocre encryption is better than strong encryption, and sometimes no encryption is better still.

Read through to the end; Schneier makes a good point about how the key management requirements of the NSAs Cold War era encryption schemes are limiting their use in today's military environment.

0 comments

Alma

Posted by Jason Terk on Wednesday, December 23

Alma, a wonderfully terrifying short film from Pixar (available for a limited time only...):

0 comments

I Say Fever

Posted by Jason Terk on Saturday, December 19

Ramona Falls, "I Say Fever":

0 comments

Airlock

Posted by Jason Terk on Thursday, December 10

Intriguing:

Airlock allows your Mac to lock itself, plain and simple. Using your iPhone or iPod Touch, Bluetooth, and a smidgen of pixie dust, Airlock determines whether you're near your computer.

0 comments

Sketchy Santas

Posted by Jason Terk on Monday, December 07

0 comments

Voodoo Sabotage

Posted by Jason Terk on Tuesday, December 01

Beastie Boys vs. The Prodigy by The Kleptones (music) and DJ Tripp (video).

0 comments