Compare commits
11 Commits
v0.1.4
...
4b97bf7fdc
Author | SHA1 | Date | |
---|---|---|---|
4b97bf7fdc | |||
86785be588 | |||
28f5616f76 | |||
657624cbd6 | |||
b496ac3691 | |||
13e7a16178 | |||
3734a3eb3d | |||
e0902d9ba4 | |||
434783165b | |||
46138d302b | |||
6b9d5be734 |
20
atom.go
20
atom.go
@ -1,10 +1,12 @@
|
|||||||
package atom
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -61,8 +63,17 @@ func isXMLMediaType(mediaType string) bool {
|
|||||||
// isValidMediaType checks whether a string is a valid media type. It returns a
|
// isValidMediaType checks whether a string is a valid media type. It returns a
|
||||||
// bool.
|
// bool.
|
||||||
func isValidMediaType(mediaType string) bool {
|
func isValidMediaType(mediaType string) bool {
|
||||||
_, _, err := mime.ParseMediaType(mediaType)
|
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||||
return err == nil
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
typeParts := strings.Split(mediaType, "/")
|
||||||
|
if len(typeParts) != 2 || typeParts[0] == "" || typeParts[1] == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// isValidLanguageTag checks whether a LanguageTag is valid. It returns a bool.
|
// isValidLanguageTag checks whether a LanguageTag is valid. It returns a bool.
|
||||||
@ -70,3 +81,8 @@ func isValidLanguageTag(tag LanguageTag) bool {
|
|||||||
_, err := language.Parse(string(tag))
|
_, err := language.Parse(string(tag))
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewURN generates an new valid IRI based on a UUID. It returns an IRI.
|
||||||
|
func NewURN() IRI {
|
||||||
|
return IRI(fmt.Sprint("urn:uuid:", uuid.New()))
|
||||||
|
}
|
||||||
|
2
date.go
2
date.go
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
type Date struct {
|
type Date struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
DateTime string
|
DateTime string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// DateTime formats a time.Time to string formated as defined by RFC3339. It
|
// DateTime formats a time.Time to string formated as defined by RFC3339. It
|
||||||
|
20
entry.go
20
entry.go
@ -34,13 +34,15 @@ type Entry struct {
|
|||||||
// the atom:entry contains an atom:source element that contains an atom:author
|
// the atom:entry contains an atom:source element that contains an atom:author
|
||||||
// element or, in an Atom Feed Document, the atom:feed element contains an
|
// element or, in an Atom Feed Document, the atom:feed element contains an
|
||||||
// atom:author element itself.
|
// atom:author element itself.
|
||||||
func (e *Entry) checkAuthors() error {
|
func (e *Entry) checkAuthors(authorInFeed bool) error {
|
||||||
if e.Authors == nil {
|
if e.Authors == nil {
|
||||||
if e.Source == nil {
|
if !authorInFeed {
|
||||||
return errors.New("no authors set in entry")
|
if e.Source == nil {
|
||||||
}
|
return errors.New("no authors set in entry")
|
||||||
if e.Source.Authors == nil {
|
}
|
||||||
return errors.New("no authors set in entry")
|
if e.Source.Authors == nil {
|
||||||
|
return errors.New("no authors set in entry")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for i, a := range e.Authors {
|
for i, a := range e.Authors {
|
||||||
@ -61,7 +63,7 @@ func NewEntry(title string) (*Entry, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Entry{
|
return &Entry{
|
||||||
ID: NewID(),
|
ID: NewID(NewURN()),
|
||||||
Title: text,
|
Title: text,
|
||||||
Updated: NewDate(time.Now()),
|
Updated: NewDate(time.Now()),
|
||||||
}, nil
|
}, nil
|
||||||
@ -138,7 +140,7 @@ func (e *Entry) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.checkAuthors(); err != nil {
|
if err := e.checkAuthors(true); err != nil {
|
||||||
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,7 +213,7 @@ func (e *Entry) Check() error {
|
|||||||
// is not an XML media type [RFC3023], does not begin with "text/", and
|
// is not an XML media type [RFC3023], does not begin with "text/", and
|
||||||
// does not end with "/xml" or "+xml".
|
// does not end with "/xml" or "+xml".
|
||||||
mediaType := e.Content.getType()
|
mediaType := e.Content.getType()
|
||||||
if !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
|
if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
|
||||||
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
|
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
4
feed.go
4
feed.go
@ -34,7 +34,7 @@ func NewFeed(title string) (*Feed, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Feed{
|
return &Feed{
|
||||||
ID: NewID(),
|
ID: NewID(NewURN()),
|
||||||
Title: text,
|
Title: text,
|
||||||
Updated: NewDate(time.Now()),
|
Updated: NewDate(time.Now()),
|
||||||
}, nil
|
}, nil
|
||||||
@ -128,7 +128,7 @@ func (f *Feed) Check() error {
|
|||||||
// least one atom:author element.
|
// least one atom:author element.
|
||||||
if f.Authors == nil {
|
if f.Authors == nil {
|
||||||
for _, e := range f.Entries {
|
for _, e := range f.Entries {
|
||||||
if err := e.checkAuthors(); err != nil {
|
if err := e.checkAuthors(false); err != nil {
|
||||||
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err)
|
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
id.go
8
id.go
@ -3,18 +3,16 @@ package atom
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ID struct {
|
type ID struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
URI IRI `xml:"uri"`
|
URI IRI `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewID creates a new ID. It returns a *ID.
|
// NewID creates a new ID. It returns a *ID.
|
||||||
func NewID() *ID {
|
func NewID(id IRI) *ID {
|
||||||
return &ID{URI: IRI(fmt.Sprint("urn:uuid:", uuid.New()))}
|
return &ID{URI: IRI(id)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
type InlineOtherContent struct {
|
type InlineOtherContent struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
AnyElement any `xml:"anyelement,omitempty"`
|
AnyElement any `xml:",chardata"`
|
||||||
Type MediaType `xml:"type,attr,omitempty"`
|
Type MediaType `xml:"type,attr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,8 +8,8 @@ import (
|
|||||||
|
|
||||||
type InlineTextContent struct {
|
type InlineTextContent struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
||||||
Texts []string `xml:"texts,omitempty"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// newInlineTextContent creates a new InlineTextContent. It returns a
|
// newInlineTextContent creates a new InlineTextContent. It returns a
|
||||||
@ -19,23 +19,11 @@ func newInlineTextContent(mediaType string, content any) (*InlineTextContent, er
|
|||||||
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
|
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
|
||||||
}
|
}
|
||||||
|
|
||||||
texts := make([]string, 0)
|
if reflect.TypeOf(content).Kind() != reflect.String {
|
||||||
|
|
||||||
t := reflect.TypeOf(content)
|
|
||||||
switch t.Kind() {
|
|
||||||
case reflect.Slice:
|
|
||||||
if t.Elem().Kind() == reflect.String {
|
|
||||||
for _, t := range content.([]string) {
|
|
||||||
texts = append(texts, t)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case reflect.String:
|
|
||||||
texts = append(texts, content.(string))
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
|
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &InlineTextContent{Type: mediaType, Texts: texts}, nil
|
return &InlineTextContent{Type: mediaType, Text: content.(string)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// isContent checks whether the InlineTextContent is a Content. It returns a
|
// isContent checks whether the InlineTextContent is a Content. It returns a
|
||||||
|
18
link.go
18
link.go
@ -38,16 +38,22 @@ func (l *Link) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.Contains(l.Rel, ":") || !isValidIRI(IRI(l.Rel)) {
|
if l.Rel != "" {
|
||||||
return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href)
|
if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
|
||||||
|
return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isValidMediaType(string(l.Type)) {
|
if l.Type != "" {
|
||||||
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
|
if !isValidMediaType(string(l.Type)) {
|
||||||
|
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isValidLanguageTag(l.HrefLang) {
|
if l.HrefLang != "" {
|
||||||
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
|
if !isValidLanguageTag(l.HrefLang) {
|
||||||
|
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Content == nil {
|
if l.Content == nil {
|
||||||
|
@ -5,7 +5,7 @@ import "errors"
|
|||||||
type PlainText struct {
|
type PlainText struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
||||||
Text string `xml:"text"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// isText checks whether the PlainText is a Text. It returns a bool.
|
// isText checks whether the PlainText is a Text. It returns a bool.
|
||||||
|
Reference in New Issue
Block a user