atom_10.go 8.0 KB

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