From cf178aa848c76632ed22aa689e8ba8d2ff57d297 Mon Sep 17 00:00:00 2001 From: Paulo Anjos Date: Tue, 29 Jan 2019 13:05:56 -0200 Subject: [PATCH] Better format for html-to-text --- .../Query/Extensions/BaseDisplay.cs | 2 +- src/DiscordNet/Utils.cs | 36 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/DiscordNet/Query/Extensions/BaseDisplay.cs b/src/DiscordNet/Query/Extensions/BaseDisplay.cs index e24ec0c..35e9084 100644 --- a/src/DiscordNet/Query/Extensions/BaseDisplay.cs +++ b/src/DiscordNet/Query/Extensions/BaseDisplay.cs @@ -26,7 +26,7 @@ private async Task GetWebDocsAsync(string url, object o) anchor = anchor.Substring(0, anchor.IndexOf('"')); summary = block.Substring(block.IndexOf("summary\">") + 9); summary = summary.Substring(0, summary.IndexOf("")); - summary = WebUtility.HtmlDecode(StripTags(summary)); + summary = Utils.ResolveHtml(summary); /*string example = block.Substring(block.IndexOf("example\">")); //TODO: Find this summary = summary.Substring(0, summary.IndexOf(""));*/ if (!(o is TypeInfoWrapper) && !IsInherited(o)) diff --git a/src/DiscordNet/Utils.cs b/src/DiscordNet/Utils.cs index d3f9bfc..ba20767 100644 --- a/src/DiscordNet/Utils.cs +++ b/src/DiscordNet/Utils.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; namespace DiscordNet { @@ -8,9 +9,38 @@ public static class Utils { public static string ResolveHtml(string html) { - //TODO: Pass summary, replace \n's with spaces, replace

's with \n's, strip tags, decode html - //ISSUE:

could be empty, check for \n on first char - return ""; + string res = WebUtility.HtmlDecode(StripTags(html.Replace('\n', ' ').Replace("

", "\n"))); + if (res[0] == '\n') + res = res.Substring(1); + return res; + } + + public static string StripTags(string source) + { + char[] array = new char[source.Length]; + int arrayIndex = 0; + bool inside = false; + + for (int i = 0; i < source.Length; i++) + { + char let = source[i]; + if (let == '<') + { + inside = true; + continue; + } + if (let == '>') + { + inside = false; + continue; + } + if (!inside) + { + array[arrayIndex] = let; + arrayIndex++; + } + } + return new string(array, 0, arrayIndex); } public static IEnumerable RandomShuffle(this IEnumerable source)