rewrite_functions.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "regexp"
  9. "strings"
  10. "github.com/PuerkitoBio/goquery"
  11. )
  12. var (
  13. youtubeRegex = regexp.MustCompile(`youtube\.com/watch\?v=(.*)`)
  14. imgRegex = regexp.MustCompile(`<img [^>]+>`)
  15. textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])`)
  16. )
  17. func addImageTitle(entryURL, entryContent string) string {
  18. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  19. if err != nil {
  20. return entryContent
  21. }
  22. matches := doc.Find("img[src][title]")
  23. if matches.Length() > 0 {
  24. matches.Each(func(i int, img *goquery.Selection) {
  25. altAttr := img.AttrOr("alt", "")
  26. srcAttr, _ := img.Attr("src")
  27. titleAttr, _ := img.Attr("title")
  28. img.ReplaceWithHtml(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + html.EscapeString(titleAttr) + `</p></figcaption></figure>`)
  29. })
  30. output, _ := doc.Find("body").First().Html()
  31. return output
  32. }
  33. return entryContent
  34. }
  35. func addDynamicImage(entryURL, entryContent string) string {
  36. doc, err := goquery.NewDocumentFromReader(strings.NewReader(entryContent))
  37. if err != nil {
  38. return entryContent
  39. }
  40. // Ordered most preferred to least preferred.
  41. candidateAttrs := []string{
  42. "data-src",
  43. "data-original",
  44. "data-orig",
  45. "data-url",
  46. "data-orig-file",
  47. "data-large-file",
  48. "data-medium-file",
  49. "data-2000src",
  50. "data-1000src",
  51. "data-800src",
  52. "data-655src",
  53. "data-500src",
  54. "data-380src",
  55. }
  56. changed := false
  57. doc.Find("img,div").Each(func(i int, img *goquery.Selection) {
  58. for _, candidateAttr := range candidateAttrs {
  59. if srcAttr, found := img.Attr(candidateAttr); found {
  60. changed = true
  61. if img.Is("img") {
  62. img.SetAttr("src", srcAttr)
  63. } else {
  64. altAttr := img.AttrOr("alt", "")
  65. img.ReplaceWithHtml(`<img src="` + srcAttr + `" alt="` + altAttr + `"/>`)
  66. }
  67. break
  68. }
  69. }
  70. })
  71. if !changed {
  72. doc.Find("noscript").Each(func(i int, noscript *goquery.Selection) {
  73. matches := imgRegex.FindAllString(noscript.Text(), 2)
  74. if len(matches) == 1 {
  75. changed = true
  76. noscript.ReplaceWithHtml(matches[0])
  77. }
  78. })
  79. }
  80. if changed {
  81. output, _ := doc.Find("body").First().Html()
  82. return output
  83. }
  84. return entryContent
  85. }
  86. func addYoutubeVideo(entryURL, entryContent string) string {
  87. matches := youtubeRegex.FindStringSubmatch(entryURL)
  88. if len(matches) == 2 {
  89. video := `<iframe width="650" height="350" frameborder="0" src="https://www.youtube-nocookie.com/embed/` + matches[1] + `" allowfullscreen></iframe>`
  90. return video + "<p>" + replaceLineFeeds(replaceTextLinks(entryContent)) + "</p>"
  91. }
  92. return entryContent
  93. }
  94. func addPDFLink(entryURL, entryContent string) string {
  95. if strings.HasSuffix(entryURL, ".pdf") {
  96. return fmt.Sprintf(`<a href="%s">PDF</a><br>%s`, entryURL, entryContent)
  97. }
  98. return entryContent
  99. }
  100. func replaceTextLinks(input string) string {
  101. return textLinkRegex.ReplaceAllString(input, `<a href="${1}">${1}</a>`)
  102. }
  103. func replaceLineFeeds(input string) string {
  104. return strings.Replace(input, "\n", "<br>", -1)
  105. }