2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 22:46:45 -04: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"
|
2022-07-04 18:50:48 -04:00
|
|
|
"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 {
|
2022-07-04 18:50:48 -04:00
|
|
|
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 {
|
2022-07-04 18:50:48 -04:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-12-20 01:08:53 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:50:48 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:50:48 -04:00
|
|
|
type opmlOutlineCollection []opmlOutline
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2022-07-04 18:50:48 -04:00
|
|
|
func (o opmlOutlineCollection) HasChildren() bool {
|
|
|
|
return len(o) > 0
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|