opml.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "encoding/xml"
  6. type opml struct {
  7. XMLName xml.Name `xml:"opml"`
  8. Version string `xml:"version,attr"`
  9. Outlines []outline `xml:"body>outline"`
  10. }
  11. type outline struct {
  12. Title string `xml:"title,attr,omitempty"`
  13. Text string `xml:"text,attr"`
  14. FeedURL string `xml:"xmlUrl,attr,omitempty"`
  15. SiteURL string `xml:"htmlUrl,attr,omitempty"`
  16. Outlines []outline `xml:"outline,omitempty"`
  17. }
  18. func (o *outline) GetTitle() string {
  19. if o.Title != "" {
  20. return o.Title
  21. }
  22. if o.Text != "" {
  23. return o.Text
  24. }
  25. if o.SiteURL != "" {
  26. return o.SiteURL
  27. }
  28. if o.FeedURL != "" {
  29. return o.FeedURL
  30. }
  31. return ""
  32. }
  33. func (o *outline) GetSiteURL() string {
  34. if o.SiteURL != "" {
  35. return o.SiteURL
  36. }
  37. return o.FeedURL
  38. }
  39. func (o *outline) IsCategory() bool {
  40. return o.Text != "" && o.SiteURL == "" && o.FeedURL == ""
  41. }
  42. func (o *outline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
  43. if o.FeedURL != "" {
  44. subscriptions = append(subscriptions, &Subcription{
  45. Title: o.GetTitle(),
  46. FeedURL: o.FeedURL,
  47. SiteURL: o.GetSiteURL(),
  48. CategoryName: category,
  49. })
  50. }
  51. return subscriptions
  52. }
  53. func (o *opml) Transform() SubcriptionList {
  54. var subscriptions SubcriptionList
  55. for _, outline := range o.Outlines {
  56. if outline.IsCategory() {
  57. for _, element := range outline.Outlines {
  58. subscriptions = element.Append(subscriptions, outline.Text)
  59. }
  60. } else {
  61. subscriptions = outline.Append(subscriptions, "")
  62. }
  63. }
  64. return subscriptions
  65. }