From bf3e8a65e16116d2612156156f263e0479fce2bd Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Fri, 13 Mar 2020 13:57:10 +0100 Subject: [PATCH] Repair "www" removal --- Commands.cs | 4 ++-- Utils.cs | 21 +++++++-------------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Commands.cs b/Commands.cs index 06c5ee8..4dfe65b 100644 --- a/Commands.cs +++ b/Commands.cs @@ -208,11 +208,11 @@ namespace RSSBot { // Remove "www." var index = linkName.IndexOf("www.", StringComparison.Ordinal); - if (index > 0) linkName = linkName.Remove(index, 4); + if (index > -1) linkName = linkName.Remove(index, 4); var content = ""; if (!string.IsNullOrWhiteSpace(entry.Content)) - content = Utils.ProcessContent(entry.Content); // magic processing missing + content = Utils.ProcessContent(entry.Content); else if (!string.IsNullOrWhiteSpace(entry.Description)) content = Utils.ProcessContent(entry.Description); diff --git a/Utils.cs b/Utils.cs index fa5ad7e..1bd4159 100644 --- a/Utils.cs +++ b/Utils.cs @@ -1,14 +1,16 @@ -using System; -using System.Linq; +using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Web; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; namespace RSSBot { public static class Utils { + private static readonly Regex RegexHtml = new Regex("<.*?>"); + public static string StripHtml(string input) { - return Regex.Replace(input, "<.*?>", String.Empty).Trim(); + return RegexHtml.Replace(input, string.Empty).Trim(); } private static string CleanRss(string input) { @@ -58,22 +60,13 @@ namespace RSSBot { } public static string ProcessContent(string input) { - string content = StripHtml(input); + var content = StripHtml(HttpUtility.HtmlDecode(input)); content = CleanRss(content); - if (content.Length > 250) { - content = content.Substring(0, 250) + "..."; - } + if (content.Length > 250) content = content.Substring(0, 250) + "..."; return content; } - public static bool IsCommand(string messageText, string command) { - return Regex.Match(messageText, - $"^/{command}(?:@{Bot.BotInfo.Username})?$", - RegexOptions.IgnoreCase - ).Success; - } - public static GroupCollection ReturnMatches(string text, string pattern) { return Regex.Match(text, pattern,