private Stream PerformFind(string url) { int index = url.IndexOf('?'); if (index == -1) return null; string queryString = url.Substring(index + 1); string query; if (!ParseQueryString(queryString).TryGetValue("q", out query)) return null; var hits = _searchManager.Search(query); var writer = new HtmlWriter(); FormatSearchResults(writer, query, hits); return new MemoryStream(Encoding.UTF8.GetBytes(writer.ToString())); }
private Stream PerformRoot() { var writer = new HtmlWriter(); FormatHome(writer); return new MemoryStream(Encoding.UTF8.GetBytes(writer.ToString())); }
private void FormatSearchResults(HtmlWriter writer, string query, IList<HelpSearchResult> hits) { writer.OpenTag("html"); FormatHead(writer); FormatSearchResultsBody(writer, query, hits); writer.CloseTag(); }
private void FormatSearchResultsBody(HtmlWriter writer, string query, IList<HelpSearchResult> hits) { writer.OpenTag("body").Attribute("class", "search-results"); writer .OpenTag("h1").Text(Labels.SearchResultsFor).Text(" ") .OpenTag("span").Attribute("class", "search-query").Text(query).CloseTag() .CloseTag(); var queryWords = ParseQuery(query); foreach (var hit in hits) { FormatHit(writer, queryWords, hit); } writer.CloseTag(); }
private void FormatHomeBody(HtmlWriter writer) { writer.OpenTag("body").Attribute("class", "search-results"); writer.OpenTag("h1").Text(Labels.NeedAssistance).CloseTag(); writer.OpenTag("p").Text(Labels.UseSearchBox).CloseTag(); writer.OpenTag("p").Text(Labels.UseHelpButton).CloseTag(); writer.CloseTag(); }
private void FormatHome(HtmlWriter writer) { writer.OpenTag("html"); FormatHead(writer); FormatHomeBody(writer); writer.CloseTag(); }
private void FormatHit(HtmlWriter writer, HashSet<string> query, HelpSearchResult hit) { writer.OpenTag("p").Attribute("class", "search-result"); writer .OpenTag("div").Attribute("class", "search-title") .OpenTag("a").Attribute("href", hit.Path); FormatEmphasis(writer, query, hit.Title); writer .CloseTag() .CloseTag(); writer.OpenTag("div").Attribute("class", "search-content"); FormatEmphasis(writer, query, hit.Content); writer.CloseTag(); writer.CloseTag(); }
private void FormatEmphasis(HtmlWriter writer, HashSet<string> query, string text) { var words = ParseContent(text); int start = -1; int end = -1; for (int i = 0; i < words.Count; i++) { if (!query.Contains(words[i].ToLower())) continue; int leading = SearchResultLeading; start = 0; for (int j = i - 1; j >= 0; j--) { leading -= words[j].Length; if (leading < 0) { start = j + 1; break; } } end = FindEnd(start, words); break; } if (start == -1) { start = 0; end = FindEnd(start, words); } bool wasContained = false; for (int j = start; j < end; j++) { bool isContained = query.Contains(words[j].ToLower()); if (isContained != wasContained) { if (isContained) writer.OpenTag("b"); else writer.CloseTag(); wasContained = isContained; } writer.Text(words[j]); } if (wasContained) writer.CloseTag(); }
private static void FormatHead(HtmlWriter writer) { writer.OpenTag("head"); writer.Tag("title", Labels.SearchResults); writer.OpenTag("link").Attribute("type", "text/css").Attribute("rel", "stylesheet").Attribute("href", "/help.css").CloseTag(); writer.OpenTag("script").Attribute("src", "/help.js").Attribute("type", "text/javascript").Text("").CloseTag(); writer.CloseTag(); }