/// This file is a part of RSS Bot for Telegram. /// License: MIT /// Author: CrazyHackGUT aka Kruzya (Sergey Gut) /// using System; using System.Web; using Telegram.Bot.Types.Enums; namespace Kruzya.TelegramBot.RichSiteSummary.Data { /// /// Implements the basic logic for rendering Database Entities in messages. /// public abstract class RepresentableEntity { protected virtual string ViewableText { get; } protected virtual string ViewableUrl { get; } protected virtual ParseMode DefaultRepresentation { get; } public override string ToString() => Representation(); public string Representation(string text) => Representation(DefaultRepresentation, text); public string Representation() => Representation(DefaultRepresentation); public string Representation(ParseMode parseMode) => Representation(parseMode, ViewableText); public string Representation(ParseMode parseMode, string text) { string content = String.Empty; switch (parseMode) { case ParseMode.Default: content = RawRepresentation(text); break; case ParseMode.Html: content = HtmlRepresentation(text); break; case ParseMode.Markdown: case ParseMode.MarkdownV2: throw new NotImplementedException(); } return content; } #region Representations public string RawRepresentation(string text) => text; public string HtmlRepresentation(string text) { var escapedText = HttpUtility.HtmlEncode(text); var escapedUrl = HttpUtility.HtmlAttributeEncode(ViewableUrl); return $"{escapedText}"; } #endregion } }