• a ruby microformat parser -


Get Started Immediately

$ irb -rubygems 
>> require 'mofo'
=> true

>> fireball = HCard.find ''
=> #<HCard:0x6db898 ... >

>> fireball.nickname
=> "gruber"

>> fireball.url
=> ""

>> fireball.n.family_name
=> "Gruber"

>> fireball.title
=> "Raconteur"

>> fireball.adr.locality
=> "Philadelphia"

>> fireball.logo
=> "[email protected]?1117572751"

Grab It

$ git clone git://
$ open


Microformats are tiny little markup definitions built on top of, usually, 

You have a blog.  You have recent posts on your blog's index page.  You have
an Atom feed.  You have recent posts on your blog's Atom feed.  See where I'm
going with this?

The hAtom microformat (or uformat) can be embedded in your existing HTML by
setting CSS classes with semantic meaning inside of your posts.  A class to signify
a post is contained within this div, a class to signify the contents of this
h3 are the post's title, a class to signify the contents of this span is the
blog post's author, etc.

You can then use a microformat parser (like, say, mofo) to extract this information
as you would from an Atom feed.  Hell, you can even convert hAtom to Atom.  It's an
insta-feed!  No extra code required!

You're already doing the work, you see.  Microformats are everywhere.  We just need
to set them free.

Check it:

  <div class="post">
    <h3>Megadeth Show Last Night</h3>
    <span class="subtitle">Posted by Chris on June 4th</span>
    <div class="content">Went to a show last night.  Megadeth.  It was alright.</div>

Right?  Normal.  Here's the same post marked up with hAtom:

  <div class="post hentry">
    <h3 class="entry-title">Megadeth Show Last Night</h3>
    <span class="subtitle">Posted by <span class="author vcard fn">Chris</span> on 
    <abbr class="updated" title="2006-06-04T10:32:10Z">June 4th</abbr></span>
    <div class="content entry-content">Went to a show last night.  Megadeth.  It was alright.</div>

All I did was add the hentry, entry-title, and entry-content classes to existing containers.  Then I
went ahead and wrapped the date in an <abbr> tag giving it a title in the microformat-standard way.  Finally
I put a div around Chris signifying it as the author field of the hEntry and making it a valid hCard by
including the vcard and fn classes.  It's really not all that hard.  Did I mess it up?  Maybe, but I'm sure I got
close.  And I didn't even use a reference.  Practice.

How'd we parse this, tho?

  $ irb -rubygems
  >> require 'mofo'
  => true

  >> post = HEntry.find ''
  => #<HEntry:0x6db898 ... > 

  >> post.entry_title
  => "Megadeth Show Last Night"

  => ["entry_content", "updated", "author", "entry_title"]

  >> post.updated
  => Sun Jun 04 10:32:10 UTC 2006

  >> post.updated.class
  => Time

  => #<HCard:0x6e7b98 @properties=["fn"], @fn="Chris">

  => "Chris"

  >> post.entry_content
  => "Went to a show last night.  Megadeth.  It was alright."

That's, like, stupid easy.  If HEntry.find gets back more than one hEntry, you'll get an array.


Everything revolves around the #find method.  Sound familiar?  Yeah.

  >> Microformat.find ""
  >> Microformat.find "/path/to/existing/file"
  >> Microformat.find :text => "microformat text"

Also, #find can be told explicitly to find all (returning an array on failure) or only find
the first (returning nil on failure).

  >> Microformat.find :all => "/existing/file"
  => [ array of microformat objects ] 

  >> Microformat.find :first => "/existing/file"
  => microformat object

  >> Microformat.find "/existing/file"
  => either an array of objects or just one object

:all and :first go outside of :text.

  >> Microformat.find :all => { :text => 'mfin text' } 

That's it.  Some microformats take specific options.


Here are the currently implemented microformats, along with a site you
can use them on today.  We want more, better, faster, stat.

- hCard     [ flickr profiles    ]
- hCalendar [       ]
- hReview   [ cork'd reviews     ] 
- hEntry    [ err the blog posts ]
- hResume   [       ]
- xoxo      [      ]
- geo       [       ]
- adr       [       ]
- xfn       [       ]

- rel-tag 
- rel-bookmark
- include-pattern

Ruby on Rails

mofo doubles as a Rails plugin. Just drop it into vendor/plugins and you are good to go, with all the available microformat parsers loaded into your application.

mofo classes are YAML and Marshal approved. This means you can cache them with DRb or memcached, or store them in a session.

More Info

=> "The homepage, check"
=> "The wiki, check"
=> "Assaf Arkin knows his MFin' stuff"
=> "Drew McClellan, Microformat wizard"
=> "mofo HQ"

Other Parsers

>> Scrapi
>> uformats


>> Steve Ivy
>> Olle Jonsson
>> Christian Carter
>> Grant Rodgers
>> Denis Defreyne
>> Andrew Turner
>> Mark Murphy


>> Chris Wanstrath
=> chris[at]ozmm[dot]org