opml.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package opml // import "miniflux.app/reader/opml"
  5. import (
  6. "encoding/xml"
  7. "strings"
  8. )
  9. // Specs: http://opml.org/spec2.opml
  10. type opmlDocument struct {
  11. XMLName xml.Name `xml:"opml"`
  12. Version string `xml:"version,attr"`
  13. Header opmlHeader `xml:"head"`
  14. Outlines opmlOutlineCollection `xml:"body>outline"`
  15. }
  16. func NewOPMLDocument() *opmlDocument {
  17. return &opmlDocument{}
  18. }
  19. type opmlHeader struct {
  20. Title string `xml:"title,omitempty"`
  21. DateCreated string `xml:"dateCreated,omitempty"`
  22. OwnerName string `xml:"ownerName,omitempty"`
  23. }
  24. type opmlOutline struct {
  25. Title string `xml:"title,attr,omitempty"`
  26. Text string `xml:"text,attr"`
  27. FeedURL string `xml:"xmlUrl,attr,omitempty"`
  28. SiteURL string `xml:"htmlUrl,attr,omitempty"`
  29. Outlines opmlOutlineCollection `xml:"outline,omitempty"`
  30. }
  31. func (o *opmlOutline) IsSubscription() bool {
  32. return strings.TrimSpace(o.FeedURL) != ""
  33. }
  34. func (o *opmlOutline) GetTitle() string {
  35. if o.Title != "" {
  36. return o.Title
  37. }
  38. if o.Text != "" {
  39. return o.Text
  40. }
  41. if o.SiteURL != "" {
  42. return o.SiteURL
  43. }
  44. if o.FeedURL != "" {
  45. return o.FeedURL
  46. }
  47. return ""
  48. }
  49. func (o *opmlOutline) GetSiteURL() string {
  50. if o.SiteURL != "" {
  51. return o.SiteURL
  52. }
  53. return o.FeedURL
  54. }
  55. type opmlOutlineCollection []opmlOutline
  56. func (o opmlOutlineCollection) HasChildren() bool {
  57. return len(o) > 0
  58. }