opml.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package opml // import "miniflux.app/v2/internal/reader/opml"
  4. import (
  5. "encoding/xml"
  6. "strings"
  7. )
  8. // Specs: http://opml.org/spec2.opml
  9. type opmlDocument struct {
  10. XMLName xml.Name `xml:"opml"`
  11. Version string `xml:"version,attr"`
  12. Header opmlHeader `xml:"head"`
  13. Outlines opmlOutlineCollection `xml:"body>outline"`
  14. }
  15. func NewOPMLDocument() *opmlDocument {
  16. return &opmlDocument{}
  17. }
  18. type opmlHeader struct {
  19. Title string `xml:"title,omitempty"`
  20. DateCreated string `xml:"dateCreated,omitempty"`
  21. OwnerName string `xml:"ownerName,omitempty"`
  22. }
  23. type opmlOutline struct {
  24. Title string `xml:"title,attr,omitempty"`
  25. Text string `xml:"text,attr"`
  26. FeedURL string `xml:"xmlUrl,attr,omitempty"`
  27. SiteURL string `xml:"htmlUrl,attr,omitempty"`
  28. Outlines opmlOutlineCollection `xml:"outline,omitempty"`
  29. }
  30. func (o *opmlOutline) IsSubscription() bool {
  31. return strings.TrimSpace(o.FeedURL) != ""
  32. }
  33. func (o *opmlOutline) GetTitle() string {
  34. if o.Title != "" {
  35. return o.Title
  36. }
  37. if o.Text != "" {
  38. return o.Text
  39. }
  40. if o.SiteURL != "" {
  41. return o.SiteURL
  42. }
  43. if o.FeedURL != "" {
  44. return o.FeedURL
  45. }
  46. return ""
  47. }
  48. func (o *opmlOutline) GetSiteURL() string {
  49. if o.SiteURL != "" {
  50. return o.SiteURL
  51. }
  52. return o.FeedURL
  53. }
  54. type opmlOutlineCollection []opmlOutline
  55. func (o opmlOutlineCollection) HasChildren() bool {
  56. return len(o) > 0
  57. }