rewrite_functions.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. candidateSrcsetAttrs := []string{
  83. "data-srcset",
  84. }
  85. changed := false
  86. doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
  87. // Src-linked candidates
  88. for _, candidateAttr := range candidateAttrs {
  89. if srcAttr, found := img.Attr(candidateAttr); found {
  90. changed = true
  91. if img.Is("img") {
  92. img.SetAttr("src", srcAttr)
  93. } else {
  94. altAttr := img.AttrOr("alt", "")
  95. img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
  96. }
  97. break
  98. }
  99. }
  100. // Srcset-linked candidates
  101. for _, candidateAttr := range candidateSrcsetAttrs {
  102. if srcAttr, found := img.Attr(candidateAttr); found {
  103. changed = true
  104. if img.Is("img") {
  105. img.SetAttr("srcset", srcAttr)
  106. } else {
  107. altAttr := img.AttrOr("alt", "")
  108. img.ReplaceWithHtml(`<img srcset="` + srcAttr + `" alt="` + altAttr + `"/>`)
  109. }
  110. break
  111. }
  112. }
  113. })
  114. if !changed {
  115. doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
  116. matches := imgRegex.FindAllString(noscript.Text(), 2)
  117. if len(matches) == 1 {
  118. changed = true
  119. noscript.ReplaceWithHtml(matches[0])
  120. }
  121. })
  122. }
  123. if changed {
  124. output, _ := doc.Find("body").First().Html()
  125. return output
  126. }
  127. return entryContent
  128. }
  129. func fixMediumImages(entryURL, entryContent string) string {
  130. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  131. if err != nil {
  132. return entryContent
  133. }
  134. doc.Find("figure.paragraph-image").Each(func(i int, paragraphImage *goquery.Selection) {
  135. noscriptElement := paragraphImage.Find("noscript")
  136. if noscriptElement.Length() > 0 {
  137. paragraphImage.ReplaceWithHtml(noscriptElement.Text())
  138. }
  139. })
  140. output, _ := doc.Find("body").First().Html()
  141. return output
  142. }
  143. func useNoScriptImages(entryURL, entryContent string) string {
  144. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  145. if err != nil {
  146. return entryContent
  147. }
  148. doc.Find("figure").Each(func(i int, figureElement *goquery.Selection) {
  149. imgElement := figureElement.Find("img")
  150. if imgElement.Length() > 0 {
  151. noscriptElement := figureElement.Find("noscript")
  152. if noscriptElement.Length() > 0 {
  153. figureElement.PrependHtml(noscriptElement.Text())
  154. imgElement.Remove()
  155. noscriptElement.Remove()
  156. }
  157. }
  158. })
  159. output, _ := doc.Find("body").First().Html()
  160. return output
  161. }
  162. func addYoutubeVideo(entryURL, entryContent string) string {
  163. matches := youtubeRegex.FindStringSubmatch(entryURL)
  164. if len(matches) == 2 {
  165. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  166. return video + `<br>` + entryContent
  167. }
  168. return entryContent
  169. }
  170. func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
  171. matches := youtubeRegex.FindStringSubmatch(entryURL)
  172. if len(matches) == 2 {
  173. video := `<iframe width="650" height="350" frameborder="0" src="https://invidio.us/embed/` + matches[1] + `" allowfullscreen></iframe>`
  174. return video + `<br>` + entryContent
  175. }
  176. return entryContent
  177. }
  178. func addInvidiousVideo(entryURL, entryContent string) string {
  179. matches := invidioRegex.FindStringSubmatch(entryURL)
  180. if len(matches) == 3 {
  181. video := `<iframe width="650" height="350" frameborder="0" src="https://` + matches[1] + `/embed/` + matches[2] + `" allowfullscreen></iframe>`
  182. return video + `<br>` + entryContent
  183. }
  184. return entryContent
  185. }
  186. func addPDFLink(entryURL, entryContent string) string {
  187. if strings.HasSuffix(entryURL, ".pdf") {
  188. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  189. }
  190. return entryContent
  191. }
  192. func replaceTextLinks(input string) string {
  193. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  194. }
  195. func replaceLineFeeds(input string) string {
  196. return strings.Replace(input, "\n", "<br>", -1)
  197. }
  198. func replaceCustom(entryContent string, searchTerm string, replaceTerm string) string {
  199. re, err := regexp.Compile(searchTerm)
  200. if err == nil {
  201. return re.ReplaceAllString(entryContent, replaceTerm)
  202. }
  203. return entryContent
  204. }
  205. func removeCustom(entryContent string, selector string) string {
  206. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  207. if err != nil {
  208. return entryContent
  209. }
  210. doc.Find(selector).Remove()
  211. output, _ := doc.Find("body").First().Html()
  212. return output
  213. }