Compare commits
5 Commits
f4dfd6d060
...
v0.4.0
Author | SHA1 | Date | |
---|---|---|---|
4c38753ff7 | |||
7f30fd5411 | |||
a7a6b5c711 | |||
57db4178d0 | |||
960889f9e7 |
32
README.md
32
README.md
@ -15,8 +15,8 @@ go get git.streifling.com/jason/atom@latest
|
||||
## Usage
|
||||
|
||||
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.
|
||||
and attributes of Atom feeds. It also provides checks for all constructs'
|
||||
adherence to RFC4287.
|
||||
|
||||
```go
|
||||
package main
|
||||
@ -29,30 +29,28 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
feed, err := atom.NewFeed("Example Feed")
|
||||
if err != nil {
|
||||
feed := atom.NewFeed("Example Feed")
|
||||
feed.Title = atom.NewText("text", "Example Feed")
|
||||
feed.ID = atom.NewID(atom.NewURN())
|
||||
if err := feed.Check(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
author := atom.NewPerson("John Doe")
|
||||
author.Email = "john.doe@example.com"
|
||||
if err := author.Check(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
feed.AddAuthor(author)
|
||||
|
||||
entry, err := atom.NewEntry("First Entry")
|
||||
if err != nil {
|
||||
entry := atom.NewEntry("First Entry")
|
||||
entry.ID = atom.NewID(atom.NewURN())
|
||||
entry.Content = atom.NewContent(atom.InlineText, "text", "This is the content of the first entry.")
|
||||
if err := entry.Check(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
content, err := atom.NewContent(atom.InlineText, "text", "This is the content of the first entry.")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
entry.Content = content
|
||||
feed.AddEntry(entry)
|
||||
|
||||
if err := feed.Check(); err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
feedString, err := feed.ToXML("utf-8")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
@ -62,7 +60,7 @@ func main() {
|
||||
```
|
||||
|
||||
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.
|
||||
provide.
|
||||
|
||||
```go
|
||||
package main
|
||||
@ -115,7 +113,7 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
The output of both ways of using it is 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"?>
|
||||
|
6
atom.go
6
atom.go
@ -2,6 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"mime"
|
||||
"regexp"
|
||||
"strings"
|
||||
@ -85,3 +86,8 @@ func isValidAttribute(attribute string) bool {
|
||||
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)
|
||||
}
|
||||
|
46
category.go
46
category.go
@ -2,9 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
@ -15,43 +13,17 @@ type Category struct {
|
||||
Label string `xml:"label,attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewCategory creates a new Category. It returns a *Category and an error.
|
||||
func NewCategory(term string) (*Category, error) {
|
||||
if term == "" {
|
||||
return nil, errors.New("error creating new category: term string empty")
|
||||
// NewCategory creates a new Category. It returns a *Category.
|
||||
func NewCategory(term string) *Category {
|
||||
return &Category{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Term: term,
|
||||
}
|
||||
}
|
||||
|
||||
return &Category{Term: term}, nil
|
||||
}
|
||||
|
||||
// SetTerm sets the Term attribute of the Category. It returns an error.
|
||||
func (c *Category) SetTerm(t string) error {
|
||||
if t == "" {
|
||||
return errors.New("error setting term of category: t string empty")
|
||||
}
|
||||
|
||||
c.Term = t
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetScheme sets the Scheme attribute of the Category. It returns an error.
|
||||
func (c *Category) SetScheme(s string) error {
|
||||
if !isValidIRI(s) {
|
||||
return fmt.Errorf("scheme %v not correctly formatted", s)
|
||||
}
|
||||
|
||||
c.Scheme = s
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLabel sets the Label attribute of the Category. It returns an error.
|
||||
func (c *Category) SetLabel(label string) error {
|
||||
if label == "" {
|
||||
return errors.New("error setting label of category: label string empty")
|
||||
}
|
||||
|
||||
c.Label = html.UnescapeString(label)
|
||||
return nil
|
||||
// SetLabel sets the Label attribute of the Category.
|
||||
func (c *Category) SetLabel(label string) {
|
||||
c.Label = Unescape(label)
|
||||
}
|
||||
|
||||
// Check checks the Category for incompatibilities with RFC4287. It returns an
|
||||
|
@ -2,7 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type CommonAttributes struct {
|
||||
@ -13,25 +13,31 @@ type CommonAttributes struct {
|
||||
|
||||
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
|
||||
// *CommonAttributes.
|
||||
func NewCommonAttributes() *CommonAttributes {
|
||||
func newCommonAttributes() *CommonAttributes {
|
||||
return new(CommonAttributes)
|
||||
}
|
||||
|
||||
// AddAttribute adds the Attribute to the CommonAttributes. It returns an error.
|
||||
func (c *CommonAttributes) AddAttribute(name, value string) error {
|
||||
if name == "" {
|
||||
return errors.New("error adding attribute: name string empty")
|
||||
}
|
||||
if value == "" {
|
||||
return errors.New("error adding attribute: value string empty")
|
||||
}
|
||||
|
||||
// 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}
|
||||
} else {
|
||||
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
|
||||
}
|
||||
|
12
content.go
12
content.go
@ -1,7 +1,5 @@
|
||||
package atom
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
InlineText = iota
|
||||
InlineXHTML
|
||||
@ -17,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("error creating new content: %v is not a valid text type", contentType)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
5
date.go
5
date.go
@ -18,7 +18,10 @@ func DateTime(t time.Time) string {
|
||||
|
||||
// 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
|
||||
|
68
entry.go
68
entry.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@ -57,31 +56,18 @@ 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,
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
ID: NewID(NewURN()),
|
||||
Title: NewText("text", title),
|
||||
Updated: NewDate(time.Now()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AddAuthor adds the Person as an author to the Entry. It returns an error.
|
||||
func (e *Entry) AddAuthor(p *Person) error {
|
||||
if p == nil {
|
||||
return errors.New("error adding author element to entry: *Person is 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
|
||||
@ -90,15 +76,10 @@ func (e *Entry) AddAuthor(p *Person) error {
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCategory adds the Category to the Entry. It returns an error.
|
||||
func (e *Entry) AddCategory(c *Category) error {
|
||||
if c == nil {
|
||||
return errors.New("error adding category element to entry: *Category is nil")
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -107,16 +88,10 @@ func (e *Entry) AddCategory(c *Category) error {
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddContributor adds the Person as a contributor to the Entry. It returns an
|
||||
// error.
|
||||
func (e *Entry) AddContributor(c *Person) error {
|
||||
if c == nil {
|
||||
return errors.New("error adding contributor element to entry: *Person is nil")
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -125,15 +100,10 @@ func (e *Entry) AddContributor(c *Person) error {
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddLink adds the Link to the Entry. It returns an error.
|
||||
func (e *Entry) AddLink(l *Link) error {
|
||||
if l == nil {
|
||||
return errors.New("error adding link element to entry: *Link is nil")
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -142,15 +112,10 @@ func (e *Entry) AddLink(l *Link) error {
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddExtension adds the ExtensionElement to the Entry. It returns an error.
|
||||
func (e *Entry) AddExtension(x *ExtensionElement) error {
|
||||
if x == nil {
|
||||
return errors.New("error adding extension element to entry: *ExtensionElement is nil")
|
||||
}
|
||||
|
||||
// AddExtension adds the ExtensionElement to the Entry.
|
||||
func (e *Entry) AddExtension(x *ExtensionElement) {
|
||||
if e.Extensions == nil {
|
||||
e.Extensions = make([]*ExtensionElement, 1)
|
||||
e.Extensions[0] = x
|
||||
@ -159,7 +124,6 @@ func (e *Entry) AddExtension(x *ExtensionElement) error {
|
||||
}
|
||||
|
||||
e.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Entry for incompatibilities with RFC4287. It returns an
|
||||
|
@ -2,7 +2,7 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type ExtensionElement struct {
|
||||
@ -11,27 +11,20 @@ type ExtensionElement struct {
|
||||
}
|
||||
|
||||
// NewExtensionElement creates a new ExtensionElement. It returns a
|
||||
// *ExtensionElement and an error.
|
||||
func NewExtensionElement(name string, value any) (*ExtensionElement, error) {
|
||||
if name == "" {
|
||||
return nil, errors.New("error adding extension attribute: name string empty")
|
||||
}
|
||||
if value == "" {
|
||||
return nil, errors.New("error adding extension attribute: value string empty")
|
||||
}
|
||||
|
||||
return &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value}, nil
|
||||
// *ExtensionElement.
|
||||
func NewExtensionElement(name string, value any) *ExtensionElement {
|
||||
return &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value}
|
||||
}
|
||||
|
||||
// Check checks the ExtensionElement for incompatibilities with RFC4287. It
|
||||
// returns an error.
|
||||
func (e *ExtensionElement) Check() error {
|
||||
if e.XMLName.Local == "" {
|
||||
return errors.New("xml name of extension empty")
|
||||
return fmt.Errorf("xml name of extension %v empty", e)
|
||||
}
|
||||
|
||||
if e.Value == nil {
|
||||
return errors.New("value element of extension empty")
|
||||
return fmt.Errorf("value of extension %v empty", e)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
78
feed.go
78
feed.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
@ -26,31 +25,18 @@ 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,
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
ID: NewID(NewURN()),
|
||||
Title: NewText("text", title),
|
||||
Updated: NewDate(time.Now()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AddAuthor adds the Person as an author to the Feed. It returns an error.
|
||||
func (f *Feed) AddAuthor(p *Person) error {
|
||||
if p == nil {
|
||||
return errors.New("error adding author element to feed: *Person is nil")
|
||||
}
|
||||
}
|
||||
|
||||
// AddAuthor adds the Person as an author to the Feed.
|
||||
func (f *Feed) AddAuthor(p *Person) {
|
||||
if f.Authors == nil {
|
||||
f.Authors = make([]*Person, 1)
|
||||
f.Authors[0] = p
|
||||
@ -59,15 +45,10 @@ func (f *Feed) AddAuthor(p *Person) error {
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCategory adds the Category to the Feed. It returns an error.
|
||||
func (f *Feed) AddCategory(c *Category) error {
|
||||
if c == nil {
|
||||
return errors.New("error adding category element to feed: *Category is nil")
|
||||
}
|
||||
|
||||
// AddCategory adds the Category to the Feed.
|
||||
func (f *Feed) AddCategory(c *Category) {
|
||||
if f.Categories == nil {
|
||||
f.Categories = make([]*Category, 1)
|
||||
f.Categories[0] = c
|
||||
@ -76,16 +57,10 @@ func (f *Feed) AddCategory(c *Category) error {
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddContributor adds the Person as a contributor to the Feed. It returns an
|
||||
// error.
|
||||
func (f *Feed) AddContributor(c *Person) error {
|
||||
if c == nil {
|
||||
return errors.New("error adding contributor element to feed: *Person is nil")
|
||||
}
|
||||
|
||||
// AddContributor adds the Person as a contributor to the Feed.
|
||||
func (f *Feed) AddContributor(c *Person) {
|
||||
if f.Contributors == nil {
|
||||
f.Contributors = make([]*Person, 1)
|
||||
f.Contributors[0] = c
|
||||
@ -94,16 +69,10 @@ func (f *Feed) AddContributor(c *Person) error {
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddLink adds the Link to the Feed. It returns an error. There should be one
|
||||
// Link with Rel "self".
|
||||
func (f *Feed) AddLink(l *Link) error {
|
||||
if l == nil {
|
||||
return errors.New("error adding link element to feed: *Link is nil")
|
||||
}
|
||||
|
||||
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
|
||||
func (f *Feed) AddLink(l *Link) {
|
||||
if f.Links == nil {
|
||||
f.Links = make([]*Link, 1)
|
||||
f.Links[0] = l
|
||||
@ -112,15 +81,10 @@ func (f *Feed) AddLink(l *Link) error {
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddExtension adds the Extension to the Feed. It returns an error.
|
||||
func (f *Feed) AddExtension(e *ExtensionElement) error {
|
||||
if e == nil {
|
||||
return errors.New("error adding extension element to feed: *ExtensionElement is nil")
|
||||
}
|
||||
|
||||
// AddExtension adds the Extension to the Feed.
|
||||
func (f *Feed) AddExtension(e *ExtensionElement) {
|
||||
if f.Extensions == nil {
|
||||
f.Extensions = make([]*ExtensionElement, 1)
|
||||
f.Extensions[0] = e
|
||||
@ -129,15 +93,10 @@ func (f *Feed) AddExtension(e *ExtensionElement) error {
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddEntry adds the Entry to the Feed. It returns an error.
|
||||
func (f *Feed) AddEntry(e *Entry) error {
|
||||
if e == nil {
|
||||
return errors.New("error adding entry element to feed: *Entry is nil")
|
||||
}
|
||||
|
||||
// AddEntry adds the Entry to the Feed.
|
||||
func (f *Feed) AddEntry(e *Entry) {
|
||||
if f.Entries == nil {
|
||||
f.Entries = make([]*Entry, 1)
|
||||
f.Entries[0] = e
|
||||
@ -146,7 +105,6 @@ func (f *Feed) AddEntry(e *Entry) error {
|
||||
}
|
||||
|
||||
f.Updated.DateTime = DateTime(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Feed for incompatibilities with RFC4287. It returns an
|
||||
|
21
generator.go
21
generator.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
)
|
||||
@ -15,22 +14,12 @@ type Generator struct {
|
||||
Text string `xml:",chardata"`
|
||||
}
|
||||
|
||||
// NewGenerator creates a new Generator. It returns a *Generator and an error.
|
||||
func NewGenerator(text string) (*Generator, error) {
|
||||
if text == "" {
|
||||
return nil, errors.New("error creating new generator: text string empty")
|
||||
// NewGenerator creates a new Generator. It returns a *Generator.
|
||||
func NewGenerator(text string) *Generator {
|
||||
return &Generator{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Text: html.UnescapeString(text),
|
||||
}
|
||||
|
||||
return &Generator{Text: html.UnescapeString(text)}, nil
|
||||
}
|
||||
|
||||
// SetURI sets the URI attribute of the Generator. It returns an error.
|
||||
func (g *Generator) SetURI(uri string) error {
|
||||
if !isValidIRI(uri) {
|
||||
return fmt.Errorf("uri %v not correctly formatted", g)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Generator for incompatibilities with RFC4287. It returns an
|
||||
|
11
icon.go
11
icon.go
@ -12,13 +12,12 @@ type Icon struct {
|
||||
URI string `xml:",chardata"` // IRI
|
||||
}
|
||||
|
||||
// NewIcon creates a new Icon. It returns a *Icon and an error.
|
||||
func NewIcon(uri string) (*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
|
||||
|
11
id.go
11
id.go
@ -12,13 +12,12 @@ type ID struct {
|
||||
URI string `xml:",chardata"` // IRI
|
||||
}
|
||||
|
||||
// NewID creates a new ID. It returns a *ID and an error.
|
||||
func NewID(uri string) (*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: uri}, nil
|
||||
}
|
||||
|
||||
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
||||
|
@ -15,13 +15,14 @@ type InlineOtherContent struct {
|
||||
|
||||
// newInlineOtherContent creates a new InlineOtherContent. It returns a
|
||||
// *InlineOtherContent and an error.
|
||||
func newInlineOtherContent(mediaType string, content any) (*InlineOtherContent, error) {
|
||||
if !isValidMediaType(mediaType) {
|
||||
return nil, fmt.Errorf("error creating new inline other content: media type %v invalid", mediaType)
|
||||
}
|
||||
func newInlineOtherContent(mediaType string, content any) *InlineOtherContent {
|
||||
mediaType, _, _ = mime.ParseMediaType(mediaType)
|
||||
|
||||
return &InlineOtherContent{Type: mediaType, AnyElement: content}, nil
|
||||
return &InlineOtherContent{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: mediaType,
|
||||
AnyElement: content,
|
||||
}
|
||||
}
|
||||
|
||||
// isContent checks whether the InlineOtherContent is a Content. It returns a
|
||||
|
@ -13,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
|
||||
|
@ -13,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
|
||||
|
29
link.go
29
link.go
@ -17,31 +17,12 @@ type Link struct {
|
||||
Length uint `xml:"length,attr,omitempty"`
|
||||
}
|
||||
|
||||
// NewLink creates a new Link. It returns a *Link and an error.
|
||||
func NewLink(href string) (*Link, error) {
|
||||
if !isValidIRI(href) {
|
||||
return nil, fmt.Errorf("href %v not correctly formatted", href)
|
||||
// NewLink creates a new Link. It returns a *Link.
|
||||
func NewLink(href string) *Link {
|
||||
return &Link{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Href: href,
|
||||
}
|
||||
|
||||
return &Link{Href: href}, nil
|
||||
}
|
||||
|
||||
// SetType sets the Type attribute of the Link. It returns an error.
|
||||
func (l *Link) SetType(t string) error {
|
||||
if !isValidMediaType(t) {
|
||||
return fmt.Errorf("type %v invalid media type", l)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetHrefLang sets the HrefLang attribute of the Link. It returns an error.
|
||||
func (l *Link) SetHrefLang(h string) error {
|
||||
if !isValidLanguageTag(h) {
|
||||
return fmt.Errorf("hreflang %v invalid language tag", l)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Link for incompatibilities with RFC4287. It returns an
|
||||
|
11
logo.go
11
logo.go
@ -11,13 +11,12 @@ type Logo struct {
|
||||
URI string `xml:",chardata"` // IRI
|
||||
}
|
||||
|
||||
// NewLogo creates a new Logo. It returns a *Logo and an error.
|
||||
func NewLogo(uri string) (*Logo, error) {
|
||||
if !isValidIRI(uri) {
|
||||
return nil, fmt.Errorf("uri %v not correctly formatted", uri)
|
||||
// NewLogo creates a new Logo. It returns a *Logo.
|
||||
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
|
||||
|
@ -14,23 +14,15 @@ type OutOfLineContent struct {
|
||||
}
|
||||
|
||||
// newOutOfLineContent creates a new OutOfLineContent. It returns a
|
||||
// *OutOfLineContent and an error.
|
||||
func newOutOfLineContent(mediaType string, content any) (*OutOfLineContent, error) {
|
||||
if !isValidMediaType(mediaType) {
|
||||
return nil, fmt.Errorf("error creating new out of line content: media type %v invalid", mediaType)
|
||||
}
|
||||
// *OutOfLineContent.
|
||||
func newOutOfLineContent(mediaType, src string) *OutOfLineContent {
|
||||
mediaType, _, _ = mime.ParseMediaType(mediaType)
|
||||
|
||||
iri, ok := content.(string)
|
||||
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, fmt.Errorf("content %v not a valid uri", iri)
|
||||
}
|
||||
|
||||
return &OutOfLineContent{Type: mediaType, SRC: iri}, nil
|
||||
}
|
||||
|
||||
// isContent checks whether the OutOfLineContent is a Content. It returns a
|
||||
|
33
person.go
33
person.go
@ -1,7 +1,6 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
)
|
||||
@ -14,38 +13,22 @@ type Person struct {
|
||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||
}
|
||||
|
||||
// NewPerson creates a new Person. It returns a *Person and an error.
|
||||
func NewPerson(name string) (*Person, error) {
|
||||
if name == "" {
|
||||
return nil, errors.New("error creating new person: name string empty")
|
||||
}
|
||||
|
||||
return &Person{Name: name}, nil
|
||||
}
|
||||
|
||||
// SetURI sets the URI element of the Person. It returns an error.
|
||||
func (l *Link) SetURI(uri string) error {
|
||||
if !isValidIRI(uri) {
|
||||
return fmt.Errorf("uri %v not correctly formatted", uri)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddExtension adds the Extension to the Person. It returns an error.
|
||||
func (p *Person) AddExtension(e *ExtensionElement) error {
|
||||
if e == nil {
|
||||
return errors.New("error adding extension element to person: *ExtensionElement is nil")
|
||||
// NewPerson creates a new Person. It returns a *Person.
|
||||
func NewPerson(name string) *Person {
|
||||
return &Person{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
// AddExtension adds the Extension to the Person.
|
||||
func (p *Person) AddExtension(e *ExtensionElement) {
|
||||
if p.Extensions == nil {
|
||||
p.Extensions = make([]*ExtensionElement, 1)
|
||||
p.Extensions[0] = e
|
||||
} else {
|
||||
p.Extensions = append(p.Extensions, e)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check checks the Person for incompatibilities with RFC4287. It returns an
|
||||
|
13
plainText.go
13
plainText.go
@ -1,7 +1,6 @@
|
||||
package atom
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -14,13 +13,13 @@ 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 and an error.
|
||||
func newPlainText(textType, content string) (*PlainText, error) {
|
||||
if content == "" {
|
||||
return nil, errors.New("error creating new plain text: content string empty")
|
||||
// newPlainText creates a new PlainText. It returns a *PlainText.
|
||||
func newPlainText(textType, content string) *PlainText {
|
||||
return &PlainText{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: textType,
|
||||
Text: content,
|
||||
}
|
||||
|
||||
return &PlainText{Type: textType, Text: content}, nil
|
||||
}
|
||||
|
||||
// Check checks the PlainText for incompatibilities with RFC4287. It returns an
|
||||
|
@ -25,7 +25,7 @@ type Source struct {
|
||||
|
||||
// NewSource creates a new Source. It returns a *Source.
|
||||
func NewSource() *Source {
|
||||
return new(Source)
|
||||
return &Source{CommonAttributes: newCommonAttributes()}
|
||||
}
|
||||
|
||||
// Check checks the Source for incompatibilities with RFC4287. It returns an
|
||||
|
11
text.go
11
text.go
@ -1,17 +1,14 @@
|
||||
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 newPlainText(textType, content)
|
||||
@ -20,6 +17,6 @@ func NewText(textType, content string) (Text, error) {
|
||||
case "xhtml":
|
||||
return newXHTMLText(textType, content)
|
||||
default:
|
||||
return nil, fmt.Errorf("%v is not a valid text type", textType)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
11
xhtmlDiv.go
11
xhtmlDiv.go
@ -2,7 +2,6 @@ package atom
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
@ -12,16 +11,12 @@ type XHTMLDiv struct {
|
||||
Content string `xml:",innerxml"`
|
||||
}
|
||||
|
||||
// NewXHTMLDiv creates a new XHTMLDiv. It returns a *XHTMLDiv and an error.
|
||||
func NewXHTMLDiv(content string) (*XHTMLDiv, error) {
|
||||
if content == "" {
|
||||
return nil, errors.New("error creating new xhtml div: content string empty")
|
||||
}
|
||||
|
||||
// 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,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the XHTMLDiv for incompatibilities with RFC4287. It returns an
|
||||
|
14
xhtmlText.go
14
xhtmlText.go
@ -13,17 +13,13 @@ 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 and an error.
|
||||
func newXHTMLText(textType, content string) (*XHTMLText, error) {
|
||||
xhtmlDiv, err := NewXHTMLDiv(content)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating new xhtml text: %v", err)
|
||||
}
|
||||
|
||||
// newPlainText creates a new PlainText. It returns a *PlainText.
|
||||
func newXHTMLText(textType, content string) *XHTMLText {
|
||||
return &XHTMLText{
|
||||
CommonAttributes: newCommonAttributes(),
|
||||
Type: textType,
|
||||
XHTMLDiv: xhtmlDiv,
|
||||
}, nil
|
||||
XHTMLDiv: NewXHTMLDiv(content),
|
||||
}
|
||||
}
|
||||
|
||||
// Check checks the XHTMLText for incompatibilities with RFC4287. It returns an
|
||||
|
Reference in New Issue
Block a user