2 # TextMate related stuff.
9 class Textmate < PluginBase
11 def phrase_to_keywords(phrase)
12 phrase.gsub(/\b(\w+)s\b/, '\1').downcase.split(/\W/).to_set
17 text.grep(%r{<li>\s*([\d.]+)\s*<a href='(.*?)'>(.*?)</a>}) do |line|
19 :title => "#{$1} #{$3}",
20 :link => "http://macromates.com/textmate/manual/#{$2}",
21 :keywords => phrase_to_keywords($3)
27 def cmd_doc(irc, line)
29 irc.reply 'USAGE: doc <search string or regex>'
32 Net::HTTP.start('macromates.com') do |http|
33 re = http.get('/textmate/manual/', { 'User-Agent' => 'CyBrowser' })
36 search_keywords = phrase_to_keywords(line)
37 entries = parse_toc re.body
38 matches = entries.find_all { |m| search_keywords.subset? m[:keywords] }
40 irc.reply 'No matches found.'
42 ranked = matches.map { |m| m.merge({ :rank => search_keywords.length.to_f / m[:keywords].length }) }
43 hit = ranked.max { |a, b| a[:rank] <=> b[:rank] }
44 irc.respond "\x02#{hit[:title]}\x0f #{hit[:link]}"
48 irc.reply "Documentation site returned an error: #{re.code} #{re.message}"
54 help :doc, 'Searches the TextMate manual for the given string or regex.'
58 text.grep(%r{<a name=["'](.*?)["'][^>]*>.*?Keywords:(.*?)</span>}) do |line|
60 :link => 'http://macromates.com/wiki/Main/FAQ#' + $1,
61 :keywords => phrase_to_keywords($2)
67 def cmd_faq(irc, line)
69 irc.reply 'USAGE: faq <search keyword(s)>'
72 Net::HTTP.start('macromates.com') do |http|
73 re = http.get('/wiki/Main/FAQ', { 'User-Agent' => 'CyBrowser' })
76 search_keywords = phrase_to_keywords(line)
77 entries = parse_faq re.body
78 matches = entries.find_all { |m| search_keywords.subset? m[:keywords] }
80 irc.reply 'No matches found.'
82 ranked = matches.collect { |m| m.merge({ :rank => search_keywords.length.to_f / m[:keywords].length }) }
83 hit = ranked.max { |a, b| a[:rank] <=> b[:rank] }
84 irc.respond hit[:link]
92 help :faq, 'Searches the TextMate FAQ for the given keyword(s).'
94 def cmd_calc(irc, line)
96 irc.reply 'USAGE: calc <expression>. The expression must be in the bc language.'
100 IO.popen('bc -l 2>&1', 'r+') do |f|
102 result = f.gets.strip
108 help :calc, "Calculates the expression given and returns the answer. The expression must be in the bc language."