Compare commits

...

9 Commits

7 changed files with 111 additions and 9 deletions

View File

@ -1,8 +1,34 @@
package atomfeed
import (
"fmt"
)
const (
InlineText = iota
InlineXHTML
InlineOther
OutOfLine
)
type Content interface {
isContent() bool
hasSRC() bool
getType() string
Check() 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)
}
}

View File

@ -1,11 +1,23 @@
package atomfeed
import "errors"
import (
"errors"
"fmt"
"mime"
)
type InlineOtherContent struct {
*CommonAttributes
AnyElement any `xml:"anyelement,omitempty"`
Type MediaType `xml:"type,attr,omitempty"`
AnyElement []*any `xml:"anyelement,omitempty"`
}
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)
}
return &InlineOtherContent{Type: MediaType(mediaType), AnyElement: content}, nil
}
func (i *InlineOtherContent) isContent() bool { return true }

View File

@ -1,6 +1,10 @@
package atomfeed
import "errors"
import (
"errors"
"fmt"
"reflect"
)
type InlineTextContent struct {
*CommonAttributes
@ -8,6 +12,30 @@ type InlineTextContent struct {
Texts []string `xml:"texts,omitempty"`
}
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)
}
texts := make([]string, 0)
t := reflect.TypeOf(content)
switch t.Kind() {
case reflect.Slice:
if t.Elem().Kind() == reflect.String {
for _, t := range content.([]string) {
texts = append(texts, t)
}
}
case reflect.String:
texts = append(texts, content.(string))
default:
return nil, fmt.Errorf("content type %T incompatible with inline text content", content)
}
return &InlineTextContent{Type: mediaType, Texts: texts}, nil
}
func (i *InlineTextContent) isContent() bool { return true }
func (i *InlineTextContent) hasSRC() bool { return false }

View File

@ -1,6 +1,10 @@
package atomfeed
import "errors"
import (
"errors"
"fmt"
"reflect"
)
type InlineXHTMLContent struct {
*CommonAttributes
@ -8,6 +12,18 @@ type InlineXHTMLContent struct {
XHTMLDiv string `xml:"xhtmldiv"`
}
func newInlineXHTMLContent(mediaType string, content any) (*InlineXHTMLContent, error) {
if mediaType != "xhtml" {
return nil, fmt.Errorf("media type %v incompatible with inline xhtml content", mediaType)
}
if reflect.TypeOf(content).Kind() != reflect.String {
return nil, fmt.Errorf("content type %T incompatible with inline xhtml content", content)
}
return &InlineXHTMLContent{Type: mediaType, XHTMLDiv: content.(string)}, nil
}
func (i *InlineXHTMLContent) isContent() bool { return true }
func (i *InlineXHTMLContent) hasSRC() bool { return false }

View File

@ -1,6 +1,12 @@
package atomfeed
import "errors"
import (
"errors"
"fmt"
"mime"
"net/url"
"reflect"
)
type OutOfLineContent struct {
*CommonAttributes
@ -8,6 +14,22 @@ type OutOfLineContent struct {
SRC URI `xml:"src,attr"`
}
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)
}
if reflect.TypeOf(content).Kind() != reflect.String {
return nil, fmt.Errorf("content type %T incompatible with out of line content", content)
}
if _, err := url.ParseRequestURI(content.(string)); err != nil {
return nil, errors.New("content not a valid uri")
}
return &OutOfLineContent{Type: MediaType(mediaType), SRC: content.(URI)}, nil
}
func (o *OutOfLineContent) isContent() bool { return true }
func (o *OutOfLineContent) hasSRC() bool { return true }

View File

@ -31,7 +31,7 @@ func (p *Person) Check() error {
if p.URI != "" {
if _, err := url.ParseRequestURI(string(p.URI)); err != nil {
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)
}
}

View File

@ -12,7 +12,7 @@ type Text interface {
func NewText(textType, content string) (Text, error) {
switch textType {
case "text":
case "text", "":
return &PlainText{Type: textType, Text: content}, nil
case "html":
return &PlainText{Type: textType, Text: html.EscapeString(content)}, nil
@ -24,8 +24,6 @@ func NewText(textType, content string) (Text, error) {
Content: content,
},
}, nil
case "":
return &PlainText{Type: "text", Text: content}, nil
default:
return nil, fmt.Errorf("%v is not a valid text type", textType)
}