From 3d4ca5fd4cf21e703f392fc109715bedbfa82978 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 22 Sep 2018 18:48:33 +0100
Subject: [PATCH] There has to be a simpler way of implementing the HTML tag
 insertion. Let's try this again....

---
 SearchBox/SearchBox.cs            | 52 ++++++++++---------------------
 SearchBox/Utilities/StringPlus.cs | 16 ++++++++++
 2 files changed, 33 insertions(+), 35 deletions(-)

diff --git a/SearchBox/SearchBox.cs b/SearchBox/SearchBox.cs
index cbb2853..e51d1a4 100644
--- a/SearchBox/SearchBox.cs
+++ b/SearchBox/SearchBox.cs
@@ -5,6 +5,7 @@ using System.Linq;
 using System.Net;
 using System.Threading.Tasks;
 using Newtonsoft.Json;
+using UnidecodeSharpFork;
 
 namespace LibSearchBox
 {
@@ -221,42 +222,23 @@ namespace LibSearchBox
 				currentLength += nextSnippet.Item2 - nextSnippet.Item1;
 			}
 
-			List<string> snippetsText = new List<string>(snippets.Select(((int, int) snippet) => {
-				string result = source.Substring(snippet.Item1, snippet.Item2 - snippet.Item1);
-
-				if (string.IsNullOrWhiteSpace(result.Trim()))
-					return "";
-
-				if (settings.Html) {
-					List<string> parts = new List<string>() { result };
-					int remainingStartIndex = snippet.Item1;
-					foreach ((int, int) tokenDef in tokenLocations) {
-						if (tokenDef.Item1 - remainingStartIndex < result.Length) {
-							string remainingString = parts.Last();
-							parts.RemoveAt(parts.Count - 1); // Remove the last element
-															 // The bit before the token
-							string nextPart = WebUtility.HtmlEncode(
-								remainingString.Substring(0, tokenDef.Item1 - remainingStartIndex)
-							);
-							// The token itself
-							nextPart += $"<span class='token'>{WebUtility.HtmlEncode(remainingString.Substring(tokenDef.Item1 - remainingStartIndex, tokenDef.Item2))}</span>";
-							parts.Add(nextPart);
-							// The bit after the token - this will be processed by the next loop, so ti doesn't need to be HTML-encoded (yet)
-							parts.Add(remainingString.Substring((tokenDef.Item1 - remainingStartIndex) + tokenDef.Item2));
-
-							// Update our marker as to where we've got up to
-							remainingStartIndex = (tokenDef.Item1 - remainingStartIndex) + tokenDef.Item2;
-						}
+			List<string> snippetsText = new List<string>(
+				snippets.Select(((int, int) snippet) => {
+					string result = source.Substring(snippet.Item1, snippet.Item2 - snippet.Item1);
+					
+					if (settings.Html) {
+						result = WebUtility.HtmlEncode(result);
+						string resultSearchable = result.Unidecode().ToLower();
+						foreach ((int, string) nextToken in tokenizer.IterateTokens()) {
+							// TODO: Insert html tags here
+							throw new NotImplementedException("HTML tag insertion hasn't been implemented yet");
+						} 
 					}
-
-					// HTML-encode the last part
-					parts[parts.Count - 1] = WebUtility.HtmlEncode(parts[parts.Count - 1]);
-
-					result = string.Join("", parts);
-				}
-
-				return result;
-			}).Where((string snippet) => !string.IsNullOrWhiteSpace(snippet)));
+					
+					return result;
+				})
+				.Where((string snippet) => !string.IsNullOrWhiteSpace(snippet))
+			);
 
 			// Add the separator at the beginning and end if we aren't at the bounds of the source document
 			if (snippets.First().Item1 > 0)
diff --git a/SearchBox/Utilities/StringPlus.cs b/SearchBox/Utilities/StringPlus.cs
index 28215e9..cb160ab 100644
--- a/SearchBox/Utilities/StringPlus.cs
+++ b/SearchBox/Utilities/StringPlus.cs
@@ -1,4 +1,5 @@
 ﻿using System;
+using System.Collections.Generic;
 
 namespace LibSearchBox.Utilities
 {
@@ -14,5 +15,20 @@ namespace LibSearchBox.Utilities
 			}
 			return str;
 		}
+
+		// From https://stackoverflow.com/a/2641383/1460422
+		public static IEnumerable<int> AllIndexesOf(this string str, string value)
+		{
+			if (String.IsNullOrEmpty(value))
+				throw new ArgumentException("Error: The string to find may not be empty.", nameof(value));
+			
+			List<int> indexes = new List<int>();
+			for (int index = 0; ; index += value.Length) {
+				index = str.IndexOf(value, index);
+				if (index == -1)
+					break;
+				yield return index;
+			}
+		}
 	}
 }
