1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
require ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'
require 'erb'
require 'open-uri'
require 'net/http'
def entity_escape(text)
text.gsub(/&(?!([a-zA-Z0-9]+|#[0-9]+|#x[0-9a-fA-F]+);)/, '&')
end
def make_link(text)
case text
when %r{\A(mailto:)?(.*?@.*\..*)\z}:
"mailto:#{$2.gsub(/./) {sprintf("&#x%02X;", $&.unpack("U")[0])}}"
when %r{http://www.(amazon.(?:com|co.uk|co.jp|ca|fr|de))/.+?/([A-Z0-9]{10})/[-a-zA-Z0-9_./%?=&]+}:
"http://#{$1}/dp/#{$2}"
when %r{\A[a-zA-Z][a-zA-Z0-9.+-]*://.*\z}:
entity_escape(text)
when %r{\A(www\..*|.*\.(com|uk|net|org|info))\z}:
"http://#{entity_escape text}"
when %r{\A.*\.(com|uk|net|org|info)\z}:
"http://#{entity_escape text}"
when %r{\A\S+\z}:
entity_escape(text)
else
"http://some-site.com/"
end
end
url = make_link %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip
if url =~ /^http:\/\// and url != 'http://some-site.com/'
eval 'title = fp.read.match(/<title>([^<>]*)<\/title>/i).to_a[1].strip rescue nil' if fp = open(url) rescue nil
end
input = STDIN.read
print ERB.new(ENV['TM_LINK_FORMAT']).result
|