mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
61 lines
1.8 KiB
C#
61 lines
1.8 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
|
|
}
|
|
} |