2 # uproar.rb - search a YAML-encoded list of links for matching tags
4 # see http://ardekantur.com/misc/uproar for information.
6 %w[ rubygems yaml open-uri hpricot ].each { |x| require x }
8 def set_option hash, name, default
10 hash[name] = i ? ARGV[i + 1] : default
15 def Uproar.search_tags file, tag
16 links = File.open( file )
18 YAML::load_documents( links ) { |doc| db << doc }
19 db.each { |l| printf "%s\n %s\n", l['title'], l['link'] if l['tags'].include? tag }
22 def Uproar.add_link file, address, tags
24 doc = Hpricot(open(address))
25 title = doc.at("title").inner_html.strip
26 puts "Adding '#{title}' to #{file}..."
28 $stderr.puts "site could not be reached."
32 link = { "title" => title, "link" => address, "tags" => ( tags ? tags.strip.gsub(/ /, ', ') : '' ) }
34 links = File.new( file, "a" )
35 links.puts link.to_yaml
43 set_option o, '-s', nil
44 set_option o, '-f', 'Links.yaml'
45 set_option o, '-t', ''
46 set_option o, '-a', nil
48 if (!o['-s'] and !o['-a']) or (o['-s'] and o['-a'])
49 $stderr.puts "please specify either a tag to search for or a link to add."
53 Uproar.search_tags o['-f'], o['-s'] if o['-s']
54 Uproar.add_link o['-f'], o['-a'], o['-t'] if o['-a']