opml.go 2.0 KB

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