atom/text.go

32 lines
640 B
Go
Raw Permalink Normal View History

2024-10-16 21:28:04 +02:00
package atom
2024-10-13 17:19:40 +02:00
import (
"fmt"
"html"
)
2024-10-13 17:19:40 +02:00
type Text interface {
2024-10-16 18:31:24 +02:00
isText() bool
2024-10-13 17:19:40 +02:00
Check() error
}
2024-10-16 19:59:28 +02:00
// NewText creates a new Text. It returns a Text and an error.
func NewText(textType, content string) (Text, error) {
switch textType {
2024-10-15 20:03:09 +02:00
case "text", "":
2024-10-15 20:09:18 +02:00
return &PlainText{Type: textType, Text: content}, nil
case "html":
return &PlainText{Type: textType, Text: html.UnescapeString(content)}, nil
case "xhtml":
2024-10-15 16:02:13 +02:00
return &XHTMLText{
Type: textType,
2024-10-17 19:44:27 +02:00
XHTMLDiv: &XHTMLDiv{
2024-10-15 16:02:13 +02:00
XMLNS: "http://www.w3.org/1999/xhtml",
Content: content,
},
}, nil
default:
return nil, fmt.Errorf("%v is not a valid text type", textType)
}
}