parser.go 1.4 KB

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