[TxMt] hyperlink helper defunct, or I have a ruby error?
Timothy Bates
timothy.c.bates at gmail.com
Mon Apr 21 09:47:03 UTC 2008
hi all,
I found the "Hyperlink helper bundle" a very useful tool for creating
wikipedia and other links.
For some time now, however, running "Lookup Word / Selection on
Wikipedia and link" has lead to this error:
env: ruby\rrequire: No such file or directory
Has this function been rolled into another bundle or support dropped?
thanks for your help,
tim
FYI, the code is
#!/usr/bin/env ruby
require "#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb"
require "#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes.rb"
require "#{ENV['TM_SUPPORT_PATH']}/lib/ui.rb"
require "#{ENV['TM_SUPPORT_PATH']}/lib/escape"
require 'cgi'
require 'open-uri'
require 'erb'
def cap_all_words(string)
return string.split(" ").map{|w| w.capitalize}.join(" ")
end
def follow_redirect(page)
pagename = page.match(/var\swgPageName\s\=\s\"(.*?)
\";/).to_a[1].gsub(/\\'/,'%27')
url = %{http://en.wikipedia.org/wiki/#{pagename}}
content = open(url).read
return url, content, pagename
end #follow_redirect
def select_link(links)
linklist = links.map { |p| p[0] }
item = TextMate::UI.request_item :title => "Multiple
Definitions", :prompt => "Please select a definition", :items =>
linklist
TextMate.exit_discard if item.nil?
return links[linklist.index(item)]
end # select_link
def check_for_search(page)
TextMate.exit_show_tool_tip "No definition found" if page.match(/
<title>([^<>]*)<\/title>/i).to_a[1].include? "Search"
return page
end # check_for_search
def check_for_multiple_results(page,phrase,pagename)
if page.match(/(<\/b>\scan\sbe:<\/p>)|(<\/b>\smay\srefer\sto:<\/p>)/
i) then
open("http://en.wikipedia.org/wiki/Special:Export/#{pagename}") {|
body|
links = []
body.read.scan(/\[\[([^|\]]*)[|]?[^\]]*\]\]/) { |link|
links.push(link) }
choice = select_link(links)
TextMate.exit_discard if choice.nil?
TextMate.exit_replace_text(%{<a href="http://en.wikipedia.org/wiki/#
{CGI.escape(choice[0]).gsub('+','_')}" title="Wikipedia Entry:
#{choice[0]}">#{phrase}</a>})
}
end
end #check_for_multiple_results
def check_for_disambiguation(page,phrase,pagename)
# if page.match(/#{phrase}[\s]?\(disambiguation\)/i) then
if page.include? "(disambiguation)"
# findlink = page.match(/\"(\/wiki\/#{pagename}_%28disambiguation
%29)\"/i)
# open("http://en.wikipedia.org#{findlink[1]}") {|body|
open("http://en.wikipedia.org/wiki/Special:Export/#{pagename}_
%28disambiguation%29") {|body|
content = body.read
links = []
content.scan(/\[\[([^|\]]*)[|]?[^\]]*\]\]/) { |link|
links.push(link) }
choice = select_link(links)
TextMate.exit_discard if choice.nil?
TextMate.exit_replace_text(%{<a href="http://en.wikipedia.org/wiki/#
{CGI.escape(choice[0]).gsub('+','_')}" title="Wikipedia Entry:
#{choice[0]}">#{phrase}</a>})
}
end
end #check_for_disambiguation
def getWikiDef(phrase)
TextMate.call_with_progress(:title => "Contacting
Wikipedia", :message => "Looking for definition of #{phrase}") do
escapedUrl = "http://en.wikipedia.org/wiki/Special:Search?
search=#{CGI.escape(phrase).gsub(/\\'/,'%27')}"
@response = open( escapedUrl,
"User-Agent" => "TextMate 1.5.5")
content = @response.read
if content.scan(/Redirected\sfrom/)
url, content, pagename = follow_redirect(content)
# confirmed = TextMate::UI.request_confirmation :button1 =>
"Accept", :button2 => "Cancel", :title => "Redirected to \"#{pagename}
\"", :prompt => "Accept Redirection?"
# TextMate.exit_discard unless confirmed
else
url = @response.base_uri
pagename = phrase
end
check_for_search(content)
check_for_multiple_results(content,phrase,pagename)
check_for_disambiguation(content,phrase,pagename)
title = "Wikipedia Entry: " + pagename.gsub(/_/, ' ')
return url, title, phrase
end
end
url, title, input = getWikiDef(STDIN.read)
print ERB.new(ENV['TM_LINK_FORMAT']).result
More information about the textmate
mailing list