atom_10.go 7.5 KB

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