# Encoding: UTF-8

[{beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby\nrequire \"\#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb\"\nrequire \"\#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes.rb\"\nrequire 'open-uri'\nrequire 'erb'\n\n\ndef getWikiDef(input)\n  TextMate.call_with_progress(:title => \"Contacting Wikipedia\", :message => \"Looking for definition of \#{input}\") do\n  \tresponse = open(\"http://en.wikipedia.org/wiki/Special:Search?search=\#{input}\",\n  \t    \"User-Agent\" => \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1\",\n  \t    \"Referer\" => \"http://www.ruby-lang.org/\") {|f| $response = f }\n  \turl = response.base_uri\n  \t# borrowed from iamrice.org\n  \tfp = open(url)\n  \ttitle = \"Wikipedia Entry: \" + fp.read.match(/<title>([^<>]*)<\\/title>/i).to_a[1]\n  \tif title.include? \"Search\"\n  \t   TextMate.exit_show_tool_tip \"No definition found\"\n  \telse\n  \t   return title.sub(/ - Wikipedia, the free encyclopedia/, ''), url\n  \tend\n  end\nend\ninput = STDIN.read\ntitle, url = getWikiDef(input)\nprint ERB.new(ENV['TM_LINK_FORMAT']).result",
  fallbackInput: "word",
  input: "selection",
  name: "Basic Wikipedia",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "71C47F5F-41FA-40E8-A13A-238BC30984D1"},
 {beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby -wKU\n# an evolution of the code from skiadas.dcostanet.net/afterthought\n# retrieves link from Google automatically and creates XHTML link\n# improved call to progress bar, added ability to retrieve title\n# of page and create title attribute in link using idea\n# borrowed from iamrice.org.  Improved regex to handle title tags \n# newlines, spaces and capitalization differences\nrequire ENV['TM_SUPPORT_PATH'] + '/lib/progress.rb'\nrequire ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'\nrequire 'net/http' \nrequire 'open-uri'\nrequire 'erb'\n\ndef getLinkWithTitle(phrase)\n  TextMate.call_with_progress(:title => \"Contacting Google\", :message => \"Retrieving Link for “\#{phrase}”…\") do\n    response = Net::HTTP.get_response(URI.parse(URI.escape(\"http://www.google.com/search?q=\#{phrase}&btnI=I'm+Feeling+Lucky\")))\n    url = response.to_hash['location'].to_a.first\n    fp = open(url)\n    title = fp.read.match(/<title>([^<>]*)<\\/title>/i).to_a[1]\n    title = title.strip\n    return url, title, phrase\n  end\nend\n\nurl, title, input = getLinkWithTitle(STDIN.read)\nprint ERB.new(ENV['TM_LINK_FORMAT']).result",
  fallbackInput: "word",
  input: "selection",
  keyEquivalent: "^@L",
  name: "Lookup Word / Selection on Google and Link",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "D2F3BE59-1349-4762-9B17-A6F79A7F81B9"},
 {beforeRunningCommand: "nop",
  bundleUUID: "1BF76350-AB8F-4FC8-9512-1129CEEE0A3E",
  command: 
   "#!/usr/bin/env ruby\nrequire \"\#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb\"\nrequire \"\#{ENV['TM_SUPPORT_PATH']}/lib/exit_codes.rb\"\nrequire \"\#{ENV['TM_SUPPORT_PATH']}/lib/ui.rb\"\nrequire 'cgi'\nrequire 'open-uri'\nrequire 'erb'\n\ndef cap_all_words(string)\n\treturn string.split(\" \").map{|w| w.capitalize}.join(\" \")\nend\n\ndef follow_redirect(page)\n  pagename = page.match(/var\\swgPageName\\s\\=\\s\\\"(.*?)\\\";/).to_a[1]\n  url = %{http://en.wikipedia.org/wiki/\#{pagename}}\n  content = open(url).read\n  return url, content, pagename\nend #follow_redirect\n\ndef select_link(links)\n    linklist = links.map { |p| p[0] }\n    item = TextMate::UI.request_item :title => \"Multiple Definitions\", :prompt => \"Please select a definition\", :items => linklist\n    TextMate.exit_discard if item.nil?\n    return links[linklist.index(item)]\nend # select_link\n\ndef check_for_search(page)\n  TextMate.exit_show_tool_tip \"No definition found\" if page.match(/<title>([^<>]*)<\\/title>/i).to_a[1].include? \"Search\"\n  return page\nend # check_for_search\n\ndef check_for_multiple_results(page,phrase,pagename)\n  if page.match(/(<\\/b>\\scan\\sbe:<\\/p>)|(<\\/b>\\smay\\srefer\\sto:<\\/p>)/i) then\n    open(\"http://en.wikipedia.org/wiki/Special:Export/\#{pagename}\") {|body|\n\t      links = []\n\t      body.read.scan(/\\[\\[([^|\\]]*)[|]?[^\\]]*\\]\\]/) { |link| \n        links.push(link) }\n       choice = select_link(links)\n       TextMate.exit_discard if choice.nil?\n       TextMate.exit_replace_text(%{<a href=\"http://en.wikipedia.org/wiki/\#{CGI.escape(choice[0]).gsub('+','_')}\" title=\"Wikipedia Entry: \#{choice[0]}\">\#{phrase}</a>})        \n        }\n  end\nend #check_for_multiple_results\n\ndef check_for_disambiguation(page,phrase,pagename)\n#  if page.match(/\#{phrase}[\\s]?\\(disambiguation\\)/i) then\n  if page.include? \"(disambiguation)\"\n\t    # findlink = page.match(/\\\"(\\/wiki\\/\#{pagename}_%28disambiguation%29)\\\"/i) \n\t    # open(\"http://en.wikipedia.org\#{findlink[1]}\") {|body|\n\t\topen(\"http://en.wikipedia.org/wiki/Special:Export/\#{pagename}_%28disambiguation%29\") {|body|\n\t      content = body.read\n\t      links = []\n\t      content.scan(/\\[\\[([^|\\]]*)[|]?[^\\]]*\\]\\]/) { |link| \n           links.push(link) }\n       choice = select_link(links)\n       TextMate.exit_discard if choice.nil?\n       TextMate.exit_replace_text(%{<a href=\"http://en.wikipedia.org/wiki/\#{CGI.escape(choice[0]).gsub('+','_')}\" title=\"Wikipedia Entry: \#{choice[0]}\">\#{phrase}</a>})        \n        }\n\tend\nend #check_for_disambiguation\n\ndef getWikiDef(phrase)\n  TextMate.call_with_progress(:title => \"Contacting Wikipedia\", :message => \"Looking for definition of \#{phrase}\") do\n    @response = open(\"http://en.wikipedia.org/wiki/Special:Search?search=\#{CGI.escape(phrase)}\",\n        \"User-Agent\" => \"TextMate 1.5.5\")\n    content = @response.read\n    if content.scan(/Redirected\\sfrom/)\n      url, content, pagename = follow_redirect(content)\n  #    confirmed = TextMate::UI.request_confirmation :button1 => \"Accept\", :button2 => \"Cancel\", :title => \"Redirected to \\\"\#{pagename}\\\"\", :prompt => \"Accept Redirection?\"\n  #    TextMate.exit_discard unless confirmed\n    else\n      url = @response.base_uri\n      pagename = phrase\n    end\n    check_for_search(content)\n    check_for_multiple_results(content,phrase,pagename)\n    check_for_disambiguation(content,phrase,pagename)\n    title = \"Wikipedia Entry: \" + pagename.gsub(/_/, ' ')\n    return url, title, phrase\n  end\nend\n\nurl, title, input = getWikiDef(STDIN.read)\nprint ERB.new(ENV['TM_LINK_FORMAT']).result\n",
  fallbackInput: "word",
  input: "selection",
  name: "Lookup Word / Selection on Wikipedia and link",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "B94A2DF9-FC40-4281-80FB-35FB2917CDE7"},
 {beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby -wKU\nrequire ENV['TM_SUPPORT_PATH'] + '/lib/escape.rb'\nrequire 'erb'\nrequire 'open-uri'\nrequire 'net/http' \n\ndef entity_escape(text)\n  text.gsub(/&(?!([a-zA-Z0-9]+|#[0-9]+|#x[0-9a-fA-F]+);)/, '&amp;')\nend\n\ndef make_link(text)\n  case text\n  when %r{\\A(mailto:)?(.*?@.*\\..*)\\z}:\n    \"mailto:\#{$2.gsub(/./) {sprintf(\"&#x%02X;\", $&.unpack(\"U\")[0])}}\"\n  when %r{http://www.(amazon.(?:com|co.uk|co.jp|ca|fr|de))/.+?/([A-Z0-9]{10})/[-a-zA-Z0-9_./%?=&]+}:\n    \"http://\#{$1}/dp/\#{$2}\"\n  when %r{\\A[a-zA-Z][a-zA-Z0-9.+-]*://.*\\z}:\n    entity_escape(text)\n  when %r{\\A(www\\..*|.*\\.(com|uk|net|org|info))\\z}:\n    \"http://\#{entity_escape text}\"\n  when %r{\\A.*\\.(com|uk|net|org|info)\\z}:\n    \"http://\#{entity_escape text}\"\n  when %r{\\A\\S+\\z}:\n    entity_escape(text)\n  else\n    \"http://example.com/\"\n  end\nend\n\nurl = make_link %x{__CF_USER_TEXT_ENCODING=$UID:0x8000100:0x8000100 pbpaste}.strip\n\nif url =~ /^http:\\/\\// and url != 'http://example.com/'\n  eval 'title = fp.read.match(/<title>([^<>]*)<\\/title>/i).to_a[1].strip rescue nil' if fp = open(url) rescue nil\nend\n\ninput = STDIN.read\nprint ERB.new(ENV['TM_LINK_FORMAT']).result\n",
  fallbackInput: "word",
  input: "selection",
  keyEquivalent: "^L",
  name: "Wrap Word / Selection as Link",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "A42DC78E-6588-476F-8A00-40B8CCC6D2E0"},
 {beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby -rjcode -Ku\nSUPPORT = ENV['TM_SUPPORT_PATH']\nDIALOG = ENV['DIALOG']\nrequire SUPPORT + '/lib/escape'\nrequire SUPPORT + '/lib/osx/plist'\nrequire SUPPORT + '/lib/exit_codes'\nrequire \"\#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo\"\nrequire 'erb'\n\ninput = STDIN.read\nquery = e_url(input)\nyahoo = NewsSearch.new('TM_YAHOO', query, 'all', 10, 1, nil, 1)\nysuggest = []\n    yahoo.parse_results.each {|result| \n        ysuggest << { \n          'title' => result['Title'].gsub('\"','&raquo;'),\n          'url' => result['Url']}\n    }\n    plist = { 'menuItems' => ysuggest }.to_plist\n\n    res = OSX::PropertyList.load(`\#{e_sh DIALOG} -up \#{e_sh plist}`)\n    TextMate.exit_show_tool_tip \"No matches found\" unless res.has_key? 'selectedMenuItem'\n    url = res['selectedMenuItem']['url']\n    title = res['selectedMenuItem']['title']\n\n    print ERB.new(ENV['TM_LINK_FORMAT']).result",
  fallbackInput: "word",
  input: "selection",
  keyEquivalent: "^Y",
  name: "Yahoo News Search",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "41D61EE6-B8A9-4C21-9ECE-E336588423DB"},
 {beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby -rjcode -Ku\nSUPPORT = ENV['TM_SUPPORT_PATH']\nDIALOG = ENV['DIALOG']\nrequire SUPPORT + '/lib/escape'\nrequire SUPPORT + '/lib/osx/plist'\nrequire SUPPORT + '/lib/exit_codes'\nrequire \"\#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo\"\n\ninput = STDIN.read\nquery = e_url(input)\nyahoo = RelatedSuggestion.new('TM_YAHOO', query, 'all', 10, 1, nil, 1)\nysuggest = []\n    yahoo.parse_analysis.each {|result| \n        ysuggest << { \n          'title' => result}\n    }\n    plist = { 'menuItems' => ysuggest }.to_plist\n\n    res = OSX::PropertyList.load(`\#{e_sh DIALOG} -up \#{e_sh plist}`)\n    abort unless res.has_key? 'selectedMenuItem'\n    TextMate.exit_replace_text res['selectedMenuItem']['title']",
  fallbackInput: "word",
  input: "selection",
  keyEquivalent: "^Y",
  name: "Yahoo Related Suggestion",
  output: "replaceSelectedText",
  scope: "text",
  uuid: "6511380B-D4C7-408B-85FF-9E98C6AAEB4A"},
 {beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby -rjcode -Ku\nSUPPORT = ENV['TM_SUPPORT_PATH']\nDIALOG = ENV['DIALOG']\nrequire SUPPORT + '/lib/escape'\nrequire SUPPORT + '/lib/osx/plist'\nrequire SUPPORT + '/lib/exit_codes'\nrequire \"\#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo\"\nrequire 'erb'\n\ninput = STDIN.read\nquery = e_url(input)\nyahoo = VideoSearch.new('TM_YAHOO', query, 'all', 10, 1, nil, 1)\nysuggest = []\n    yahoo.parse_results.each {|result| \n        ysuggest << { \n          'title' => result['Title'].gsub('\"','&raquo;'),\n          'url' => result['Url']}\n    }\n    plist = { 'menuItems' => ysuggest }.to_plist\n\n    res = OSX::PropertyList.load(`\#{e_sh DIALOG} -up \#{e_sh plist}`)\n    TextMate.exit_show_tool_tip \"No matches found\" unless res.has_key? 'selectedMenuItem'\n    url = res['selectedMenuItem']['url']\n    title = res['selectedMenuItem']['title']\n\n    print ERB.new(ENV['TM_LINK_FORMAT']).result",
  fallbackInput: "word",
  input: "selection",
  keyEquivalent: "^Y",
  name: "Yahoo Video Search",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "50A08D59-18D6-48C1-938D-6644906F5C83"},
 {beforeRunningCommand: "nop",
  command: 
   "#!/usr/bin/env ruby -rjcode -Ku\nSUPPORT = ENV['TM_SUPPORT_PATH']\nDIALOG = ENV['DIALOG']\nrequire SUPPORT + '/lib/escape'\nrequire SUPPORT + '/lib/osx/plist'\nrequire SUPPORT + '/lib/exit_codes'\nrequire \"\#{ENV['TM_BUNDLE_SUPPORT']}/lib/yahoo\"\nrequire 'erb'\n\ninput = STDIN.read\nquery = e_url(input)\nyahoo = WebSearch.new('TM_YAHOO', query, 'all', 10, 1, nil, 1)\nysuggest = []\n    yahoo.parse_results.each {|result| \n        ysuggest << { \n          'title' => result['Title'].gsub('\"','&raquo;'),\n          'url' => result['Url']}\n    }\n    plist = { 'menuItems' => ysuggest }.to_plist\n\n    res = OSX::PropertyList.load(`\#{e_sh DIALOG} -up \#{e_sh plist}`)\n    TextMate.exit_show_tool_tip \"No matches found\" unless res.has_key? 'selectedMenuItem'\n    url = res['selectedMenuItem']['url']\n    title = res['selectedMenuItem']['title']\n\n    print ERB.new(ENV['TM_LINK_FORMAT']).result",
  fallbackInput: "word",
  input: "selection",
  keyEquivalent: "^Y",
  name: "Yahoo Web Search",
  output: "insertAsSnippet",
  scope: "text",
  uuid: "2E03E8C8-DC99-4D15-AE46-0E8DCE6BBE6F"}]
