Reduce Telegram tool message noise
This commit is contained in:
@@ -66,6 +66,108 @@ func SummaryDetailsHTMLLimited(summary, details string, limit int) string {
|
||||
return SummaryDetailsHTML(summary, suffix)
|
||||
}
|
||||
|
||||
func FitHTMLMessage(htmlText string, limit int) string {
|
||||
if limit <= 0 {
|
||||
limit = TelegramHTMLMessageLimit
|
||||
}
|
||||
htmlText = strings.TrimSpace(htmlText)
|
||||
if len([]rune(htmlText)) <= limit {
|
||||
return htmlText
|
||||
}
|
||||
|
||||
const open = "<blockquote expandable>"
|
||||
const close = "</blockquote>"
|
||||
const truncatedQuote = "...[truncated]"
|
||||
for len([]rune(htmlText)) > limit {
|
||||
contentStart, contentEnd, content := largestBlockquoteContent(htmlText, open, close)
|
||||
if contentStart < 0 {
|
||||
return truncateHTMLText(htmlText, limit)
|
||||
}
|
||||
|
||||
contentRunes := []rune(strings.TrimSpace(html.UnescapeString(content)))
|
||||
over := len([]rune(htmlText)) - limit
|
||||
keep := len(contentRunes) - over - 80
|
||||
if keep < 0 {
|
||||
keep = 0
|
||||
}
|
||||
if keep >= len(contentRunes) {
|
||||
keep = len(contentRunes) - 1
|
||||
}
|
||||
replacementText := truncatedQuote
|
||||
if keep > 0 {
|
||||
replacementText = strings.TrimSpace(string(contentRunes[:keep])) + "\n" + truncatedQuote
|
||||
}
|
||||
replacement := EscapeHTML(replacementText)
|
||||
if replacement == content {
|
||||
return summaryOnlyHTML(htmlText, limit)
|
||||
}
|
||||
htmlText = htmlText[:contentStart] + replacement + htmlText[contentEnd:]
|
||||
}
|
||||
return htmlText
|
||||
}
|
||||
|
||||
func largestBlockquoteContent(htmlText, open, close string) (int, int, string) {
|
||||
bestStart := -1
|
||||
bestEnd := -1
|
||||
bestContent := ""
|
||||
searchFrom := 0
|
||||
for {
|
||||
start := strings.Index(htmlText[searchFrom:], open)
|
||||
if start < 0 {
|
||||
break
|
||||
}
|
||||
start += searchFrom
|
||||
contentStart := start + len(open)
|
||||
end := strings.Index(htmlText[contentStart:], close)
|
||||
if end < 0 {
|
||||
break
|
||||
}
|
||||
end += contentStart
|
||||
content := htmlText[contentStart:end]
|
||||
if bestStart < 0 || len([]rune(content)) > len([]rune(bestContent)) {
|
||||
bestStart = contentStart
|
||||
bestEnd = end
|
||||
bestContent = content
|
||||
}
|
||||
searchFrom = end + len(close)
|
||||
}
|
||||
return bestStart, bestEnd, bestContent
|
||||
}
|
||||
|
||||
func summaryOnlyHTML(htmlText string, limit int) string {
|
||||
const open = "<blockquote expandable>"
|
||||
const close = "</blockquote>"
|
||||
for {
|
||||
start := strings.Index(htmlText, open)
|
||||
if start < 0 {
|
||||
break
|
||||
}
|
||||
end := strings.Index(htmlText[start+len(open):], close)
|
||||
if end < 0 {
|
||||
break
|
||||
}
|
||||
end += start + len(open) + len(close)
|
||||
htmlText = strings.TrimRight(htmlText[:start], "\n") + "\n" + strings.TrimLeft(htmlText[end:], "\n")
|
||||
}
|
||||
return truncateHTMLText(strings.TrimSpace(htmlText), limit)
|
||||
}
|
||||
|
||||
func truncateHTMLText(htmlText string, limit int) string {
|
||||
if limit <= 0 {
|
||||
limit = TelegramHTMLMessageLimit
|
||||
}
|
||||
suffix := "\n...[truncated]"
|
||||
runes := []rune(htmlText)
|
||||
if len(runes) <= limit {
|
||||
return htmlText
|
||||
}
|
||||
keep := limit - len([]rune(suffix))
|
||||
if keep < 0 {
|
||||
keep = 0
|
||||
}
|
||||
return string(runes[:keep]) + suffix
|
||||
}
|
||||
|
||||
func ChunkText(text string, max int) []string {
|
||||
if max <= 0 {
|
||||
max = TelegramMessageLimit
|
||||
|
||||
Reference in New Issue
Block a user