|
| 1 | +<%@ Page Language="C#" EnableViewState="False" %> |
| 2 | + |
| 3 | +<script runat="server"> |
| 4 | +//=============================================================================================================== |
| 5 | +// System : Sandcastle Help File Builder |
| 6 | +// File : SearchHelp.aspx |
| 7 | +// Author : Eric Woodruff ([email protected]) |
| 8 | +// Updated : 05/15/2014 |
| 9 | +// Note : Copyright 2007-2015, Eric Woodruff, All rights reserved |
| 10 | +// Compiler: Microsoft C# |
| 11 | +// |
| 12 | +// This file contains the code used to search for keywords within the help topics using the full-text index |
| 13 | +// files created by the help file builder. |
| 14 | +// |
| 15 | +// This code is published under the Microsoft Public License (Ms-PL). A copy of the license should be |
| 16 | +// distributed with the code. It can also be found at the project website: https://GitHub.com/EWSoftware/SHFB. This |
| 17 | +// notice, the author's name, and all copyright notices must remain intact in all applications, documentation, |
| 18 | +// and source files. |
| 19 | +// |
| 20 | +// Date Who Comments |
| 21 | +// ============================================================================================================== |
| 22 | +// 06/24/2007 EFW Created the code |
| 23 | +// 02/17/2012 EFW Switched to JSON serialization to support websites that use something other than ASP.NET |
| 24 | +// such as PHP. |
| 25 | +// 05/15/2014 EFW Updated for use with the lightweight website presentation styles |
| 26 | +//=============================================================================================================== |
| 27 | +
|
| 28 | +/// <summary> |
| 29 | +/// This class is used to track the results and their rankings |
| 30 | +/// </summary> |
| 31 | +private class Ranking |
| 32 | +{ |
| 33 | + public string Filename, PageTitle; |
| 34 | + public int Rank; |
| 35 | +
|
| 36 | + public Ranking(string file, string title, int rank) |
| 37 | + { |
| 38 | + Filename = file; |
| 39 | + PageTitle = title; |
| 40 | + Rank = rank; |
| 41 | + } |
| 42 | +} |
| 43 | +
|
| 44 | +/// <summary> |
| 45 | +/// Render the search results |
| 46 | +/// </summary> |
| 47 | +/// <param name="writer">The writer to which the results are written</param> |
| 48 | +protected override void Render(HtmlTextWriter writer) |
| 49 | +{ |
| 50 | + JavaScriptSerializer jss = new JavaScriptSerializer(); |
| 51 | + string searchText, ftiFile; |
| 52 | + char letter; |
| 53 | + bool sortByTitle = false; |
| 54 | +
|
| 55 | + jss.MaxJsonLength = Int32.MaxValue; |
| 56 | +
|
| 57 | + // The keywords for which to search should be passed in the query string |
| 58 | + searchText = this.Request.QueryString["Keywords"]; |
| 59 | +
|
| 60 | + if(String.IsNullOrEmpty(searchText)) |
| 61 | + { |
| 62 | + writer.Write("<strong>Nothing found</strong>"); |
| 63 | + return; |
| 64 | + } |
| 65 | +
|
| 66 | + // An optional SortByTitle option can also be specified |
| 67 | + if(this.Request.QueryString["SortByTitle"] != null) |
| 68 | + sortByTitle = Convert.ToBoolean(this.Request.QueryString["SortByTitle"]); |
| 69 | +
|
| 70 | + List<string> keywords = this.ParseKeywords(searchText); |
| 71 | + List<char> letters = new List<char>(); |
| 72 | + List<string> fileList; |
| 73 | + Dictionary<string, List<long>> ftiWords, wordDictionary = new Dictionary<string,List<long>>(); |
| 74 | +
|
| 75 | + // Load the file index |
| 76 | + using(StreamReader sr = new StreamReader(Server.MapPath("fti/FTI_Files.json"))) |
| 77 | + { |
| 78 | + fileList = jss.Deserialize<List<string>>(sr.ReadToEnd()); |
| 79 | + } |
| 80 | +
|
| 81 | + // Load the required word index files |
| 82 | + foreach(string word in keywords) |
| 83 | + { |
| 84 | + letter = word[0]; |
| 85 | +
|
| 86 | + if(!letters.Contains(letter)) |
| 87 | + { |
| 88 | + letters.Add(letter); |
| 89 | + ftiFile = Server.MapPath(String.Format(CultureInfo.InvariantCulture, "fti/FTI_{0}.json", (int)letter)); |
| 90 | +
|
| 91 | + if(File.Exists(ftiFile)) |
| 92 | + { |
| 93 | + using(StreamReader sr = new StreamReader(ftiFile)) |
| 94 | + { |
| 95 | + ftiWords = jss.Deserialize<Dictionary<string, List<long>>>(sr.ReadToEnd()); |
| 96 | + } |
| 97 | +
|
| 98 | + foreach(string ftiWord in ftiWords.Keys) |
| 99 | + wordDictionary.Add(ftiWord, ftiWords[ftiWord]); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + // Perform the search and return the results as a block of HTML |
| 105 | + writer.Write(this.Search(keywords, fileList, wordDictionary, sortByTitle)); |
| 106 | +} |
| 107 | +
|
| 108 | +/// <summary> |
| 109 | +/// Split the search text up into keywords |
| 110 | +/// </summary> |
| 111 | +/// <param name="keywords">The keywords to parse</param> |
| 112 | +/// <returns>A list containing the words for which to search</returns> |
| 113 | +private List<string> ParseKeywords(string keywords) |
| 114 | +{ |
| 115 | + List<string> keywordList = new List<string>(); |
| 116 | + string checkWord; |
| 117 | + string[] words = Regex.Split(keywords, @"\W+"); |
| 118 | +
|
| 119 | + foreach(string word in words) |
| 120 | + { |
| 121 | + checkWord = word.ToLower(CultureInfo.InvariantCulture); |
| 122 | + |
| 123 | + if(checkWord.Length > 2 && !Char.IsDigit(checkWord[0]) && !keywordList.Contains(checkWord)) |
| 124 | + keywordList.Add(checkWord); |
| 125 | + } |
| 126 | +
|
| 127 | + return keywordList; |
| 128 | +} |
| 129 | +
|
| 130 | +/// <summary> |
| 131 | +/// Search for the specified keywords and return the results as a block of HTML |
| 132 | +/// </summary> |
| 133 | +/// <param name="keywords">The keywords for which to search</param> |
| 134 | +/// <param name="fileInfo">The file list</param> |
| 135 | +/// <param name="wordDictionary">The dictionary used to find the words</param> |
| 136 | +/// <param name="sortByTitle">True to sort by title, false to sort by ranking</param> |
| 137 | +/// <returns>A block of HTML representing the search results</returns> |
| 138 | +private string Search(List<string> keywords, List<string> fileInfo, |
| 139 | + Dictionary<string, List<long>> wordDictionary, bool sortByTitle) |
| 140 | +{ |
| 141 | + StringBuilder sb = new StringBuilder(10240); |
| 142 | + Dictionary<string, List<long>> matches = new Dictionary<string, List<long>>(); |
| 143 | + List<long> occurrences; |
| 144 | + List<int> matchingFileIndices = new List<int>(), occurrenceIndices = new List<int>(); |
| 145 | + List<Ranking> rankings = new List<Ranking>(); |
| 146 | +
|
| 147 | + string filename, title; |
| 148 | + string[] fileIndex; |
| 149 | + bool isFirst = true; |
| 150 | + int idx, wordCount, matchCount; |
| 151 | +
|
| 152 | + foreach(string word in keywords) |
| 153 | + { |
| 154 | + if(!wordDictionary.TryGetValue(word, out occurrences)) |
| 155 | + return "<strong>Nothing found</strong>"; |
| 156 | +
|
| 157 | + matches.Add(word, occurrences); |
| 158 | + occurrenceIndices.Clear(); |
| 159 | +
|
| 160 | + // Get a list of the file indices for this match |
| 161 | + foreach(long entry in occurrences) |
| 162 | + occurrenceIndices.Add((int)(entry >> 16)); |
| 163 | + |
| 164 | + if(isFirst) |
| 165 | + { |
| 166 | + isFirst = false; |
| 167 | + matchingFileIndices.AddRange(occurrenceIndices); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + // After the first match, remove files that do not appear for |
| 172 | + // all found keywords. |
| 173 | + for(idx = 0; idx < matchingFileIndices.Count; idx++) |
| 174 | + if(!occurrenceIndices.Contains(matchingFileIndices[idx])) |
| 175 | + { |
| 176 | + matchingFileIndices.RemoveAt(idx); |
| 177 | + idx--; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +
|
| 182 | + if(matchingFileIndices.Count == 0) |
| 183 | + return "<strong>Nothing found</strong>"; |
| 184 | +
|
| 185 | + // Rank the files based on the number of times the words occurs |
| 186 | + foreach(int index in matchingFileIndices) |
| 187 | + { |
| 188 | + // Split out the title, filename, and word count |
| 189 | + fileIndex = fileInfo[index].Split('\x0'); |
| 190 | +
|
| 191 | + title = fileIndex[0]; |
| 192 | + filename = fileIndex[1]; |
| 193 | + wordCount = Convert.ToInt32(fileIndex[2]); |
| 194 | + matchCount = 0; |
| 195 | + |
| 196 | + foreach(string word in keywords) |
| 197 | + { |
| 198 | + occurrences = matches[word]; |
| 199 | +
|
| 200 | + foreach(long entry in occurrences) |
| 201 | + if((int)(entry >> 16) == index) |
| 202 | + matchCount += (int)(entry & 0xFFFF); |
| 203 | + } |
| 204 | +
|
| 205 | + rankings.Add(new Ranking(filename, title, matchCount * 1000 / wordCount)); |
| 206 | + |
| 207 | + if(rankings.Count > 99) |
| 208 | + break; |
| 209 | + } |
| 210 | +
|
| 211 | + // Sort by rank in descending order or by page title in ascending order |
| 212 | + rankings.Sort(delegate (Ranking x, Ranking y) |
| 213 | + { |
| 214 | + if(!sortByTitle) |
| 215 | + return y.Rank - x.Rank; |
| 216 | +
|
| 217 | + return x.PageTitle.CompareTo(y.PageTitle); |
| 218 | + }); |
| 219 | +
|
| 220 | + // Format the file list and return the results |
| 221 | + sb.Append("<ol>"); |
| 222 | +
|
| 223 | + foreach(Ranking r in rankings) |
| 224 | + sb.AppendFormat("<li><a href=\"{0}\" target=\"_blank\">{1}</a></li>", r.Filename, r.PageTitle); |
| 225 | +
|
| 226 | + sb.Append("</ol>"); |
| 227 | +
|
| 228 | + if(rankings.Count < matchingFileIndices.Count) |
| 229 | + sb.AppendFormat("<p>Omitted {0} more results</p>", matchingFileIndices.Count - rankings.Count); |
| 230 | +
|
| 231 | + return sb.ToString(); |
| 232 | +} |
| 233 | +</script> |
0 commit comments