parser.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. "fmt"
  7. "io"
  8. "miniflux.app/v2/internal/reader/encoding"
  9. )
  10. // Parse reads an OPML file and returns a SubcriptionList.
  11. func Parse(data io.Reader) (SubcriptionList, error) {
  12. opmlDocument := NewOPMLDocument()
  13. decoder := xml.NewDecoder(data)
  14. decoder.Entity = xml.HTMLEntity
  15. decoder.Strict = false
  16. decoder.CharsetReader = encoding.CharsetReader
  17. err := decoder.Decode(opmlDocument)
  18. if err != nil {
  19. return nil, fmt.Errorf("opml: unable to parse document: %w", err)
  20. }
  21. return getSubscriptionsFromOutlines(opmlDocument.Outlines, ""), nil
  22. }
  23. func getSubscriptionsFromOutlines(outlines opmlOutlineCollection, category string) (subscriptions SubcriptionList) {
  24. for _, outline := range outlines {
  25. if outline.IsSubscription() {
  26. subscriptions = append(subscriptions, &Subcription{
  27. Title: outline.GetTitle(),
  28. FeedURL: outline.FeedURL,
  29. SiteURL: outline.GetSiteURL(),
  30. CategoryName: category,
  31. })
  32. } else if outline.Outlines.HasChildren() {
  33. subscriptions = append(subscriptions, getSubscriptionsFromOutlines(outline.Outlines, outline.Text)...)
  34. }
  35. }
  36. return subscriptions
  37. }