Probably use net::http to grab the page, and use the response codes to fail gracefully. Otherwise test for a valid response using curl before calling open?
Hi everybody.
As some of you may have figured out, I work a lot in HTML and CSS and I have a fetish for descriptive titles on links. I've been rewriting the wrap selection as link command and have it all worked out to grab the title of the link in the clipboard if it starts with http(s). I can't get it to validate the link, though.
Here's the text of the command:
#!/usr/bin/env ruby
require "#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb"
require 'open-uri'
title = #{ENV['TM_SELECTED_TEXT']}
s = IO.popen('pbpaste', 'r+').read
if s.match(/(http(s?):\/\/)/) then
TextMate.call_with_progress(:title => "Opening Link", :message => "Retrieving Title for #{s}") do
begin
fp = open(s)
title = fp.read.match(/<title>([^<>]*)<\/title>/i).to_a[1].strip
link = s
rescue
title = "site unreachable"
end
end
end
print %{<a href="${1:#{link}}" title="${2:#{title}}" >$TM_SELECTED_TEXT</a>}
I've tried all kinds of overly complex methods that all yield the same results when the link is bogus:
/usr/lib/ruby/1.8/timeout.rb:42:in /bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 3: syntax error: unexpected end of fileconnect'
from /usr/lib/ruby/1.8/net/protocol.rb:64:in /bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 3: syntax error: unexpected end of fileopen'
from /usr/lib/ruby/1.8/net/http.rb:430:in /bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 3: syntax error: unexpected end of filestart'
from /usr/lib/ruby/1.8/net/http.rb:324:in /bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 3: syntax error: unexpected end of fileproxy_open'
from /usr/lib/ruby/1.8/open-uri.rb:525:in /bin/bash: -c: line 1: unexpected EOF while looking for matching `''
/bin/bash: -c: line 5: syntax error: unexpected end of filepopen'
from /Users/brett/Library/Application Support/TextMate/Support/lib/progress.rb:11:in
This is what I get (and should get, I think) when I run it from a Ruby dialog:
<a href="${1:
http://site.com}" title="${2:site unreachable}" >$TM_SELECTED_TEXT</a>
It works great when the site in the clipboard is valid. Any suggestions would be appreciated.
Thanks,
Brett
Brett Terpstra : Art Director
Circle Six Design, Inc.
111 Riverfront Dr, Suite 204
______________________________________________________________________
(threading gets destroyed and the universe will collapse if you don't)