atom_common.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 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 atom // import "miniflux.app/reader/atom"
  5. import "strings"
  6. type atomPerson struct {
  7. Name string `xml:"name"`
  8. Email string `xml:"email"`
  9. }
  10. func (a *atomPerson) String() string {
  11. name := ""
  12. switch {
  13. case a.Name != "":
  14. name = a.Name
  15. case a.Email != "":
  16. name = a.Email
  17. }
  18. return strings.TrimSpace(name)
  19. }
  20. type atomLink struct {
  21. URL string `xml:"href,attr"`
  22. Type string `xml:"type,attr"`
  23. Rel string `xml:"rel,attr"`
  24. Length string `xml:"length,attr"`
  25. }
  26. type atomLinks []*atomLink
  27. func (a atomLinks) originalLink() string {
  28. for _, link := range a {
  29. if strings.ToLower(link.Rel) == "alternate" {
  30. return strings.TrimSpace(link.URL)
  31. }
  32. if link.Rel == "" && link.Type == "" {
  33. return strings.TrimSpace(link.URL)
  34. }
  35. }
  36. return ""
  37. }
  38. func (a atomLinks) firstLinkWithRelation(relation string) string {
  39. for _, link := range a {
  40. if strings.ToLower(link.Rel) == relation {
  41. return strings.TrimSpace(link.URL)
  42. }
  43. }
  44. return ""
  45. }
  46. func (a atomLinks) firstLinkWithRelationAndType(relation string, contentTypes ...string) string {
  47. for _, link := range a {
  48. if strings.ToLower(link.Rel) == relation {
  49. for _, contentType := range contentTypes {
  50. if strings.ToLower(link.Type) == contentType {
  51. return strings.TrimSpace(link.URL)
  52. }
  53. }
  54. }
  55. }
  56. return ""
  57. }