Files

60 lines
1.7 KiB
C#
Raw Permalink Normal View History

2023-07-29 15:03:45 +03:00
// This file is a part of RSS Bot for Telegram.
// License: MIT
// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
//
2020-03-02 00:00:52 +04:00
using System;
using System.Web;
using Telegram.Bot.Types.Enums;
2023-07-29 15:14:52 +03:00
namespace Kruzya.TelegramBot.RichSiteSummary.Data;
/// <summary>
/// Implements the basic logic for rendering Database Entities in messages.
/// </summary>
public abstract class RepresentableEntity
2020-03-02 00:00:52 +04:00
{
2023-07-29 15:14:52 +03:00
protected virtual string ViewableText { get; }
protected virtual string ViewableUrl { get; }
protected virtual ParseMode DefaultRepresentation { get; }
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public override string ToString() => Representation();
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public string Representation(string text) => Representation(DefaultRepresentation, text);
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public string Representation() => Representation(DefaultRepresentation);
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public string Representation(ParseMode parseMode) => Representation(parseMode, ViewableText);
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public string Representation(ParseMode parseMode, string text)
{
string content = String.Empty;
switch (parseMode)
2020-03-02 00:00:52 +04:00
{
2023-07-29 15:14:52 +03:00
case ParseMode.Html:
content = HtmlRepresentation(text);
break;
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
case ParseMode.Markdown:
case ParseMode.MarkdownV2:
throw new NotImplementedException();
2020-03-02 00:00:52 +04:00
}
2023-07-29 15:14:52 +03:00
return content;
}
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
#region Representations
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public string RawRepresentation(string text)
=> text;
2020-03-02 00:00:52 +04:00
2023-07-29 15:14:52 +03:00
public string HtmlRepresentation(string text)
{
var escapedText = HttpUtility.HtmlEncode(text);
var escapedUrl = HttpUtility.HtmlAttributeEncode(ViewableUrl);
return $"<a href=\"{escapedUrl}\">{escapedText}</a>";
2020-03-02 00:00:52 +04:00
}
2023-07-29 15:14:52 +03:00
#endregion
2020-03-02 00:00:52 +04:00
}