Compare commits
7 Commits
574e2331a9
...
7f30fd5411
Author | SHA1 | Date | |
---|---|---|---|
7f30fd5411 | |||
a7a6b5c711 | |||
57db4178d0 | |||
960889f9e7 | |||
f4dfd6d060 | |||
d4e7bce5e2 | |||
411cd89b7c |
25
README.md
25
README.md
@ -1,23 +1,24 @@
|
||||
# atom
|
||||
|
||||
An extensible Atom feed generator library that aims to be very close to
|
||||
RFC4287. It diligently checks for compliance with the standard and provides
|
||||
functions for easy creation and extension of elements.
|
||||
An extensible Atom feed generator library that aims to be very close to RFC4287.
|
||||
It diligently checks for compliance with the standard and provides functions for
|
||||
easy creation and extension of elements.
|
||||
|
||||
## Installation
|
||||
|
||||
To install the latest version of the module, use the following command:
|
||||
|
||||
```
|
||||
```sh
|
||||
go get git.streifling.com/jason/atom@latest
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
This library provides easy to use functions to create and extend elements of an
|
||||
Atom feed. The intended way of using it entails using these functions.
|
||||
This library provides convenient functions to safely create and extend elements
|
||||
and attributes of an Atom feed. This is because it can be hard to know all
|
||||
pitfalls of RFC4287. The intended way of using atom is with these functions.
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -60,10 +61,10 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
It is also possible to use this library in a way similar to what other
|
||||
libraries would provide. This, of course, making it easier to make mistakes.
|
||||
It is also possible to use this library in a way similar to what other libraries
|
||||
would provide. This is, of course, making it easier to make mistakes.
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -114,9 +115,9 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
The output of both ways of using it being an RFC4287 compliant Atom feed:
|
||||
The output of both ways of using it is an RFC4287 compliant Atom feed:
|
||||
|
||||
```
|
||||
```xml
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<feed xmlns="http://www.w3.org/2005/Atom">
|
||||
<author>
|
||||
|
37
atom.go
37
atom.go
@ -2,6 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"mime"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -10,18 +11,11 @@ import (
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type (
|
||||
EmailAddress string
|
||||
LanguageTag string
|
||||
MediaType string
|
||||
IRI string
|
||||
)
|
||||
|
||||
// isValidIRI checks whether an IRI is valid or not. It returns a bool.
|
||||
// https://www.w3.org/2011/04/XMLSchema/TypeLibrary-IRI-RFC3987.xsd
|
||||
func isValidIRI(iri IRI) bool {
|
||||
func isValidIRI(iri string) bool {
|
||||
pattern := `((([A-Za-z])[A-Za-z0-9+\-\.]*):((//(((([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------!$&'()*+,;=:]|(%[0-9A-Fa-f][0-9A-Fa-f]))*@))?((\[((((([0-9A-Fa-f]{0,4}:)){6}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|(::(([0-9A-Fa-f]{0,4}:)){5}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|(([0-9A-Fa-f]{0,4})?::(([0-9A-Fa-f]{0,4}:)){4}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:))?[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:)){3}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,2}[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:)){2}(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,3}[0-9A-Fa-f]{0,4}))?::[0-9A-Fa-f]{0,4}:(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,4}[0-9A-Fa-f]{0,4}))?::(([0-9A-Fa-f]{0,4}:[0-9A-Fa-f]{0,4})|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))))|((((([0-9A-Fa-f]{0,4}:)){0,5}[0-9A-Fa-f]{0,4}))?::[0-9A-Fa-f]{0,4})|((((([0-9A-Fa-f]{0,4}:)){0,6}[0-9A-Fa-f]{0,4}))?::))|(v[0-9A-Fa-f]+\.[A-Za-z0-9\-\._~!$&'()*+,;=:]+))\])|(([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5]))\.([0-9]|([1-9][0-9])|(1([0-9]){2})|(2[0-4][0-9])|(25[0-5])))|(([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=]))*)((:[0-9]*))?)((/(([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*)|(/(((([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))+((/(([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*))?)|((([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))+((/(([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@]))*))*)|)((\?(([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@])|[---/?])*))?((#((([A-Za-z0-9\-\._~ -豈-﷏ﷰ-𐀀-𠀀-𰀀------------]|(%[0-9A-Fa-f][0-9A-Fa-f])|[!$&'()*+,;=:@])|/|\?))*))?)`
|
||||
return regexp.MustCompile(pattern).MatchString(string(iri))
|
||||
return regexp.MustCompile(pattern).MatchString(iri)
|
||||
}
|
||||
|
||||
// isCorrectlyEscaped checks whether a string is correctly escaped as per
|
||||
@ -40,8 +34,8 @@ func isCorrectlyEscaped(text string) bool {
|
||||
|
||||
// isCompositeMediaType checks whether a string is a composite media type. It
|
||||
// returns a bool.
|
||||
func isCompositeMediaType(mediaType string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||
func isCompositeMediaType(m string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(m)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@ -51,8 +45,8 @@ func isCompositeMediaType(mediaType string) bool {
|
||||
|
||||
// isXMLMediaType checks whether a string is an xml media type. It returns a
|
||||
// bool.
|
||||
func isXMLMediaType(mediaType string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||
func isXMLMediaType(m string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(m)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@ -62,8 +56,8 @@ func isXMLMediaType(mediaType string) bool {
|
||||
|
||||
// isValidMediaType checks whether a string is a valid media type. It returns a
|
||||
// bool.
|
||||
func isValidMediaType(mediaType string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||
func isValidMediaType(m string) bool {
|
||||
mediaType, _, err := mime.ParseMediaType(m)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@ -77,8 +71,8 @@ func isValidMediaType(mediaType string) bool {
|
||||
}
|
||||
|
||||
// isValidLanguageTag checks whether a LanguageTag is valid. It returns a bool.
|
||||
func isValidLanguageTag(tag LanguageTag) bool {
|
||||
_, err := language.Parse(string(tag))
|
||||
func isValidLanguageTag(languageTag string) bool {
|
||||
_, err := language.Parse(languageTag)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
@ -89,6 +83,11 @@ func isValidAttribute(attribute string) bool {
|
||||
}
|
||||
|
||||
// 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()))
|
||||
func NewURN() string {
|
||||
return fmt.Sprint("urn:uuid:", uuid.New())
|
||||
}
|
||||
|
||||
// Unescape unescapes a string. It returns an IRI.
|
||||
func Unescape(s string) string {
|
||||
return html.UnescapeString(s)
|
||||
}
|
||||
|
19
category.go
19
category.go
@ -2,44 +2,45 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
XMLName xml.Name `xml:"category"`
|
||||
*CommonAttributes
|
||||
Term string `xml:"term,attr"`
|
||||
Scheme IRI `xml:"scheme,attr,omitempty"`
|
||||
Scheme string `xml:"scheme,attr,omitempty"` // IRI
|
||||
Label string `xml:"label,attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewCategory creates a new Category. It returns a *Category.
|
||||
func NewCategory(term string) *Category {
|
||||
return &Category{Term: term}
|
||||
return &Category{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Term: term,
|
||||
}
|
||||
}
|
||||
|
||||
// SetLabel sets the label of the Category.
|
||||
// SetLabel sets the Label attribute of the Category.
|
||||
func (c *Category) SetLabel(label string) {
|
||||
c.Label = html.UnescapeString(label)
|
||||
c.Label = Unescape(label)
|
||||
}
|
||||
|
||||
// Check checks the Category for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (c *Category) Check() error {
|
||||
if c.Term == "" {
|
||||
return errors.New("term attribute of category empty")
|
||||
return fmt.Errorf("term attribute of category %v empty", c)
|
||||
}
|
||||
|
||||
if c.Scheme != "" {
|
||||
if !isValidIRI(c.Scheme) {
|
||||
return fmt.Errorf("scheme attribute %v of category not correctly formatted", c.Scheme)
|
||||
return fmt.Errorf("scheme attribute of category %v not correctly formatted", c)
|
||||
}
|
||||
}
|
||||
|
||||
if !isCorrectlyEscaped(c.Label) {
|
||||
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
|
||||
return fmt.Errorf("label attribute of category %v not correctly escaped", c)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -2,22 +2,23 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type CommonAttributes struct {
|
||||
Base IRI `xml:"base,attr,omitempty"`
|
||||
Lang LanguageTag `xml:"lang,attr,omitempty"`
|
||||
Base string `xml:"base,attr,omitempty"` // IRI
|
||||
Lang string `xml:"lang,attr,omitempty"` // LanguageTag
|
||||
UndefinedAttributes []*xml.Attr `xml:",attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
|
||||
// *CommonAttributes.
|
||||
func NewCommonAttributes() *CommonAttributes {
|
||||
func newCommonAttributes() *CommonAttributes {
|
||||
return new(CommonAttributes)
|
||||
}
|
||||
|
||||
// AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
|
||||
func (c *CommonAttributes) AddExtensionAttribute(name, value string) {
|
||||
// AddAttribute adds the Attribute to the CommonAttributes.
|
||||
func (c *CommonAttributes) AddAttribute(name, value string) {
|
||||
if c.UndefinedAttributes == nil {
|
||||
c.UndefinedAttributes = make([]*xml.Attr, 1)
|
||||
c.UndefinedAttributes[0] = &xml.Attr{Name: xml.Name{Local: name}, Value: value}
|
||||
@ -25,3 +26,18 @@ func (c *CommonAttributes) AddExtensionAttribute(name, value string) {
|
||||
c.UndefinedAttributes = append(c.UndefinedAttributes, &xml.Attr{Name: xml.Name{Local: name}, Value: value})
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
|
||||
// returns an error.
|
||||
func (c *CommonAttributes) Check() error {
|
||||
for i, u := range c.UndefinedAttributes {
|
||||
if u.Name.Local == "" {
|
||||
return fmt.Errorf("xml name of undefined attribute %v empty", i)
|
||||
}
|
||||
if u.Value == "" {
|
||||
return fmt.Errorf("value of undefined attribute %v empty", i)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
14
content.go
14
content.go
@ -1,9 +1,5 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const (
|
||||
InlineText = iota
|
||||
InlineXHTML
|
||||
@ -19,17 +15,17 @@ type Content interface {
|
||||
}
|
||||
|
||||
// NewContent creates a new Content. It returns a Content and an error.
|
||||
func NewContent(contentType int, mediaType string, content any) (Content, error) {
|
||||
func NewContent(contentType int, mediaType string, content any) Content {
|
||||
switch contentType {
|
||||
case 0:
|
||||
return newInlineTextContent(mediaType, content)
|
||||
return newInlineTextContent(mediaType, content.(string))
|
||||
case 1:
|
||||
return newInlineXHTMLContent(mediaType, content)
|
||||
return newInlineXHTMLContent(mediaType, content.(*XHTMLDiv))
|
||||
case 2:
|
||||
return newInlineOtherContent(mediaType, content)
|
||||
case 3:
|
||||
return newOutOfLineContent(mediaType, content)
|
||||
return newOutOfLineContent(mediaType, content.(string))
|
||||
default:
|
||||
return nil, fmt.Errorf("%v is not a valid text type", contentType)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
7
date.go
7
date.go
@ -13,12 +13,15 @@ type Date struct {
|
||||
// DateTime formats a time.Time to string formated as defined by RFC3339. It
|
||||
// returns a string.
|
||||
func DateTime(t time.Time) string {
|
||||
return string(t.Format(time.RFC3339))
|
||||
return t.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
// NewDate creates a new Date. It returns a *Date.
|
||||
func NewDate(t time.Time) *Date {
|
||||
return &Date{DateTime: DateTime(t)}
|
||||
return &Date{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
DateTime: DateTime(t),
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the Date for incompatibilities with RFC4287. It returns an
|
||||
|
70
entry.go
70
entry.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@ -40,16 +39,16 @@ func (e *Entry) checkAuthors(authorInFeed bool) error {
|
||||
if e.Authors == nil {
|
||||
if !authorInFeed {
|
||||
if e.Source == nil {
|
||||
return errors.New("no authors set in entry")
|
||||
return fmt.Errorf("no authors set in entry %v", e)
|
||||
}
|
||||
if e.Source.Authors == nil {
|
||||
return errors.New("no authors set in entry")
|
||||
return fmt.Errorf("no authors set in entry %v", e)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i, a := range e.Authors {
|
||||
if err := a.Check(); err != nil {
|
||||
return fmt.Errorf("author element %v of entry: %v", i, err)
|
||||
return fmt.Errorf("author element %v of entry %v: %v", i, e, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -57,23 +56,14 @@ func (e *Entry) checkAuthors(authorInFeed bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewEntry creates a new Entry. It returns a *Entry and an error.
|
||||
func NewEntry(title string) (*Entry, error) {
|
||||
text, err := NewText("text", title)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new entry: %v", err)
|
||||
}
|
||||
|
||||
id, err := NewID(NewURN())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new entry: %v", err)
|
||||
}
|
||||
|
||||
// NewEntry creates a new Entry. It returns a *Entry.
|
||||
func NewEntry(title string) *Entry {
|
||||
return &Entry{
|
||||
ID: id,
|
||||
Title: text,
|
||||
Updated: NewDate(time.Now()),
|
||||
}, nil
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
ID: NewID(NewURN()),
|
||||
Title: NewText("text", title),
|
||||
Updated: NewDate(time.Now()),
|
||||
}
|
||||
}
|
||||
|
||||
// AddAuthor adds the Person as an author to the Entry.
|
||||
@ -140,72 +130,72 @@ func (e *Entry) AddExtension(x *ExtensionElement) {
|
||||
// error.
|
||||
func (e *Entry) Check() error {
|
||||
if e.ID == nil {
|
||||
return errors.New("no id element of entry")
|
||||
return fmt.Errorf("no id element of entry %v", e)
|
||||
} else {
|
||||
if err := e.ID.Check(); err != nil {
|
||||
return fmt.Errorf("id element of entry: %v", err)
|
||||
return fmt.Errorf("id element of entry %v: %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := e.checkAuthors(true); err != nil {
|
||||
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("entry %v: %v", e, err)
|
||||
}
|
||||
|
||||
for i, c := range e.Categories {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
return fmt.Errorf("category element %v of entry %v: %v", i, e, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Content != nil {
|
||||
if err := e.Content.Check(); err != nil {
|
||||
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("content element of entry %v: %v", e, err)
|
||||
}
|
||||
} else {
|
||||
// atom:entry elements that contain no child atom:content element MUST
|
||||
// contain at least one atom:link element with a rel attribute value of
|
||||
// "alternate".
|
||||
if !alternateRelExists(e.Links) {
|
||||
return errors.New("no content element of entry %v and no link element with rel \"alternate\"")
|
||||
return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range e.Contributors {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
return fmt.Errorf("contributor element %v of entry %v: %v", i, e, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, l := range e.Links {
|
||||
if err := l.Check(); err != nil {
|
||||
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
return fmt.Errorf("link element %v of entry %v: %v", i, e, err)
|
||||
}
|
||||
}
|
||||
if hasAlternateDuplicateLinks(e.Links) {
|
||||
return fmt.Errorf("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e.ID.URI)
|
||||
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e)
|
||||
}
|
||||
|
||||
if e.Published != nil {
|
||||
if err := e.Published.Check(); err != nil {
|
||||
return fmt.Errorf("published element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("published element of entry %v: %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Rights != nil {
|
||||
if err := e.Rights.Check(); err != nil {
|
||||
return fmt.Errorf("rights element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("rights element of entry %v: %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Source != nil {
|
||||
if err := e.Source.Check(); err != nil {
|
||||
return fmt.Errorf("source element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("source element of entry %v: %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Summary != nil {
|
||||
if err := e.Summary.Check(); err != nil {
|
||||
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("summary element of entry %v: %v", e, err)
|
||||
}
|
||||
} else {
|
||||
// atom:entry elements MUST contain an atom:summary element in either
|
||||
@ -213,7 +203,7 @@ func (e *Entry) Check() error {
|
||||
// the atom:entry contains an atom:content that has a "src" attribute
|
||||
// (and is thus empty).
|
||||
if e.Content.hasSRC() {
|
||||
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e.ID.URI)
|
||||
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e)
|
||||
}
|
||||
// the atom:entry contains content that is encoded in Base64; i.e., the
|
||||
// "type" attribute of atom:content is a MIME media type [MIMEREG], but
|
||||
@ -221,29 +211,29 @@ func (e *Entry) Check() error {
|
||||
// does not end with "/xml" or "+xml".
|
||||
mediaType := e.Content.getType()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Title == nil {
|
||||
return fmt.Errorf("no title element of entry %v", e.ID.URI)
|
||||
return fmt.Errorf("no title element of entry %v", e)
|
||||
} else {
|
||||
if err := e.Title.Check(); err != nil {
|
||||
return fmt.Errorf("title element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("title element of entry %v: %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
if e.Updated == nil {
|
||||
return fmt.Errorf("no updated element of entry %v", e.ID.URI)
|
||||
return fmt.Errorf("no updated element of entry %v", e)
|
||||
} else {
|
||||
if err := e.Updated.Check(); err != nil {
|
||||
return fmt.Errorf("updated element of entry %v: %v", e.ID.URI, err)
|
||||
return fmt.Errorf("updated element of entry %v: %v", e, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, x := range e.Extensions {
|
||||
if err := x.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err)
|
||||
return fmt.Errorf("extension element %v of entry %v: %v", i, e, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ExtensionElement struct {
|
||||
@ -19,8 +19,12 @@ func NewExtensionElement(name string, value any) *ExtensionElement {
|
||||
// Check checks the ExtensionElement for incompatibilities with RFC4287. It
|
||||
// returns an error.
|
||||
func (e *ExtensionElement) Check() error {
|
||||
if e.XMLName.Local == "" {
|
||||
return fmt.Errorf("xml name of extension %v empty", e)
|
||||
}
|
||||
|
||||
if e.Value == nil {
|
||||
return errors.New("value element of extension element empty")
|
||||
return fmt.Errorf("value of extension %v empty", e)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
62
feed.go
62
feed.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
@ -26,23 +25,14 @@ type Feed struct {
|
||||
Entries []*Entry `xml:",omitempty"`
|
||||
}
|
||||
|
||||
// NewFeed creates a new Feed. It returns a *Feed and an error.
|
||||
func NewFeed(title string) (*Feed, error) {
|
||||
text, err := NewText("text", title)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new feed: %v", err)
|
||||
}
|
||||
|
||||
id, err := NewID(NewURN())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new feed: %v", err)
|
||||
}
|
||||
|
||||
// NewFeed creates a new Feed. It returns a *Feed.
|
||||
func NewFeed(title string) *Feed {
|
||||
return &Feed{
|
||||
ID: id,
|
||||
Title: text,
|
||||
Updated: NewDate(time.Now()),
|
||||
}, nil
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
ID: NewID(NewURN()),
|
||||
Title: NewText("text", title),
|
||||
Updated: NewDate(time.Now()),
|
||||
}
|
||||
}
|
||||
|
||||
// AddAuthor adds the Person as an author to the Feed.
|
||||
@ -121,10 +111,10 @@ func (f *Feed) AddEntry(e *Entry) {
|
||||
// error.
|
||||
func (f *Feed) Check() error {
|
||||
if f.ID == nil {
|
||||
return errors.New("no id element of feed")
|
||||
return fmt.Errorf("no id element of feed %v", f)
|
||||
} else {
|
||||
if err := f.ID.Check(); err != nil {
|
||||
return fmt.Errorf("id element of feed: %v", err)
|
||||
return fmt.Errorf("id element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -134,93 +124,93 @@ func (f *Feed) Check() error {
|
||||
if f.Authors == nil {
|
||||
for _, e := range f.Entries {
|
||||
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, err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for i, a := range f.Authors {
|
||||
if err := a.Check(); err != nil {
|
||||
return fmt.Errorf("author element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
return fmt.Errorf("author element %v of feed %v: %v", i, f, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range f.Categories {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("category element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
return fmt.Errorf("category element %v of feed %v: %v", i, f, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range f.Contributors {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("contributor element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
return fmt.Errorf("contributor element %v of feed %v: %v", i, f, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Generator != nil {
|
||||
if err := f.Generator.Check(); err != nil {
|
||||
return fmt.Errorf("generator element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("generator element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Icon != nil {
|
||||
if err := f.Icon.Check(); err != nil {
|
||||
return fmt.Errorf("icon element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("icon element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, l := range f.Links {
|
||||
if err := l.Check(); err != nil {
|
||||
return fmt.Errorf("link element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
return fmt.Errorf("link element %v of feed %v: %v", i, f, err)
|
||||
}
|
||||
}
|
||||
if hasAlternateDuplicateLinks(f.Links) {
|
||||
return fmt.Errorf("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f.ID.URI)
|
||||
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in feed %v", f)
|
||||
}
|
||||
|
||||
if f.Logo != nil {
|
||||
if err := f.Logo.Check(); err != nil {
|
||||
return fmt.Errorf("logo element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("logo element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Rights != nil {
|
||||
if err := f.Rights.Check(); err != nil {
|
||||
return fmt.Errorf("rights element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("rights element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Subtitle != nil {
|
||||
if err := f.Subtitle.Check(); err != nil {
|
||||
return fmt.Errorf("subtitle element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("subtitle element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Title == nil {
|
||||
return fmt.Errorf("no title element of feed %v", f.ID.URI)
|
||||
return fmt.Errorf("no title element of feed %v", f)
|
||||
} else {
|
||||
if err := f.Title.Check(); err != nil {
|
||||
return fmt.Errorf("title element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("title element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
if f.Updated == nil {
|
||||
return fmt.Errorf("no updated element of feed %v", f.ID)
|
||||
return fmt.Errorf("no updated element of feed %v", f)
|
||||
} else {
|
||||
if err := f.Updated.Check(); err != nil {
|
||||
return fmt.Errorf("updated element of feed %v: %v", f.ID.URI, err)
|
||||
return fmt.Errorf("updated element of feed %v: %v", f, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, x := range f.Extensions {
|
||||
if err := x.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
return fmt.Errorf("extension element %v of feed %v: %v", i, f, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, n := range f.Entries {
|
||||
if err := n.Check(); err != nil {
|
||||
return fmt.Errorf("entry element %v of feed %v: %v", i, f.ID.URI, err)
|
||||
return fmt.Errorf("entry element %v of feed %v: %v", i, f, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
14
generator.go
14
generator.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
)
|
||||
@ -10,14 +9,17 @@ import (
|
||||
type Generator struct {
|
||||
XMLName xml.Name `xml:"generator"`
|
||||
*CommonAttributes
|
||||
URI IRI `xml:"uri,attr,omitempty"`
|
||||
URI string `xml:"uri,attr,omitempty"` // IRI
|
||||
Version string `xml:"version,attr,omitempty"`
|
||||
Text string `xml:",chardata"`
|
||||
}
|
||||
|
||||
// NewGenerator creates a new Generator. It returns a *Generator.
|
||||
func NewGenerator(text string) *Generator {
|
||||
return &Generator{Text: html.UnescapeString(text)}
|
||||
return &Generator{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Text: html.UnescapeString(text),
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the Generator for incompatibilities with RFC4287. It returns an
|
||||
@ -25,16 +27,16 @@ func NewGenerator(text string) *Generator {
|
||||
func (g *Generator) Check() error {
|
||||
if g.URI != "" {
|
||||
if !isValidIRI(g.URI) {
|
||||
return fmt.Errorf("uri attribute %v of generator not correctly formatted", g.URI)
|
||||
return fmt.Errorf("uri attribute of generator %v not correctly formatted", g)
|
||||
}
|
||||
}
|
||||
|
||||
if g.Text == "" {
|
||||
return errors.New("text element of generator empty")
|
||||
return fmt.Errorf("text element of generator %v empty", g)
|
||||
}
|
||||
|
||||
if !isCorrectlyEscaped(g.Text) {
|
||||
return fmt.Errorf("text element %v of generator not correctly escaped", g.Text)
|
||||
return fmt.Errorf("text element of generator %v not correctly escaped", g)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
15
icon.go
15
icon.go
@ -9,16 +9,15 @@ import (
|
||||
type Icon struct {
|
||||
XMLName xml.Name `xml:"icon"`
|
||||
*CommonAttributes
|
||||
URI IRI `xml:",chardata"`
|
||||
URI string `xml:",chardata"` // IRI
|
||||
}
|
||||
|
||||
// NewIcon creates a new Icon. It returns a *Icon and an error.
|
||||
func NewIcon(uri IRI) (*Icon, error) {
|
||||
if !isValidIRI(uri) {
|
||||
return nil, fmt.Errorf("uri %v not correctly formatted", uri)
|
||||
// NewIcon creates a new Icon. It returns a *Icon.
|
||||
func NewIcon(uri string) *Icon {
|
||||
return &Icon{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
URI: uri,
|
||||
}
|
||||
|
||||
return &Icon{URI: uri}, nil
|
||||
}
|
||||
|
||||
// Check checks the Icon for incompatibilities with RFC4287. It returns an
|
||||
@ -28,7 +27,7 @@ func (i *Icon) Check() error {
|
||||
return errors.New("uri element of icon empty")
|
||||
} else {
|
||||
if !isValidIRI(i.URI) {
|
||||
return fmt.Errorf("uri attribute %v of icon not correctly formatted", i.URI)
|
||||
return fmt.Errorf("uri attribute of icon %v not correctly formatted", i)
|
||||
}
|
||||
}
|
||||
|
||||
|
15
id.go
15
id.go
@ -9,16 +9,15 @@ import (
|
||||
type ID struct {
|
||||
XMLName xml.Name `xml:"id"`
|
||||
*CommonAttributes
|
||||
URI IRI `xml:",chardata"`
|
||||
URI string `xml:",chardata"` // IRI
|
||||
}
|
||||
|
||||
// NewID creates a new ID. It returns a *ID and an error.
|
||||
func NewID(uri IRI) (*ID, error) {
|
||||
if !isValidIRI(uri) {
|
||||
return nil, fmt.Errorf("uri %v not correctly formatted", uri)
|
||||
// NewID creates a new ID. It returns a *ID.
|
||||
func NewID(uri string) *ID {
|
||||
return &ID{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
URI: uri,
|
||||
}
|
||||
|
||||
return &ID{URI: IRI(uri)}, nil
|
||||
}
|
||||
|
||||
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
||||
@ -27,7 +26,7 @@ func (i *ID) Check() error {
|
||||
return errors.New("uri element of id empty")
|
||||
} else {
|
||||
if !isValidIRI(i.URI) {
|
||||
return fmt.Errorf("uri element %v of id not correctly formatted", i.URI)
|
||||
return fmt.Errorf("uri element of id %v not correctly formatted", i)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
)
|
||||
@ -10,18 +9,20 @@ import (
|
||||
type InlineOtherContent struct {
|
||||
XMLName xml.Name `xml:"content"`
|
||||
*CommonAttributes
|
||||
AnyElement any `xml:",chardata"`
|
||||
Type MediaType `xml:"type,attr,omitempty"`
|
||||
AnyElement any `xml:",chardata"`
|
||||
Type string `xml:"type,attr,omitempty"` // MediaType
|
||||
}
|
||||
|
||||
// newInlineOtherContent creates a new InlineOtherContent. It returns a
|
||||
// *InlineOtherContent and an error.
|
||||
func newInlineOtherContent(mediaType string, content any) (*InlineOtherContent, error) {
|
||||
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
||||
return nil, fmt.Errorf("media type %v incompatible with inline other content", mediaType)
|
||||
}
|
||||
func newInlineOtherContent(mediaType string, content any) *InlineOtherContent {
|
||||
mediaType, _, _ = mime.ParseMediaType(mediaType)
|
||||
|
||||
return &InlineOtherContent{Type: MediaType(mediaType), AnyElement: content}, nil
|
||||
return &InlineOtherContent{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: mediaType,
|
||||
AnyElement: content,
|
||||
}
|
||||
}
|
||||
|
||||
// isContent checks whether the InlineOtherContent is a Content. It returns a
|
||||
@ -33,19 +34,19 @@ func (i *InlineOtherContent) isContent() bool { return true }
|
||||
func (i *InlineOtherContent) hasSRC() bool { return false }
|
||||
|
||||
// getType returns the Type of the InlineOtherContent as a string.
|
||||
func (i *InlineOtherContent) getType() string { return string(i.Type) }
|
||||
func (i *InlineOtherContent) getType() string { return i.Type }
|
||||
|
||||
// Check checks the InlineOtherContent for incompatibilities with RFC4287. It
|
||||
// returns an error.
|
||||
func (i *InlineOtherContent) Check() error {
|
||||
mediaType := i.getType()
|
||||
|
||||
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
||||
return fmt.Errorf("type attribute %v incompatible with inline other content", mediaType)
|
||||
if !isValidMediaType(mediaType) {
|
||||
return fmt.Errorf("type attribute of inline other content %v invalid media type", i)
|
||||
}
|
||||
|
||||
if isCompositeMediaType(mediaType) {
|
||||
return errors.New("type attribute of inline other content must not be a composite type")
|
||||
return fmt.Errorf("type attribute of inline other content %v must not be a composite type", i)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -14,18 +13,13 @@ type InlineTextContent struct {
|
||||
}
|
||||
|
||||
// newInlineTextContent creates a new InlineTextContent. It returns a
|
||||
// *InlineTextContent and an error.
|
||||
func newInlineTextContent(mediaType string, content any) (*InlineTextContent, error) {
|
||||
if mediaType != "text" && mediaType != "html" && mediaType != "" {
|
||||
return nil, fmt.Errorf("media type %v incompatible with inline text content", mediaType)
|
||||
// *InlineTextContent.
|
||||
func newInlineTextContent(mediaType, text string) *InlineTextContent {
|
||||
return &InlineTextContent{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: mediaType,
|
||||
Text: text,
|
||||
}
|
||||
|
||||
text, ok := content.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
|
||||
}
|
||||
|
||||
return &InlineTextContent{Type: mediaType, Text: text}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the InlineTextContent is a Content. It returns a
|
||||
@ -43,7 +37,7 @@ func (i *InlineTextContent) getType() string { return i.Type }
|
||||
// returns an error.
|
||||
func (i *InlineTextContent) Check() error {
|
||||
if i.Type != "" && i.Type != "text" && i.Type != "html" {
|
||||
return errors.New("type attribute of inline text content must be text or html if not omitted")
|
||||
return fmt.Errorf("type attribute of inline text content %v must be text or html if not omitted", i)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -14,18 +13,13 @@ type InlineXHTMLContent struct {
|
||||
}
|
||||
|
||||
// newInlineXHTMLContent creates a new InlineXHTMLContent. It returns a
|
||||
// *InlineXHTMLContent and an error.
|
||||
func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent, error) {
|
||||
if mediaType != "xhtml" {
|
||||
return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
|
||||
// *InlineXHTMLContent.
|
||||
func newInlineXHTMLContent(mediaType string, div *XHTMLDiv) *InlineXHTMLContent {
|
||||
return &InlineXHTMLContent{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: mediaType,
|
||||
XHTMLDiv: div,
|
||||
}
|
||||
|
||||
xhtmlDiv, ok := content.(*XHTMLDiv)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
|
||||
}
|
||||
|
||||
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: xhtmlDiv}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the InlineXHTMLContent is a Content. It returns a
|
||||
@ -43,11 +37,11 @@ func (i *InlineXHTMLContent) getType() string { return i.Type }
|
||||
// returns an error.
|
||||
func (i *InlineXHTMLContent) Check() error {
|
||||
if i.Type != "xhtml" {
|
||||
return errors.New("type attribute of inline xhtml content must be xhtml")
|
||||
return fmt.Errorf("type attribute of inline xhtml content %v must be xhtml", i)
|
||||
}
|
||||
|
||||
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 fmt.Errorf("xhtml div element of inline xhtml content %v: %v", i, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
32
link.go
32
link.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
@ -10,45 +9,48 @@ import (
|
||||
type Link struct {
|
||||
XMLName xml.Name `xml:"link"`
|
||||
*CommonAttributes
|
||||
Title string `xml:"title,attr,omitempty"`
|
||||
Href IRI `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"`
|
||||
Title string `xml:"title,attr,omitempty"`
|
||||
Href string `xml:"href,attr"` // IRI
|
||||
Rel string `xml:"rel,attr,omitempty"`
|
||||
Type string `xml:"type,attr,omitempty"` // MediaType
|
||||
HrefLang string `xml:"hreflang,attr,omitempty"` // LanguageTag
|
||||
Length uint `xml:"length,attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewLink creates a new Link. It returns a *Link.
|
||||
func NewLink(href string) *Link {
|
||||
return &Link{Href: IRI(href)}
|
||||
return &Link{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Href: href,
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the Link for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (l *Link) Check() error {
|
||||
if l.Href == "" {
|
||||
return errors.New("href attribute of link empty")
|
||||
return fmt.Errorf("href attribute of link %v empty", l)
|
||||
} else {
|
||||
if !isValidIRI(l.Href) {
|
||||
return fmt.Errorf("href attribute %v of link not correctly formatted", l.Href)
|
||||
return fmt.Errorf("href attribute of link %v not correctly formatted", l)
|
||||
}
|
||||
}
|
||||
|
||||
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 strings.Contains(l.Rel, ":") && !isValidIRI(l.Rel) {
|
||||
return fmt.Errorf("rel attribute of link %v not correctly formatted", l)
|
||||
}
|
||||
}
|
||||
|
||||
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 !isValidMediaType(l.Type) {
|
||||
return fmt.Errorf("type attribute of link %v invalid media type", l)
|
||||
}
|
||||
}
|
||||
|
||||
if l.HrefLang != "" {
|
||||
if !isValidLanguageTag(l.HrefLang) {
|
||||
return fmt.Errorf("hreflang attribute %v of link %v invalid language tag", l.Type, l.HrefLang)
|
||||
return fmt.Errorf("hreflang attribute of link %v invalid language tag", l)
|
||||
}
|
||||
}
|
||||
|
||||
|
16
logo.go
16
logo.go
@ -2,33 +2,31 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Logo struct {
|
||||
XMLName xml.Name `xml:"logo"`
|
||||
*CommonAttributes
|
||||
URI IRI `xml:",chardata"`
|
||||
URI string `xml:",chardata"` // IRI
|
||||
}
|
||||
|
||||
// NewLogo creates a new Logo. It returns a *Logo.
|
||||
func NewLogo(uri IRI) (*Logo, error) {
|
||||
if !isValidIRI(uri) {
|
||||
return nil, fmt.Errorf("uri %v not correctly formatted", uri)
|
||||
func NewLogo(uri string) *Logo {
|
||||
return &Logo{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
URI: uri,
|
||||
}
|
||||
|
||||
return &Logo{URI: uri}, nil
|
||||
}
|
||||
|
||||
// Check checks the Logo for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (l *Logo) Check() error {
|
||||
if l.URI == "" {
|
||||
return errors.New("uri element of logo empty")
|
||||
return fmt.Errorf("uri element of logo %v empty", l)
|
||||
} else {
|
||||
if !isValidIRI(l.URI) {
|
||||
return fmt.Errorf("uri element %v of logo not correctly formatted", l.URI)
|
||||
return fmt.Errorf("uri element of logo %v not correctly formatted", l)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
)
|
||||
@ -10,27 +9,20 @@ import (
|
||||
type OutOfLineContent struct {
|
||||
XMLName xml.Name `xml:"content"`
|
||||
*CommonAttributes
|
||||
Type MediaType `xml:"type,attr,omitempty"`
|
||||
SRC IRI `xml:"src,attr"`
|
||||
Type string `xml:"type,attr,omitempty"` // MediaType
|
||||
SRC string `xml:"src,attr"` // IRI
|
||||
}
|
||||
|
||||
// newOutOfLineContent creates a new OutOfLineContent. It returns a
|
||||
// *OutOfLineContent and an error.
|
||||
func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
|
||||
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
||||
return nil, fmt.Errorf("media type %v incompatible with out of line content", mediaType)
|
||||
}
|
||||
// *OutOfLineContent.
|
||||
func newOutOfLineContent(mediaType, src string) *OutOfLineContent {
|
||||
mediaType, _, _ = mime.ParseMediaType(mediaType)
|
||||
|
||||
iri, ok := content.(IRI)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
|
||||
return &OutOfLineContent{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: mediaType,
|
||||
SRC: src,
|
||||
}
|
||||
|
||||
if !isValidIRI(iri) {
|
||||
return nil, errors.New("content not a valid uri")
|
||||
}
|
||||
|
||||
return &OutOfLineContent{Type: MediaType(mediaType), SRC: iri}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the OutOfLineContent is a Content. It returns a
|
||||
@ -42,23 +34,23 @@ func (o *OutOfLineContent) isContent() bool { return true }
|
||||
func (o *OutOfLineContent) hasSRC() bool { return true }
|
||||
|
||||
// getType returns the Type of the OutOfLineContent as a string.
|
||||
func (o *OutOfLineContent) getType() string { return string(o.Type) }
|
||||
func (o *OutOfLineContent) getType() string { return o.Type }
|
||||
|
||||
// Check checks the OutOfLineContent for incompatibilities with RFC4287. It
|
||||
// returns an error.
|
||||
func (o *OutOfLineContent) Check() error {
|
||||
mediaType := o.getType()
|
||||
|
||||
if mediaType, _, err := mime.ParseMediaType(mediaType); err != nil {
|
||||
return fmt.Errorf("type attribute %v incompatible with out of line content", mediaType)
|
||||
if !isValidMediaType(mediaType) {
|
||||
return fmt.Errorf("type attribute of out of line content %v invalid media type", o)
|
||||
}
|
||||
|
||||
if isCompositeMediaType(mediaType) {
|
||||
return errors.New("type attribute of out of line content must not be a composite type")
|
||||
return fmt.Errorf("type attribute of out of line content %v must not be a composite type", o)
|
||||
}
|
||||
|
||||
if o.SRC == "" {
|
||||
return errors.New("src attribute of out of line content empty")
|
||||
return fmt.Errorf("src attribute of out of line content %v empty", o)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
20
person.go
20
person.go
@ -1,7 +1,6 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
)
|
||||
@ -9,14 +8,17 @@ import (
|
||||
type Person struct {
|
||||
*CommonAttributes
|
||||
Name string `xml:"name"`
|
||||
URI IRI `xml:"uri,omitempty"`
|
||||
Email EmailAddress `xml:"email,omitempty"`
|
||||
URI string `xml:"uri,omitempty"` // IRI
|
||||
Email string `xml:"email,omitempty"` // EmailAddress
|
||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||
}
|
||||
|
||||
// NewPerson creates a new Person. It returns a *Person.
|
||||
func NewPerson(name string) *Person {
|
||||
return &Person{Name: name}
|
||||
return &Person{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
// AddExtension adds the Extension to the Person.
|
||||
@ -33,25 +35,25 @@ func (p *Person) AddExtension(e *ExtensionElement) {
|
||||
// error.
|
||||
func (p *Person) Check() error {
|
||||
if p.Name == "" {
|
||||
return errors.New("name element of person element empty")
|
||||
return fmt.Errorf("name element of person %v empty", p)
|
||||
}
|
||||
|
||||
if p.URI != "" {
|
||||
if !isValidIRI(p.URI) {
|
||||
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
|
||||
return fmt.Errorf("uri element of person %v not correctly formatted", p)
|
||||
}
|
||||
}
|
||||
|
||||
if p.Email != "" {
|
||||
if _, err := mail.ParseAddress(string(p.Email)); err != nil {
|
||||
return fmt.Errorf("email element of person %v not correctly formatted", p.Name)
|
||||
if _, err := mail.ParseAddress(p.Email); err != nil {
|
||||
return fmt.Errorf("email element of person %v not correctly formatted", p)
|
||||
}
|
||||
}
|
||||
|
||||
if p.Extensions != nil {
|
||||
for i, e := range p.Extensions {
|
||||
if err := e.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of person %v: %v", i, p.Name, err)
|
||||
return fmt.Errorf("extension element %v of person %v: %v", i, p, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
19
plainText.go
19
plainText.go
@ -1,6 +1,8 @@
|
||||
package atom
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type PlainText struct {
|
||||
*CommonAttributes
|
||||
@ -11,19 +13,28 @@ type PlainText struct {
|
||||
// isText checks whether the PlainText is a Text. It returns a bool.
|
||||
func (p *PlainText) isText() bool { return true }
|
||||
|
||||
// newPlainText creates a new PlainText. It returns a *PlainText.
|
||||
func newPlainText(textType, content string) *PlainText {
|
||||
return &PlainText{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: textType,
|
||||
Text: content,
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the PlainText for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (p *PlainText) Check() error {
|
||||
if p.Type != "" && p.Type != "text" && p.Type != "html" {
|
||||
return errors.New("type attribute of plain text must be text or html if not omitted")
|
||||
return fmt.Errorf("type attribute of plain text %v must be text or html if not omitted", p)
|
||||
}
|
||||
|
||||
if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
|
||||
return errors.New("text element of plain text not correctly escaped")
|
||||
return fmt.Errorf("text element of plain text %v not correctly escaped", p)
|
||||
}
|
||||
|
||||
if p.Text == "" {
|
||||
return errors.New("text element of plain text empty")
|
||||
return fmt.Errorf("text element of plain text %v empty", p)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
31
source.go
31
source.go
@ -23,84 +23,89 @@ type Source struct {
|
||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||
}
|
||||
|
||||
// NewSource creates a new Source. It returns a *Source.
|
||||
func NewSource() *Source {
|
||||
return &Source{CommonAttributes: newCommonAttributes()}
|
||||
}
|
||||
|
||||
// Check checks the Source for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (s *Source) Check() error {
|
||||
for i, a := range s.Authors {
|
||||
if err := a.Check(); err != nil {
|
||||
return fmt.Errorf("author element %v of source %v: %v", i, s.ID.URI, err)
|
||||
return fmt.Errorf("author element %v of source %v: %v", i, s, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range s.Categories {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("category element %v of source %v: %v", i, s.ID.URI, err)
|
||||
return fmt.Errorf("category element %v of source %v: %v", i, s, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, c := range s.Contributors {
|
||||
if err := c.Check(); err != nil {
|
||||
return fmt.Errorf("contributor element %v of source %v: %v", i, s.ID.URI, err)
|
||||
return fmt.Errorf("contributor element %v of source %v: %v", i, s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Generator != nil {
|
||||
if err := s.Generator.Check(); err != nil {
|
||||
return fmt.Errorf("generator element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("generator element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Icon != nil {
|
||||
if err := s.Icon.Check(); err != nil {
|
||||
return fmt.Errorf("icon element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("icon element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.ID != nil {
|
||||
if err := s.ID.Check(); err != nil {
|
||||
return fmt.Errorf("id element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("id element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, l := range s.Links {
|
||||
if err := l.Check(); err != nil {
|
||||
return fmt.Errorf("link element %v of source %v: %v", i, s.ID.URI, err)
|
||||
return fmt.Errorf("link element %v of source %v: %v", i, s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Logo != nil {
|
||||
if err := s.Logo.Check(); err != nil {
|
||||
return fmt.Errorf("logo element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("logo element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Rights != nil {
|
||||
if err := s.Rights.Check(); err != nil {
|
||||
return fmt.Errorf("rights element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("rights element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Subtitle != nil {
|
||||
if err := s.Subtitle.Check(); err != nil {
|
||||
return fmt.Errorf("subtitle element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("subtitle element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Title != nil {
|
||||
if err := s.Title.Check(); err != nil {
|
||||
return fmt.Errorf("title element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("title element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.Updated != nil {
|
||||
if err := s.Updated.Check(); err != nil {
|
||||
return fmt.Errorf("updated element of source %v: %v", s.ID.URI, err)
|
||||
return fmt.Errorf("updated element of source %v: %v", s, err)
|
||||
}
|
||||
}
|
||||
|
||||
for i, e := range s.Extensions {
|
||||
if err := e.Check(); err != nil {
|
||||
return fmt.Errorf("extension element %v of source %v: %v", i, s.ID.URI, err)
|
||||
return fmt.Errorf("extension element %v of source %v: %v", i, s, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
23
text.go
23
text.go
@ -1,31 +1,22 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
)
|
||||
import "html"
|
||||
|
||||
type Text interface {
|
||||
isText() bool
|
||||
Check() error
|
||||
}
|
||||
|
||||
// NewText creates a new Text. It returns a Text and an error.
|
||||
func NewText(textType, content string) (Text, error) {
|
||||
// NewText creates a new Text. It returns a Text.
|
||||
func NewText(textType, content string) Text {
|
||||
switch textType {
|
||||
case "text", "":
|
||||
return &PlainText{Type: textType, Text: content}, nil
|
||||
return newPlainText(textType, content)
|
||||
case "html":
|
||||
return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
|
||||
return newPlainText(textType, html.UnescapeString(content))
|
||||
case "xhtml":
|
||||
return &XHTMLText{
|
||||
Type: textType,
|
||||
XHTMLDiv: &XHTMLDiv{
|
||||
XMLNS: "http://www.w3.org/1999/xhtml",
|
||||
Content: content,
|
||||
},
|
||||
}, nil
|
||||
return newXHTMLText(textType, content)
|
||||
default:
|
||||
return nil, fmt.Errorf("%v is not a valid text type", textType)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type XHTMLDiv struct {
|
||||
@ -23,7 +23,7 @@ func NewXHTMLDiv(content string) *XHTMLDiv {
|
||||
// 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 fmt.Errorf("xmlns attribute of xhtml text %v must be http://www.w3.org/1999/xhtml", x)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
12
xhtmlText.go
12
xhtmlText.go
@ -1,7 +1,6 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -14,11 +13,20 @@ type XHTMLText struct {
|
||||
// isText checks whether the XHTMLText is a Text. It returns a bool.
|
||||
func (x *XHTMLText) isText() bool { return true }
|
||||
|
||||
// newPlainText creates a new PlainText. It returns a *PlainText.
|
||||
func newXHTMLText(textType, content string) *XHTMLText {
|
||||
return &XHTMLText{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: textType,
|
||||
XHTMLDiv: NewXHTMLDiv(content),
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the XHTMLText for incompatibilities with RFC4287. It returns an
|
||||
// error.
|
||||
func (x *XHTMLText) Check() error {
|
||||
if x.Type != "xhtml" {
|
||||
return errors.New("type attribute of xhtml text must be xhtml")
|
||||
return fmt.Errorf("type attribute of xhtml text %v must be xhtml", x)
|
||||
}
|
||||
|
||||
if err := x.XHTMLDiv.Check(); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user