use HTML tokenizer to parse artist biography

This commit is contained in:
Drew Weymouth
2023-02-01 17:03:24 -08:00
parent 844930f0ae
commit 4bc25c9b0f
4 changed files with 47 additions and 7 deletions
+32
View File
@@ -3,6 +3,10 @@ package util
import (
"fmt"
"math"
"strings"
"fyne.io/fyne/v2/widget"
"golang.org/x/net/html"
)
func SecondsToTimeString(s float64) string {
@@ -15,3 +19,31 @@ func SecondsToTimeString(s float64) string {
return fmt.Sprintf("%3d:%02d", min, sec)
}
func RichTextSegsFromHTMLString(s string) []widget.RichTextSegment {
tokr := html.NewTokenizer(strings.NewReader(s))
var segs []widget.RichTextSegment
var isLink bool
var done bool
for !done {
tt := tokr.Next()
switch {
case tt == html.ErrorToken:
done = true
case tt == html.StartTagToken:
t := tokr.Token()
isLink = t.Data == "a"
case tt == html.EndTagToken:
isLink = false
case tt == html.TextToken:
t := tokr.Token()
// for now, skip displaying Navidrome's "Read more on Last.FM" link
if !isLink {
segs = append(segs, &widget.TextSegment{Text: t.Data})
}
}
}
return segs
}