atom_10.go 7.7 KB

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