From: Ross Bamford Date: 2006-05-01T01:13:39+09:00 Subject: Re: [QUIZ] cat2rafb (#77) --=-LXIu4kVBgsyR9WyVUdvE Content-Type: text/plain Content-Transfer-Encoding: 7bit My solution is attached (It's also at http://rubyurl.com/mgX but I know these messages are archived so I included it here). It's a pretty short thing and not the cleanest code but this was one of those 'just get it written' things for me :) Synopsis: cat2rafb.rb [-nNickname] [-dDesc] [-lLanguage] [-cConvtabs] [-r] [[-tText] | [FILES, ...]] Options: -n - Specify nickname to paste with (default: ENV['USER'] || 'unknown') -d - Description of paste (default: none) -l - Language for syntax highlighting (default: Ruby unless -R option is passed: then Plain Text) -c - Convert tabs to specified number of spaces (default: No) -r - Display only rubyurl for this paste (default: both rafb & rubyurl) -R - Rafb only - don't get a rubyurl for this paste -t - Specify text on commandline, rather than reading files. If no -t or filenames are supplied, STDIN is read. -t precludes reading of any file. To get a list of valid language and tab-conversion lengths, you can just pass the appropriate option with no value, e.g. ./cat2rafb.rb -l ./cat2rafb.rb -c Examples: ./cat2rafb.rb somefile.txt ./cat2rafb.rb -nMyNick -lC++ -R myfile.c ./cat2rafb.rb -n'Long Nick' -d'Ruby code' -t'print if ~/^\d+:/' -r ./cat2rafb.rb -c2 -lruby -nmetoo -d'From stdin' Thanks for another interesting quiz, a rafb paster seems like a good thing to have :) -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk --=-LXIu4kVBgsyR9WyVUdvE Content-Disposition: attachment; filename=cat2rafb.rb Content-Type: text/plain; name=cat2rafb.rb; charset=utf-8 Content-Transfer-Encoding: 7bit #!/usr/local/bin/ruby require 'net/http' require 'uri' LANG = %w{C89 C C++ C# Java Pascal Perl PHP PL/I Python Ruby Scheme SQL VB XML Plain\ Text} TABS = %w{No 2 3 4 5 6 8} RAFB, RURL = %w{http://rafb.net/paste/paste.php http://rubyurl.com/rubyurl/create}.map { |u| URI.parse(u) } rurlmode = !ARGV.delete('-R') and output_ofs = (ARGV.delete('-r') ? -1 : 0) (form = {'lang' => (rurlmode ? 'Ruby' : 'Plain Text'), 'nick' => ENV['USER'] || 'unknown', 'desc' => '', 'cvt_tabs' => 'No', 'text' => nil}).keys.each do |key| ARGV.reject! { |a| form[key] = $1 if a =~ /-#{key[0,1]}=?(.*)/ } end form['lang'] = LANG.detect {|l| l.casecmp(form['lang']) == 0} or raise "Unrecognized language (allowed values are #{LANG.inspect})" form['cvt_tabs'] = TABS.detect {|l| l.casecmp(form['cvt_tabs']) == 0} or raise "Unrecognized tab length (allowed values are #{TABS.inspect})" form['text'] ||= ARGF.read begin puts(((q = [[RAFB, form]]).map do |url,form| if (res = Net::HTTP.post_form(url,form)).is_a?(Net::HTTPRedirection) if (loc = res['location']) =~ /\/rubyurl\/show\/(.*)/ "http://rubyurl.com/#$1" else rfb = "http://rafb.net#{loc}" q << [RURL, {'rubyurl[website_url]' => rfb}] if rurlmode rfb end else res.error! end end)[(output_ofs || 0)..-1].join("\n")) rescue $!.set_backtrace([]) and raise end --=-LXIu4kVBgsyR9WyVUdvE--