]> git.giacomolli.uk - onechat/blobdiff - telegram_markdown.go
Check the complete message before committing
[onechat] / telegram_markdown.go
index b6ea5a285d20b1740fe882fe4f9a1f5444eff974..658cac252c5a934e5b2e6ea1c90c33ca56e3f5aa 100644 (file)
@@ -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)
+}