2024-10-13 17:19:40 +02:00
|
|
|
package atomfeed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
*CommonAttributes
|
|
|
|
Title *Text `xml:"title,attr,omitempty"`
|
|
|
|
Content *Content `xml:"content"`
|
|
|
|
Href URI `xml:"href,attr"`
|
|
|
|
Rel string `xml:"rel,attr,omitempty"`
|
|
|
|
Type MediaType `xml:"type,attr,omitempty"`
|
|
|
|
HrefLang LanguageTag `xml:"hreflang,attr,omitempty"`
|
|
|
|
Length uint `xml:"length,attr,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Link) Check() error {
|
|
|
|
if l.Href == "" {
|
|
|
|
return errors.New("href attribute of link empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Title != nil {
|
|
|
|
if err := (*l.Title).Check(); err != nil {
|
|
|
|
return fmt.Errorf("title attribute of link %v: %v", l.Href, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.Content == nil {
|
|
|
|
return fmt.Errorf("no content element of link %v", l.Href)
|
|
|
|
} else {
|
|
|
|
if err := (*l.Content).Check(); err != nil {
|
|
|
|
return fmt.Errorf("content element of link %v: %v", l.Href, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-15 18:44:02 +02:00
|
|
|
|
|
|
|
// atom:feed/entry elements MUST NOT contain more than one atom:link element
|
|
|
|
// with a rel attribute value of "alternate" that has the same combination of
|
|
|
|
// type and hreflang attribute values.
|
|
|
|
func hasAlternateDuplicateLinks(l []*Link) bool {
|
|
|
|
linkMap := make(map[string]bool)
|
|
|
|
|
|
|
|
for _, link := range l {
|
|
|
|
if link.Rel == "alternate" {
|
|
|
|
key := fmt.Sprint(link.Type, "|", link.HrefLang)
|
|
|
|
if linkMap[key] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
linkMap[key] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|