10 Commits

23 changed files with 114 additions and 41 deletions

22
atom.go
View File

@ -1,10 +1,12 @@
package atomfeed
package atom
import (
"fmt"
"mime"
"regexp"
"strings"
"github.com/google/uuid"
"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
// bool.
func isValidMediaType(mediaType string) bool {
_, _, err := mime.ParseMediaType(mediaType)
return err == nil
mediaType, _, err := mime.ParseMediaType(mediaType)
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.
@ -70,3 +81,8 @@ func isValidLanguageTag(tag LanguageTag) bool {
_, err := language.Parse(string(tag))
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()))
}

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import "fmt"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"fmt"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"
@ -34,10 +34,15 @@ type Entry struct {
// 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
// atom:author element itself.
func (e *Entry) checkAuthors() error {
func (e *Entry) checkAuthors(authorInFeed bool) error {
if e.Authors == nil {
if e.Source.Authors == nil {
return errors.New("no authors set in entry")
if !authorInFeed {
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")
}
}
} else {
for i, a := range e.Authors {
@ -64,6 +69,54 @@ func NewEntry(title string) (*Entry, error) {
}, nil
}
// AddAuthor adds the Person as an author to the Entry.
func (e *Entry) AddAuthor(p *Person) {
if e.Authors == nil {
e.Authors = make([]*Person, 1)
e.Authors[0] = p
} else {
e.Authors = append(e.Authors, p)
}
e.Updated.DateTime = DateTime(time.Now())
}
// AddCategory adds the Category to the Entry.
func (e *Entry) AddCategory(c *Category) {
if e.Categories == nil {
e.Categories = make([]*Category, 1)
e.Categories[0] = c
} else {
e.Categories = append(e.Categories, c)
}
e.Updated.DateTime = DateTime(time.Now())
}
// AddContributor adds the Person as a contributor to the Entry.
func (e *Entry) AddContributor(c *Person) {
if e.Contributors == nil {
e.Contributors = make([]*Person, 1)
e.Contributors[0] = c
} else {
e.Contributors = append(e.Contributors, c)
}
e.Updated.DateTime = DateTime(time.Now())
}
// AddLink adds the Link to the Entry.
func (e *Entry) AddLink(l *Link) {
if e.Links == nil {
e.Links = make([]*Link, 1)
e.Links[0] = l
} else {
e.Links = append(e.Links, l)
}
e.Updated.DateTime = DateTime(time.Now())
}
// AddExtension adds the ExtensionElement to the Entry.
func (e *Entry) AddExtension(x *ExtensionElement) {
if e.Extensions == nil {
@ -87,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)
}
@ -160,7 +213,7 @@ func (e *Entry) Check() error {
// is not an XML media type [RFC3023], does not begin with "text/", and
// does not end with "/xml" or "+xml".
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)
}
}

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"encoding/xml"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"encoding/xml"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"encoding/xml"
@ -128,7 +128,7 @@ func (f *Feed) Check() error {
// least one atom:author element.
if f.Authors == nil {
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)
}
}

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

8
id.go
View File

@ -1,10 +1,8 @@
package atomfeed
package atom
import (
"errors"
"fmt"
"github.com/google/uuid"
)
type ID struct {
@ -13,8 +11,8 @@ type ID struct {
}
// NewID creates a new ID. It returns a *ID.
func NewID() *ID {
return &ID{URI: IRI(fmt.Sprint("urn:uuid:", uuid.New()))}
func NewID(id string) *ID {
return &ID{URI: IRI(id)}
}
// Check checks the ID for incompatibilities with RFC4287. It returns an error.

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

20
link.go
View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"
@ -38,16 +38,22 @@ func (l *Link) Check() error {
}
}
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 l.Rel != "" {
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)) {
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
if l.Type != "" {
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) {
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
if 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 {

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import "errors"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import "fmt"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"fmt"

View File

@ -1,4 +1,4 @@
package atomfeed
package atom
import (
"encoding/xml"