* Implement function to prune string keeping HTML closing tags Fixes #1587 * change const name remove unneeded comment * move pruneHTML to separated file * move const back to telegram.go * Add unit tests for string array manipulation and HTML pruning Introduce comprehensive test cases for stringArr methods (Push, Pop, Unshift, Shift, String) to ensure correct behavior and state management. Additionally, add tests for HTML pruning functions (pruneHTML, pruneStringToWord) to validate handling of length constraints and formatting scenarios. * Improve behavior * Fix pruneHTML to count visible text only, add parent text pruning - Fix bug where HTML tags were counted toward the character limit instead of only visible text content - Add pruning for parent comment text in Telegram notifications - Simplify pruneStringToWord using strings.LastIndex - Remove unused stringArr type and its tests - Consolidate and simplify test cases --------- Co-authored-by: Umputun <umputun@gmail.com> Co-authored-by: Dmitry Verkhoturov <paskal.07@gmail.com>
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package notify
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
// pruneHTML prunes string keeping HTML closing tags.
|
|
// maxLength applies to visible text only, not HTML tags.
|
|
func pruneHTML(htmlText string, maxLength int) string {
|
|
var result strings.Builder
|
|
var endTokens []string
|
|
visibleLen := 0
|
|
|
|
suffix := "..."
|
|
suffixLen := len(suffix)
|
|
|
|
tokenizer := html.NewTokenizer(strings.NewReader(htmlText))
|
|
for {
|
|
if tokenizer.Next() == html.ErrorToken {
|
|
return result.String()
|
|
}
|
|
token := tokenizer.Token()
|
|
|
|
switch token.Type {
|
|
case html.CommentToken, html.DoctypeToken:
|
|
continue
|
|
|
|
case html.StartTagToken:
|
|
endTokens = append([]string{fmt.Sprintf("</%s>", token.Data)}, endTokens...)
|
|
result.WriteString(token.String())
|
|
|
|
case html.EndTagToken:
|
|
if len(endTokens) > 0 {
|
|
endTokens = endTokens[1:]
|
|
}
|
|
result.WriteString(token.String())
|
|
|
|
case html.SelfClosingTagToken:
|
|
result.WriteString(token.String())
|
|
|
|
case html.TextToken:
|
|
text := token.String()
|
|
if visibleLen+len(text)+suffixLen > maxLength {
|
|
remaining := maxLength - visibleLen - suffixLen
|
|
text = pruneStringToWord(text, remaining)
|
|
result.WriteString(text)
|
|
result.WriteString(suffix)
|
|
for _, endTag := range endTokens {
|
|
result.WriteString(endTag)
|
|
}
|
|
return result.String()
|
|
}
|
|
visibleLen += len(text)
|
|
result.WriteString(text)
|
|
}
|
|
}
|
|
}
|
|
|
|
// pruneStringToWord prunes string to specified length respecting word boundaries
|
|
func pruneStringToWord(text string, maxLength int) string {
|
|
if maxLength <= 0 {
|
|
return ""
|
|
}
|
|
if len(text) <= maxLength {
|
|
return text
|
|
}
|
|
|
|
// find last space at or before maxLength to cut at word boundary
|
|
lastSpace := strings.LastIndex(text[:maxLength+1], " ")
|
|
if lastSpace <= 0 {
|
|
return ""
|
|
}
|
|
return text[:lastSpace]
|
|
}
|