microdata, Rails, and microdatajs
Published: 2011-06-11 23:02:27 -0400
With the recent release of schema.org and its use of microdata, I wanted to try to add some microdata to my pages. In development it would be nice to confirm what metadata can be extracted, but the tools I found were lacking.
There are few tools right now that I have found that will allow you to see the microdata. There’s the Rich Snippets Testing Tool from Google, but you have to enter a URL which does not work well in development before a site is even up on any public server. The other current problem with the Rich Snippets tool is that it has yet to be updated for some of the schema.org item types. While the microdata parsing will work and display properly, the possible rich snippets will not show up.
The mida gem allows for parsing microdata, but only works under Ruby 1.9, and my current project still relies on Ruby 1.8.7. It was easy enough with rvm to set up a separate gemset under Ruby 1.9 and do something like the follow:
nil insures that all of the items aren’t displayed again non-pretty
printed, so you only see the pretty printed version. The problem is the “pretty”
version isn’t so easy to read. It was also annoying to have to move back and
forth between my code editor and a terminal just to run this.
The Dive into HTML5 book presents some good links at the end of the chapter on microdata. One link is to Live Microdata which allows you to enter whole pages or snippets containing microdata and it will parse it into JSON and Turtle. You can download the source code for this and easily load up the live microdata page and enter snippets.
Within /app/views/layouts/_microdata.html.erb place the following:
The script does rely on jQuery to wait for the document ready, but that could be rewritten to work without it.
Now when you reload your page with embedded microdata you should see some pretty printed JSON at the bottom of the page with the microdata items from that page. I added prettify for JSON highlighting, but it doesn’t add much benefit so I left it out of the above.
Hopefully more and better tools will be coming to help develop microdata rich sites. If you know of a good tool or browser extension for developing sites with microdata, I would be very interested in hearing about them.