X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/80e348724041a694002fc38f57e8868b9b9a2be9..448c931a776c3e3f65ca2eab11da0c062d597818:/telegram_markdown.go?ds=sidebyside diff --git a/telegram_markdown.go b/telegram_markdown.go index b6ea5a2..658cac2 100644 --- a/telegram_markdown.go +++ b/telegram_markdown.go @@ -23,21 +23,6 @@ type TelegramMarkdownText struct { entities []TelegramEntity } -// add appends text. -func (m *TelegramMarkdownText) add(text string) { - m.text.WriteString(text) - - m.units += telegramUTF16Units(text) -} - -// entity keeps entity, unless it is empty, because Telegram refuses an entity -// of length 0. -func (m *TelegramMarkdownText) entity(entity TelegramEntity) { - if entity.Length > 0 { - m.entities = append(m.entities, entity) - } -} - // inline appends one line of Markdown with its inline marks as entities. A // backslash before an ASCII punctuation character makes the character plain // text. @@ -83,6 +68,34 @@ func (m *TelegramMarkdownText) inline(line string) { m.add(line[plain:]) } +// add appends text. +func (m *TelegramMarkdownText) add(text string) { + m.text.WriteString(text) + + m.units += telegramUTF16Units(text) +} + +// entity keeps entity, unless it is empty, because Telegram refuses an entity +// of length 0. +func (m *TelegramMarkdownText) entity(entity TelegramEntity) { + if entity.Length > 0 { + m.entities = append(m.entities, entity) + } +} + +// telegramUTF16Units returns the number of UTF-16 code units of text. +func telegramUTF16Units(text string) int { + units := 0 + for _, char := range text { + units++ + if char > 0xFFFF { + units++ + } + } + + return units +} + // telegramMarkdownEntities converts the Markdown of a reply to plain text and // Telegram message entities. It knows bold (** and __), italic (* and _), // strikethrough (~~), inline code, fenced code blocks with an optional @@ -150,27 +163,6 @@ func telegramEscapeTags(markdown string) string { return telegramRewriteProse(markdown, telegramTagEscaper.Replace) } -// telegramUnescapeTags gives back the Markdown that telegramEscapeTags escaped. -// It is exact, because after the escape each & outside code starts & or -// <. It also works on a part of telegramSplitRich, because a part has the -// same code as the text that it comes from. -func telegramUnescapeTags(markdown string) string { - return telegramRewriteProse(markdown, telegramTagUnescaper.Replace) -} - -// telegramUTF16Units returns the number of UTF-16 code units of text. -func telegramUTF16Units(text string) int { - units := 0 - for _, char := range text { - units++ - if char > 0xFFFF { - units++ - } - } - - return units -} - // telegramRewriteProse returns markdown with rewrite applied to each part of a // line outside inline code and fenced code blocks. A code block that does not // close runs to the end, as in Markdown. @@ -237,3 +229,11 @@ func telegramRewriteLineProse(out *strings.Builder, line string, rewrite func(st out.WriteString(rewrite(line[prose:])) } + +// telegramUnescapeTags gives back the Markdown that telegramEscapeTags escaped. +// It is exact, because after the escape each & outside code starts & or +// <. It also works on a part of telegramSplitRich, because a part has the +// same code as the text that it comes from. +func telegramUnescapeTags(markdown string) string { + return telegramRewriteProse(markdown, telegramTagUnescaper.Replace) +}