-}
-
-// telegramMarkSpan returns the inline mark that starts at index of line and
-// closes in line. It returns false when no mark starts there or the mark does
-// not close.
-func telegramMarkSpan(line string, index int) (TelegramMarkdownSpan, bool) {
- switch line[index] {
- case '`':
- return telegramCodeSpan(line, index)
- case '[':
- return telegramLinkSpan(line, index)
- case '~':
- return telegramDelimited(line, index, "~~", "strikethrough")
- case '*', '_':
- double := line[index : index+1]
- double += double
- span, found := telegramDelimited(line, index, double, "bold")
- if found {
- return span, true
- }
-
- return telegramDelimited(line, index, line[index:index+1], "italic")
- default:
- return TelegramMarkdownSpan{}, false
- }
-}
-
-// telegramCodeSpan returns the inline code that starts at index: a run of
-// backticks, the code, and a run of the same length. One space on each side of
-// the code is left out, as in Markdown.
-func telegramCodeSpan(line string, index int) (TelegramMarkdownSpan, bool) {
- run := telegramBackticks(line[index:])
- for search := index + run; search < len(line); {
- next := strings.IndexByte(line[search:], '`')
- if next < 0 {
- break
- }
-
- next += search
- length := telegramBackticks(line[next:])
- if length != run {
- search = next + length
- continue
- }
-
- content := line[index+run : next]
- if strings.TrimSpace(content) == "" {
- break
- }
-
- if len(content) > 1 && content[0] == ' ' && content[len(content)-1] == ' ' {
- content = content[1 : len(content)-1]
- }
-
- return TelegramMarkdownSpan{
- kind: "code",
- content: content,
- end: next + run,
- }, true
- }
-
- return TelegramMarkdownSpan{}, false
-}
-
-// telegramLinkSpan returns the link [text](url) that starts at index. The text
-// can hold brackets in pairs, and the URL holds no space.
-func telegramLinkSpan(line string, index int) (TelegramMarkdownSpan, bool) {
- depth := 0
- for closing := index; closing < len(line); closing++ {
- switch line[closing] {
- case '\\':
- closing++
- case '[':
- depth++
- case ']':
- depth--
- }
-
- if depth > 0 {
- continue
- }
-
- rest := line[closing+1:]
- if closing == index+1 || !strings.HasPrefix(rest, "(") {
- return TelegramMarkdownSpan{}, false
- }
-
- url, _, found := strings.Cut(rest[1:], ")")
- if !found || url == "" || strings.ContainsAny(url, " \t") {
- return TelegramMarkdownSpan{}, false
- }
-
- return TelegramMarkdownSpan{
- kind: "text_link",
- content: line[index+1 : closing],
- end: closing + 2 + len(url) + 1,
- url: url,
- }, true
- }
-
- return TelegramMarkdownSpan{}, false
-}
-
-// telegramDelimited returns the span of kind between the mark at index and the
-// same mark later in line. An opening mark is followed by a character that is
-// not a space, and a closing mark follows one. A single mark is not a part of a
-// double one. A mark of underscores does not open or close inside a word, so
-// snake_case stays as text. A code span inside the content is skipped whole.
-func telegramDelimited(line string, index int, mark, kind string) (TelegramMarkdownSpan, bool) {
- char := mark[0]
- after := index + len(mark)
- if !strings.HasPrefix(line[index:], mark) || after >= len(line) || telegramIsSpace(line[after]) {
- return TelegramMarkdownSpan{}, false
- }
-
- if len(mark) == 1 && (line[after] == char || index > 0 && line[index-1] == char) {
- return TelegramMarkdownSpan{}, false
- }
-
- if char == '_' && index > 0 && telegramWordCharBefore(line[:index]) {
- return TelegramMarkdownSpan{}, false
- }
-
- for closing := after + 1; closing < len(line); closing++ {
- switch {
- case line[closing] == '\\':
- closing++
- continue
- case line[closing] == '`':
- span, found := telegramCodeSpan(line, closing)
- if found {
- closing = span.end - 1
- }
-
- continue
- case !strings.HasPrefix(line[closing:], mark) || telegramIsSpace(line[closing-1]):
- continue
- }
-
- end := closing + len(mark)
- if len(mark) == 1 && (line[closing-1] == char || end < len(line) && line[end] == char) {
- continue
- }
-
- if char == '_' && telegramWordChar(line[end:]) {
- continue
- }
-
- return TelegramMarkdownSpan{
- kind: kind,
- content: line[after:closing],
- end: end,
- }, true
- }
-
- return TelegramMarkdownSpan{}, false
-}
-
-// telegramBackticks returns the length of the run of backticks at the start of
-// text.
-func telegramBackticks(text string) int {
- return len(text) - len(strings.TrimLeft(text, "`"))
-}
-
-// telegramIsSpace tells if char is a space, a tab, or a line break.
-func telegramIsSpace(char byte) bool {
- return char == ' ' || char == '\t' || char == '\n' || char == '\r'
-}
-
-// telegramWordCharBefore tells if the character that ends text is a letter or a
-// digit.
-func telegramWordCharBefore(text string) bool {
- char, _ := utf8.DecodeLastRuneInString(text)
- return text != "" && (unicode.IsLetter(char) || unicode.IsDigit(char))
-}