atom_10.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/xml"
  6. "html"
  7. "strings"
  8. "miniflux.app/v2/internal/reader/media"
  9. )
  10. // The "atom:feed" element is the document (i.e., top-level) element of
  11. // an Atom Feed Document, acting as a container for metadata and data
  12. // associated with the feed. Its element children consist of metadata
  13. // elements followed by zero or more atom:entry child elements.
  14. //
  15. // Specs:
  16. // https://tools.ietf.org/html/rfc4287
  17. // https://validator.w3.org/feed/docs/atom.html
  18. type atom10Feed struct {
  19. XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
  20. // Language is the natural language of the feed, declared by an
  21. // xml:lang attribute on the atom:feed element. The tag is
  22. // namespace-qualified so that lang attributes from other namespaces
  23. // cannot override the real xml:lang value.
  24. Language string `xml:"http://www.w3.org/XML/1998/namespace lang,attr"`
  25. // The "atom:id" element conveys a permanent, universally unique
  26. // identifier for an entry or feed.
  27. //
  28. // Its content MUST be an IRI, as defined by [RFC3987]. Note that the
  29. // definition of "IRI" excludes relative references. Though the IRI
  30. // might use a dereferencable scheme, Atom Processors MUST NOT assume it
  31. // can be dereferenced.
  32. //
  33. // atom:feed elements MUST contain exactly one atom:id element.
  34. ID string `xml:"http://www.w3.org/2005/Atom id"`
  35. // The "atom:title" element is a Text construct that conveys a human-
  36. // readable title for an entry or feed.
  37. //
  38. // atom:feed elements MUST contain exactly one atom:title element.
  39. Title atom10Text `xml:"http://www.w3.org/2005/Atom title"`
  40. // The "atom:subtitle" element is a Text construct that
  41. // contains a human-readable description or subtitle for the feed.
  42. Subtitle atom10Text `xml:"http://www.w3.org/2005/Atom subtitle"`
  43. // The "atom:author" element is a Person construct that indicates the
  44. // author of the entry or feed.
  45. //
  46. // atom:feed elements MUST contain one or more atom:author elements,
  47. // unless all of the atom:feed element's child atom:entry elements
  48. // contain at least one atom:author element.
  49. Authors atomPersons `xml:"http://www.w3.org/2005/Atom author"`
  50. // The "atom:icon" element's content is an IRI reference [RFC3987] that
  51. // identifies an image that provides iconic visual identification for a
  52. // feed.
  53. //
  54. // atom:feed elements MUST NOT contain more than one atom:icon element.
  55. Icon string `xml:"http://www.w3.org/2005/Atom icon"`
  56. // The "atom:logo" element's content is an IRI reference [RFC3987] that
  57. // identifies an image that provides visual identification for a feed.
  58. //
  59. // atom:feed elements MUST NOT contain more than one atom:logo element.
  60. Logo string `xml:"http://www.w3.org/2005/Atom logo"`
  61. // atom:feed elements SHOULD contain one atom:link element with a rel
  62. // attribute value of "self". This is the preferred URI for
  63. // retrieving Atom Feed Documents representing this Atom feed.
  64. //
  65. // atom:feed elements MUST NOT contain more than one atom:link
  66. // element with a rel attribute value of "alternate" that has the
  67. // same combination of type and hreflang attribute values.
  68. Links atomLinks `xml:"http://www.w3.org/2005/Atom link"`
  69. // The "atom:category" element conveys information about a category
  70. // associated with an entry or feed. This specification assigns no
  71. // meaning to the content (if any) of this element.
  72. //
  73. // atom:feed elements MAY contain any number of atom:category
  74. // elements.
  75. Categories atomCategories `xml:"http://www.w3.org/2005/Atom category"`
  76. Entries []atom10Entry `xml:"http://www.w3.org/2005/Atom entry"`
  77. }
  78. type atom10Entry struct {
  79. // The "atom:id" element conveys a permanent, universally unique
  80. // identifier for an entry or feed.
  81. //
  82. // Its content MUST be an IRI, as defined by [RFC3987]. Note that the
  83. // definition of "IRI" excludes relative references. Though the IRI
  84. // might use a dereferencable scheme, Atom Processors MUST NOT assume it
  85. // can be dereferenced.
  86. //
  87. // atom:entry elements MUST contain exactly one atom:id element.
  88. ID string `xml:"http://www.w3.org/2005/Atom id"`
  89. // Language is the natural language of the entry, declared by an
  90. // xml:lang attribute on the atom:entry element. The tag is
  91. // namespace-qualified so that lang attributes from other namespaces
  92. // cannot override the real xml:lang value.
  93. Language string `xml:"http://www.w3.org/XML/1998/namespace lang,attr"`
  94. // The "atom:title" element is a Text construct that conveys a human-
  95. // readable title for an entry or feed.
  96. //
  97. // atom:entry elements MUST contain exactly one atom:title element.
  98. Title atom10Text `xml:"http://www.w3.org/2005/Atom title"`
  99. // The "atom:published" element is a Date construct indicating an
  100. // instant in time associated with an event early in the life cycle of
  101. // the entry.
  102. Published string `xml:"http://www.w3.org/2005/Atom published"`
  103. // The "atom:updated" element is a Date construct indicating the most
  104. // recent instant in time when an entry or feed was modified in a way
  105. // the publisher considers significant. Therefore, not all
  106. // modifications necessarily result in a changed atom:updated value.
  107. //
  108. // atom:entry elements MUST contain exactly one atom:updated element.
  109. Updated string `xml:"http://www.w3.org/2005/Atom updated"`
  110. // atom:entry elements MUST NOT contain more than one atom:link
  111. // element with a rel attribute value of "alternate" that has the
  112. // same combination of type and hreflang attribute values.
  113. Links atomLinks `xml:"http://www.w3.org/2005/Atom link"`
  114. // atom:entry elements MUST contain an atom:summary element in either
  115. // of the following cases:
  116. // * the atom:entry contains an atom:content that has a "src"
  117. // attribute (and is thus empty).
  118. // * the atom:entry contains content that is encoded in Base64;
  119. // i.e., the "type" attribute of atom:content is a MIME media type
  120. // [MIMEREG], but is not an XML media type [RFC3023], does not
  121. // begin with "text/", and does not end with "/xml" or "+xml".
  122. //
  123. // atom:entry elements MUST NOT contain more than one atom:summary
  124. // element.
  125. Summary atom10Text `xml:"http://www.w3.org/2005/Atom summary"`
  126. // atom:entry elements MUST NOT contain more than one atom:content
  127. // element.
  128. Content atom10Text `xml:"http://www.w3.org/2005/Atom content"`
  129. // The "atom:author" element is a Person construct that indicates the
  130. // author of the entry or feed.
  131. //
  132. // atom:entry elements MUST contain one or more atom:author elements
  133. Authors atomPersons `xml:"http://www.w3.org/2005/Atom author"`
  134. // The "atom:category" element conveys information about a category
  135. // associated with an entry or feed. This specification assigns no
  136. // meaning to the content (if any) of this element.
  137. //
  138. // atom:entry elements MAY contain any number of atom:category
  139. // elements.
  140. Categories atomCategories `xml:"http://www.w3.org/2005/Atom category"`
  141. media.MediaItemElement
  142. }
  143. // A Text construct contains human-readable text, usually in small
  144. // quantities. The content of Text constructs is Language-Sensitive.
  145. // Specs: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1
  146. // Text: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.1
  147. // HTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.2
  148. // XHTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.3
  149. type atom10Text struct {
  150. Type string `xml:"type,attr"`
  151. CharData string `xml:",chardata"`
  152. InnerXML string `xml:",innerxml"`
  153. XHTMLRootElement atomXHTMLRootElement `xml:"http://www.w3.org/1999/xhtml div"`
  154. }
  155. func (a *atom10Text) body() string {
  156. var content string
  157. if strings.EqualFold(a.Type, "xhtml") {
  158. content = a.xhtmlContent()
  159. } else {
  160. content = a.CharData
  161. }
  162. return strings.TrimSpace(content)
  163. }
  164. func (a *atom10Text) title() string {
  165. var content string
  166. switch {
  167. case strings.EqualFold(a.Type, "xhtml"):
  168. content = a.xhtmlContent()
  169. case strings.Contains(a.InnerXML, "<![CDATA["):
  170. content = html.UnescapeString(a.CharData)
  171. default:
  172. content = a.CharData
  173. }
  174. return strings.TrimSpace(content)
  175. }
  176. func (a *atom10Text) xhtmlContent() string {
  177. if a.XHTMLRootElement.XMLName.Local == "div" {
  178. return a.XHTMLRootElement.InnerXML
  179. }
  180. return a.InnerXML
  181. }
  182. type atomXHTMLRootElement struct {
  183. XMLName xml.Name `xml:"div"`
  184. InnerXML string `xml:",innerxml"`
  185. }