rewrite_functions.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(`invidio\.us\/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 addYoutubeVideo(entryURL, entryContent string) string {
  113. matches := youtubeRegex.FindStringSubmatch(entryURL)
  114. if len(matches) == 2 {
  115. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  116. return video + `<br>` + entryContent
  117. }
  118. return entryContent
  119. }
  120. func addYoutubeVideoUsingInvidiousPlayer(entryURL, entryContent string) string {
  121. matches := youtubeRegex.FindStringSubmatch(entryURL)
  122. if len(matches) == 2 {
  123. video := `<iframe width="650" height="350" frameborder="0" src="https://invidio.us/embed/` + matches[1] + `" allowfullscreen></iframe>`
  124. return video + `<br>` + entryContent
  125. }
  126. return entryContent
  127. }
  128. func addInvidiousVideo(entryURL, entryContent string) string {
  129. matches := invidioRegex.FindStringSubmatch(entryURL)
  130. fmt.Println(matches)
  131. if len(matches) == 2 {
  132. video := `<iframe width="650" height="350" frameborder="0" src="https://invidio.us/embed/` + matches[1] + `" allowfullscreen></iframe>`
  133. return video + `<br>` + entryContent
  134. }
  135. return entryContent
  136. }
  137. func addPDFLink(entryURL, entryContent string) string {
  138. if strings.HasSuffix(entryURL, ".pdf") {
  139. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  140. }
  141. return entryContent
  142. }
  143. func replaceTextLinks(input string) string {
  144. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  145. }
  146. func replaceLineFeeds(input string) string {
  147. return strings.Replace(input, "\n", "<br>", -1)
  148. }