1
0
Fork 0
miniflux/internal/reader/opml/opml.go

91 lines
1.9 KiB
Go
Raw Normal View History

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
2017-11-20 00:10:04 -05:00
package opml // import "miniflux.app/v2/internal/reader/opml"
2017-11-20 00:10:04 -05:00
2019-03-26 22:49:59 -04:00
import (
"encoding/xml"
"strings"
2019-03-26 22:49:59 -04:00
)
2017-11-20 00:10:04 -05:00
2021-12-16 14:42:43 -05:00
// Specs: http://opml.org/spec2.opml
type opmlDocument struct {
XMLName xml.Name `xml:"opml"`
Version string `xml:"version,attr"`
Header opmlHeader `xml:"head"`
Outlines opmlOutlineCollection `xml:"body>outline"`
2017-11-20 00:10:04 -05:00
}
2021-12-16 14:42:43 -05:00
func NewOPMLDocument() *opmlDocument {
return &opmlDocument{}
2017-11-20 00:10:04 -05:00
}
2021-12-16 14:42:43 -05:00
type opmlHeader struct {
Title string `xml:"title,omitempty"`
DateCreated string `xml:"dateCreated,omitempty"`
OwnerName string `xml:"ownerName,omitempty"`
}
type opmlOutline struct {
Title string `xml:"title,attr,omitempty"`
Text string `xml:"text,attr"`
FeedURL string `xml:"xmlUrl,attr,omitempty"`
SiteURL string `xml:"htmlUrl,attr,omitempty"`
Outlines opmlOutlineCollection `xml:"outline,omitempty"`
}
func (outline opmlOutline) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
type opmlOutlineXml opmlOutline
outlineType := ""
if outline.IsSubscription() {
outlineType = "rss"
}
return e.EncodeElement(struct {
opmlOutlineXml
Type string `xml:"type,attr,omitempty"`
}{
opmlOutlineXml: opmlOutlineXml(outline),
Type: outlineType,
}, start)
}
func (o *opmlOutline) IsSubscription() bool {
return strings.TrimSpace(o.FeedURL) != ""
2021-12-16 14:42:43 -05:00
}
func (o *opmlOutline) GetTitle() string {
2017-11-20 00:10:04 -05:00
if o.Title != "" {
return o.Title
}
if o.Text != "" {
return o.Text
}
if o.SiteURL != "" {
return o.SiteURL
}
if o.FeedURL != "" {
return o.FeedURL
}
return ""
}
2021-12-16 14:42:43 -05:00
func (o *opmlOutline) GetSiteURL() string {
2017-11-20 00:10:04 -05:00
if o.SiteURL != "" {
return o.SiteURL
}
return o.FeedURL
}
type opmlOutlineCollection []opmlOutline
2017-11-20 00:10:04 -05:00
func (o opmlOutlineCollection) HasChildren() bool {
return len(o) > 0
2017-11-20 00:10:04 -05:00
}