Compare commits
75 Commits
f2c6fce7c9
...
v0.2.1
Author | SHA1 | Date | |
---|---|---|---|
3172a4865a | |||
3b3f1f7e41 | |||
9c38048bd2 | |||
26e0c99150 | |||
6117876a59 | |||
cb61d90cae | |||
30623fdfe0 | |||
4ae8cecb17 | |||
8e226b48ef | |||
478d679985 | |||
d11b229691 | |||
e73b78ef30 | |||
5a82f1799f | |||
73624eadd8 | |||
17aa4b1a15 | |||
040d7a6b7b | |||
705b651f08 | |||
4b97bf7fdc | |||
86785be588 | |||
28f5616f76 | |||
657624cbd6 | |||
b496ac3691 | |||
13e7a16178 | |||
3734a3eb3d | |||
e0902d9ba4 | |||
434783165b | |||
46138d302b | |||
6b9d5be734 | |||
2ef8d8c9df | |||
97fe20f364 | |||
0c24f80d09 | |||
c5a6e0c44f | |||
3774f41f1b | |||
7a2e79d205 | |||
e12cd132d9 | |||
03ec0d2ddf | |||
4220a2acc6 | |||
6a76bdc0c3 | |||
31cc803440 | |||
f11dee630d | |||
f8c36a7045 | |||
4fe133a394 | |||
39bd0776c5 | |||
082e71a698 | |||
b76e529ca3 | |||
bc9fd49d18 | |||
a65aa0a740 | |||
566227773e | |||
92d71fdbde | |||
f27116930a | |||
c200d5bf73 | |||
14696371e2 | |||
4f70da9a17 | |||
1f7f7966ef | |||
7764589dd3 | |||
9df834d927 | |||
ea79900bf3 | |||
b26d6370f5 | |||
15b74c2675 | |||
d96e2c61bb | |||
31b6e51cb8 | |||
4bad8ae99f | |||
068f61dc2c | |||
987feb8226 | |||
6322566a54 | |||
209059f2b4 | |||
e3b9ff0225 | |||
b08b62e794 | |||
9920e077eb | |||
6782d0c847 | |||
656ae8ad46 | |||
4f3135dcc6 | |||
f6bbc2fa6f | |||
1ecda50f34 | |||
cd61cb4f27 |
6
LICENSE
6
LICENSE
@ -208,8 +208,8 @@ If you develop a new program, and you want it to be of the greatest possible use
|
|||||||
|
|
||||||
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
|
To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the “copyright” line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
atom-feed
|
atom
|
||||||
Copyright (C) 2024 jason
|
Copyright (C) 2024 Jason Streifling
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
@ -221,7 +221,7 @@ Also add information on how to contact you by electronic and paper mail.
|
|||||||
|
|
||||||
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
|
If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
atom-feed Copyright (C) 2024 jason
|
atom Copyright (C) 2024 Jason Streifling
|
||||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
|
This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
# atom-feed
|
# atom
|
||||||
|
|
||||||
An extensible implementation of an Atom feed.
|
An extensible implementation of an Atom feed that aims to be very close to RFC4287.
|
||||||
|
84
atom.go
84
atom.go
@ -1,8 +1,88 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"mime"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"golang.org/x/text/language"
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
EmailAddress string
|
EmailAddress string
|
||||||
LanguageTag string
|
LanguageTag string
|
||||||
MediaType string
|
MediaType string
|
||||||
URI 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 {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
// isCorrectlyEscaped checks whether a string is correctly escaped as per
|
||||||
|
// RFC4287. It returns a bool.
|
||||||
|
func isCorrectlyEscaped(text string) bool {
|
||||||
|
relevantEntities := []string{"&", "<", ">", """, "'"}
|
||||||
|
|
||||||
|
for _, entity := range relevantEntities {
|
||||||
|
if strings.Contains(text, entity) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// isCompositeMediaType checks whether a string is a composite media type. It
|
||||||
|
// returns a bool.
|
||||||
|
func isCompositeMediaType(mediaType string) bool {
|
||||||
|
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.HasPrefix(mediaType, "multipart/") || strings.HasPrefix(mediaType, "message/")
|
||||||
|
}
|
||||||
|
|
||||||
|
// isXMLMediaType checks whether a string is an xml media type. It returns a
|
||||||
|
// bool.
|
||||||
|
func isXMLMediaType(mediaType string) bool {
|
||||||
|
mediaType, _, err := mime.ParseMediaType(mediaType)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.HasSuffix(mediaType, "/xml") || strings.HasSuffix(mediaType, "+xml")
|
||||||
|
}
|
||||||
|
|
||||||
|
// isValidMediaType checks whether a string is a valid media type. It returns a
|
||||||
|
// bool.
|
||||||
|
func isValidMediaType(mediaType string) bool {
|
||||||
|
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.
|
||||||
|
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()))
|
||||||
|
}
|
||||||
|
36
category.go
36
category.go
@ -1,30 +1,46 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Category struct {
|
type Category struct {
|
||||||
|
XMLName xml.Name `xml:"category"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Content *Content `xml:"content"`
|
Term string `xml:"term,attr"`
|
||||||
Term string `xml:"term,attr"`
|
Scheme IRI `xml:"scheme,attr,omitempty"`
|
||||||
Scheme URI `xml:"scheme,attr,omitempty"`
|
Label string `xml:"label,attr,omitempty"`
|
||||||
Label string `xml:"label,attr,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCategory creates a new Category. It returns a *Category.
|
||||||
|
func NewCategory(term string) *Category {
|
||||||
|
return &Category{Term: term}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLabel sets the label of the Category.
|
||||||
|
func (c *Category) SetLabel(label string) {
|
||||||
|
c.Label = html.UnescapeString(label)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the Category for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (c *Category) Check() error {
|
func (c *Category) Check() error {
|
||||||
if c.Term == "" {
|
if c.Term == "" {
|
||||||
return errors.New("term attribute of category empty")
|
return errors.New("term attribute of category empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Content == nil {
|
if c.Scheme != "" {
|
||||||
return errors.New("no content element of category")
|
if !isValidIRI(c.Scheme) {
|
||||||
} else {
|
return fmt.Errorf("scheme attribute %v of category not correctly formatted", c.Scheme)
|
||||||
if err := (*c.Content).Check(); err != nil {
|
|
||||||
return fmt.Errorf("content element of category: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isCorrectlyEscaped(c.Label) {
|
||||||
|
return fmt.Errorf("label attribute %v of category not correctly escaped", c.Label)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,31 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
type CommonAttributes struct {
|
type CommonAttributes struct {
|
||||||
Base URI `xml:"base,attr,omitempty"`
|
Base IRI `xml:"base,attr,omitempty"`
|
||||||
Lang LanguageTag `xml:"lang,attr,omitempty"`
|
Lang LanguageTag `xml:"lang,attr,omitempty"`
|
||||||
UndefinedAttributes []*ExtensionAttribute `xml:",any"`
|
UndefinedAttributes []*ExtensionAttribute `xml:",any"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewCommonAttributes creates a new set of CommonAttributes. It returns a
|
||||||
|
// *CommonAttributes.
|
||||||
|
func NewCommonAttributes() *CommonAttributes {
|
||||||
|
return new(CommonAttributes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddExtensionAttribute adds the ExtensionAttribute to the CommonAttributes.
|
||||||
|
func (c *CommonAttributes) AddExtensionAttribute(e *ExtensionAttribute) {
|
||||||
|
if c.UndefinedAttributes == nil {
|
||||||
|
c.UndefinedAttributes = make([]*ExtensionAttribute, 1)
|
||||||
|
c.UndefinedAttributes[0] = e
|
||||||
|
} else {
|
||||||
|
c.UndefinedAttributes = append(c.UndefinedAttributes, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the CommonAttributes for incompatibilities with RFC4287. It
|
||||||
|
// returns an error.
|
||||||
func (c *CommonAttributes) Check() error {
|
func (c *CommonAttributes) Check() error {
|
||||||
for i, e := range c.UndefinedAttributes {
|
for i, e := range c.UndefinedAttributes {
|
||||||
if err := e.Check(); err != nil {
|
if err := e.Check(); err != nil {
|
||||||
|
33
content.go
33
content.go
@ -1,6 +1,35 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
InlineText = iota
|
||||||
|
InlineXHTML
|
||||||
|
InlineOther
|
||||||
|
OutOfLine
|
||||||
|
)
|
||||||
|
|
||||||
type Content interface {
|
type Content interface {
|
||||||
|
isContent() bool
|
||||||
|
hasSRC() bool
|
||||||
|
getType() string
|
||||||
Check() error
|
Check() error
|
||||||
IsContent() bool
|
}
|
||||||
|
|
||||||
|
// NewContent creates a new Content. It returns a Content and an error.
|
||||||
|
func NewContent(contentType int, mediaType string, content any) (Content, error) {
|
||||||
|
switch contentType {
|
||||||
|
case 0:
|
||||||
|
return newInlineTextContent(mediaType, content)
|
||||||
|
case 1:
|
||||||
|
return newInlineXHTMLContent(mediaType, content)
|
||||||
|
case 2:
|
||||||
|
return newInlineOtherContent(mediaType, content)
|
||||||
|
case 3:
|
||||||
|
return newOutOfLineContent(mediaType, content)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("%v is not a valid text type", contentType)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
9
date.go
9
date.go
@ -1,4 +1,4 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -7,17 +7,22 @@ import (
|
|||||||
|
|
||||||
type Date struct {
|
type Date struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
DateTime string
|
DateTime string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DateTime formats a time.Time to string formated as defined by RFC3339. It
|
||||||
|
// returns a string.
|
||||||
func DateTime(t time.Time) string {
|
func DateTime(t time.Time) string {
|
||||||
return string(t.Format(time.RFC3339))
|
return string(t.Format(time.RFC3339))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewDate creates a new Date. It returns a *Date.
|
||||||
func NewDate(t time.Time) *Date {
|
func NewDate(t time.Time) *Date {
|
||||||
return &Date{DateTime: DateTime(t)}
|
return &Date{DateTime: DateTime(t)}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check checks the Date for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (d *Date) Check() error {
|
func (d *Date) Check() error {
|
||||||
if d.DateTime == "" {
|
if d.DateTime == "" {
|
||||||
return errors.New("date time element of date is empty")
|
return errors.New("date time element of date is empty")
|
||||||
|
183
entry.go
183
entry.go
@ -1,9 +1,11 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// It is advisable that each atom:entry element contain a non-empty atom:title
|
// It is advisable that each atom:entry element contain a non-empty atom:title
|
||||||
@ -11,26 +13,38 @@ import (
|
|||||||
// a non-empty atom:summary element when the entry contains no atom:content
|
// a non-empty atom:summary element when the entry contains no atom:content
|
||||||
// element.
|
// element.
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
|
XMLName xml.Name `xml:"entry"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Authors []*Person `xml:"author,omitempty"`
|
Authors []*Person `xml:"author,omitempty"`
|
||||||
Categories []*Category `xml:"category,omitempty"`
|
Categories []*Category `xml:",omitempty"`
|
||||||
Content *Content `xml:"content,omitempty"`
|
Content Content `xml:",omitempty"`
|
||||||
Contributors []*Person `xml:"contributors,omitempty"`
|
Contributors []*Person `xml:"contributors,omitempty"`
|
||||||
ID *ID `xml:"id"`
|
ID *ID
|
||||||
Links []*Link `xml:"link,omitempty"`
|
Links []*Link `xml:",omitempty"`
|
||||||
Published *Date `xml:"published,omitempty"`
|
Published *Date `xml:"published,omitempty"`
|
||||||
Rights Text `xml:"rights,omitempty"`
|
Rights Text `xml:"rights,omitempty"`
|
||||||
Source *Source `xml:"source,omitempty"`
|
Source *Source `xml:",omitempty"`
|
||||||
Summary Text `xml:"summary,omitempty"`
|
Summary Text `xml:"summary,omitempty"`
|
||||||
Title Text `xml:"title"`
|
Title Text `xml:"title"`
|
||||||
Updated *Date `xml:"updated"`
|
Updated *Date `xml:"updated"`
|
||||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Entry) checkAuthors() error {
|
// checkAuthors checks the entry's authors for incompatibilities with RFC4287.
|
||||||
|
// It returns an errors.
|
||||||
|
// atom:entry elements MUST contain one or more atom:author elements, unless
|
||||||
|
// 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(authorInFeed bool) error {
|
||||||
if e.Authors == nil {
|
if e.Authors == nil {
|
||||||
if e.Source.Authors == nil {
|
if !authorInFeed {
|
||||||
return errors.New("no authors set in entry")
|
if e.Source == nil {
|
||||||
|
return errors.New("no authors set in entry")
|
||||||
|
}
|
||||||
|
if e.Source.Authors == nil {
|
||||||
|
return errors.New("no authors set in entry")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for i, a := range e.Authors {
|
for i, a := range e.Authors {
|
||||||
@ -43,10 +57,87 @@ func (e *Entry) checkAuthors() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Entry) AddExtension(name string, value any) {
|
// NewEntry creates a new Entry. It returns a *Entry and an error.
|
||||||
e.Extensions = append(e.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Entry{
|
||||||
|
ID: id,
|
||||||
|
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 {
|
||||||
|
e.Extensions = make([]*ExtensionElement, 1)
|
||||||
|
e.Extensions[0] = x
|
||||||
|
} else {
|
||||||
|
e.Extensions = append(e.Extensions, x)
|
||||||
|
}
|
||||||
|
|
||||||
|
e.Updated.DateTime = DateTime(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the Entry for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (e *Entry) Check() error {
|
func (e *Entry) Check() error {
|
||||||
if e.ID == nil {
|
if e.ID == nil {
|
||||||
return errors.New("no id element of entry")
|
return errors.New("no id element of entry")
|
||||||
@ -56,40 +147,44 @@ func (e *Entry) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.checkAuthors(); err != nil {
|
if err := e.checkAuthors(true); err != nil {
|
||||||
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Categories != nil {
|
for i, c := range e.Categories {
|
||||||
for i, c := range e.Categories {
|
if err := c.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Content != nil {
|
if e.Content != nil {
|
||||||
if err := (*e.Content).Check(); err != 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.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
// atom:entry elements that contain no child atom:content element MUST
|
||||||
if e.Contributors != nil {
|
// contain at least one atom:link element with a rel attribute value of
|
||||||
for i, c := range e.Contributors {
|
// "alternate".
|
||||||
if err := c.Check(); err != nil {
|
if !alternateRelExists(e.Links) {
|
||||||
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err)
|
return errors.New("no content element of entry %v and no link element with rel \"alternate\"")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Links != nil {
|
for i, c := range e.Contributors {
|
||||||
for i, l := range e.Links {
|
if err := c.Check(); err != nil {
|
||||||
if err := l.Check(); err != nil {
|
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err)
|
||||||
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
if e.Published != nil {
|
if e.Published != nil {
|
||||||
if err := e.Published.Check(); err != 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.ID.URI, err)
|
||||||
@ -112,6 +207,22 @@ func (e *Entry) Check() error {
|
|||||||
if err := e.Summary.Check(); err != 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.ID.URI, err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// atom:entry elements MUST contain an atom:summary element in either
|
||||||
|
// of the following cases:
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
// 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 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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Title == nil {
|
if e.Title == nil {
|
||||||
@ -130,11 +241,9 @@ func (e *Entry) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Extensions != nil {
|
for i, x := range e.Extensions {
|
||||||
for i, x := range e.Extensions {
|
if err := x.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,17 +1,24 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ExtensionAttribute struct {
|
type ExtensionAttribute struct {
|
||||||
Value any `xml:",attr"`
|
Attr string `xml:",attr"`
|
||||||
XMLName xml.Name
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewExtensionAttribute creates a new ExtensionAttribute. It returns a
|
||||||
|
// *ExtensionAttribute.
|
||||||
|
func NewExtensionAttribute(name, value string) *ExtensionAttribute {
|
||||||
|
return &ExtensionAttribute{Attr: fmt.Sprint(name, `="`, value, `"`)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the ExtensionAttribute for incompatibilities with RFC4287. It
|
||||||
|
// returns an error.
|
||||||
func (e *ExtensionAttribute) Check() error {
|
func (e *ExtensionAttribute) Check() error {
|
||||||
if e.Value == nil {
|
if e.Attr == "" {
|
||||||
return errors.New("value element of extension attribute empty")
|
return errors.New("value element of extension attribute empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
@ -10,10 +10,14 @@ type ExtensionElement struct {
|
|||||||
XMLName xml.Name
|
XMLName xml.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewExtensionElement creates a new ExtensionElement. It returns a
|
||||||
|
// *ExtensionElement.
|
||||||
func NewExtensionElement(name string, value any) *ExtensionElement {
|
func NewExtensionElement(name string, value any) *ExtensionElement {
|
||||||
return &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value}
|
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 {
|
func (e *ExtensionElement) Check() error {
|
||||||
if e.Value == nil {
|
if e.Value == nil {
|
||||||
return errors.New("value element of extension element empty")
|
return errors.New("value element of extension element empty")
|
||||||
|
132
feed.go
132
feed.go
@ -1,4 +1,4 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
@ -10,56 +10,42 @@ import (
|
|||||||
type Feed struct {
|
type Feed struct {
|
||||||
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Authors []*Person `xml:"author,omitempty"`
|
Authors []*Person `xml:"author,omitempty"`
|
||||||
Categories []*Category `xml:"category,omitempty"`
|
Categories []*Category `xml:",omitempty"`
|
||||||
Contributors []*Person `xml:"contributor,omitempty"`
|
Contributors []*Person `xml:"contributor,omitempty"`
|
||||||
Generator *Generator `xml:"generator,omitempty"`
|
Generator *Generator `xml:",omitempty"`
|
||||||
Icon *Icon `xml:"icon,omitempty"`
|
Icon *Icon `xml:",omitempty"`
|
||||||
ID *ID `xml:"id"`
|
ID *ID
|
||||||
Links []*Link `xml:"link,omitempty"`
|
Links []*Link `xml:",omitempty"`
|
||||||
Logo *Logo `xml:"logo,omitempty"`
|
Logo *Logo `xml:",omitempty"`
|
||||||
Rights Text `xml:"rights,omitempty"`
|
Rights Text `xml:"rights,omitempty"`
|
||||||
Subtitle Text `xml:"subtitle,omitempty"`
|
Subtitle Text `xml:"subtitle,omitempty"`
|
||||||
Title Text `xml:"title"`
|
Title Text `xml:"title"`
|
||||||
Updated *Date `xml:"updated"`
|
Updated *Date `xml:"updated"`
|
||||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||||
Entries []*Entry `xml:"entry,omitempty"`
|
Entries []*Entry `xml:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// atom:feed elements MUST NOT contain more than one atom:link element with a
|
// NewFeed creates a new Feed. It returns a *Feed and an error.
|
||||||
// rel attribute value of "alternate" that has the same combination of type and
|
|
||||||
// hreflang attribute values.
|
|
||||||
func hasAlternateDuplicateLinks(l []*Link) bool {
|
|
||||||
linkMap := make(map[string]bool)
|
|
||||||
|
|
||||||
for _, link := range l {
|
|
||||||
if link.Rel == "alternate" {
|
|
||||||
key := fmt.Sprint(link.Type, "|", link.HrefLang)
|
|
||||||
if linkMap[key] {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
linkMap[key] = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFeed creates a new feed.
|
|
||||||
func NewFeed(title string) (*Feed, error) {
|
func NewFeed(title string) (*Feed, error) {
|
||||||
text, err := NewText("text", title)
|
text, err := NewText("text", title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating new feed: %v", err)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
return &Feed{
|
return &Feed{
|
||||||
ID: NewID(),
|
ID: id,
|
||||||
Title: text,
|
Title: text,
|
||||||
Updated: NewDate(time.Now()),
|
Updated: NewDate(time.Now()),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAuthor adds the person as an author to the feed.
|
// AddAuthor adds the Person as an author to the Feed.
|
||||||
func (f *Feed) AddAuthor(p *Person) {
|
func (f *Feed) AddAuthor(p *Person) {
|
||||||
if f.Authors == nil {
|
if f.Authors == nil {
|
||||||
f.Authors = make([]*Person, 1)
|
f.Authors = make([]*Person, 1)
|
||||||
@ -71,7 +57,7 @@ func (f *Feed) AddAuthor(p *Person) {
|
|||||||
f.Updated.DateTime = DateTime(time.Now())
|
f.Updated.DateTime = DateTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCategory adds the category to the feed.
|
// AddCategory adds the Category to the Feed.
|
||||||
func (f *Feed) AddCategory(c *Category) {
|
func (f *Feed) AddCategory(c *Category) {
|
||||||
if f.Categories == nil {
|
if f.Categories == nil {
|
||||||
f.Categories = make([]*Category, 1)
|
f.Categories = make([]*Category, 1)
|
||||||
@ -83,7 +69,7 @@ func (f *Feed) AddCategory(c *Category) {
|
|||||||
f.Updated.DateTime = DateTime(time.Now())
|
f.Updated.DateTime = DateTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddContributor adds the contributor to the feed.
|
// AddContributor adds the Person as a contributor to the Feed.
|
||||||
func (f *Feed) AddContributor(c *Person) {
|
func (f *Feed) AddContributor(c *Person) {
|
||||||
if f.Contributors == nil {
|
if f.Contributors == nil {
|
||||||
f.Contributors = make([]*Person, 1)
|
f.Contributors = make([]*Person, 1)
|
||||||
@ -95,8 +81,7 @@ func (f *Feed) AddContributor(c *Person) {
|
|||||||
f.Updated.DateTime = DateTime(time.Now())
|
f.Updated.DateTime = DateTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddLink adds the link to the feed.
|
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
|
||||||
// There should be one link with rel "self".
|
|
||||||
func (f *Feed) AddLink(l *Link) {
|
func (f *Feed) AddLink(l *Link) {
|
||||||
if f.Links == nil {
|
if f.Links == nil {
|
||||||
f.Links = make([]*Link, 1)
|
f.Links = make([]*Link, 1)
|
||||||
@ -108,7 +93,7 @@ func (f *Feed) AddLink(l *Link) {
|
|||||||
f.Updated.DateTime = DateTime(time.Now())
|
f.Updated.DateTime = DateTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddExtension adds the extension to the feed.
|
// AddExtension adds the Extension to the Feed.
|
||||||
func (f *Feed) AddExtension(e *ExtensionElement) {
|
func (f *Feed) AddExtension(e *ExtensionElement) {
|
||||||
if f.Extensions == nil {
|
if f.Extensions == nil {
|
||||||
f.Extensions = make([]*ExtensionElement, 1)
|
f.Extensions = make([]*ExtensionElement, 1)
|
||||||
@ -120,7 +105,7 @@ func (f *Feed) AddExtension(e *ExtensionElement) {
|
|||||||
f.Updated.DateTime = DateTime(time.Now())
|
f.Updated.DateTime = DateTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddEntry adds the entry to the feed.
|
// AddEntry adds the Entry to the Feed.
|
||||||
func (f *Feed) AddEntry(e *Entry) {
|
func (f *Feed) AddEntry(e *Entry) {
|
||||||
if f.Entries == nil {
|
if f.Entries == nil {
|
||||||
f.Entries = make([]*Entry, 1)
|
f.Entries = make([]*Entry, 1)
|
||||||
@ -132,7 +117,8 @@ func (f *Feed) AddEntry(e *Entry) {
|
|||||||
f.Updated.DateTime = DateTime(time.Now())
|
f.Updated.DateTime = DateTime(time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check checks the feed for incompatibilities with RFC4287.
|
// Check checks the Feed for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (f *Feed) Check() error {
|
func (f *Feed) Check() error {
|
||||||
if f.ID == nil {
|
if f.ID == nil {
|
||||||
return errors.New("no id element of feed")
|
return errors.New("no id element of feed")
|
||||||
@ -142,9 +128,12 @@ func (f *Feed) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// atom:feed elements MUST contain one or more atom:author elements, unless
|
||||||
|
// all of the atom:feed element's child atom:entry elements contain at
|
||||||
|
// least one atom:author element.
|
||||||
if f.Authors == nil {
|
if f.Authors == nil {
|
||||||
for _, e := range f.Entries {
|
for _, e := range f.Entries {
|
||||||
if err := e.checkAuthors(); err != nil {
|
if err := e.checkAuthors(false); err != nil {
|
||||||
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err)
|
return fmt.Errorf("no authors set in feed %v: %v", f.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,19 +145,15 @@ func (f *Feed) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Categories != nil {
|
for i, c := range f.Categories {
|
||||||
for i, c := range f.Categories {
|
if err := c.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Contributors != nil {
|
for i, c := range f.Contributors {
|
||||||
for i, c := range f.Contributors {
|
if err := c.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,15 +169,13 @@ func (f *Feed) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Links != nil {
|
for i, l := range f.Links {
|
||||||
for i, l := range f.Links {
|
if err := l.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if hasAlternateDuplicateLinks(f.Links) {
|
if hasAlternateDuplicateLinks(f.Links) {
|
||||||
return errors.New("links with with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found")
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Logo != nil {
|
if f.Logo != nil {
|
||||||
@ -229,45 +212,22 @@ func (f *Feed) Check() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Extensions != nil {
|
for i, x := range f.Extensions {
|
||||||
for i, x := range f.Extensions {
|
if err := x.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Entries != nil {
|
for i, n := range f.Entries {
|
||||||
for i, n := range f.Entries {
|
if err := n.Check(); err != nil {
|
||||||
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.ID.URI, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create complete link or delete
|
// ToXML converts the Feed to XML. It returns a string and an error.
|
||||||
func (f *Feed) Standardize() {
|
|
||||||
if f.Links == nil {
|
|
||||||
f.Links = make([]*Link, 1)
|
|
||||||
f.Links[0] = &Link{Rel: "self"}
|
|
||||||
} else {
|
|
||||||
selfExists := false
|
|
||||||
for _, l := range f.Links {
|
|
||||||
if l.Rel == "self" {
|
|
||||||
selfExists = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !selfExists {
|
|
||||||
f.Links = append(f.Links, &Link{Rel: "self"})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ToXML converts the feed to XML.
|
|
||||||
func (f *Feed) ToXML(encoding string) (string, error) {
|
func (f *Feed) ToXML(encoding string) (string, error) {
|
||||||
xml, err := xml.MarshalIndent(f, "", " ")
|
xml, err := xml.MarshalIndent(f, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
31
generator.go
31
generator.go
@ -1,18 +1,41 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"html"
|
||||||
|
)
|
||||||
|
|
||||||
type Generator struct {
|
type Generator struct {
|
||||||
|
XMLName xml.Name `xml:"generator"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
URI URI `xml:"uri,attr,omitempty"`
|
URI IRI `xml:"uri,attr,omitempty"`
|
||||||
Version string `xml:"version,attr,omitempty"`
|
Version string `xml:"version,attr,omitempty"`
|
||||||
Text string `xml:"text"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewGenerator creates a new Generator. It returns a *Generator.
|
||||||
|
func NewGenerator(text string) *Generator {
|
||||||
|
return &Generator{Text: html.UnescapeString(text)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the Generator for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (g *Generator) Check() error {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if g.Text == "" {
|
if g.Text == "" {
|
||||||
return errors.New("text element of generator empty")
|
return errors.New("text element of generator empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !isCorrectlyEscaped(g.Text) {
|
||||||
|
return fmt.Errorf("text element %v of generator not correctly escaped", g.Text)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
7
go.mod
7
go.mod
@ -1,5 +1,8 @@
|
|||||||
module streifling.com/jason/atom-feed
|
module git.streifling.com/jason/atom
|
||||||
|
|
||||||
go 1.23.2
|
go 1.23.2
|
||||||
|
|
||||||
require github.com/google/uuid v1.6.0
|
require (
|
||||||
|
github.com/google/uuid v1.6.0
|
||||||
|
golang.org/x/text v0.19.0
|
||||||
|
)
|
||||||
|
2
go.sum
2
go.sum
@ -1,2 +1,4 @@
|
|||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||||
|
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||||
|
26
icon.go
26
icon.go
@ -1,15 +1,35 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type Icon struct {
|
type Icon struct {
|
||||||
|
XMLName xml.Name `xml:"icon"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
URI URI `xml:"uri"`
|
URI IRI `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Icon{URI: uri}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the Icon for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (i *Icon) Check() error {
|
func (i *Icon) Check() error {
|
||||||
if i.URI == "" {
|
if i.URI == "" {
|
||||||
return errors.New("uri element of icon empty")
|
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 nil
|
return nil
|
||||||
|
22
id.go
22
id.go
@ -1,24 +1,34 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ID struct {
|
type ID struct {
|
||||||
|
XMLName xml.Name `xml:"id"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
URI URI `xml:"uri"`
|
URI IRI `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewID() *ID {
|
// NewID creates a new ID. It returns a *ID and an error.
|
||||||
return &ID{URI: URI(fmt.Sprint("urn:uuid:", uuid.New()))}
|
func NewID(uri IRI) (*ID, error) {
|
||||||
|
if !isValidIRI(uri) {
|
||||||
|
return nil, fmt.Errorf("uri %v not correctly formatted", uri)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ID{URI: IRI(uri)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check checks the ID for incompatibilities with RFC4287. It returns an error.
|
||||||
func (i *ID) Check() error {
|
func (i *ID) Check() error {
|
||||||
if i.URI == "" {
|
if i.URI == "" {
|
||||||
return errors.New("uri element of id empty")
|
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 nil
|
return nil
|
||||||
|
@ -1,11 +1,52 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"mime"
|
||||||
|
)
|
||||||
|
|
||||||
type InlineOtherContent struct {
|
type InlineOtherContent struct {
|
||||||
|
XMLName xml.Name `xml:"content"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
|
AnyElement any `xml:",chardata"`
|
||||||
Type MediaType `xml:"type,attr,omitempty"`
|
Type MediaType `xml:"type,attr,omitempty"`
|
||||||
AnyElement []*any `xml:"anyelement,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InlineOtherContent) IsContent() bool { return true }
|
// 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 (i *InlineOtherContent) Check() error { return nil }
|
return &InlineOtherContent{Type: MediaType(mediaType), AnyElement: content}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// isContent checks whether the InlineOtherContent is a Content. It returns a
|
||||||
|
// bool.
|
||||||
|
func (i *InlineOtherContent) isContent() bool { return true }
|
||||||
|
|
||||||
|
// hasSRC checks whether the InlineOtherContent has a SRC attribute. It returns
|
||||||
|
// a bool.
|
||||||
|
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) }
|
||||||
|
|
||||||
|
// 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 isCompositeMediaType(mediaType) {
|
||||||
|
return errors.New("type attribute of inline other content must not be a composite type")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,15 +1,46 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type InlineTextContent struct {
|
type InlineTextContent struct {
|
||||||
|
XMLName xml.Name `xml:"content"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
||||||
Texts []string `xml:"texts,omitempty"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InlineTextContent) IsContent() bool { return true }
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
// bool.
|
||||||
|
func (i *InlineTextContent) isContent() bool { return true }
|
||||||
|
|
||||||
|
// hasSRC checks whether the InlineTextContent has a SRC attribute. It returns
|
||||||
|
// a bool.
|
||||||
|
func (i *InlineTextContent) hasSRC() bool { return false }
|
||||||
|
|
||||||
|
// getType returns the Type of the InlineTextContent as a string.
|
||||||
|
func (i *InlineTextContent) getType() string { return i.Type }
|
||||||
|
|
||||||
|
// Check checks the InlineTextContent for incompatibilities with RFC4287. It
|
||||||
|
// returns an error.
|
||||||
func (i *InlineTextContent) Check() error {
|
func (i *InlineTextContent) Check() error {
|
||||||
if i.Type != "" && i.Type != "text" && i.Type != "html" {
|
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 errors.New("type attribute of inline text content must be text or html if not omitted")
|
||||||
|
@ -1,22 +1,53 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type InlineXHTMLContent struct {
|
type InlineXHTMLContent struct {
|
||||||
|
XMLName xml.Name `xml:"content"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
|
XHTMLDiv *XHTMLDiv
|
||||||
Type string `xml:"type,attr"`
|
Type string `xml:"type,attr"`
|
||||||
XHTMLDiv string `xml:"xhtmldiv"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *InlineXHTMLContent) IsContent() bool { return true }
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
// bool.
|
||||||
|
func (i *InlineXHTMLContent) isContent() bool { return true }
|
||||||
|
|
||||||
|
// hasSRC checks whether the InlineXHTMLContent has a SRC attribute. It returns
|
||||||
|
// a bool.
|
||||||
|
func (i *InlineXHTMLContent) hasSRC() bool { return false }
|
||||||
|
|
||||||
|
// getType returns the Type of the InlineXHTMLContent as a string.
|
||||||
|
func (i *InlineXHTMLContent) getType() string { return i.Type }
|
||||||
|
|
||||||
|
// Check checks the InlineXHTMLContent for incompatibilities with RFC4287. It
|
||||||
|
// returns an error.
|
||||||
func (i *InlineXHTMLContent) Check() error {
|
func (i *InlineXHTMLContent) Check() error {
|
||||||
if i.Type != "xhtml" {
|
if i.Type != "xhtml" {
|
||||||
return errors.New("type attribute of inline xhtml content must be xhtml")
|
return errors.New("type attribute of inline xhtml content must be xhtml")
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.XHTMLDiv == "" {
|
if err := i.XHTMLDiv.Check(); err != nil {
|
||||||
return errors.New("xhtmlDiv element of inline xhtml content empty")
|
return fmt.Errorf("xhtml div element %v of inline xhtml content %v: %v", i.XHTMLDiv, i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
78
link.go
78
link.go
@ -1,39 +1,89 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Link struct {
|
type Link struct {
|
||||||
|
XMLName xml.Name `xml:"link"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Title *Text `xml:"title,attr,omitempty"`
|
Title string `xml:"title,attr,omitempty"`
|
||||||
Content *Content `xml:"content"`
|
Href IRI `xml:"href,attr"`
|
||||||
Href URI `xml:"href,attr"`
|
|
||||||
Rel string `xml:"rel,attr,omitempty"`
|
Rel string `xml:"rel,attr,omitempty"`
|
||||||
Type MediaType `xml:"type,attr,omitempty"`
|
Type MediaType `xml:"type,attr,omitempty"`
|
||||||
HrefLang LanguageTag `xml:"hreflang,attr,omitempty"`
|
HrefLang LanguageTag `xml:"hreflang,attr,omitempty"`
|
||||||
Length uint `xml:"length,attr,omitempty"`
|
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)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the Link for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (l *Link) Check() error {
|
func (l *Link) Check() error {
|
||||||
if l.Href == "" {
|
if l.Href == "" {
|
||||||
return errors.New("href attribute of link empty")
|
return errors.New("href attribute of link empty")
|
||||||
}
|
} else {
|
||||||
|
if !isValidIRI(l.Href) {
|
||||||
if l.Title != nil {
|
return fmt.Errorf("href attribute %v of link not correctly formatted", l.Href)
|
||||||
if err := (*l.Title).Check(); err != nil {
|
|
||||||
return fmt.Errorf("title attribute of link %v: %v", l.Href, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.Content == nil {
|
if l.Rel != "" {
|
||||||
return fmt.Errorf("no content element of link %v", l.Href)
|
if strings.Contains(l.Rel, ":") && !isValidIRI(IRI(l.Rel)) {
|
||||||
} else {
|
return fmt.Errorf("rel attribute %v of link %v not correctly formatted", l.Rel, l.Href)
|
||||||
if err := (*l.Content).Check(); err != nil {
|
}
|
||||||
return fmt.Errorf("content element of link %v: %v", l.Href, err)
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasAlternateDuplicateLinks checks whether multiple Links with Rel
|
||||||
|
// "alternate" also have Type and HrefLang in common. It returns a bool.
|
||||||
|
// atom:feed/entry elements MUST NOT contain more than one atom:link element
|
||||||
|
// with a rel attribute value of "alternate" that has the same combination of
|
||||||
|
// type and hreflang attribute values.
|
||||||
|
func hasAlternateDuplicateLinks(l []*Link) bool {
|
||||||
|
linkMap := make(map[string]bool)
|
||||||
|
|
||||||
|
for _, link := range l {
|
||||||
|
if link.Rel == "alternate" {
|
||||||
|
key := fmt.Sprint(link.Type, "|", link.HrefLang)
|
||||||
|
if linkMap[key] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
linkMap[key] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// alternateRelExists checks whether multiple Links with Rel "alternate" exist.
|
||||||
|
// It returns a bool.
|
||||||
|
func alternateRelExists(l []*Link) bool {
|
||||||
|
for _, link := range l {
|
||||||
|
if link.Rel == "alternate" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
26
logo.go
26
logo.go
@ -1,15 +1,35 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type Logo struct {
|
type Logo struct {
|
||||||
|
XMLName xml.Name `xml:"logo"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
URI URI `xml:"uri"`
|
URI IRI `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Logo{URI: uri}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check checks the Logo for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (l *Logo) Check() error {
|
func (l *Logo) Check() error {
|
||||||
if l.URI == "" {
|
if l.URI == "" {
|
||||||
return errors.New("uri element of logo empty")
|
return errors.New("uri element of logo empty")
|
||||||
|
} else {
|
||||||
|
if !isValidIRI(l.URI) {
|
||||||
|
return fmt.Errorf("uri element %v of logo not correctly formatted", l.URI)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -1,16 +1,62 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"mime"
|
||||||
|
)
|
||||||
|
|
||||||
type OutOfLineContent struct {
|
type OutOfLineContent struct {
|
||||||
|
XMLName xml.Name `xml:"content"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Type MediaType `xml:"type,attr,omitempty"`
|
Type MediaType `xml:"type,attr,omitempty"`
|
||||||
SRC URI `xml:"src,attr"`
|
SRC IRI `xml:"src,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OutOfLineContent) IsContent() bool { return true }
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
iri, ok := content.(IRI)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
// bool.
|
||||||
|
func (o *OutOfLineContent) isContent() bool { return true }
|
||||||
|
|
||||||
|
// hasSRC checks whether the OutOfLineContent has a SRC attribute. It returns a
|
||||||
|
// bool.
|
||||||
|
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) }
|
||||||
|
|
||||||
|
// Check checks the OutOfLineContent for incompatibilities with RFC4287. It
|
||||||
|
// returns an error.
|
||||||
func (o *OutOfLineContent) Check() 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 isCompositeMediaType(mediaType) {
|
||||||
|
return errors.New("type attribute of out of line content must not be a composite type")
|
||||||
|
}
|
||||||
|
|
||||||
if o.SRC == "" {
|
if o.SRC == "" {
|
||||||
return errors.New("src attribute of out of line content empty")
|
return errors.New("src attribute of out of line content empty")
|
||||||
}
|
}
|
||||||
|
23
person.go
23
person.go
@ -1,37 +1,44 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"net/url"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Person struct {
|
type Person struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Name string `xml:"name"`
|
Name string `xml:"name"`
|
||||||
URI URI `xml:"uri,omitempty"`
|
URI IRI `xml:"uri,omitempty"`
|
||||||
Email EmailAddress `xml:"email,omitempty"`
|
Email EmailAddress `xml:"email,omitempty"`
|
||||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewPerson creates a new Person. It returns a *Person.
|
||||||
func NewPerson(name string) *Person {
|
func NewPerson(name string) *Person {
|
||||||
return &Person{Name: name}
|
return &Person{Name: name}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Person) AddExtension(name string, value any) {
|
// AddExtension adds the Extension to the Person.
|
||||||
p.Extensions = append(p.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check checks the Person for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (p *Person) Check() error {
|
func (p *Person) Check() error {
|
||||||
if p.Name == "" {
|
if p.Name == "" {
|
||||||
return errors.New("name element of person element empty")
|
return errors.New("name element of person element empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.URI != "" {
|
if p.URI != "" {
|
||||||
if _, err := url.ParseRequestURI(string(p.URI)); err != nil {
|
if !isValidIRI(p.URI) {
|
||||||
return fmt.Errorf("email element of person %v not correctly formatted", p.Name)
|
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
plainText.go
13
plainText.go
@ -1,20 +1,27 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
type PlainText struct {
|
type PlainText struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
Type string `xml:"type,attr,omitempty"` // Must be text or html
|
||||||
Text string `xml:"text"`
|
Text string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PlainText) IsText() bool { return true }
|
// isText checks whether the PlainText is a Text. It returns a bool.
|
||||||
|
func (p *PlainText) isText() bool { return true }
|
||||||
|
|
||||||
|
// Check checks the PlainText for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (p *PlainText) Check() error {
|
func (p *PlainText) Check() error {
|
||||||
if p.Type != "" && p.Type != "text" && p.Type != "html" {
|
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 errors.New("type attribute of plain text must be text or html if not omitted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if p.Type == "html" && !isCorrectlyEscaped(p.Text) {
|
||||||
|
return errors.New("text element of plain text not correctly escaped")
|
||||||
|
}
|
||||||
|
|
||||||
if p.Text == "" {
|
if p.Text == "" {
|
||||||
return errors.New("text element of plain text empty")
|
return errors.New("text element of plain text empty")
|
||||||
}
|
}
|
||||||
|
90
source.go
90
source.go
@ -1,110 +1,106 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
type Source struct {
|
type Source struct {
|
||||||
|
XMLName xml.Name `xml:"source"`
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
Authors []*Person `xml:"author,omitempty"`
|
Authors []*Person `xml:"author,omitempty"`
|
||||||
Categories []*Category `xml:"category,omitempty"`
|
Categories []*Category `xml:",omitempty"`
|
||||||
Contributors []*Person `xml:"contributor,omitempty"`
|
Contributors []*Person `xml:"contributor,omitempty"`
|
||||||
Generator *Generator `xml:"generator,omitempty"`
|
Generator *Generator `xml:",omitempty"`
|
||||||
Icon *Icon `xml:"icon,omitempty"`
|
Icon *Icon `xml:",omitempty"`
|
||||||
ID *ID `xml:"id,omitempty"`
|
ID *ID `xml:",omitempty"`
|
||||||
Links []*Link `xml:"link,omitempty"`
|
Links []*Link `xml:",omitempty"`
|
||||||
Logo *Logo `xml:"logo,omitempty"`
|
Logo *Logo `xml:",omitempty"`
|
||||||
Rights *Text `xml:"rights,omitempty"`
|
Rights Text `xml:"rights,omitempty"`
|
||||||
Subtitle *Text `xml:"subtitle,omitempty"`
|
Subtitle Text `xml:"subtitle,omitempty"`
|
||||||
Title *Text `xml:"title,omitempty"`
|
Title Text `xml:"title,omitempty"`
|
||||||
Updated *Date `xml:"updated,omitempty"`
|
Updated *Date `xml:"updated,omitempty"`
|
||||||
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check checks the Source for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (s *Source) Check() error {
|
func (s *Source) Check() error {
|
||||||
if s.Authors != nil {
|
for i, a := range s.Authors {
|
||||||
for i, a := range s.Authors {
|
if err := a.Check(); err != nil {
|
||||||
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", i, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Categories != nil {
|
for i, c := range s.Categories {
|
||||||
for i, c := range s.Categories {
|
if err := c.Check(); err != nil {
|
||||||
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", i, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Contributors != nil {
|
for i, c := range s.Contributors {
|
||||||
for i, c := range s.Contributors {
|
if err := c.Check(); err != nil {
|
||||||
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", i, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Generator != nil {
|
if s.Generator != nil {
|
||||||
if err := s.Generator.Check(); err != nil {
|
if err := s.Generator.Check(); err != nil {
|
||||||
return fmt.Errorf("generator element of source: %v", err)
|
return fmt.Errorf("generator element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Icon != nil {
|
if s.Icon != nil {
|
||||||
if err := s.Icon.Check(); err != nil {
|
if err := s.Icon.Check(); err != nil {
|
||||||
return fmt.Errorf("icon element of source: %v", err)
|
return fmt.Errorf("icon element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.ID != nil {
|
if s.ID != nil {
|
||||||
if err := s.ID.Check(); err != nil {
|
if err := s.ID.Check(); err != nil {
|
||||||
return fmt.Errorf("id element of source: %v", err)
|
return fmt.Errorf("id element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Links != nil {
|
for i, l := range s.Links {
|
||||||
for i, l := range s.Links {
|
if err := l.Check(); err != nil {
|
||||||
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", i, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Logo != nil {
|
if s.Logo != nil {
|
||||||
if err := s.Logo.Check(); err != nil {
|
if err := s.Logo.Check(); err != nil {
|
||||||
return fmt.Errorf("logo element of source: %v", err)
|
return fmt.Errorf("logo element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Rights != nil {
|
if s.Rights != nil {
|
||||||
if err := (*s.Rights).Check(); err != nil {
|
if err := s.Rights.Check(); err != nil {
|
||||||
return fmt.Errorf("rights element of source: %v", err)
|
return fmt.Errorf("rights element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Subtitle != nil {
|
if s.Subtitle != nil {
|
||||||
if err := (*s.Subtitle).Check(); err != nil {
|
if err := s.Subtitle.Check(); err != nil {
|
||||||
return fmt.Errorf("subtitle element of source: %v", err)
|
return fmt.Errorf("subtitle element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Title != nil {
|
if s.Title != nil {
|
||||||
if err := (*s.Title).Check(); err != nil {
|
if err := s.Title.Check(); err != nil {
|
||||||
return fmt.Errorf("title element of source: %v", err)
|
return fmt.Errorf("title element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Updated != nil {
|
if s.Updated != nil {
|
||||||
if err := s.Updated.Check(); err != nil {
|
if err := s.Updated.Check(); err != nil {
|
||||||
return fmt.Errorf("updated element of source: %v", err)
|
return fmt.Errorf("updated element of source %v: %v", s.ID.URI, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.Extensions != nil {
|
for i, e := range s.Extensions {
|
||||||
for i, e := range s.Extensions {
|
if err := e.Check(); err != nil {
|
||||||
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", i, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
text.go
13
text.go
@ -1,4 +1,4 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -6,26 +6,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Text interface {
|
type Text interface {
|
||||||
|
isText() bool
|
||||||
Check() error
|
Check() error
|
||||||
IsText() bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewText creates a new Text. It returns a Text and an error.
|
||||||
func NewText(textType, content string) (Text, error) {
|
func NewText(textType, content string) (Text, error) {
|
||||||
switch textType {
|
switch textType {
|
||||||
case "text":
|
case "text", "":
|
||||||
return &PlainText{Type: textType, Text: content}, nil
|
return &PlainText{Type: textType, Text: content}, nil
|
||||||
case "html":
|
case "html":
|
||||||
return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil
|
return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
|
||||||
case "xhtml":
|
case "xhtml":
|
||||||
return &XHTMLText{
|
return &XHTMLText{
|
||||||
Type: textType,
|
Type: textType,
|
||||||
XHTMLDiv: XHTMLDiv{
|
XHTMLDiv: &XHTMLDiv{
|
||||||
XMLNS: "http://www.w3.org/1999/xhtml",
|
XMLNS: "http://www.w3.org/1999/xhtml",
|
||||||
Content: content,
|
Content: content,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
case "":
|
|
||||||
return &PlainText{Type: "text", Text: content}, nil
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("%v is not a valid text type", textType)
|
return nil, fmt.Errorf("%v is not a valid text type", textType)
|
||||||
}
|
}
|
||||||
|
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
|
||||||
|
}
|
21
xhtmlText.go
21
xhtmlText.go
@ -1,31 +1,28 @@
|
|||||||
package atomfeed
|
package atom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type XHTMLDiv struct {
|
|
||||||
XMLName xml.Name `xml:"div"`
|
|
||||||
XMLNS string `xml:"xmlns,attr"`
|
|
||||||
Content string `xml:",innerxml"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type XHTMLText struct {
|
type XHTMLText struct {
|
||||||
*CommonAttributes
|
*CommonAttributes
|
||||||
|
XHTMLDiv *XHTMLDiv
|
||||||
Type string `xml:"type,attr"` // Must be xhtml
|
Type string `xml:"type,attr"` // Must be xhtml
|
||||||
XHTMLDiv XHTMLDiv
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *XHTMLText) IsText() bool { return true }
|
// isText checks whether the XHTMLText is a Text. It returns a bool.
|
||||||
|
func (x *XHTMLText) isText() bool { return true }
|
||||||
|
|
||||||
|
// Check checks the XHTMLText for incompatibilities with RFC4287. It returns an
|
||||||
|
// error.
|
||||||
func (x *XHTMLText) Check() error {
|
func (x *XHTMLText) Check() error {
|
||||||
if x.Type != "xhtml" {
|
if x.Type != "xhtml" {
|
||||||
return errors.New("type attribute of xhtml text must be xhtml")
|
return errors.New("type attribute of xhtml text must be xhtml")
|
||||||
}
|
}
|
||||||
|
|
||||||
if x.XHTMLDiv.XMLNS != "http://www.w3.org/1999/xhtml" {
|
if err := x.XHTMLDiv.Check(); err != nil {
|
||||||
return errors.New("xmlns attribute of xhtml text must be http://www.w3.org/1999/xhtml")
|
return fmt.Errorf("xhtml div element %v of xhtml text %v: %v", x.XHTMLDiv, x, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user