Skip to content

Instantly share code, notes, and snippets.

@spraints
Created June 24, 2009 14:57
Show Gist options
  • Save spraints/135314 to your computer and use it in GitHub Desktop.
Save spraints/135314 to your computer and use it in GitHub Desktop.
# Takes an OPML file from google reader and spits out a set of <li>s for
# each unique blog in the list, sorted by name, with the tags in ()s.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
def get_tag(opml_node)
if opml_node.nil?
nil
elsif opml_node['htmlUrl'].nil?
opml_node['title']
else
get_tag(opml_node.parent)
end
end
opml_file = ARGV[0]
relevant_tags = ARGV[1..-1]
blogs = {}
doc = Nokogiri::XML(File.read(opml_file))
doc.xpath('//outline').each do |entry|
name = entry['title']
url = entry['htmlUrl']
if url
blogs[url] ||= {:name => name, :tags => []}
blogs[url][:tags] << get_tag(entry)
end
end
unless relevant_tags.empty?
blogs.each_key do |k|
blogs[k][:tags].reject! { |tag| ! relevant_tags.include?(tag) }
end
blogs.delete_if { |key, value| value[:tags].empty? }
end
puts "<ul>"
blogs.keys.sort_by { |k| blogs[k][:name].downcase.sub(/^the /,'').gsub(/[^\w\d]/, '') }.each do |url|
blog = blogs[url]
s = " <li><a href=\"#{url}\">#{blog[:name]}</a>"
s << " (#{blog[:tags].join(', ')})" unless blog[:tags].empty?
s << "</li>"
puts s
end
puts "</ul>"
puts <<SUMMARY
<div>Count: #{blogs.size}</div>
<div>This list was produced by downloading <a href="http://www.google.com/reader/subscriptions/export">google reader's OPML listing of my subscriptions</a> and running this:<br/>
<code>ruby <a href="http://gist.github.com/135314">ompl_to_li.rb</a> #{ARGV.join(' ')}</code>
</div>
SUMMARY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment