]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/dictclient.rb
grouphug plugin: help text simplification
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / dictclient.rb
index 2cd782cefae88cc232a6b5a719354fbd0c3bc170..903b3f1ea50299a27b1b6e15d97cf69265fcc3fc 100644 (file)
@@ -42,30 +42,35 @@ class ::Definition
   end
 
   def body
-    definition[1..-1].join.gsub(/\s+/, ' ').strip
+    # two or more consecutive newlines are replaced with double spaces, while single
+    # newlines are replaced with single spaces
+    lb = /\r?\n/
+    definition[1..-1].join.
+      gsub(/\s*(:#{lb}){2,}\s*/, '  ').
+      gsub(/\s*#{lb}\s*/, ' ').strip
   end
 end
 
 class DictClientPlugin < Plugin
-  BotConfig.register BotConfigStringValue.new('dictclient.server',
+  Config.register Config::StringValue.new('dictclient.server',
     :default => 'dict.org',
     :desc => _('Hostname or hostname:port of the DICT server used to lookup words'))
-  BotConfig.register BotConfigIntegerValue.new('dictclient.max_defs_before_collapse',
+  Config.register Config::IntegerValue.new('dictclient.max_defs_before_collapse',
     :default => 4,
     :desc => _('When multiple databases reply a number of definitions that above this limit, only the database names will be listed. Otherwise, the full definitions from each database are replied'))
-  BotConfig.register BotConfigIntegerValue.new('dictclient.max_length_per_def',
+  Config.register Config::IntegerValue.new('dictclient.max_length_per_def',
     :default => 200,
     :desc => _('Each definition is truncated to this length'))
-  BotConfig.register BotConfigStringValue.new('dictclient.headword_format',
+  Config.register Config::StringValue.new('dictclient.headword_format',
     :default => "#{Bold}<headword>#{Bold}",
     :desc => _('Format of headwords; <word> will be replaced with the actual word'))
-  BotConfig.register BotConfigStringValue.new('dictclient.database_format',
+  Config.register Config::StringValue.new('dictclient.database_format',
     :default => "#{Underline}<database>#{Underline}",
     :desc => _('Format of database names; <database> will be replaced with the database name'))
-  BotConfig.register BotConfigStringValue.new('dictclient.definition_format',
+  Config.register Config::StringValue.new('dictclient.definition_format',
     :default => '<headword>: <definition> -<database>',
     :desc => _('Format of definitions. <word> will be replaced with the formatted headword, <def> will be replaced with the truncated definition, and <database> with the formatted database name'))
-  BotConfig.register BotConfigStringValue.new('dictclient.match_format',
+  Config.register Config::StringValue.new('dictclient.match_format',
     :default => '<matches>––<database>',
     :desc => _('Format of match results. <matches> will be replaced with the formatted headwords, <database> with the formatted database name'))
   
@@ -127,7 +132,7 @@ class DictClientPlugin < Plugin
             ).gsub(
               '<definition>', r.body.truncate(@bot.config['dictclient.max_length_per_def'])
             )
-          }.join ' '
+          }.join ' '
         end
       else
         _("No definition for %{phrase} found from %{database}.") % 
@@ -162,7 +167,7 @@ class DictClientPlugin < Plugin
   def cmd_databases(m, params)
     with_dict(m) do |d|
       m.reply _("Databases: %{list}") % {
-        :list => d.show_db.collect {|db, des| "#{format_database db}: #{des}"}.join('; ')
+        :list => d.show_db.collect {|db, des| "#{format_database db}: #{des}"}.join(' | ')
       }
     end
   end
@@ -170,13 +175,22 @@ class DictClientPlugin < Plugin
   def cmd_strategies(m, params)
     with_dict(m) do |d|
       m.reply _("Strategies: %{list}") % {
-        :list => d.show_strat.collect {|s, des| "#{s}: #{des}"}.join('; ')
+        :list => d.show_strat.collect {|s, des| "#{s}: #{des}"}.join(' | ')
       }
     end
   end
     
   def help(plugin, topic='')
-    _("define <phrase> [from <database>] => Show definition of a phrase; match <phrase> [using <strategy>] [from <database>] => Show matching phrases; dictclient databases => List databases; dictclient strategies => List strategies")
+    case topic
+    when 'define'
+      _('define <phrase> [from <database>] => Show definition of a phrase')
+    when 'match'
+      _('match <phrase> [using <strategy>] [from <database>] => Show phrases matching the given pattern')
+    when 'server information'
+      _('dictclient databases => List databases; dictclient strategies => List strategies')
+    else
+      _('look up phrases on the configured DICT server. topics: define, match, server information')
+    end
   end
 end
 
@@ -184,12 +198,14 @@ plugin = DictClientPlugin.new
 
 plugin.map 'define *phrase [from :database]',
            :action => 'cmd_define',
-           :defaults => {:database => DICT::ALL_DATABASES}
+           :defaults => {:database => DICT::ALL_DATABASES},
+           :threaded => true
 
 plugin.map 'match *phrase [using :strategy] [from :database]',
            :action => 'cmd_match',
            :defaults => {:database => DICT::ALL_DATABASES,
-                         :strategy => DICT::DEFAULT_MATCH_STRATEGY }
+                         :strategy => DICT::DEFAULT_MATCH_STRATEGY },
+           :threaded => true
 
-plugin.map 'dictclient databases', :action => 'cmd_databases'
-plugin.map 'dictclient strategies', :action => 'cmd_strategies'
+plugin.map 'dictclient databases', :action => 'cmd_databases', :thread => true
+plugin.map 'dictclient strategies', :action => 'cmd_strategies', :thread => true