using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Telegram.Bot.Types.Enums;
namespace TelegramBotBase.Markdown
{
///
/// https://core.telegram.org/bots/api#markdownv2-style
///
public static class Generator
{
public static ParseMode OutputMode { get; set; } = ParseMode.Markdown;
///
/// Generates a link with title in Markdown or HTML
///
///
///
///
///
public static String Link(this String url, String title = null, String tooltip = null)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "[" + (title ?? url) + "](" + url + " " + (tooltip ?? "") + ")";
case ParseMode.Html:
return $"{title ?? ""}";
}
return url;
}
///
/// Returns a Link to the User, title is optional.
///
///
///
///
public static String MentionUser(this int userId, String title = null)
{
return Link("tg://user?id=" + userId.ToString(), title);
}
///
/// Returns a Link to the User, title is optional.
///
///
///
///
public static String MentionUser(this String username, String title = null)
{
return Link("tg://user?id=" + username, title);
}
///
/// Returns a bold text in Markdown or HTML
///
///
///
public static String Bold(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "*" + text + "*";
case ParseMode.Html:
return "" + text + "";
}
return text;
}
///
/// Returns a strike through in Markdown or HTML
///
///
///
public static String Strikesthrough(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "~" + text + "~";
case ParseMode.Html:
return "" + text + "";
}
return text;
}
///
/// Returns a italic text in Markdown or HTML
///
///
///
public static String Italic(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "_" + text + "_";
case ParseMode.Html:
return "" + text + "";
}
return text;
}
///
/// Returns a underline text in Markdown or HTML
///
///
///
public static String Underline(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "__" + text + "__";
case ParseMode.Html:
return "" + text + "";
}
return text;
}
///
/// Returns a monospace text in Markdown or HTML
///
///
///
public static String Monospace(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "`" + text + "`";
case ParseMode.Html:
return "" + text + "";
}
return text;
}
///
/// Returns a multi monospace text in Markdown or HTML
///
///
///
public static String MultiMonospace(this String text)
{
switch (OutputMode)
{
case ParseMode.Markdown:
return "```" + text + "```";
case ParseMode.Html:
return "" + text + "
";
}
return text;
}
///
/// Escapes all characters as stated in the documentation: https://core.telegram.org/bots/api#markdownv2-style
///
///
///
public static String MarkdownV2Escape(this String text, params char[] toKeep)
{
char[] toEscape = new char[] { '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!' };
return text.EscapeAll(toEscape.Where(a => !toKeep.Contains(a)).Select(a => a.ToString()).ToArray());
}
public static string EscapeAll(this string seed, String[] chars, char escapeCharacter = '\\')
{
return chars.Aggregate(seed, (str, cItem) => str.Replace(cItem, escapeCharacter + cItem));
}
}
}