Compare commits
16 Commits
v0.1.2
...
17aa4b1a15
Author | SHA1 | Date | |
---|---|---|---|
17aa4b1a15 | |||
040d7a6b7b | |||
705b651f08 | |||
4b97bf7fdc | |||
86785be588 | |||
28f5616f76 | |||
657624cbd6 | |||
b496ac3691 | |||
13e7a16178 | |||
3734a3eb3d | |||
e0902d9ba4 | |||
434783165b | |||
46138d302b | |||
6b9d5be734 | |||
2ef8d8c9df | |||
97fe20f364 |
20
atom.go
20
atom.go
@ -1,10 +1,12 @@
|
||||
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()))
|
||||
}
|
||||
|
23
category.go
23
category.go
@ -8,19 +8,14 @@ import (
|
||||
|
||||
type Category struct {
|
||||
*CommonAttributes
|
||||
Content Content `xml:"content"` // undefinedContent in RFC4287
|
||||
Term string `xml:"term,attr"`
|
||||
Scheme IRI `xml:"scheme,attr,omitempty"`
|
||||
Label string `xml:"label,attr,omitempty"`
|
||||
Content string `xml:",any"` // undefinedContent in RFC4287
|
||||
Term string `xml:"term,attr"`
|
||||
Scheme IRI `xml:"scheme,attr,omitempty"`
|
||||
Label string `xml:"label,attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewCategory creates a new Category. It returns a *Category and an error.
|
||||
func NewCategory(term string) (*Category, error) {
|
||||
content, err := NewContent(InlineText, "", "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating content element: %v", err)
|
||||
}
|
||||
|
||||
func NewCategory(term, content string) (*Category, error) {
|
||||
return &Category{Term: term, Content: content}, nil
|
||||
}
|
||||
|
||||
@ -46,12 +41,8 @@ func (c *Category) Check() error {
|
||||
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
|
||||
}
|
||||
|
||||
if c.Content == nil {
|
||||
return errors.New("no content element of category")
|
||||
} else {
|
||||
if err := c.Content.Check(); err != nil {
|
||||
return fmt.Errorf("content element of category: %v", err)
|
||||
}
|
||||
if c.Content == "" {
|
||||
return errors.New("content element of category empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
2
date.go
2
date.go
@ -7,7 +7,7 @@ import (
|
||||
|
||||
type Date struct {
|
||||
*CommonAttributes
|
||||
DateTime string
|
||||
DateTime string `xml:",chardata"`
|
||||
}
|
||||
|
||||
// DateTime formats a time.Time to string formated as defined by RFC3339. It
|
||||
|
65
entry.go
65
entry.go
@ -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 {
|
||||
@ -58,12 +63,60 @@ func NewEntry(title string) (*Entry, error) {
|
||||
}
|
||||
|
||||
return &Entry{
|
||||
ID: NewID(),
|
||||
ID: NewID(NewURN()),
|
||||
Title: text,
|
||||
Updated: NewDate(time.Now()),
|
||||
}, 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)
|
||||
}
|
||||
}
|
||||
|
4
feed.go
4
feed.go
@ -34,7 +34,7 @@ func NewFeed(title string) (*Feed, error) {
|
||||
}
|
||||
|
||||
return &Feed{
|
||||
ID: NewID(),
|
||||
ID: NewID(NewURN()),
|
||||
Title: text,
|
||||
Updated: NewDate(time.Now()),
|
||||
}, nil
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
8
id.go
8
id.go
@ -3,18 +3,16 @@ package atom
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ID struct {
|
||||
*CommonAttributes
|
||||
URI IRI `xml:"uri"`
|
||||
URI IRI `xml:",chardata"`
|
||||
}
|
||||
|
||||
// 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 IRI) *ID {
|
||||
return &ID{URI: IRI(id)}
|
||||
}
|
||||
|
||||
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
type InlineOtherContent struct {
|
||||
*CommonAttributes
|
||||
AnyElement any `xml:"anyelement,omitempty"`
|
||||
AnyElement any `xml:",chardata"`
|
||||
Type MediaType `xml:"type,attr,omitempty"`
|
||||
}
|
||||
|
||||
|
@ -3,13 +3,12 @@ package atom
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type InlineTextContent struct {
|
||||
*CommonAttributes
|
||||
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
||||
Texts []string `xml:"texts,omitempty"`
|
||||
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
||||
Text string `xml:",chardata"`
|
||||
}
|
||||
|
||||
// newInlineTextContent creates a new InlineTextContent. It returns a
|
||||
@ -19,23 +18,12 @@ func newInlineTextContent(mediaType string, content any) (*InlineTextContent, er
|
||||
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
|
||||
}
|
||||
|
||||
texts := make([]string, 0)
|
||||
|
||||
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:
|
||||
text, ok := content.(string)
|
||||
if !ok {
|
||||
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: text}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the InlineTextContent is a Content. It returns a
|
||||
|
@ -3,13 +3,12 @@ package atom
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type InlineXHTMLContent struct {
|
||||
*CommonAttributes
|
||||
Type string `xml:"type,attr"`
|
||||
XHTMLDiv string `xml:"xhtmldiv"`
|
||||
XHTMLDiv XHTMLDiv
|
||||
}
|
||||
|
||||
// newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a
|
||||
@ -19,11 +18,12 @@ func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent,
|
||||
return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
|
||||
}
|
||||
|
||||
if reflect.TypeOf(content).Kind() != reflect.String {
|
||||
xhtmlDiv, ok := content.(XHTMLDiv)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
|
||||
}
|
||||
|
||||
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: content.(string)}, nil
|
||||
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: xhtmlDiv}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the InlineXHTMLContent is a Content. It returns a
|
||||
@ -44,8 +44,8 @@ func (i *InlineXHTMLContent) Check() error {
|
||||
return errors.New("type attribute of inline xhtml content must be xhtml")
|
||||
}
|
||||
|
||||
if i.XHTMLDiv == "" {
|
||||
return errors.New("xhtmlDiv element of inline xhtml content empty")
|
||||
if err := i.XHTMLDiv.Check(); err != nil {
|
||||
return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
45
link.go
45
link.go
@ -9,7 +9,7 @@ import (
|
||||
type Link struct {
|
||||
*CommonAttributes
|
||||
Title string `xml:"title,attr,omitempty"`
|
||||
Content Content `xml:"content"` // undefinedContent in RFC4287
|
||||
Content string `xml:",any"` // undefinedContent in RFC4287
|
||||
Href IRI `xml:"href,attr"`
|
||||
Rel string `xml:"rel,attr,omitempty"`
|
||||
Type MediaType `xml:"type,attr,omitempty"`
|
||||
@ -18,12 +18,7 @@ type Link struct {
|
||||
}
|
||||
|
||||
// NewLink creates a new Link. It returns a *Link and an error.
|
||||
func NewLink(href string) (*Link, error) {
|
||||
content, err := NewContent(InlineText, "", "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating content element: %v", err)
|
||||
}
|
||||
|
||||
func NewLink(href, content string) (*Link, error) {
|
||||
return &Link{Href: IRI(href), Content: content}, nil
|
||||
}
|
||||
|
||||
@ -38,26 +33,28 @@ 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 !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.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)
|
||||
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 l.Type != "" {
|
||||
if !isValidMediaType(string(l.Type)) {
|
||||
return fmt.Errorf("type attribute %v of link %v invalid media type", l.Type, l.Href)
|
||||
}
|
||||
}
|
||||
|
||||
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 == "" {
|
||||
return errors.New("content element of link empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type OutOfLineContent struct {
|
||||
@ -20,15 +19,16 @@ func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, erro
|
||||
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
|
||||
}
|
||||
|
||||
if reflect.TypeOf(content).Kind() != reflect.String {
|
||||
iri, ok := content.(IRI)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
|
||||
}
|
||||
|
||||
if !isValidIRI(content.(IRI)) {
|
||||
if !isValidIRI(iri) {
|
||||
return nil, errors.New("content not a valid uri")
|
||||
}
|
||||
|
||||
return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(IRI)}, nil
|
||||
return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the OutOfLineContent is a Content. It returns a
|
||||
|
@ -5,7 +5,7 @@ import "errors"
|
||||
type PlainText struct {
|
||||
*CommonAttributes
|
||||
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.
|
||||
|
30
xhtmlDiv.go
Normal file
30
xhtmlDiv.go
Normal file
@ -0,0 +1,30 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type XHTMLDiv struct {
|
||||
XMLName xml.Name `xml:"div"`
|
||||
XMLNS string `xml:"xmlns,attr"`
|
||||
Content string `xml:",innerxml"`
|
||||
}
|
||||
|
||||
// NewXHTMLDiv creates a new XHTMLDiv. It returns a *XHTMLDiv.
|
||||
func NewXHTMLDiv(content string) *XHTMLDiv {
|
||||
return &XHTMLDiv{
|
||||
XMLNS: "http://www.w3.org/1999/xhtml",
|
||||
Content: content,
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the XHTMLDiv for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (x *XHTMLDiv) Check() error {
|
||||
if x.XMLNS != "http://www.w3.org/1999/xhtml" {
|
||||
return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
12
xhtmlText.go
12
xhtmlText.go
@ -1,16 +1,10 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type XHTMLDiv struct {
|
||||
XMLName xml.Name `xml:"div"`
|
||||
XMLNS string `xml:"xmlns,attr"`
|
||||
Content string `xml:",innerxml"`
|
||||
}
|
||||
|
||||
type XHTMLText struct {
|
||||
*CommonAttributes
|
||||
Type string `xml:"type,attr"` // Must be xhtml
|
||||
@ -27,8 +21,8 @@ func (x *XHTMLText) Check() error {
|
||||
return errors.New("type attribute of xhtml text must be xhtml")
|
||||
}
|
||||
|
||||
if x.XHTMLDiv.XMLNS != "http://www.w3.org/1999/xhtml" {
|
||||
return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml")
|
||||
if err := x.XHTMLDiv.Check(); err != nil {
|
||||
return fmt.Errorf("xhtml div element %v of xhtml text %v: %v", x.XHTMLDiv, x, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user