mirror of
https://github.com/nkanaev/yarr.git
synced 2025-09-16 11:20:14 +00:00
reorganizing content-related packages
This commit is contained in:
41
src/content/htmlutil/utils.go
Normal file
41
src/content/htmlutil/utils.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package htmlutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func HTML(node *html.Node) string {
|
||||
writer := strings.Builder{}
|
||||
html.Render(&writer, node)
|
||||
return writer.String()
|
||||
}
|
||||
|
||||
func InnerHTML(node *html.Node) string {
|
||||
writer := strings.Builder{}
|
||||
for c := node.FirstChild; c != nil; c = c.NextSibling {
|
||||
html.Render(&writer, c)
|
||||
}
|
||||
return writer.String()
|
||||
}
|
||||
|
||||
func Attr(node *html.Node, key string) string {
|
||||
for _, a := range node.Attr {
|
||||
if strings.EqualFold(a.Key, key) {
|
||||
return a.Val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func Text(node *html.Node) string {
|
||||
text := make([]string, 0)
|
||||
isTextNode := func(n *html.Node) bool {
|
||||
return n.Type == html.TextNode
|
||||
}
|
||||
for _, n := range FindNodes(node, isTextNode) {
|
||||
text = append(text, strings.TrimSpace(n.Data))
|
||||
}
|
||||
return strings.Join(text, " ")
|
||||
}
|
Reference in New Issue
Block a user