atom_10.go 7.6 KB

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