missing files

This commit is contained in:
Niko Abeler 2022-08-27 23:01:14 +02:00
parent b6e996c3d9
commit 3bd168de00
2 changed files with 119 additions and 0 deletions

83
webmention.go Normal file
View File

@ -0,0 +1,83 @@
package owl
import (
"bytes"
"errors"
"net/http"
"strings"
"golang.org/x/net/html"
)
type HttpRetriever interface {
Get(url string) ([]byte, error)
}
type OwlHttpRetriever struct{}
type ParsedHEntry struct {
Title string
}
func (ret *OwlHttpRetriever) Get(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, err
}
var data []byte
_, err = resp.Body.Read(data)
// TODO: encoding
return data, err
}
func collectText(n *html.Node, buf *bytes.Buffer) {
if n.Type == html.TextNode {
buf.WriteString(n.Data)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
collectText(c, buf)
}
}
func ParseHEntry(data []byte) (ParsedHEntry, error) {
doc, err := html.Parse(strings.NewReader(string(data)))
if err != nil {
return ParsedHEntry{}, err
}
var interpretHFeed func(*html.Node, *ParsedHEntry, bool) (ParsedHEntry, error)
interpretHFeed = func(n *html.Node, curr *ParsedHEntry, parent bool) (ParsedHEntry, error) {
attrs := n.Attr
for _, attr := range attrs {
if attr.Key == "class" && strings.Contains(attr.Val, "p-name") {
buf := &bytes.Buffer{}
collectText(n, buf)
curr.Title = buf.String()
return *curr, nil
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
interpretHFeed(c, curr, false)
}
return *curr, nil
}
var findHFeed func(*html.Node) (ParsedHEntry, error)
findHFeed = func(n *html.Node) (ParsedHEntry, error) {
attrs := n.Attr
for _, attr := range attrs {
if attr.Key == "class" && strings.Contains(attr.Val, "h-entry") {
return interpretHFeed(n, &ParsedHEntry{}, true)
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
entry, err := findHFeed(c)
if err == nil {
return entry, nil
}
}
return ParsedHEntry{}, errors.New("no h-entry found")
}
return findHFeed(doc)
}

36
webmention_test.go Normal file
View File

@ -0,0 +1,36 @@
package owl_test
import (
"h4kor/owl-blogs"
"testing"
)
//
// https://www.w3.org/TR/webmention/#h-webmention-verification
//
func TestParseValidHEntry(t *testing.T) {
html := []byte("<div class=\"h-entry\"><div class=\"p-name\">Foo</div></div>")
entry, err := owl.ParseHEntry(html)
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if entry.Title != "Foo" {
t.Errorf("Wrong Title. Expected %v, got %v", "Foo", entry.Title)
}
}
func TestParseValidHEntryWithoutTitle(t *testing.T) {
html := []byte("<div class=\"h-entry\"></div><div class=\"p-name\">Foo</div>")
entry, err := owl.ParseHEntry(html)
if err != nil {
t.Errorf("Unable to parse feed: %v", err)
}
if entry.Title != "" {
t.Errorf("Wrong Title. Expected %v, got %v", "Foo", entry.Title)
}
}