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
13 def search_tags file, tag
14 links = File.open( file )
16 YAML::load_documents( links ) { |doc| db << doc }
19 db.each { |l| matches << l if l['tags'].include? tag }
20 matches.each { |l| printf "%s\n %s\n", l['title'], l['link'] }
23 def add_link file, address, tags
25 doc = Hpricot(open(address))
26 title = doc.at("title").inner_html.strip
27 puts "Adding '#{title}' to #{file}..."
29 $stderr.puts "site could not be reached."
33 links = File.new( file, "a" )
35 links.puts "title: '#{title.gsub(/'/, '\'\'')}'"
36 links.puts "link: #{address}"
37 tags = tags ? tags.strip.gsub(/ /, ', ') : ''
38 links.puts "tags: [#{tags}]"
44 set_option o, '-s', nil
45 set_option o, '-f', 'Links.yaml'
46 set_option o, '-t', ''
47 set_option o, '-a', nil
49 if (!o['-s'] and !o['-a']) or (o['-s'] and o['-a'])
50 $stderr.puts "please specify either a tag to search for or a link to add."
54 search_tags o['-f'], o['-s'] if o['-s']
55 add_link o['-f'], o['-a'], o['-t'] if o['-a']