atom_03.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package atom // import "miniflux.app/v2/internal/reader/atom"
  4. import (
  5. "encoding/base64"
  6. "html"
  7. "strings"
  8. )
  9. // Specs: http://web.archive.org/web/20060811235523/http://www.mnot.net/drafts/draft-nottingham-atom-format-02.html
  10. type atom03Feed struct {
  11. Version string `xml:"version,attr"`
  12. Language string `xml:"lang,attr"`
  13. // The "atom:id" element's content conveys a permanent, globally unique identifier for the feed.
  14. // It MUST NOT change over time, even if the feed is relocated. atom:feed elements MAY contain an atom:id element,
  15. // but MUST NOT contain more than one. The content of this element, when present, MUST be a URI.
  16. ID string `xml:"http://purl.org/atom/ns# id"`
  17. // The "atom:title" element is a Content construct that conveys a human-readable title for the feed.
  18. // atom:feed elements MUST contain exactly one atom:title element.
  19. // If the feed describes a Web resource, its content SHOULD be the same as that resource's title.
  20. Title atom03Content `xml:"http://purl.org/atom/ns# title"`
  21. // The "atom:link" element is a Link construct that conveys a URI associated with the feed.
  22. // The nature of the relationship as well as the link itself is determined by the element's content.
  23. // atom:feed elements MUST contain at least one atom:link element with a rel attribute value of "alternate".
  24. // atom:feed elements MUST NOT contain more than one atom:link element with a rel attribute value of "alternate" that has the same type attribute value.
  25. // atom:feed elements MAY contain additional atom:link elements beyond those described above.
  26. Links atomLinks `xml:"http://purl.org/atom/ns# link"`
  27. // The "atom:author" element is a Person construct that indicates the default author of the feed.
  28. // atom:feed elements MUST contain exactly one atom:author element,
  29. // UNLESS all of the atom:feed element's child atom:entry elements contain an atom:author element.
  30. // atom:feed elements MUST NOT contain more than one atom:author element.
  31. Author AtomPerson `xml:"http://purl.org/atom/ns# author"`
  32. // The "atom:entry" element's represents an individual entry that is contained by the feed.
  33. // atom:feed elements MAY contain one or more atom:entry elements.
  34. Entries []atom03Entry `xml:"http://purl.org/atom/ns# entry"`
  35. }
  36. type atom03Entry struct {
  37. // The "atom:id" element's content conveys a permanent, globally unique identifier for the entry.
  38. // It MUST NOT change over time, even if other representations of the entry (such as a web representation pointed to by the entry's atom:link element) are relocated.
  39. // If the same entry is syndicated in two atom:feeds published by the same entity, the entry's atom:id MUST be the same in both feeds.
  40. ID string `xml:"id"`
  41. Language string `xml:"lang,attr"`
  42. // The "atom:title" element is a Content construct that conveys a human-readable title for the entry.
  43. // atom:entry elements MUST have exactly one "atom:title" element.
  44. // If an entry describes a Web resource, its content SHOULD be the same as that resource's title.
  45. Title atom03Content `xml:"title"`
  46. // The "atom:modified" element is a Date construct that indicates the time that the entry was last modified.
  47. // atom:entry elements MUST contain an atom:modified element, but MUST NOT contain more than one.
  48. // The content of an atom:modified element MUST have a time zone whose value SHOULD be "UTC".
  49. Modified string `xml:"modified"`
  50. // The "atom:issued" element is a Date construct that indicates the time that the entry was issued.
  51. // atom:entry elements MUST contain an atom:issued element, but MUST NOT contain more than one.
  52. // The content of an atom:issued element MAY omit a time zone.
  53. Issued string `xml:"issued"`
  54. // The "atom:created" element is a Date construct that indicates the time that the entry was created.
  55. // atom:entry elements MAY contain an atom:created element, but MUST NOT contain more than one.
  56. // The content of an atom:created element MUST have a time zone whose value SHOULD be "UTC".
  57. // If atom:created is not present, its content MUST considered to be the same as that of atom:modified.
  58. Created string `xml:"created"`
  59. // The "atom:link" element is a Link construct that conveys a URI associated with the entry.
  60. // The nature of the relationship as well as the link itself is determined by the element's content.
  61. // atom:entry elements MUST contain at least one atom:link element with a rel attribute value of "alternate".
  62. // atom:entry elements MUST NOT contain more than one atom:link element with a rel attribute value of "alternate" that has the same type attribute value.
  63. // atom:entry elements MAY contain additional atom:link elements beyond those described above.
  64. Links atomLinks `xml:"link"`
  65. // The "atom:summary" element is a Content construct that conveys a short summary, abstract or excerpt of the entry.
  66. // atom:entry elements MAY contain an atom:created element, but MUST NOT contain more than one.
  67. Summary atom03Content `xml:"summary"`
  68. // The "atom:content" element is a Content construct that conveys the content of the entry.
  69. // atom:entry elements MAY contain one or more atom:content elements.
  70. Content atom03Content `xml:"content"`
  71. // The "atom:author" element is a Person construct that indicates the default author of the entry.
  72. // atom:entry elements MUST contain exactly one atom:author element,
  73. // UNLESS the atom:feed element containing them contains an atom:author element itself.
  74. // atom:entry elements MUST NOT contain more than one atom:author element.
  75. Author AtomPerson `xml:"author"`
  76. }
  77. type atom03Content struct {
  78. // Content constructs MAY have a "type" attribute, whose value indicates the media type of the content.
  79. // When present, this attribute's value MUST be a registered media type [RFC2045].
  80. // If not present, its value MUST be considered to be "text/plain".
  81. Type string `xml:"type,attr"`
  82. // Content constructs MAY have a "mode" attribute, whose value indicates the method used to encode the content.
  83. // When present, this attribute's value MUST be listed below.
  84. // If not present, its value MUST be considered to be "xml".
  85. //
  86. // "xml": A mode attribute with the value "xml" indicates that the element's content is inline xml (for example, namespace-qualified XHTML).
  87. //
  88. // "escaped": A mode attribute with the value "escaped" indicates that the element's content is an escaped string.
  89. // Processors MUST unescape the element's content before considering it as content of the indicated media type.
  90. //
  91. // "base64": A mode attribute with the value "base64" indicates that the element's content is base64-encoded [RFC2045].
  92. // Processors MUST decode the element's content before considering it as content of the the indicated media type.
  93. Mode string `xml:"mode,attr"`
  94. CharData string `xml:",chardata"`
  95. InnerXML string `xml:",innerxml"`
  96. }
  97. func (a *atom03Content) content() string {
  98. content := ""
  99. switch a.Mode {
  100. case "xml":
  101. content = a.InnerXML
  102. case "escaped":
  103. content = a.CharData
  104. case "base64":
  105. b, err := base64.StdEncoding.DecodeString(a.CharData)
  106. if err == nil {
  107. content = string(b)
  108. }
  109. default:
  110. content = a.CharData
  111. }
  112. if a.Type != "text/html" {
  113. content = html.EscapeString(content)
  114. }
  115. return strings.TrimSpace(content)
  116. }