rewrite_functions.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package rewrite // import "miniflux.app/reader/rewrite"
  5. import (
  6. "fmt"
  7. "html"
  8. "net/url"
  9. "regexp"
  10. "strings"
  11. "miniflux.app/config"
  12. "github.com/PuerkitoBio/goquery"
  13. )
  14. var (
  15. youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  16. youtubeIdRegex = regexp.MustCompile(`youtube_id"?\s*[:=]\s*"([a-zA-Z0-9_-]{11})"`)
  17. invidioRegex = regexp.MustCompile(`https?:\/\/(.*)\/watch\?v=(.*)`)
  18. imgRegex = regexp.MustCompile(`<img [^>]+>`)
  19. textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  20. )
  21. func addImageTitle(entryURL, entryContent string) string {
  22. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  23. if err != nil {
  24. return entryContent
  25. }
  26. matches := doc.Find("img[src][title]")
  27. if matches.Length() > 0 {
  28. matches.Each(func(i int, img *goquery.Selection) {
  29. altAttr := img.AttrOr("alt", "")
  30. srcAttr, _ := img.Attr("src")
  31. titleAttr, _ := img.Attr("title")
  32. img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + html.EscapeString(titleAttr) + `</p></figcaption></figure>`)
  33. })
  34. output, _ := doc.Find("body").First().Html()
  35. return output
  36. }
  37. return entryContent
  38. }
  39. func addMailtoSubject(entryURL, entryContent string) string {
  40. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  41. if err != nil {
  42. return entryContent
  43. }
  44. matches := doc.Find(`a[href^="mailto:"]`)
  45. if matches.Length() > 0 {
  46. matches.Each(func(i int, a *goquery.Selection) {
  47. hrefAttr, _ := a.Attr("href")
  48. mailto, err := url.Parse(hrefAttr)
  49. if err != nil {
  50. return
  51. }
  52. subject := mailto.Query().Get("subject")
  53. if subject == "" {
  54. return
  55. }
  56. a.AppendHtml(" [" + html.EscapeString(subject) + "]")
  57. })
  58. output, _ := doc.Find("body").First().Html()
  59. return output
  60. }
  61. return entryContent
  62. }
  63. func addDynamicImage(entryURL, entryContent string) string {
  64. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  65. if err != nil {
  66. return entryContent
  67. }
  68. // Ordered most preferred to least preferred.
  69. candidateAttrs := []string{
  70. "data-src",
  71. "data-original",
  72. "data-orig",
  73. "data-url",
  74. "data-orig-file",
  75. "data-large-file",
  76. "data-medium-file",
  77. "data-2000src",
  78. "data-1000src",
  79. "data-800src",
  80. "data-655src",
  81. "data-500src",
  82. "data-380src",
  83. }
  84. candidateSrcsetAttrs := []string{
  85. "data-srcset",
  86. }
  87. changed := false
  88. doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
  89. // Src-linked candidates
  90. for _, candidateAttr := range candidateAttrs {
  91. if srcAttr, found := img.Attr(candidateAttr); found {
  92. changed = true
  93. if img.Is("img") {
  94. img.SetAttr("src", srcAttr)
  95. } else {
  96. altAttr := img.AttrOr("alt", "")
  97. img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
  98. }
  99. break
  100. }
  101. }
  102. // Srcset-linked candidates
  103. for _, candidateAttr := range candidateSrcsetAttrs {
  104. if srcAttr, found := img.Attr(candidateAttr); found {
  105. changed = true
  106. if img.Is("img") {
  107. img.SetAttr("srcset", srcAttr)
  108. } else {
  109. altAttr := img.AttrOr("alt", "")
  110. img.ReplaceWithHtml(`<img srcset="` + srcAttr + `" alt="` + altAttr + `"/>`)
  111. }
  112. break
  113. }
  114. }
  115. })
  116. if !changed {
  117. doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
  118. matches := imgRegex.FindAllString(noscript.Text(), 2)
  119. if len(matches) == 1 {
  120. changed = true
  121. noscript.ReplaceWithHtml(matches[0])
  122. }
  123. })
  124. }
  125. if changed {
  126. output, _ := doc.Find("body").First().Html()
  127. return output
  128. }
  129. return entryContent
  130. }
  131. func fixMediumImages(entryURL, entryContent string) string {
  132. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  133. if err != nil {
  134. return entryContent
  135. }
  136. doc.Find("figure.paragraph-image").Each(func(i int, paragraphImage *goquery.Selection) {
  137. noscriptElement := paragraphImage.Find("noscript")
  138. if noscriptElement.Length() > 0 {
  139. paragraphImage.ReplaceWithHtml(noscriptElement.Text())
  140. }
  141. })
  142. output, _ := doc.Find("body").First().Html()
  143. return output
  144. }
  145. func useNoScriptImages(entryURL, entryContent string) string {
  146. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  147. if err != nil {
  148. return entryContent
  149. }
  150. doc.Find("figure").Each(func(i int, figureElement *goquery.Selection) {
  151. imgElement := figureElement.Find("img")
  152. if imgElement.Length() > 0 {
  153. noscriptElement := figureElement.Find("noscript")
  154. if noscriptElement.Length() > 0 {
  155. figureElement.PrependHtml(noscriptElement.Text())
  156. imgElement.Remove()
  157. noscriptElement.Remove()
  158. }
  159. }
  160. })
  161. output, _ := doc.Find("body").First().Html()
  162. return output
  163. }
  164. func addYoutubeVideo(entryURL, entryContent string) string {
  165. matches := youtubeRegex.FindStringSubmatch(entryURL)
  166. if len(matches) == 2 {
  167. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  168. return video + `<br>` + entryContent
  169. }
  170. return entryContent
  171. }
  172. func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
  173. matches := youtubeRegex.FindStringSubmatch(entryURL)
  174. if len(matches) == 2 {
  175. video := `<iframe width="650" height="350" frameborder="0" src="https://` + config.Opts.InvidiousInstance() + `/embed/` + matches[1] + `" allowfullscreen></iframe>`
  176. return video + `<br>` + entryContent
  177. }
  178. return entryContent
  179. }
  180. func addYoutubeVideoFromId(entryContent string) string {
  181. matches := youtubeIdRegex.FindAllStringSubmatch(entryContent, -1)
  182. if matches == nil {
  183. return entryContent
  184. }
  185. sb := strings.Builder{}
  186. for _, match := range matches {
  187. if len(match) == 2 {
  188. sb.WriteString(`<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/`)
  189. sb.WriteString(match[1])
  190. sb.WriteString(`" allowfullscreen></iframe><br>`)
  191. }
  192. }
  193. sb.WriteString(entryContent)
  194. return sb.String()
  195. }
  196. func addInvidiousVideo(entryURL, entryContent string) string {
  197. matches := invidioRegex.FindStringSubmatch(entryURL)
  198. if len(matches) == 3 {
  199. video := `<iframe width="650" height="350" frameborder="0" src="https://` + matches[1] + `/embed/` + matches[2] + `" allowfullscreen></iframe>`
  200. return video + `<br>` + entryContent
  201. }
  202. return entryContent
  203. }
  204. func addPDFLink(entryURL, entryContent string) string {
  205. if strings.HasSuffix(entryURL, ".pdf") {
  206. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  207. }
  208. return entryContent
  209. }
  210. func replaceTextLinks(input string) string {
  211. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  212. }
  213. func replaceLineFeeds(input string) string {
  214. return strings.Replace(input, "\n", "<br>", -1)
  215. }
  216. func replaceCustom(entryContent string, searchTerm string, replaceTerm string) string {
  217. re, err := regexp.Compile(searchTerm)
  218. if err == nil {
  219. return re.ReplaceAllString(entryContent, replaceTerm)
  220. }
  221. return entryContent
  222. }
  223. func removeCustom(entryContent string, selector string) string {
  224. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  225. if err != nil {
  226. return entryContent
  227. }
  228. doc.Find(selector).Remove()
  229. output, _ := doc.Find("body").First().Html()
  230. return output
  231. }
  232. func addCastopodEpisode(entryURL, entryContent string) string {
  233. player := `<iframe width="650" frameborder="0" src="` + entryURL + `/embed/light"></iframe>`
  234. return player + `<br>` + entryContent
  235. }