rewrite_functions.go 7.2 KB

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