public void HtmlHelp(ICommandInterpreter _ci) { CommandInterpreter ci = ((CommandInterpreter)_ci); HtmlLightDocument doc = new HtmlLightDocument(ci.GetHtmlHelp("help")); XmlLightElement e = doc.SelectRequiredNode("/html/body/h1[2]"); XmlLightElement body = e.Parent; int i = body.Children.IndexOf(e); body.Children.RemoveRange(i, body.Children.Count - i); StringWriter sw = new StringWriter(); // Command index sw.WriteLine("<html><body>"); sw.WriteLine("<h1>All Commands:</h1>"); sw.WriteLine("<blockquote><ul>"); ILookup <string, ICommand> categories = ci.Commands.Where(c => c.Visible).ToLookup(c => c.Category ?? "Unk"); foreach (IGrouping <string, ICommand> group in categories.OrderBy(g => g.Key)) { sw.WriteLine("<li><a href=\"#{0}\">{0}</a></li>", group.Key); sw.WriteLine("<ul>"); foreach (ICommand cmd in group) { sw.WriteLine("<li><a href=\"#{0}\">{0}</a> - {1}</li>", cmd.DisplayName, HttpUtility.HtmlEncode(cmd.Description)); } sw.WriteLine("</ul>"); } sw.WriteLine("</ul></blockquote>"); // Command Help foreach (IGrouping <string, ICommand> group in categories.OrderBy(g => g.Key)) { sw.WriteLine("<h2><a name=\"{0}\"></a>{0} Commands:</h2>", group.Key); sw.WriteLine("<blockquote>"); foreach (ICommand cmd in group) { e = new HtmlLightDocument(ci.GetHtmlHelp(cmd.DisplayName)).SelectRequiredNode("/html/body/h3"); sw.WriteLine("<a name=\"{0}\"></a>", cmd.DisplayName); sw.WriteLine(e.InnerXml); sw.WriteLine(e.NextSibling.NextSibling.InnerXml); } sw.WriteLine("</blockquote>"); } e = new HtmlLightDocument(sw.ToString()).SelectRequiredNode("/html/body"); body.Children.AddRange(e.Children); string html = body.Parent.InnerXml; string path = Path.Combine(Path.GetTempPath(), "HttpClone.Help.html"); File.WriteAllText(path, html); System.Diagnostics.Process.Start(path); }
public void HtmlHelp(ICommandInterpreter _ci) { CommandInterpreter ci = ((CommandInterpreter)_ci); HtmlLightDocument doc = new HtmlLightDocument(ci.GetHtmlHelp("help")); XmlLightElement e = doc.SelectRequiredNode("/html/body/h1[2]"); XmlLightElement body = e.Parent; int i = body.Children.IndexOf(e); body.Children.RemoveRange(i, body.Children.Count - i); StringWriter sw = new StringWriter(); // Command index sw.WriteLine("<html><body>"); sw.WriteLine("<h1>All Commands:</h1>"); sw.WriteLine("<blockquote><ul>"); ILookup<string, ICommand> categories = ci.Commands.Where(c => c.Visible).ToLookup(c => c.Category ?? "Unk"); foreach (IGrouping<string, ICommand> group in categories.OrderBy(g => g.Key)) { sw.WriteLine("<li><a href=\"#{0}\">{0}</a></li>", group.Key); sw.WriteLine("<ul>"); foreach (ICommand cmd in group) sw.WriteLine("<li><a href=\"#{0}\">{0}</a> - {1}</li>", cmd.DisplayName, HttpUtility.HtmlEncode(cmd.Description)); sw.WriteLine("</ul>"); } sw.WriteLine("</ul></blockquote>"); // Command Help foreach (IGrouping<string, ICommand> group in categories.OrderBy(g => g.Key)) { sw.WriteLine("<h2><a name=\"{0}\"></a>{0} Commands:</h2>", group.Key); sw.WriteLine("<blockquote>"); foreach (ICommand cmd in group) { e = new HtmlLightDocument(ci.GetHtmlHelp(cmd.DisplayName)).SelectRequiredNode("/html/body/h3"); sw.WriteLine("<a name=\"{0}\"></a>", cmd.DisplayName); sw.WriteLine(e.InnerXml); sw.WriteLine(e.NextSibling.NextSibling.InnerXml); } sw.WriteLine("</blockquote>"); } e = new HtmlLightDocument(sw.ToString()).SelectRequiredNode("/html/body"); body.Children.AddRange(e.Children); string html = body.Parent.InnerXml; string path = Path.Combine(Path.GetTempPath(), "HttpClone.Help.html"); File.WriteAllText(path, html); System.Diagnostics.Process.Start(path); }
private string CreateTemplate(string html) { HtmlLightDocument doc = new HtmlLightDocument(html); //Add css link: XmlLightElement cssLink = new XmlLightElement(doc.SelectRequiredNode("/html/head"), "link"); cssLink.Attributes["type"] = "text/css"; cssLink.Attributes["rel"] = "stylesheet"; cssLink.Attributes["href"] = new Uri(_baseUri, "search.css").AbsoluteUri; XmlLightElement startFrom = doc.Root; if (_config.Searching.XPathBase != null) { startFrom = startFrom.SelectRequiredNode(_config.Searching.XPathBase.XPath); } if (_config.Searching.FormXPath != null) { XmlLightElement form = startFrom.SelectRequiredNode(_config.Searching.FormXPath.XPath); foreach (XmlLightElement textbox in form.Select(".//input[@type='text']")) { textbox.Attributes["value"] = String.Empty; } } if (_config.Searching.TermsXPath != null) { InsertTag(startFrom, _config.Searching.TermsXPath.XPath, _config.Searching.TermsXPath.ReplaceOption, "search-terms"); } if (_config.Searching.ResultXPath != null) { InsertTag(startFrom, _config.Searching.ResultXPath.XPath, _config.Searching.ResultXPath.ReplaceOption, "search-result"); } using (StringWriter sw = new StringWriter()) { doc.WriteUnformatted(sw); return(sw.ToString()); } }
public void BuildIndex() { if(_config == null) throw new InvalidOperationException("The <search> element is missing from the configuration."); Dictionary<string, string> hashes = new Dictionary<string, string>(StringComparer.Ordinal); foreach (KeyValuePair<string, ContentRecord> item in _content) { if (item.Key == SearchTemplate.SearchPath || item.Key == SearchTemplate.TemplatePath || item.Key == _config.TemplateUri) continue; if (item.Value.HasContentStoreId == false) continue; if (!_mimeInfo[item.Value.MimeType].Indexed || _mimeInfo[item.Value.MimeType].Type != ContentFormat.Html) continue; if (item.Value.HasHashContents) { if (hashes.ContainsKey(item.Value.HashContents)) continue; hashes.Add(item.Value.HashContents, item.Key); } string title = null, blurb = null, date = null; string content = Encoding.UTF8.GetString(_content.ReadContent(item.Value, true)); HtmlLightDocument xdoc = new HtmlLightDocument(content); XmlLightElement found, selectFrom = _config.XPathBase == null ? xdoc.Root : xdoc.SelectRequiredNode(_config.XPathBase.XPath); bool ignore = false; foreach(var xpath in _config.Conditions.SafeEnumeration()) { if(null != selectFrom.SelectSingleNode(xpath.XPath)) { ignore = true; break; } } if (ignore) continue; if (_config.TitlePath != null && selectFrom.TrySelectNode(_config.TitlePath.XPath, out found)) title = found.InnerText.Trim(); else if (_config.TitlePath == null && false == _mimeInfo.TryGetTitle(item.Value.MimeType, content, out title)) title = null; if (String.IsNullOrEmpty(title)) continue; if (_config.BlubXPath != null) { StringBuilder tmp = new StringBuilder(); foreach (XmlLightElement e in selectFrom.Select(_config.BlubXPath.XPath)) { if (e.IsText) tmp.Append(e.Value); else { foreach (XmlLightElement txt in e.Select(".//text()")) tmp.Append(txt.Value); } } if (tmp.Length == 0) tmp.Append(selectFrom.SelectRequiredNode(_config.BlubXPath.XPath).InnerText); blurb = tmp.ToString(); } DateTime dtvalue = item.Value.DateCreated; if (_config.DateXPath != null && selectFrom.TrySelectNode(_config.DateXPath.XPath, out found)) { DateTime contentDate; string dtText = found.InnerText.Trim(); dtText = DateTimeClean.Replace(dtText, m => m.Value.Substring(0, m.Length - 2)); if (!String.IsNullOrEmpty(_config.DateXPath.DateFormat)) { if (DateTime.TryParseExact(dtText, _config.DateXPath.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out contentDate)) dtvalue = contentDate; else throw new FormatException("Unable to parse date/time: " + dtText); } else if (DateTime.TryParse(dtText, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out contentDate)) dtvalue = contentDate; else throw new FormatException("Unable to parse date/time: " + dtText); } date = dtvalue.ToString("yyyy-MM-dd HH:mm:ss"); StringWriter indexed = new StringWriter(); indexed.WriteLine(title); foreach(var xpath in _config.Indexed.SafeEnumeration()) { foreach (var indexItem in selectFrom.Select(xpath.XPath)) { string innerText = indexItem.InnerText; indexed.WriteLine(innerText); indexed.WriteLine(NonAlphaNum.Replace(innerText, " "));//again, removing all special characters. } } if (String.IsNullOrEmpty(blurb)) blurb = indexed.ToString().Substring(title.Length).Trim(); title = WhiteSpaces.Replace(TrimString(title, _config.TitlePath != null ? (uint)_config.TitlePath.MaxLength : BlurbLength), " "); blurb = WhiteSpaces.Replace(TrimString(blurb, _config.BlubXPath != null ? (uint)_config.BlubXPath.MaxLength : BlurbLength), " "); string text = indexed.ToString(); using (TextReader rdr = new StringReader(text)) AddToIndex(item.Key, date, title, blurb, rdr); } }
private string CreateTemplate(string html) { HtmlLightDocument doc = new HtmlLightDocument(html); //Add css link: XmlLightElement cssLink = new XmlLightElement(doc.SelectRequiredNode("/html/head"), "link"); cssLink.Attributes["type"] = "text/css"; cssLink.Attributes["rel"] = "stylesheet"; cssLink.Attributes["href"] = new Uri(_baseUri, "search.css").AbsoluteUri; XmlLightElement startFrom = doc.Root; if (_config.Searching.XPathBase != null) startFrom = startFrom.SelectRequiredNode(_config.Searching.XPathBase.XPath); if(_config.Searching.FormXPath != null) { XmlLightElement form = startFrom.SelectRequiredNode(_config.Searching.FormXPath.XPath); foreach (XmlLightElement textbox in form.Select(".//input[@type='text']")) textbox.Attributes["value"] = String.Empty; } if(_config.Searching.TermsXPath != null) { InsertTag(startFrom, _config.Searching.TermsXPath.XPath, _config.Searching.TermsXPath.ReplaceOption, "search-terms"); } if (_config.Searching.ResultXPath != null) { InsertTag(startFrom, _config.Searching.ResultXPath.XPath, _config.Searching.ResultXPath.ReplaceOption, "search-result"); } using (StringWriter sw = new StringWriter()) { doc.WriteUnformatted(sw); return sw.ToString(); } }
public void BuildIndex() { if (_config == null) { throw new InvalidOperationException("The <search> element is missing from the configuration."); } Dictionary <string, string> hashes = new Dictionary <string, string>(StringComparer.Ordinal); foreach (KeyValuePair <string, ContentRecord> item in _content) { if (item.Key == SearchTemplate.SearchPath || item.Key == SearchTemplate.TemplatePath || item.Key == _config.TemplateUri) { continue; } if (item.Value.HasContentStoreId == false) { continue; } if (!_mimeInfo[item.Value.MimeType].Indexed || _mimeInfo[item.Value.MimeType].Type != ContentFormat.Html) { continue; } if (item.Value.HasHashContents) { if (hashes.ContainsKey(item.Value.HashContents)) { continue; } hashes.Add(item.Value.HashContents, item.Key); } string title = null, blurb = null, date = null; string content = Encoding.UTF8.GetString(_content.ReadContent(item.Value, true)); HtmlLightDocument xdoc = new HtmlLightDocument(content); XmlLightElement found, selectFrom = _config.XPathBase == null ? xdoc.Root : xdoc.SelectRequiredNode(_config.XPathBase.XPath); bool ignore = false; foreach (var xpath in _config.Conditions.SafeEnumeration()) { if (null != selectFrom.SelectSingleNode(xpath.XPath)) { ignore = true; break; } } if (ignore) { continue; } if (_config.TitlePath != null && selectFrom.TrySelectNode(_config.TitlePath.XPath, out found)) { title = found.InnerText.Trim(); } else if (_config.TitlePath == null && false == _mimeInfo.TryGetTitle(item.Value.MimeType, content, out title)) { title = null; } if (String.IsNullOrEmpty(title)) { continue; } if (_config.BlubXPath != null) { StringBuilder tmp = new StringBuilder(); foreach (XmlLightElement e in selectFrom.Select(_config.BlubXPath.XPath)) { if (e.IsText) { tmp.Append(e.Value); } else { foreach (XmlLightElement txt in e.Select(".//text()")) { tmp.Append(txt.Value); } } } if (tmp.Length == 0) { tmp.Append(selectFrom.SelectRequiredNode(_config.BlubXPath.XPath).InnerText); } blurb = tmp.ToString(); } DateTime dtvalue = item.Value.DateCreated; if (_config.DateXPath != null && selectFrom.TrySelectNode(_config.DateXPath.XPath, out found)) { DateTime contentDate; string dtText = found.InnerText.Trim(); dtText = DateTimeClean.Replace(dtText, m => m.Value.Substring(0, m.Length - 2)); if (!String.IsNullOrEmpty(_config.DateXPath.DateFormat)) { if (DateTime.TryParseExact(dtText, _config.DateXPath.DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out contentDate)) { dtvalue = contentDate; } else { throw new FormatException("Unable to parse date/time: " + dtText); } } else if (DateTime.TryParse(dtText, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out contentDate)) { dtvalue = contentDate; } else { throw new FormatException("Unable to parse date/time: " + dtText); } } date = dtvalue.ToString("yyyy-MM-dd HH:mm:ss"); StringWriter indexed = new StringWriter(); indexed.WriteLine(title); foreach (var xpath in _config.Indexed.SafeEnumeration()) { foreach (var indexItem in selectFrom.Select(xpath.XPath)) { string innerText = indexItem.InnerText; indexed.WriteLine(innerText); indexed.WriteLine(NonAlphaNum.Replace(innerText, " "));//again, removing all special characters. } } if (String.IsNullOrEmpty(blurb)) { blurb = indexed.ToString().Substring(title.Length).Trim(); } title = WhiteSpaces.Replace(TrimString(title, _config.TitlePath != null ? (uint)_config.TitlePath.MaxLength : BlurbLength), " "); blurb = WhiteSpaces.Replace(TrimString(blurb, _config.BlubXPath != null ? (uint)_config.BlubXPath.MaxLength : BlurbLength), " "); string text = indexed.ToString(); using (TextReader rdr = new StringReader(text)) AddToIndex(item.Key, date, title, blurb, rdr); } }