2 using System.Collections.Generic;
3 using System.Text.RegularExpressions;
5 namespace bytex64.WebThing {
6 public class SearchHandler {
8 Dictionary<string,ISearchPlugin> Handlers;
10 public SearchHandler(WebThing wt) {
11 Handlers = new Dictionary<string,ISearchPlugin>();
14 foreach (SearchHandlerPair p in Config.SearchHandlers) {
15 AddHandler(p.Shortcut, p.Plugin);
19 public void AddHandler(string key, string plugin) {
20 if (Handlers.ContainsKey(key)) {
21 Console.WriteLine("Cannot add search handler {0}: shortcut {1} already registered", plugin, key);
24 if (!wt.Plugins.Plugins.ContainsKey(plugin)) {
25 Console.WriteLine("Cannot add search handler {0}: Plugin for {0} not loaded", plugin);
29 WebThingPlugin p = wt.Plugins.Plugins[plugin];
30 Type ptype = p.GetType();
32 if (ptype.GetInterface("ISearchPlugin") != null) {
33 Handlers[key] = (ISearchPlugin) p;
34 Console.WriteLine("Added handler {0} for key {1}", plugin, key);
36 Console.WriteLine("Cannot add search handler {0}: {0} Does not implement ISearchPlugin", plugin);
40 // Call a search handler based on the leading word
41 public string Transform(string search) {
42 Regex get_handler_re = new Regex(@"^([\w-]+)\s+");
43 Match m = get_handler_re.Match(search);
45 string key = m.Groups[1].Value;
46 string query = get_handler_re.Replace(search, "");
47 if (!Handlers.ContainsKey(key)) {
48 Console.WriteLine("Could not search with {0}: No search handler defined", key);
51 return Handlers[key].SearchTransform(query);