atom_common.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, contentType string) string {
  47. for _, link := range a {
  48. if strings.ToLower(link.Rel) == relation && strings.ToLower(link.Type) == contentType {
  49. return strings.TrimSpace(link.URL)
  50. }
  51. }
  52. return ""
  53. }