Skip to content

Commit

Permalink
Better format for html-to-text
Browse files Browse the repository at this point in the history
  • Loading branch information
SubZero0 committed Jan 29, 2019
1 parent 18d7488 commit cf178aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/DiscordNet/Query/Extensions/BaseDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private async Task<DocsHttpResult> GetWebDocsAsync(string url, object o)
anchor = anchor.Substring(0, anchor.IndexOf('"'));
summary = block.Substring(block.IndexOf("summary\">") + 9);
summary = summary.Substring(0, summary.IndexOf("</div>"));
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("</div>"));*/
if (!(o is TypeInfoWrapper) && !IsInherited(o))
Expand Down
36 changes: 33 additions & 3 deletions src/DiscordNet/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace DiscordNet
{
public static class Utils
{
public static string ResolveHtml(string html)
{
//TODO: Pass summary, replace \n's with spaces, replace </p>'s with \n's, strip tags, decode html
//ISSUE: <p></p> could be empty, check for \n on first char
return "";
string res = WebUtility.HtmlDecode(StripTags(html.Replace('\n', ' ').Replace("</p>", "\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<T> RandomShuffle<T>(this IEnumerable<T> source)
Expand Down

0 comments on commit cf178aa

Please sign in to comment.