Added simple Markdown/HTML Generator

Use with:

"Bold Text".Bold();

"Other text".Italic();

"www.google.de".AsLink("Google")
This commit is contained in:
FlorianDahn 2019-08-28 17:32:08 +02:00
parent 1a76b2b3f8
commit 6a2ac7a309

View File

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types.Enums;
namespace TelegramBotBase.Markdown
{
public static class Generator
{
public static ParseMode OutputMode { get; set; } = ParseMode.Markdown;
/// <summary>
/// Generates a link with title in Markdown or HTML
/// </summary>
/// <param name="url"></param>
/// <param name="title"></param>
/// <param name="tooltip"></param>
/// <returns></returns>
public static String AsLink(this String url, String title = null, String tooltip = null)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "[" + (title ?? url) + "](" + url + " " + (tooltip ?? "") + ")";
case ParseMode.Html:
return $"<a href=\"{url}\" title=\"{tooltip ?? ""}\">{title ?? ""}</b>";
}
return url;
}
/// <summary>
/// Returns a Link to the User, title is optional.
/// </summary>
/// <param name="userId"></param>
/// <param name="title"></param>
/// <returns></returns>
public static String MentionUser(this long userId, String title = null)
{
return AsLink("tg://user?id=" + userId.ToString(), title);
}
/// <summary>
/// Returns a bold link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String Bold(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "**" + text + "**";
case ParseMode.Html:
return "<b>" + text + "</b>";
}
return text;
}
/// <summary>
/// Returns a italic link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String Italic(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "__" + text + "__";
case ParseMode.Html:
return "<i>" + text + "</i>";
}
return text;
}
/// <summary>
/// Returns a monospace link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String Monospace(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "`" + text + "`";
case ParseMode.Html:
return "<code>" + text + "</code>";
}
return text;
}
/// <summary>
/// Returns a multi monospace link in Markdown or HTML
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static String MultiMonospace(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "```" + text + "```";
case ParseMode.Html:
return "<pre>" + text + "</pre>";
}
return text;
}
}
}