Files
telegram-bot/modules/RichSiteSummary/Data/RepresentableEntity.cs
T
2023-07-29 15:14:52 +03:00

60 lines
1.7 KiB
C#

// This file is a part of RSS Bot for Telegram.
// License: MIT
// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
//
using System;
using System.Web;
using Telegram.Bot.Types.Enums;
namespace Kruzya.TelegramBot.RichSiteSummary.Data;
/// <summary>
/// Implements the basic logic for rendering Database Entities in messages.
/// </summary>
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.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 $"<a href=\"{escapedUrl}\">{escapedText}</a>";
}
#endregion
}