Compare commits
No commits in common. "7764589dd3a2df61379e4cb02d36eca8daec4868" and "987feb822615886361262a7bb1bf811a1f821470" have entirely different histories.
7764589dd3
...
987feb8226
26
content.go
26
content.go
@ -1,34 +1,8 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,11 @@
|
||||
package atomfeed
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
type InlineOtherContent struct {
|
||||
*CommonAttributes
|
||||
AnyElement any `xml:"anyelement,omitempty"`
|
||||
Type MediaType `xml:"type,attr,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
|
||||
AnyElement []*any `xml:"anyelement,omitempty"`
|
||||
}
|
||||
|
||||
func (i *InlineOtherContent) isContent() bool { return true }
|
||||
|
@ -1,10 +1,6 @@
|
||||
package atomfeed
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
type InlineTextContent struct {
|
||||
*CommonAttributes
|
||||
@ -12,30 +8,6 @@ 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 }
|
||||
|
@ -1,10 +1,6 @@
|
||||
package atomfeed
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
type InlineXHTMLContent struct {
|
||||
*CommonAttributes
|
||||
@ -12,18 +8,6 @@ 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 }
|
||||
|
@ -1,12 +1,6 @@
|
||||
package atomfeed
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"net/url"
|
||||
"reflect"
|
||||
)
|
||||
import "errors"
|
||||
|
||||
type OutOfLineContent struct {
|
||||
*CommonAttributes
|
||||
@ -14,22 +8,6 @@ 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 }
|
||||
|
@ -31,7 +31,7 @@ func (p *Person) Check() error {
|
||||
|
||||
if p.URI != "" {
|
||||
if _, err := url.ParseRequestURI(string(p.URI)); err != nil {
|
||||
return fmt.Errorf("uri element of person %v not correctly formatted", p.Name)
|
||||
return fmt.Errorf("email element of person %v not correctly formatted", p.Name)
|
||||
}
|
||||
}
|
||||
|
||||
|
4
text.go
4
text.go
@ -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,6 +24,8 @@ 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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user