hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Parsing Ta-da Lists #

by why in bits

Ruby is becoming the language for conversion of idea-to-task, for recording and processing notes. We’re all sitting here watching 37signals and DHH pour out streams of Rails applications for organizing your thoughts.

Tobias Luetke demostrates how simple it is to parse out your Ta-da lists with Ruby’s built-in XML reader:

 xml = Document.new(body)
 self.items = []
 XPath.each(xml, "//item/") do |elem| 
   task = Task.new
   task.title = XPath.match(elem, "title/text()").to_s
   task.date  = XPath.match(elem, "pubDate/text()").to_s
   task.link  = XPath.match(elem, "link/text()").to_s
   items << task
 end

See the whole script on his site.

Update: I’m just going to add an alternate way of performing the above, since there’s so many ways around REXML. It’s like spelunking. Here’s another tunnel you can take.

 xml.each_element("//item/") do |elem|
   task = Task.new
   task.title = elem.text("title")
   task.date  = elem.text("pubDate")
   task.link  = elem.text("link")
   items << task
 end

Update #2: A second script has surfaced on del.icio.us. This one parses the HTML of the public Todo list. More fun XPath (just an excerpt.)

 TADA_XPATH = '//*[contains(@id,"%s_items")]//*[@class="itemtext"]'

 xml = Document.new(html)
 XPath.each(xml, TADA_XPATH % filter_by) do |elem|
   kind = XPath.match(elem, 'ancestor::table').
            first.attribute('id').to_s
   task = "* #{elem.text}" 
   task << ' (completed)' if kind =~ /^completed/
   tasks << task
 end

Comments are closed for this entry.