opml.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. type opmlHeader struct {
  16. Title string `xml:"title,omitempty"`
  17. DateCreated string `xml:"dateCreated,omitempty"`
  18. OwnerName string `xml:"ownerName,omitempty"`
  19. }
  20. type opmlOutline struct {
  21. Title string `xml:"title,attr,omitempty"`
  22. Text string `xml:"text,attr"`
  23. FeedURL string `xml:"xmlUrl,attr,omitempty"`
  24. SiteURL string `xml:"htmlUrl,attr,omitempty"`
  25. Description string `xml:"description,attr,omitempty"`
  26. Outlines opmlOutlineCollection `xml:"outline,omitempty"`
  27. }
  28. func (o opmlOutline) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  29. type opmlOutlineXml opmlOutline
  30. outlineType := ""
  31. if o.IsSubscription() {
  32. outlineType = "rss"
  33. }
  34. return e.EncodeElement(struct {
  35. opmlOutlineXml
  36. Type string `xml:"type,attr,omitempty"`
  37. }{
  38. opmlOutlineXml: opmlOutlineXml(o),
  39. Type: outlineType,
  40. }, start)
  41. }
  42. func (o opmlOutline) IsSubscription() bool {
  43. return strings.TrimSpace(o.FeedURL) != ""
  44. }
  45. func (o opmlOutline) GetTitle() string {
  46. if o.Title != "" {
  47. return o.Title
  48. }
  49. if o.Text != "" {
  50. return o.Text
  51. }
  52. if o.SiteURL != "" {
  53. return o.SiteURL
  54. }
  55. if o.FeedURL != "" {
  56. return o.FeedURL
  57. }
  58. return ""
  59. }
  60. func (o opmlOutline) GetSiteURL() string {
  61. if o.SiteURL != "" {
  62. return o.SiteURL
  63. }
  64. return o.FeedURL
  65. }
  66. type opmlOutlineCollection []opmlOutline
  67. func (o opmlOutlineCollection) HasChildren() bool {
  68. return len(o) > 0
  69. }