rewrite_functions.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "github.com/PuerkitoBio/goquery"
  12. )
  13. var (
  14. youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  15. invidioRegex = regexp.MustCompile(`https?:\/\/(.*)\/watch\?v=(.*)`)
  16. imgRegex = regexp.MustCompile(`<img [^>]+>`)
  17. textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  18. )
  19. func addImageTitle(entryURL, entryContent string) string {
  20. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  21. if err != nil {
  22. return entryContent
  23. }
  24. matches := doc.Find("img[src][title]")
  25. if matches.Length() > 0 {
  26. matches.Each(func(i int, img *goquery.Selection) {
  27. altAttr := img.AttrOr("alt", "")
  28. srcAttr, _ := img.Attr("src")
  29. titleAttr, _ := img.Attr("title")
  30. img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + html.EscapeString(titleAttr) + `</p></figcaption></figure>`)
  31. })
  32. output, _ := doc.Find("body").First().Html()
  33. return output
  34. }
  35. return entryContent
  36. }
  37. func addMailtoSubject(entryURL, entryContent string) string {
  38. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  39. if err != nil {
  40. return entryContent
  41. }
  42. matches := doc.Find(`a[href^="mailto:"]`)
  43. if matches.Length() > 0 {
  44. matches.Each(func(i int, a *goquery.Selection) {
  45. hrefAttr, _ := a.Attr("href")
  46. mailto, err := url.Parse(hrefAttr)
  47. if err != nil {
  48. return
  49. }
  50. subject := mailto.Query().Get("subject")
  51. if subject == "" {
  52. return
  53. }
  54. a.AppendHtml(" [" + html.EscapeString(subject) + "]")
  55. })
  56. output, _ := doc.Find("body").First().Html()
  57. return output
  58. }
  59. return entryContent
  60. }
  61. func addDynamicImage(entryURL, entryContent string) string {
  62. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  63. if err != nil {
  64. return entryContent
  65. }
  66. // Ordered most preferred to least preferred.
  67. candidateAttrs := []string{
  68. "data-src",
  69. "data-original",
  70. "data-orig",
  71. "data-url",
  72. "data-orig-file",
  73. "data-large-file",
  74. "data-medium-file",
  75. "data-2000src",
  76. "data-1000src",
  77. "data-800src",
  78. "data-655src",
  79. "data-500src",
  80. "data-380src",
  81. }
  82. changed := false
  83. doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
  84. for _, candidateAttr := range candidateAttrs {
  85. if srcAttr, found := img.Attr(candidateAttr); found {
  86. changed = true
  87. if img.Is("img") {
  88. img.SetAttr("src", srcAttr)
  89. } else {
  90. altAttr := img.AttrOr("alt", "")
  91. img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
  92. }
  93. break
  94. }
  95. }
  96. })
  97. if !changed {
  98. doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
  99. matches := imgRegex.FindAllString(noscript.Text(), 2)
  100. if len(matches) == 1 {
  101. changed = true
  102. noscript.ReplaceWithHtml(matches[0])
  103. }
  104. })
  105. }
  106. if changed {
  107. output, _ := doc.Find("body").First().Html()
  108. return output
  109. }
  110. return entryContent
  111. }
  112. func fixMediumImages(entryURL, entryContent string) string {
  113. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  114. if err != nil {
  115. return entryContent
  116. }
  117. doc.Find("figure.paragraph-image").Each(func(i int, paragraphImage *goquery.Selection) {
  118. noscriptElement := paragraphImage.Find("noscript")
  119. if noscriptElement.Length() > 0 {
  120. paragraphImage.ReplaceWithHtml(noscriptElement.Text())
  121. }
  122. })
  123. output, _ := doc.Find("body").First().Html()
  124. return output
  125. }
  126. func useNoScriptImages(entryURL, entryContent string) string {
  127. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  128. if err != nil {
  129. return entryContent
  130. }
  131. doc.Find("figure").Each(func(i int, figureElement *goquery.Selection) {
  132. imgElement := figureElement.Find("img")
  133. if imgElement.Length() > 0 {
  134. noscriptElement := figureElement.Find("noscript")
  135. if noscriptElement.Length() > 0 {
  136. figureElement.PrependHtml(noscriptElement.Text())
  137. imgElement.Remove()
  138. noscriptElement.Remove()
  139. }
  140. }
  141. })
  142. output, _ := doc.Find("body").First().Html()
  143. return output
  144. }
  145. func addYoutubeVideo(entryURL, entryContent string) string {
  146. matches := youtubeRegex.FindStringSubmatch(entryURL)
  147. if len(matches) == 2 {
  148. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  149. return video + `<br>` + entryContent
  150. }
  151. return entryContent
  152. }
  153. func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
  154. matches := youtubeRegex.FindStringSubmatch(entryURL)
  155. if len(matches) == 2 {
  156. video := `<iframe width="650" height="350" frameborder="0" src="https://invidio.us/embed/` + matches[1] + `" allowfullscreen></iframe>`
  157. return video + `<br>` + entryContent
  158. }
  159. return entryContent
  160. }
  161. func addInvidiousVideo(entryURL, entryContent string) string {
  162. matches := invidioRegex.FindStringSubmatch(entryURL)
  163. if len(matches) == 3 {
  164. video := `<iframe width="650" height="350" frameborder="0" src="https://` + matches[1] + `/embed/` + matches[2] + `" allowfullscreen></iframe>`
  165. return video + `<br>` + entryContent
  166. }
  167. return entryContent
  168. }
  169. func addPDFLink(entryURL, entryContent string) string {
  170. if strings.HasSuffix(entryURL, ".pdf") {
  171. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  172. }
  173. return entryContent
  174. }
  175. func replaceTextLinks(input string) string {
  176. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  177. }
  178. func replaceLineFeeds(input string) string {
  179. return strings.Replace(input, "\n", "<br>", -1)
  180. }
  181. func replaceCustom(entryContent string, searchTerm string, replaceTerm string) string {
  182. re, err := regexp.Compile(searchTerm)
  183. if err == nil {
  184. return re.ReplaceAllString(entryContent, replaceTerm)
  185. }
  186. return entryContent
  187. }