atom_common.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 atomAuthors []*atomPerson
  21. func (a atomAuthors) String() string {
  22. var authors []string
  23. for _, person := range a {
  24. authors = append(authors, person.String())
  25. }
  26. return strings.Join(authors, ", ")
  27. }
  28. type atomLink struct {
  29. URL string `xml:"href,attr"`
  30. Type string `xml:"type,attr"`
  31. Rel string `xml:"rel,attr"`
  32. Length string `xml:"length,attr"`
  33. }
  34. type atomLinks []*atomLink
  35. func (a atomLinks) originalLink() string {
  36. for _, link := range a {
  37. if strings.ToLower(link.Rel) == "alternate" {
  38. return strings.TrimSpace(link.URL)
  39. }
  40. if link.Rel == "" && (link.Type == "" || link.Type == "text/html") {
  41. return strings.TrimSpace(link.URL)
  42. }
  43. }
  44. return ""
  45. }
  46. func (a atomLinks) firstLinkWithRelation(relation string) string {
  47. for _, link := range a {
  48. if strings.ToLower(link.Rel) == relation {
  49. return strings.TrimSpace(link.URL)
  50. }
  51. }
  52. return ""
  53. }
  54. func (a atomLinks) firstLinkWithRelationAndType(relation string, contentTypes ...string) string {
  55. for _, link := range a {
  56. if strings.ToLower(link.Rel) == relation {
  57. for _, contentType := range contentTypes {
  58. if strings.ToLower(link.Type) == contentType {
  59. return strings.TrimSpace(link.URL)
  60. }
  61. }
  62. }
  63. }
  64. return ""
  65. }