/
anton.bessolitsyn
/
easyapi
Обзор
Документация
Войти
/
anton.bessolitsyn
/
easyapi
Код
Запросы
0
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
gpxdata
Utilities/StyleOptionHelper.cs
133 строки
6 KB
Anton Bessolitsyn
Added comment to StyleOptionHelper
12 мар 2025, 12:27
12 мар 2025, 12:27
9b99b14
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Components.Forms; using System.Collections.Generic; using System.Data.SqlTypes; using System.Globalization; using System.Text; using static System.Net.Mime.MediaTypeNames; namespace easyapi.Utilities { /// <summary> /// Класс помощник по работе со стилями текста из телеграм. Стили конвертированы в словарь телеграмм ботом. /// На данный момент класс поможет применить стили к тексту как HTML. public static class StyleOptionHelper { //public StyleOptionHelper Instance { get => instance; } //private readonly static StyleOptionHelper instance = new(); static HtmlStyleOption Bold = new() { openTag = "<b>", closeTag = "</b>", }; static HtmlStyleOption Italic = new() { openTag = "<i>", closeTag = "</i>" }; static HtmlStyleOption Strikethrough = new() { openTag = "<s>", closeTag = "</s>" }; static HtmlStyleOption TextLink = new() { openTag = @"<a href=""url"">", closeTag = "</a>" }; static HtmlStyleOption? GetHtmlStyleOption(Dictionary<string, string> styleOption) { if (styleOption.TryGetValue("type", out string? style)) { HtmlStyleOption option; switch (style) { case "TextLink": option = TextLink; option.openTag = option.openTag.Replace("url", styleOption["url"]); break; case "Url": option = TextLink; option.openTag = option.openTag.Replace("url", styleOption["url"]); break; case "Strikethrough": option = Strikethrough; break; case "Italic": option = Italic; break; case "Bold": option = Bold; break; default: return null; } option.startIndex = int.Parse(styleOption["offset"], CultureInfo.InvariantCulture); option.length = int.Parse(styleOption["length"], CultureInfo.InvariantCulture); option.endIndex = option.startIndex + option.length; return option; } return null; } static public string ApplyStyleOptionsToTextAsHtml(string text, Dictionary<string, string>[] styleOptions) { if (styleOptions == null) return text; var styleOptionsList = new List<HtmlStyleOption>(); for (int i = 0; i < styleOptions.Length; i++) { var option = GetHtmlStyleOption(styleOptions[i]); if (option != null && (option.Value.startIndex + option.Value.length - 1 < text.Length)) { styleOptionsList.Add(option.Value); } } Queue<HtmlStyleOption> stylesQueue = new(); styleOptionsList = [.. styleOptionsList.OrderBy(o => o.startIndex)]; foreach (var option in styleOptionsList) { stylesQueue.Enqueue(option); } return ApplyHtmlStyleOptionsToSubString(text, stylesQueue); } static private string ApplyHtmlStyleOptionsToSubString(string sourceText, Queue<HtmlStyleOption> stylesQueue) { if (stylesQueue.Count == 0) return sourceText; int offset = 0; if (stylesQueue.Count == 1) { HtmlStyleOption currentElement = stylesQueue.Dequeue(); string substring = sourceText.Substring(currentElement.startIndex, currentElement.length); sourceText = sourceText.Remove(currentElement.startIndex + offset, substring.Length); sourceText = sourceText.Insert(currentElement.startIndex + offset, currentElement.GetStyleText(substring)); } else { while (stylesQueue.Count > 0) { HtmlStyleOption currentElement = stylesQueue.Dequeue(); var localQueue = new Queue<HtmlStyleOption>(); while (stylesQueue.Count > 0 && stylesQueue.Peek().endIndex < currentElement.endIndex + 1) { var so = stylesQueue.Dequeue(); localQueue.Enqueue(so.Offset(-currentElement.startIndex)); } string substring = sourceText.Substring(currentElement.startIndex + offset, currentElement.length); string styledSubstring = ApplyHtmlStyleOptionsToSubString(substring, localQueue); sourceText = sourceText.Remove(currentElement.startIndex + offset, substring.Length); sourceText = sourceText.Insert(currentElement.startIndex + offset, currentElement.GetStyleText(styledSubstring)); offset += styledSubstring.Length - substring.Length + currentElement.GetTagsLength(); } } return sourceText; } struct HtmlStyleOption { public string openTag; public string closeTag; public int startIndex; public int endIndex; public int length; public string GetStyleText(string text) => openTag + text + closeTag; public int GetTagsLength() => openTag.Length + closeTag.Length; public HtmlStyleOption Offset(int offset) { startIndex += offset; endIndex += offset; return this; } } } }