opml.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 (outline opmlOutline) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  31. type opmlOutlineXml opmlOutline
  32. outlineType := ""
  33. if outline.IsSubscription() {
  34. outlineType = "rss"
  35. }
  36. return e.EncodeElement(struct {
  37. opmlOutlineXml
  38. Type string `xml:"type,attr,omitempty"`
  39. }{
  40. opmlOutlineXml: opmlOutlineXml(outline),
  41. Type: outlineType,
  42. }, start)
  43. }
  44. func (o *opmlOutline) IsSubscription() bool {
  45. return strings.TrimSpace(o.FeedURL) != ""
  46. }
  47. func (o *opmlOutline) GetTitle() string {
  48. if o.Title != "" {
  49. return o.Title
  50. }
  51. if o.Text != "" {
  52. return o.Text
  53. }
  54. if o.SiteURL != "" {
  55. return o.SiteURL
  56. }
  57. if o.FeedURL != "" {
  58. return o.FeedURL
  59. }
  60. return ""
  61. }
  62. func (o *opmlOutline) GetSiteURL() string {
  63. if o.SiteURL != "" {
  64. return o.SiteURL
  65. }
  66. return o.FeedURL
  67. }
  68. type opmlOutlineCollection []opmlOutline
  69. func (o opmlOutlineCollection) HasChildren() bool {
  70. return len(o) > 0
  71. }