X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=SearchHandler.cs;fp=SearchHandler.cs;h=54d50092a6d85cafed45c7ba5b1393f9dba8e257;hb=11835c28c3407f9bbd4aa68b875a528d13ff0d73;hp=0000000000000000000000000000000000000000;hpb=b244412c1fbb99111bf820bb72f5b6146174405f;p=WebThing.git diff --git a/SearchHandler.cs b/SearchHandler.cs new file mode 100644 index 0000000..54d5009 --- /dev/null +++ b/SearchHandler.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace bytex64.WebThing { + public class SearchHandler { + WebThing wt; + Dictionary Handlers; + + public SearchHandler(WebThing wt) { + Handlers = new Dictionary(); + this.wt = wt; + + foreach (SearchHandlerPair p in Config.SearchHandlers) { + AddHandler(p.Shortcut, p.Plugin); + } + } + + public void AddHandler(string key, string plugin) { + if (Handlers.ContainsKey(key)) { + Console.WriteLine("Cannot add search handler {0}: shortcut {1} already registered", plugin, key); + return; + } + if (!wt.Plugins.Plugins.ContainsKey(plugin)) { + Console.WriteLine("Cannot add search handler {0}: Plugin for {0} not loaded", plugin); + return; + } + + WebThingPlugin p = wt.Plugins.Plugins[plugin]; + Type ptype = p.GetType(); + + if (ptype.GetInterface("ISearchPlugin") != null) { + Handlers[key] = (ISearchPlugin) p; + Console.WriteLine("Added handler {0} for key {1}", plugin, key); + } else { + Console.WriteLine("Cannot add search handler {0}: {0} Does not implement ISearchPlugin", plugin); + } + } + + // Call a search handler based on the leading word + public string Transform(string search) { + Regex get_handler_re = new Regex(@"^([\w-]+)\s+"); + Match m = get_handler_re.Match(search); + if (m.Success) { + string key = m.Groups[1].Value; + string query = get_handler_re.Replace(search, ""); + if (!Handlers.ContainsKey(key)) { + Console.WriteLine("Could not search with {0}: No search handler defined", key); + return null; + } + return Handlers[key].SearchTransform(query); + } else { + return null; + } + } + } +}